Skip to content

Commit 4745b6a

Browse files
author
Brandon Satrom
authored
Merge pull request #44 from bsatrom/feature/mqtt-for-azure
Feature/mqtt for azure
2 parents 17f65c6 + 3311075 commit 4745b6a

File tree

5 files changed

+221
-44
lines changed

5 files changed

+221
-44
lines changed

brew-buddy-firmware/Users/bsatrom/Development/brew-buddy/brew-buddy-firmware/src/brew-buddy-firmware.ino

Whitespace-only changes.

brew-buddy-firmware/project.properties

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@ dependencies.SparkFunMAX31855k=0.0.1
33
dependencies.SdFat=1.0.16
44
dependencies.Adafruit_ILI9341_RK=1.2.1
55
dependencies.Adafruit_ImageReader=1.0.8
6-
dependencies.MQTT=0.4.29
6+
dependencies.MQTT-TLS=0.2.20
7+
dependencies.JsonParserGeneratorRK=0.0.6

brew-buddy-firmware/src/brew-buddy-firmware.cpp

+89-31
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,23 @@
1515
#include "SdFat.h"
1616

1717
// 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"
1920

20-
// App Version Constant
21+
// Json parser for working with MQTT responses from Azure IoT Hub
22+
#include "JsonParserGeneratorRK.h"
2123
void setup();
2224
void loop();
2325
void resetFermentationVariables();
2426
long getFermentationRate();
27+
int checkBatterylevel(String args);
2528
int checkFermentationRate(String args);
2629
int checkTemp(String args);
27-
int checkVoltage();
30+
int getBattPercentage();
2831
void activateBrewStage();
2932
int setBrewMode(String command);
3033
void printSplash();
34+
void clearTopBar();
3135
void clearScreen();
3236
void printHeadingTextLine(String text);
3337
void printSubheadingLine(String text);
@@ -43,15 +47,20 @@ void displayTempHeading();
4347
void displayTempHistoryHeading();
4448
void displayFermentationRate(long rate);
4549
void displayTime(float elapsedTime);
46-
void displayVoltage(int voltage);
50+
void displayBattLevel(int voltage);
4751
void displayLowBattAlert();
4852
String calcTimeToDisplay(float elapsedTime);
4953
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
5158
#define APP_VERSION "v1.0"
5259

5360
SYSTEM_THREAD(ENABLED);
5461

62+
String deviceID;
63+
5564
// Thermocouple Variables
5665
const uint8_t CHIP_SELECT_PIN = D4;
5766
// SCK, MISO & MOSI are defined on Particle 3rd Gen HW at A6-A8
@@ -74,7 +83,7 @@ int32_t width = 0, // BMP image dimensions
7483
height = 0;
7584

7685
#define TFT_SPEED 120000000
77-
#define BATT_LOW_VOLTAGE 90
86+
#define BATT_LOW_VOLTAGE 30
7887

7988
// SD Card
8089
SdFat sd;
@@ -139,6 +148,7 @@ MQTT client("brew-buddy-hub.azure-devices.net", 8883, mqttCB);
139148
void setup()
140149
{
141150
Serial.begin(9600);
151+
deviceID = System.deviceID();
142152

143153
pinMode(KNOCK_PIN, INPUT_PULLDOWN);
144154

@@ -150,6 +160,7 @@ void setup()
150160
Particle.function("setMode", setBrewMode);
151161
Particle.function("checkTemp", checkTemp);
152162
Particle.function("checkRate", checkFermentationRate);
163+
Particle.function("checkBatt", checkBatterylevel);
153164

154165
// Initialize TFT
155166
tft.begin(TFT_SPEED);
@@ -171,22 +182,30 @@ void setup()
171182
printSubheadingLine("Waiting for Brew...");
172183

173184
// 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");
176192

177193
//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");
179196
if (client.isConnected())
180197
{
181198
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");
182204
}
183205
else
184206
{
185207
Particle.publish("mqtt/status", "failed");
186-
} */
187-
188-
Particle.publish("Version", APP_VERSION);
189-
Particle.publish("Status", "Brew Buddy Online");
208+
}
190209
}
191210

192211
void loop()
@@ -208,7 +227,7 @@ void loop()
208227
lastKnock = fermentationStartTime;
209228

210229
clearScreen();
211-
tft.setCursor(0, 10);
230+
tft.setCursor(0, 30);
212231
tft.setTextColor(ILI9341_YELLOW);
213232
tft.setTextSize(2);
214233
tft.print("Fermentation started");
@@ -265,13 +284,14 @@ void loop()
265284

