Skip to content

Commit a02cb6f

Browse files
Fixed low memory issues in configurations with many sensors and a ST7735 Bug
1 parent e106ce2 commit a02cb6f

File tree

5 files changed

+136
-81
lines changed

5 files changed

+136
-81
lines changed

Sensatio_FW_ESP8266.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
SOURCE: https://github.com/sensate-io/firmware-esp8266.git
1212
1313
@section HISTORY
14+
v42 - Fixed low memory issues in configurations with many sensors and a ST7735 Bug
1415
v41 - Changed IDE, Sensatio, Renamed Display Class to support more types
1516
v40 - New Display Structure to enable Display Rotation, different Styles etc.
1617
v39 - ReAdded Support for VEML6075 and SI1145 UVI Sensors, added auto-reinit if sensor fails
@@ -31,8 +32,8 @@
3132
VisualisationHelper* vHelper;
3233
Display* display = NULL;
3334

34-
int currentVersion = 41;
35-
boolean printMemory = false;
35+
const int currentVersion = 42;
36+
const boolean printMemory = false;
3637

3738
//String board = "Generic";
3839
//char firmwareType[] = "ESP8266";

src/controller/Bridge.cpp

Lines changed: 92 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
SOURCE: https://github.com/sensate-io/firmware-esp8266.git
1212
1313
@section HISTORY
14+
v42 - Fixed low memory issues in configurations with many sensors and a ST7735 Bug
1415
v41 - Renamed Display Class to support more types
1516
v40 - New Display Structure to enable Display Rotation, different Styles etc.
1617
v36 - Greatly improved reliability of connectivity
@@ -44,6 +45,7 @@ extern int displayMode;
4445
extern int displayHeight;
4546
extern int displayWidth;
4647
extern int displayRotation;
48+
extern boolean printMemory;
4749

4850
extern struct rst_info resetInfo;
4951

@@ -80,6 +82,9 @@ int sensorCycle = 1;
8082

8183
extern VisualisationHelper* vHelper;
8284

85+
int portNumber = 0;
86+
bool foundPorts = false;
87+
8388
std::unique_ptr<BearSSL::WiFiClientSecure>sslClient(new BearSSL::WiFiClientSecure);
8489

8590
bool initSSL()
@@ -360,10 +365,11 @@ bool getBridgeConfig() {
360365
{
361366
Serial.print("o");
362367
String payload = httpClient.getString();
368+
httpClient.end();
363369

364-
int portNumber = 0;
370+
portNumber = 0;
371+
foundPorts = false;
365372

366-
bool foundPorts = false;
367373
configRetry=0;
368374

369375
if (payload != NULL && payload != "")
@@ -387,46 +393,67 @@ bool getBridgeConfig() {
387393
}
388394
}
389395
}
396+
397+
configureBridge(bridgeConfig);
398+
yield();
399+
390400
if(bridgeConfig.containsKey("p"))
391401
{
392-
configureBridge(bridgeConfig);
402+
JsonArray& portConfigArray = bridgeConfig["p"];
403+
handlePortConfigArray(portConfigArray);
393404
yield();
394405

395-
JsonArray& configArray = bridgeConfig["p"];
406+
}
407+
if(bridgeConfig.containsKey("ppc"))
408+
{
409+
int portPageCount = bridgeConfig["ppc"];
410+
Serial.println("Config contains "+String(portPageCount)+ " port pages");
396411

397-
for (JsonObject& configEntry : configArray) {
398-
399-
int portRow = portNumber;
412+
for(int i=1;i<portPageCount;i++)
413+
{
414+
Serial.println("Fetching port page "+String(i));
400415

401-
if (configEntry.containsKey("sod") && !configEntry["sod"])
402-
{
403-
portRow = -1;
404-
}
405-
else
406-
{
407-
portNumber++;
408-
configureDisplayValueData(portRow, configEntry);
409-
}
410-
411-
if (configEntry.containsKey("et"))
412-
configureExpansionPort(portRow, configEntry);
413-
else
414-
configurePort(portRow, configEntry);
416+
String pageUrlString = bridgeURL + "/" + apiVersion + "/bridge/" + getUUID() + "/" + String(i);
417+
httpClient.begin(*sslClient, pageUrlString);
415418

416-
foundPorts = true;
417-
yield();
418-
}
419+
httpClient.addHeader("Content-Type", "application/json");
419420

420-
if(display!=NULL)
421-
{
422-
display->clear(false);
423-
display->drawProductLogo();
424-
display->drawString(0, 10, "Waiting for sensors...");
425-
initVisualisationHelper(bridgeConfig);
426-
}
421+
Serial.print("g");
422+
int pageHttpCode = httpClient.GET();
423+
424+
if (pageHttpCode == HTTP_CODE_OK)
425+
{
426+
Serial.print("o");
427+
428+
String pagePayload = httpClient.getString();
429+
httpClient.end();
427430

431+
if (pagePayload != NULL && pagePayload != "")
432+
{
433+
JsonObject& bridgePortConfig = jsonBuffer.parseObject(pagePayload);
434+
if(bridgePortConfig.containsKey("p"))
435+
{
436+
JsonArray& pagedPortConfigArray = bridgePortConfig["p"];
437+
handlePortConfigArray(pagedPortConfigArray);
438+
}
439+
440+
}
441+
}
442+
else
443+
httpClient.end();
444+
445+
yield();
446+
}
428447
}
429448

