Skip to content

Commit 13c6204

Browse files
author
Brandon Satrom
committed
add batt check logic and low batt notification; closes #38; closes #35
1 parent 3dcb042 commit 13c6204

File tree

3 files changed

+221
-34
lines changed

3 files changed

+221
-34
lines changed

brew-buddy-firmware/project.properties

+1
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ 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

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

+135-21
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,17 @@
1414
// Include for SD Card reader on TFT
1515
#include "SdFat.h"
1616

17+
// Include MQTT Library to enable the Azure IoT Hub to call back to our device
18+
#include "MQTT.h"
19+
1720
// App Version Constant
1821
void setup();
1922
void loop();
2023
void resetFermentationVariables();
2124
long getFermentationRate();
25+
int checkFermentationRate(String args);
26+
int checkTemp(String args);
27+
int checkVoltage();
2228
void activateBrewStage();
2329
int setBrewMode(String command);
2430
void printSplash();
@@ -31,14 +37,17 @@ void postFermentationRate();
3137
void printReading(float reading);
3238
void displayStageName(String stagename);
3339
void displayFermentationHeading();
40+
void displayFermentationModeDelta();
3441
void displayTimeHeading();
3542
void displayTempHeading();
3643
void displayTempHistoryHeading();
3744
void displayFermentationRate(long rate);
3845
void displayTime(float elapsedTime);
46+
void displayVoltage(int voltage);
47+
void displayLowBattAlert();
3948
String calcTimeToDisplay(float elapsedTime);
4049
void updateChart(float temp);
41-
#line 16 "/Users/bsatrom/Development/brew-buddy/brew-buddy-firmware/src/brew-buddy-firmware.ino"
50+
#line 19 "/Users/bsatrom/Development/brew-buddy/brew-buddy-firmware/src/brew-buddy-firmware.ino"
4251
#define APP_VERSION "v1.0"
4352

4453
SYSTEM_THREAD(ENABLED);
@@ -65,6 +74,7 @@ int32_t width = 0, // BMP image dimensions
6574
height = 0;
6675

6776
#define TFT_SPEED 120000000
77+
#define BATT_LOW_VOLTAGE 90
6878

6979
// SD Card
7080
SdFat sd;
@@ -85,6 +95,10 @@ float previousTemp = 0;
8595
unsigned long postInterval = 120000;
8696
unsigned long previousPostMillis = 0;
8797

98+
// Timing variables for checking the battery state
99+
unsigned long battInterval = 300000;
100+
unsigned long previousBattMillis = 0;
101+
88102
//Text Size Variables
89103
const uint8_t headingTextSize = 4;
90104
const uint8_t subheadTextSize = 2;
@@ -113,9 +127,15 @@ QueueArray<long> knockArray;
113127
float fermentationRate = 0; // knocks per ms
114128
unsigned long lastKnock;
115129

130+
String messageBase = "bb/";
131+
116132
String brewStage;
117133
String brewId;
118134

135+
// MQTT Callback fw declaration and client
136+
void mqttCB(char *topic, byte *payload, unsigned int length);
137+
MQTT client("brew-buddy-hub.azure-devices.net", 8883, mqttCB);
138+
119139
void setup()
120140
{
121141
Serial.begin(9600);
@@ -128,6 +148,8 @@ void setup()
128148

129149
//Brew Stage cloud functions
130150
Particle.function("setMode", setBrewMode);
151+
Particle.function("checkTemp", checkTemp);
152+
Particle.function("checkRate", checkFermentationRate);
131153

132154
// Initialize TFT
133155
tft.begin(TFT_SPEED);
@@ -148,12 +170,29 @@ void setup()
148170
tft.fillScreen(ILI9341_BLACK);
149171
printSubheadingLine("Waiting for Brew...");
150172

173+
// Check and display the battery level
174+
int voltage = checkVoltage();
175+
displayVoltage(voltage);
176+
177+
//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");
179+
if (client.isConnected())
180+
{
181+
Particle.publish("mqtt/status", "connected");
182+
}
183+
else
184+
{
185+
Particle.publish("mqtt/status", "failed");
186+
} */
187+
151188
Particle.publish("Version", APP_VERSION);
152189
Particle.publish("Status", "Brew Buddy Online");
153190
}
154191