266285
if (currentMillis - previousBattMillis > battInterval)
267286
{
268-
int voltage = checkVoltage();
287+
previousBattMillis = millis();
269288

270-
displayVoltage(voltage);
271-
Particle.publish(messageBase + "voltage", String(voltage));
289+
int battLevel = getBattPercentage();
272290

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)
275295
{
276296
displayLowBattAlert();
277297
Particle.publish(messageBase + "alert", "low-batt");
@@ -291,6 +311,11 @@ void loop()
291311
postFermentationRate();
292312
}
293313
}
314+
315+
if (client.isConnected())
316+
{
317+
client.loop();
318+
}
294319
}
295320

296321
void resetFermentationVariables()
@@ -319,6 +344,14 @@ long getFermentationRate()
319344
return rate;
320345
}
321346

347+
int checkBatterylevel(String args)
348+
{
349+
int battLevel = getBattPercentage();
350+
Particle.publish(messageBase + "batt-level", String(battLevel));
351+
352+
return 1;
353+
}
354+
322355
int checkFermentationRate(String args)
323356
{
324357
if (isFermentationMode)
@@ -344,18 +377,17 @@ int checkTemp(String args)
344377
return 0;
345378
}
346379

347-
int checkVoltage()
380+
int getBattPercentage()
348381
{
349382
int voltage = analogRead(BATT) * 0.0011224;
350383

351-
return (int)((voltage / 4.2) * 100 + 0.5)
384+
return (int)((voltage / 4.2) * 100 + 0.5);
352385
}
353386

354387
void activateBrewStage()
355388
{
356389
startTime = millis();
357390

358-
displayStageName(brewStage);
359391
displayTempHeading();
360392
displayTempHistoryHeading();
361393
displayTimeHeading();
@@ -444,9 +476,16 @@ void printSplash()
444476
delay(3000);
445477
}
446478

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+
447486
void clearScreen()
448487
{
449-
tft.fillScreen(ILI9341_BLACK);
488+
tft.fillRect(0, 30, 240, 320, ILI9341_BLACK);
450489
tft.setCursor(0, 0);
451490
tft.setTextColor(ILI9341_WHITE);
452491
}
@@ -526,7 +565,7 @@ void displayStageName(String stagename)
526565

527566
void displayFermentationHeading()
528567
{
529-
tft.setCursor(0, 100);
568+
tft.setCursor(0, 110);
530569
tft.setTextSize(2);
531570
tft.println("Fermentation Rate");
532571
tft.println("(in seconds)");
@@ -536,7 +575,7 @@ void displayFermentationModeDelta()
536575
{
537576
unsigned long fermentationDelta = fermentationStartTime - fermentationModeStartTime;
538577

539-
tft.setCursor(0, 40);
578+
tft.setCursor(0, 60);
540579
tft.setTextSize(2);
541580
tft.println("Time to fermentation (hours)");
542581
tft.println(fermentationDelta / 36000000.00);
@@ -565,8 +604,8 @@ void displayTempHistoryHeading()
565604

566605
void displayFermentationRate(long rate)
567606
{
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);
570609
tft.setTextSize(fermentationRateSize);
571610
tft.println(rate);
572611
}
@@ -581,8 +620,9 @@ void displayTime(float elapsedTime)
581620
tft.println(timeString);
582621
}
583622

584-
void displayVoltage(int voltage)
623+
void displayBattLevel(int voltage)
585624
{
625+
clearTopBar();
586626
tft.setCursor(160, 10);
587627
tft.setTextSize(1);
588628
tft.print("Batt: ");
@@ -592,10 +632,11 @@ void displayVoltage(int voltage)
592632

593633
void displayLowBattAlert()
594634
{
595-
tft.setCursor(160, 30);
635+
tft.setCursor(10, 10);
596636
tft.setTextSize(2);
597-
tft.setTextSize(ILI9341_RED);
637+
tft.setTextColor(ILI9341_RED);
598638
tft.println("LOW BATT");
639+
tft.setTextColor(ILI9341_WHITE);
599640
}
600641

601642
String calcTimeToDisplay(float elapsedTime)
@@ -675,5 +716,22 @@ void updateChart(float temp)
675716

676717
void mqttCB(char *topic, byte *payload, unsigned int length)
677718
{
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+
}
679737
}

0 commit comments

Comments
 (0)