449+
if(display!=NULL)
450+
{
451+
display->clear(false);
452+
display->drawProductLogo();
453+
display->drawString(0, 10, "Waiting for sensors...");
454+
initVisualisationHelper(bridgeConfig);
455+
}
456+
430457
jsonBuffer.clear();
431458

432459
httpClient.end();
@@ -461,6 +488,39 @@ bool getBridgeConfig() {
461488

462489
}
463490

491+
void handlePortConfigArray(JsonArray& portConfigArray) {
492+
493+
for (JsonObject& configEntry : portConfigArray) {
494+
495+
int portRow = portNumber;
496+
497+
if (configEntry.containsKey("sod") && !configEntry["sod"])
498+
{
499+
portRow = -1;
500+
}
501+
else
502+
{
503+
portNumber++;
504+
configureDisplayValueData(portRow, configEntry);
505+
}
506+
507+
if (configEntry.containsKey("et"))
508+
configureExpansionPort(portRow, configEntry);
509+
else
510+
configurePort(portRow, configEntry);
511+
512+
foundPorts = true;
513+
yield();
514+
}
515+
516+
if(printMemory)
517+
{
518+
Serial.print("HEAP: ");
519+
Serial.println(ESP.getFreeHeap());
520+
}
521+
522+
}
523+
464524
void configureBridge(JsonObject& bridgeConfig) {
465525

466526
powerMode = bridgeConfig["pm"];
@@ -552,7 +612,7 @@ void configureBridge(JsonObject& bridgeConfig) {
552612
}
553613
}
554614

