15
15
#include " SdFat.h"
16
16
17
17
// Include MQTT Library to enable the Azure IoT Hub to call back to our device
18
- #include " MQTT.h"
18
+ #include " MQTT-TLS.h"
19
+ #include " certs/certs.h"
19
20
20
- // App Version Constant
21
+ // Json parser for working with MQTT responses from Azure IoT Hub
22
+ #include " JsonParserGeneratorRK.h"
21
23
void setup ();
22
24
void loop ();
23
25
void resetFermentationVariables ();
24
26
long getFermentationRate ();
27
+ int checkBatterylevel (String args);
25
28
int checkFermentationRate (String args);
26
29
int checkTemp (String args);
27
- int checkVoltage ();
30
+ int getBattPercentage ();
28
31
void activateBrewStage ();
29
32
int setBrewMode (String command);
30
33
void printSplash ();
34
+ void clearTopBar ();
31
35
void clearScreen ();
32
36
void printHeadingTextLine (String text);
33
37
void printSubheadingLine (String text);
@@ -43,15 +47,20 @@ void displayTempHeading();
43
47
void displayTempHistoryHeading ();
44
48
void displayFermentationRate (long rate);
45
49
void displayTime (float elapsedTime);
46
- void displayVoltage (int voltage);
50
+ void displayBattLevel (int voltage);
47
51
void displayLowBattAlert ();
48
52
String calcTimeToDisplay (float elapsedTime);
49
53
void updateChart (float temp);
50
- #line 19 "/Users/bsatrom/Development/brew-buddy/brew-buddy-firmware/src/brew-buddy-firmware.ino"
54
+ #line 21 "/Users/bsatrom/Development/brew-buddy/brew-buddy-firmware/src/brew-buddy-firmware.ino"
55
+ JsonParserStatic<2048 , 100 > jsonParser;
56
+
57
+ // App Version Constant
51
58
#define APP_VERSION " v1.0"
52
59
53
60
SYSTEM_THREAD (ENABLED);
54
61
62
+ String deviceID;
63
+
55
64
// Thermocouple Variables
56
65
const uint8_t CHIP_SELECT_PIN = D4;
57
66
// SCK, MISO & MOSI are defined on Particle 3rd Gen HW at A6-A8
@@ -74,7 +83,7 @@ int32_t width = 0, // BMP image dimensions
74
83
height = 0 ;
75
84
76
85
#define TFT_SPEED 120000000
77
- #define BATT_LOW_VOLTAGE 90
86
+ #define BATT_LOW_VOLTAGE 30
78
87
79
88
// SD Card
80
89
SdFat sd;
@@ -139,6 +148,7 @@ MQTT client("brew-buddy-hub.azure-devices.net", 8883, mqttCB);
139
148
void setup ()
140
149
{
141
150
Serial.begin (9600 );
151
+ deviceID = System.deviceID ();
142
152
143
153
pinMode (KNOCK_PIN, INPUT_PULLDOWN);
144
154
@@ -150,6 +160,7 @@ void setup()
150
160
Particle.function (" setMode" , setBrewMode);
151
161
Particle.function (" checkTemp" , checkTemp);
152
162
Particle.function (" checkRate" , checkFermentationRate);
163
+ Particle.function (" checkBatt" , checkBatterylevel);
153
164
154
165
// Initialize TFT
155
166
tft.begin (TFT_SPEED);
@@ -171,22 +182,30 @@ void setup()
171
182
printSubheadingLine (" Waiting for Brew..." );
172
183
173
184
// Check and display the battery level
174
- int voltage = checkVoltage ();
175
- displayVoltage (voltage);
185
+ int battLevel = getBattPercentage ();
186
+ // displayBattLevel(battLevel);
187
+
188
+ waitUntil (Particle.connected );
189
+
190
+ Particle.publish (" Version" , APP_VERSION);
191
+ Particle.publish (" Status" , " Brew Buddy Online" );
176
192
177
193
// Connect to Azure MQTT Server
178
- /* client.connect("e00fce681290df8ab5487791", "brew-buddy-hub.azure-devices.net/e00fce681290df8ab5487791/?api-version=2018-06-30", "SharedAccessSignature sr=brew-buddy-hub.azure-devices.net&sig=RKWH%2FV8CD595YeAnXOZ8jXsSYMnWf6RiJBnzhUoxCzE%3D&skn=iothubowner&se=1552683541");
194
+ client.enableTls (certificates, sizeof (certificates));
195
+ client.connect (deviceID, " brew-buddy-hub.azure-devices.net/" + deviceID, " SharedAccessSignature sr=brew-buddy-hub.azure-devices.net&sig=kNKhEIQaObc74GPvEoK4fi3W1ot65f5aQDOFnTdkaqY%3D&skn=iothubowner&se=1553119278" );
179
196
if (client.isConnected ())
180
197
{
181
198
Particle.publish (" mqtt/status" , " connected" );
199
+ bool methodSubResult = client.subscribe (" $iothub/methods/#" , MQTT::QOS0);
200
+ bool msgSubResult = client.subscribe (" devices/" + deviceID + " /messages/devicebound/#" , MQTT::QOS0);
201
+
202
+ Particle.publish (" matt/method-sub-result" , methodSubResult ? " subscribed to hub methods" : " subscription failed" );
203
+ Particle.publish (" matt/method-sub-result" , msgSubResult ? " subscribed to hub messages" : " subscription failed" );
182
204
}
183
205
else
184
206
{
185
207
Particle.publish (" mqtt/status" , " failed" );
186
- } */
187
-
188
- Particle.publish (" Version" , APP_VERSION);
189
- Particle.publish (" Status" , " Brew Buddy Online" );
208
+ }
190
209
}
191
210
192
211
void loop ()
@@ -208,7 +227,7 @@ void loop()
208
227
lastKnock = fermentationStartTime;
209
228
210
229
clearScreen ();
211
- tft.setCursor (0 , 10 );
230
+ tft.setCursor (0 , 30 );
212
231
tft.setTextColor (ILI9341_YELLOW);
213
232
tft.setTextSize (2 );
214
233
tft.print (" Fermentation started" );
@@ -265,13 +284,14 @@ void loop()
265
284
266
285
if (currentMillis - previousBattMillis > battInterval)
267
286
{
268
- int voltage = checkVoltage ();
287
+ previousBattMillis = millis ();
269
288
270
- displayVoltage (voltage);
271
- Particle.publish (messageBase + " voltage" , String (voltage));
289
+ int battLevel = getBattPercentage ();
272
290
273
- // If voltage < 30%, show a low batt message and publish message to cloud
274
- if (voltage < BATT_LOW_VOLTAGE)
291
+ displayBattLevel (battLevel);
292
+ Particle.publish (messageBase + " batt-level" , String (battLevel));
293
+
294
+ if (battLevel < BATT_LOW_VOLTAGE)
275
295
{
276
296
displayLowBattAlert ();
277
297
Particle.publish (messageBase + " alert" , " low-batt" );
@@ -291,6 +311,11 @@ void loop()
291
311
postFermentationRate ();
292
312
}
293
313
}
314
+
315
+ if (client.isConnected ())
316
+ {
317
+ client.loop ();
318
+ }
294
319
}
295
320
296
321
void resetFermentationVariables ()
@@ -319,6 +344,14 @@ long getFermentationRate()
319
344
return rate;
320
345
}
321
346
347
+ int checkBatterylevel (String args)
348
+ {
349
+ int battLevel = getBattPercentage ();
350
+ Particle.publish (messageBase + " batt-level" , String (battLevel));
351
+
352
+ return 1 ;
353
+ }
354
+
322
355
int checkFermentationRate (String args)
323
356
{
324
357
if (isFermentationMode)
@@ -344,18 +377,17 @@ int checkTemp(String args)
344
377
return 0 ;
345
378
}
346
379
347
- int checkVoltage ()
380
+ int getBattPercentage ()
348
381
{
349
382
int voltage = analogRead (BATT) * 0.0011224 ;
350
383
351
- return (int )((voltage / 4.2 ) * 100 + 0.5 )
384
+ return (int )((voltage / 4.2 ) * 100 + 0.5 );
352
385
}
353
386
354
387
void activateBrewStage ()
355
388
{
356
389
startTime = millis ();
357
390
358
- displayStageName (brewStage);
359
391
displayTempHeading ();
360
392
displayTempHistoryHeading ();
361
393
displayTimeHeading ();
@@ -444,9 +476,16 @@ void printSplash()
444
476
delay (3000 );
445
477
}
446
478
479
+ void clearTopBar ()
480
+ {
481
+ tft.fillRect (0 , 0 , 240 , 30 , ILI9341_BLACK);
482
+ tft.setCursor (0 , 0 );
483
+ tft.setTextColor (ILI9341_WHITE);
484
+ }
485
+
447
486
void clearScreen ()
448
487
{
449
- tft.fillScreen ( ILI9341_BLACK);
488
+ tft.fillRect ( 0 , 30 , 240 , 320 , ILI9341_BLACK);
450
489
tft.setCursor (0 , 0 );
451
490
tft.setTextColor (ILI9341_WHITE);
452
491
}
@@ -526,7 +565,7 @@ void displayStageName(String stagename)
526
565
527
566
void displayFermentationHeading ()
528
567
{
529
- tft.setCursor (0 , 100 );
568
+ tft.setCursor (0 , 110 );
530
569
tft.setTextSize (2 );
531
570
tft.println (" Fermentation Rate" );
532
571
tft.println (" (in seconds)" );
@@ -536,7 +575,7 @@ void displayFermentationModeDelta()
536
575
{
537
576
unsigned long fermentationDelta = fermentationStartTime - fermentationModeStartTime;
538
577
539
- tft.setCursor (0 , 40 );
578
+ tft.setCursor (0 , 60 );
540
579
tft.setTextSize (2 );
541
580
tft.println (" Time to fermentation (hours)" );
542
581
tft.println (fermentationDelta / 36000000.00 );
@@ -565,8 +604,8 @@ void displayTempHistoryHeading()
565
604
566
605
void displayFermentationRate (long rate)
567
606
{
568
- tft.fillRect (0 , 80 , 240 , fermentationRateSize * pixelMultiplier, ILI9341_BLACK);
569
- tft.setCursor (0 , 80 );
607
+ tft.fillRect (0 , 150 , 240 , fermentationRateSize * pixelMultiplier, ILI9341_BLACK);
608
+ tft.setCursor (0 , 150 );
570
609
tft.setTextSize (fermentationRateSize);
571
610
tft.println (rate);
572
611
}
@@ -581,8 +620,9 @@ void displayTime(float elapsedTime)
581
620
tft.println (timeString);
582
621
}
583
622
584
- void displayVoltage (int voltage)
623
+ void displayBattLevel (int voltage)
585
624
{
625
+ clearTopBar ();
586
626
tft.setCursor (160 , 10 );
587
627
tft.setTextSize (1 );
588
628
tft.print (" Batt: " );
@@ -592,10 +632,11 @@ void displayVoltage(int voltage)
592
632
593
633
void displayLowBattAlert ()
594
634
{
595
- tft.setCursor (160 , 30 );
635
+ tft.setCursor (10 , 10 );
596
636
tft.setTextSize (2 );
597
- tft.setTextSize (ILI9341_RED);
637
+ tft.setTextColor (ILI9341_RED);
598
638
tft.println (" LOW BATT" );
639
+ tft.setTextColor (ILI9341_WHITE);
599
640
}
600
641
601
642
String calcTimeToDisplay (float elapsedTime)
@@ -675,5 +716,22 @@ void updateChart(float temp)
675
716
676
717
void mqttCB (char *topic, byte *payload, unsigned int length)
677
718
{
678
- Particle.publish (" mqtt/sub" , topic);
719
+ char pload[length + 1 ];
720
+ memcpy (pload, payload, length);
721
+ pload[length] = NULL ;
722
+
723
+ Serial.printlnf (" Topic: %s" , topic);
724
+ Serial.printlnf (" Payload: %s" , pload);
725
+
726
+ jsonParser.clear ();
727
+ jsonParser.addString (pload);
728
+
729
+ if (jsonParser.parse ())
730
+ {
731
+ String methodName = jsonParser.getReference ().key (" methodName" ).valueString ();
732
+
733
+ Particle.publish (" mqtt/message-method" , methodName);
734
+
735
+ setBrewMode (methodName);
736
+ }
679
737
}
0 commit comments