Skip to content

Commit 2aab358

Browse files
Fix core panic due to HTTPClient
displayCryptoPrice() and taskUpdateHUE() both used the same HTTPClient instance, thus crashing the core once either called http.end()
1 parent 3293db8 commit 2aab358

File tree

2 files changed

+39
-41
lines changed

2 files changed

+39
-41
lines changed

src/system/nixie.h

Lines changed: 28 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#define SH_CP 26 // Clock
2323
#define ST_CP 25 // Latch
2424

25+
bool nixieSetupComplete = false;
2526
bool nixieAutonomous = true;
2627
bool cycleNixies = false;
2728
bool crypto = false;
@@ -44,6 +45,8 @@ int opto2 = 15;
4445
int opto3 = 4;
4546
int opto4 = 5;
4647

48+
HTTPClient httpNIXIE;
49+
4750
// Structs
4851
struct nixieConfigStruct {
4952
char crypto_asset[8];
@@ -93,16 +96,16 @@ int displayCryptoPrice(String ticker, String quote) {
9396
return 0;
9497

9598
// Obtain ticker price from binance
96-
http.useHTTP10(true);
97-
http.begin("https://api.binance.com/api/v3/ticker/price?symbol=" + ticker + quote);
99+
httpNIXIE.useHTTP10(true);
100+
httpNIXIE.begin("https://api.binance.com/api/v3/ticker/price?symbol=" + ticker + quote);
98101

99-
http.GET();
102+
httpNIXIE.GET();
100103

101-
if (http.connected()) {
104+
if (httpNIXIE.connected()) {
102105
DynamicJsonDocument doc(1024);
103-
DeserializationError error = deserializeJson(doc, http.getStream());
106+
DeserializationError error = deserializeJson(doc, httpNIXIE.getStream());
104107

105-
http.end();
108+
httpNIXIE.end();
106109

107110
if (error) {
108111
Serial.print(F("[X] Crypto: Could not deserialize JSON: "));
@@ -196,19 +199,6 @@ String parseNixieConfig(int mode) {
196199

197200
void taskSetupNixie(void* parameter) {
198201
while (!FlashFSready) { vTaskDelay(500); }
199-
200-
// opto-Isolator stuff
201-
pinMode(opto1, OUTPUT);
202-
pinMode(opto2, OUTPUT);
203-
pinMode(opto3, OUTPUT);
204-
pinMode(opto4, OUTPUT);
205-
206-
// Prepare PWM
207-
ledcSetup(1, 200, 8);
208-
ledcAttachPin(opto1, 1);
209-
ledcAttachPin(opto2, 1);
210-
ledcAttachPin(opto3, 1);
211-
ledcAttachPin(opto4, 1);
212202

213203
if (!(LITTLEFS.exists("/config/nixieConfig.json"))) {
214204
Serial.println(F("[T] Nixie: No config found."));
@@ -239,6 +229,7 @@ void taskSetupNixie(void* parameter) {
239229
Serial.println(F("[T] Nixie: Config found."));
240230
}
241231

232+
nixieSetupComplete = true;
242233
vTaskDelete(NULL);
243234
}
244235

@@ -329,12 +320,28 @@ void taskUpdateNixie(void* parameter) {
329320
}
330321

331322
void taskUpdateNixieBrightness(void* parameter) {
332-
while (!FlashFSready) { vTaskDelay(500); }
323+
// Wait for LittleFS and taskSetupNixie to be ready
324+
while (!FlashFSready && !nixieSetupComplete) { vTaskDelay(500); }
325+
326+
// opto-Isolator stuff
327+
pinMode(opto1, OUTPUT);
328+
pinMode(opto2, OUTPUT);
329+
pinMode(opto3, OUTPUT);
330+
pinMode(opto4, OUTPUT);
331+
332+
// Prepare PWM
333+
ledcSetup(1, 200, 8);
334+
ledcAttachPin(opto1, 1);
335+
ledcAttachPin(opto2, 1);
336+
ledcAttachPin(opto3, 1);
337+
ledcAttachPin(opto4, 1);
338+
339+
Serial.println("[T] Nixie: Starting brightness updater...");
333340
for (;;) {
334341
// Set brightness of nixies
335342
ledcWrite(1, parseNixieConfig(5).toInt());
336343

337-
vTaskDelay(20);
344+
vTaskDelay(100);
338345
}
339346
}
340347

src/system/philipsHue.h

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
// VARIABLES
2020
// ---------------------
2121

22-
HTTPClient http;
22+
HTTPClient httpHUE;
2323

2424
// Structs
2525
struct hueConfigStruct {
@@ -77,14 +77,14 @@ int getHueLightIndex() {
7777
String URI = "http://" + parseHUEconfig(1) + "/api/" + parseHUEconfig(2) + "/lights";
7878
int lightsAmount = 0;
7979

80-
http.useHTTP10(true);
80+
httpHUE.useHTTP10(true);
8181

82-
if (http.begin(URI)) {
83-
if (http.GET() > 0) {
82+
if (httpHUE.begin(URI)) {
83+
if (httpHUE.GET() > 0) {
8484
// Deserialize JSON response from HUE bridge
8585
// Ref: https://arduinojson.org/v6/how-to/use-arduinojson-with-httpclient/
8686
DynamicJsonDocument doc(3200);
87-
deserializeJson(doc, http.getStream());
87+
deserializeJson(doc, httpHUE.getStream());
8888

8989
JsonObject root = doc.as<JsonObject>();
9090

@@ -103,7 +103,7 @@ int getHueLightIndex() {
103103
Serial.println("[!] HUE: Cannot init http for lights index.");
104104
}
105105

106-
http.end();
106+
httpHUE.end();
107107

108108
return lightsAmount;
109109
}
@@ -115,21 +115,21 @@ char sendHUELightReq(int lightID, bool state) {
115115
String URI = "http://" + parseHUEconfig(1) + "/api/" + parseHUEconfig(2) + "/lights/" + lightID + "/state";
116116
//Serial.println(URI);
117117

118-
http.begin(URI);
118+
httpHUE.begin(URI);
119119

120120
// POST and return response
121121
String request;
122122

123-
http.addHeader("Content-Type", "application/json");
123+
httpHUE.addHeader("Content-Type", "application/json");
124124
if (state == false) { request = "{\"on\": false}"; }
125125
else { request = "{\"on\": true}"; }
126126

127-
int httpResponse = http.PUT(request);
127+
int httpResponse = httpHUE.PUT(request);
128128

129129
/*Serial.print("[i] HUE: HTTP Response: ");
130130
Serial.println(httpResponse);*/
131131

132-
http.end();
132+
httpHUE.end();
133133

134134
return httpResponse;
135135
}
@@ -142,16 +142,7 @@ void taskSetupHUE(void* paramter) {
142142
Serial.println("[T] HUE: Looking for config...");
143143

144144
// Wait for FS mount
145-
int i = 0;
146-
while (!FlashFSready) {
147-
if (i > 10) {
148-
Serial.println("[X] HUE: FS mount timeout.");
149-
vTaskDelete(NULL);
150-
}
151-
152-
i++;
153-
vTaskDelay(500);
154-
}
145+
while (!FlashFSready) { vTaskDelay(1000); }
155146

156147
// Open HUE config
157148
if (!(LITTLEFS.exists("/config/hueConfig.json"))) {

0 commit comments

Comments
 (0)