555-
if(requireInitI2C)
615+
if(requireInitI2C && (i2cSCLPort != i2cSDAPort))
556616
{
557617
Serial.println("Init I2C Bus: SDA-"+String(i2cSDAPort)+", SCL-"+String(i2cSCLPort));
558618
Wire.begin(i2cSDAPort, i2cSCLPort);

src/controller/Bridge.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
SOURCE: https://github.com/sensate-io/firmware-esp8266.git
1212
1313
@section HISTORY
14+
v42 - Fixed low memory issues in configurations with many sensors and a ST7735 Bug
1415
v40 - New Display Structure to enable Display Rotation, different Styles etc.
1516
v36 - Greatly improved reliability of connectivity
1617
v35 - Added Support for VEML6075 and SI1145 UVI Sensors
@@ -71,5 +72,6 @@ void doPowerSaving();
7172
void doPowerSavingInit(boolean);
7273
uint8_t translateGPIOPort(String);
7374
void tryInitMQTT();
75+
void handlePortConfigArray(JsonArray&);
7476

7577
#endif

src/output/display/DisplayST7735.cpp

Lines changed: 37 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
SOURCE: https://github.com/sensate-io/firmware-esp8266.git
1212
1313
@section HISTORY
14+
v42 - Fixed Umlaut Handling
1415
v41 - New Display Type ST7735, New Display Mode
1516
*/
1617
/**************************************************************************/
@@ -76,9 +77,9 @@ void DisplayST7735::drawData(unsigned long currentMillis) {
7677
{
7778
DisplayValueData* displayValueData = vHelper->getDataForPosition(currentMillis, i);
7879
if(displayValueData!=NULL)
79-
drawValue(i, displayValueData->getName(), displayValueData->getShortName(), displayValueData->getValue(), displayValueData->getUnit());
80+
drawValue(i, displayValueData->getName(), displayValueData->getShortName(), displayValueData->getValue(), displayValueData->getUnit());
8081
else
81-
clearValue(i);
82+
clearValue(i);
8283
}
8384

8485
}
@@ -96,11 +97,11 @@ void DisplayST7735::drawValue(int position, String name, String shortName, Strin
9697
switch(displayMode)
9798
{
9899
case 2:
99-
drawValueClassicRA(position, name, shortName, valueString);
100-
break;
100+
drawValueClassicRA(position, name, shortName, valueString);
101+
break;
101102
default:
102-
drawValueClassic(position, name, shortName, valueString);
103-
break;
103+
drawValueClassic(position, name, shortName, valueString);
104+
break;
104105
}
105106

106107
}
@@ -109,32 +110,20 @@ void DisplayST7735::drawValueClassic(int position, String name, String shortName
109110

110111
if(!isResetting && displayEnabled)
111112
{
112-
name.replace("ä", "ae");
113-
name.replace("ü", "ue");
114-
name.replace("ö", "oe");
115-
name.replace("ß", "sz");
116-
name.replace("Ä", "Ae");
117-
name.replace("Ü", "Ue");
118-
name.replace("Ö", "Oe");
119-
120-
String text = name+": "+valueString;
121-
if(getStringWidth(text,8)>=displayWidth)
122-
{
123-
shortName.replace("ä", "ae");
124-
shortName.replace("ü", "ue");
125-
shortName.replace("ö", "oe");
126-
shortName.replace("ß", "sz");
127-
shortName.replace("Ä", "Ae");
128-
shortName.replace("Ü", "Ue");
129-
shortName.replace("Ö", "Oe");
130-
text = shortName+": "+valueString;
131-
}
113+
114+
valueString.replace("°",String((char)0xF7));
115+
116+
String text = replaceUmlauts(name)+": "+valueString;
117+
118+
if(getStringWidth(text,8)>=displayWidth)
119+
{
120+
text = replaceUmlauts(shortName)+": "+valueString;
121+
}
132122

133123
display->setCursor(0, position*16); // @suppress("Method cannot be resolved")
134124
display->setTextColor(ST7735_WHITE, ST7735_BLACK); // @suppress("Method cannot be resolved")
135125
display->print(text); // @suppress("Method cannot be resolved")
136126

137-
138127
int diffWidth = displayWidth - getStringWidth(text,8);
139128
if(diffWidth>0)
140129
{
@@ -149,34 +138,22 @@ void DisplayST7735::drawValueClassicRA(int position, String name, String shortNa
149138

150139
if(!isResetting && displayEnabled)
151140
{
141+
152142
display->setTextColor(ST7735_WHITE, ST7735_BLACK); // @suppress("Method cannot be resolved")
153143

144+
valueString.replace("°",String((char)0xF7));
145+
154146
int valueWidth = getStringWidth(valueString,8);
155147
int valuePosition = displayWidth-valueWidth;
156148

157149
display->setCursor(valuePosition, position*16); // @suppress("Method cannot be resolved")
158150
display->print(valueString); // @suppress("Method cannot be resolved")
159151

160-
name.replace("ä", "ae");
161-
name.replace("ü", "ue");
162-
name.replace("ö", "oe");
163-
name.replace("ß", "sz");
164-
name.replace("Ä", "Ae");
165-
name.replace("Ü", "Ue");
166-
name.replace("Ö", "Oe");
167-
168-
String text = name+":";
169-
if(getStringWidth(text,8)>=valuePosition)
170-
{
171-
shortName.replace("ä", "ae");
172-
shortName.replace("ü", "ue");
173-
shortName.replace("ö", "oe");
174-
shortName.replace("ß", "sz");
175-
shortName.replace("Ä", "Ae");
176-
shortName.replace("Ü", "Ue");
177-
shortName.replace("Ö", "Oe");
178-
text = shortName+":";
179-
}
152+
String text = replaceUmlauts(name)+":";
153+
if(getStringWidth(text,8)>=valuePosition)
154+
{
155+
text = replaceUmlauts(shortName)+":";
156+
}
180157

181158
display->setCursor(0, position*16); // @suppress("Method cannot be resolved")
182159
display->print(text); // @suppress("Method cannot be resolved")
@@ -257,3 +234,16 @@ int DisplayST7735::getStringWidth(String text, int textHeight) {
257234
return w;
258235

259236
}
237+
238+
String DisplayST7735::replaceUmlauts(String original)
239+
{
240+
original.replace("ä", String((char)0x84));
241+
original.replace("ü", String((char)0x81));
242+
original.replace("ö", String((char)0x94));
243+
original.replace("ß", String((char)0xE0));
244+
original.replace("Ä", String((char)0x8E));
245+
original.replace("Ü", String((char)0x9A));
246+
original.replace("Ö", String((char)0x99));
247+
248+
return original;
249+
}

src/output/display/DisplayST7735.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
SOURCE: https://github.com/sensate-io/firmware-esp8266.git
1212
1313
@section HISTORY
14+
v42 - Fixed Umlaut Handling
1415
v41 - New Display Type ST7735, New Display Mode
1516
*/
1617
/**************************************************************************/
@@ -26,6 +27,7 @@ class DisplayST7735 : public Display {
2627
private:
2728
Adafruit_ST7735 *display;
2829
int type;
30+
String replaceUmlauts(String original);
2931
public:
3032
DisplayST7735 (bool, int);
3133
void drawProductLogo();

0 commit comments

Comments
 (0)