155192
void loop()
156193
{
194+
unsigned long currentMillis = millis();
195+
157196
if (isFermentationMode)
158197
{
159198
int16_t knockVal = analogRead(KNOCK_PIN) / 16;
@@ -176,10 +215,10 @@ void loop()
176215
tft.setTextColor(ILI9341_WHITE);
177216

178217
displayFermentationHeading();
179-
// TODO: print delta btw mode start and fermentation start
218+
displayFermentationModeDelta();
180219

181220
waitUntil(Particle.connected);
182-
Particle.publish("fermentation/state", "start");
221+
Particle.publish(messageBase + "ferment/state", "start");
183222
}
184223
else
185224
{
@@ -193,8 +232,6 @@ void loop()
193232
}
194233
else if (isBrewingMode)
195234
{
196-
unsigned long currentMillis = millis();
197-
198235
if (currentMillis - previousTempMillis > tempInterval)
199236
{
200237
previousTempMillis = currentMillis;
@@ -224,19 +261,34 @@ void loop()
224261

225262
displayTime(elapsedTime);
226263
}
264+
}
227265

228-
if (currentMillis - previousPostMillis > postInterval)
266+
if (currentMillis - previousBattMillis > battInterval)
267+
{
268+
int voltage = checkVoltage();
269+
270+
displayVoltage(voltage);
271+
Particle.publish(messageBase + "voltage", String(voltage));
272+
273+
// If voltage < 30%, show a low batt message and publish message to cloud
274+
if (voltage < BATT_LOW_VOLTAGE)
229275
{
230-
previousPostMillis = millis();
276+
displayLowBattAlert();
277+
Particle.publish(messageBase + "alert", "low-batt");
278+
}
279+
}
231280

232-
if (isBrewingMode)
233-
{
234-
postTemp(lastTemp);
235-
}
236-
else if (isFermentationMode)
237-
{
238-
postFermentationRate();
239-
}
281+
if (currentMillis - previousPostMillis > postInterval)
282+
{
283+
previousPostMillis = millis();
284+
285+
if (isBrewingMode)
286+
{
287+
postTemp(lastTemp);
288+
}
289+
else if (isFermentationMode)
290+
{
291+
postFermentationRate();
240292
}
241293
}
242294
}
@@ -267,6 +319,38 @@ long getFermentationRate()
267319
return rate;
268320
}
269321

322+
int checkFermentationRate(String args)
323+
{
324+
if (isFermentationMode)
325+
{
326+
postFermentationRate();
327+
328+
return 1;
329+
}
330+
331+
return 0;
332+
}
333+
334+
int checkTemp(String args)
335+
{
336+
if (isBrewingMode)
337+
{
338+
float temp = readTemp();
339+
postTemp(temp);
340+
341+
return 1;
342+
}
343+
344+
return 0;
345+
}
346+
347+
int checkVoltage()
348+
{
349+
int voltage = analogRead(BATT) * 0.0011224;
350+
351+
return (int)((voltage / 4.2) * 100 + 0.5)
352+
}
353+
270354
void activateBrewStage()
271355
{
272356
startTime = millis();
@@ -398,14 +482,12 @@ float readTemp()
398482

399483
void postTemp(float temp)
400484
{
401-
String payload = "{ \"temperature\":" + String(temp, 2) + ", \"time\": \"" + millis() + "\" }";
402-
Particle.publish("brewing/temp", payload);
485+
Particle.publish(messageBase + "brew/temp", String(temp, 2));
403486
}
404487

405488
void postFermentationRate()
406489
{
407-
String payload = "{ \"current_rate\":" + String(fermentationRate, 2) + ", \"time\": \"" + millis() + "\" }";
408-
Particle.publish("fermentation/rate", payload);
490+
Particle.publish(messageBase + "ferment/rate", String(fermentationRate, 2));
409491
}
410492

411493
void printReading(float reading)
@@ -423,7 +505,7 @@ void printReading(float reading)
423505
}
424506
else if (reading > 110)
425507
{
426-
tft.setTextSize(ILI9341_RED);
508+
tft.setTextColor(ILI9341_RED);
427509
}
428510

429511
tft.setTextSize(tempTextSize);
@@ -444,12 +526,22 @@ void displayStageName(String stagename)
444526

445527
void displayFermentationHeading()
446528
{
447-
tft.setCursor(0, 40);
529+
tft.setCursor(0, 100);
448530
tft.setTextSize(2);
449531
tft.println("Fermentation Rate");
450532
tft.println("(in seconds)");
451533
}
452534

535+
void displayFermentationModeDelta()
536+
{
537+
unsigned long fermentationDelta = fermentationStartTime - fermentationModeStartTime;
538+
539+
tft.setCursor(0, 40);
540+
tft.setTextSize(2);
541+
tft.println("Time to fermentation (hours)");
542+
tft.println(fermentationDelta / 36000000.00);
543+
}
544+
453545
void displayTimeHeading()
454546
{
455547
tft.setCursor(0, 120);
@@ -489,6 +581,23 @@ void displayTime(float elapsedTime)
489581
tft.println(timeString);
490582
}
491583

584+
void displayVoltage(int voltage)
585+
{
586+
tft.setCursor(160, 10);
587+
tft.setTextSize(1);
588+
tft.print("Batt: ");
589+
tft.print(voltage);
590+
tft.println("%");
591+
}
592+
593+
void displayLowBattAlert()
594+
{
595+
tft.setCursor(160, 30);
596+
tft.setTextSize(2);
597+
tft.setTextSize(ILI9341_RED);
598+
tft.println("LOW BATT");
599+
}
600+
492601
String calcTimeToDisplay(float elapsedTime)
493602
{
494603
String padZero = "0";
@@ -563,3 +672,8 @@ void updateChart(float temp)
563672
tempGraphArray.enqueue(currentLocation);
564673
}
565674
}
675+
676+
void mqttCB(char *topic, byte *payload, unsigned int length)
677+
{
678+
Particle.publish("mqtt/sub", topic);
679+
}

0 commit comments

Comments
 (0)