Skip to content

Commit fb25ce4

Browse files
committed
Platform io platform version set to 3.0.2
Prevent system crash with Exception (0) due to software serial bug in the latest core version. esp8266/Arduino#8830
1 parent a12b778 commit fb25ce4

File tree

5 files changed

+291
-420
lines changed

5 files changed

+291
-420
lines changed

platformio.ini

+11-9
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,21 @@
99
; https://docs.platformio.org/page/projectconf.html
1010

1111
[env:d1_mini]
12-
platform = espressif8266
12+
platform = espressif8266@^3.2.0 ; software serial Exception 0 bug will be fixed in espressif8266 core 3.1.2 https://github.com/esp8266/Arduino/issues/8830
1313
board = d1_mini
1414
framework = arduino
15-
upload_speed = 115200
15+
upload_speed = 57600
1616
monitor_speed = 115200
1717
board_build.filesystem = littlefs
18+
lib_compat_mode = strict
19+
lib_ldf_mode = chain+
1820
upload_port = /dev/ttyUSB0
1921
lib_deps =
2022
bblanchon/ArduinoJson@^6.19.4
21-
knolleary/PubSubClient @ ^2.8
22-
alanswx/ESPAsyncWiFiManager@^0.31
23-
me-no-dev/ESP Async WebServer@^1.2.3
24-
me-no-dev/ESPAsyncTCP@^1.2.2
25-
adafruit/Adafruit AHTX0@^2.0.3
26-
adafruit/Adafruit BMP280 Library@^2.6.6
27-
adafruit/Adafruit SGP30 Sensor@^2.0.0
23+
sparkfun/SparkFun SGP30 Arduino Library@^1.0.5
24+
sparkfun/SparkFun Qwiic Humidity AHT20@^1.0.3
25+
arkhipenko/TaskScheduler@^3.7.0
26+
knolleary/PubSubClient @^2.8
27+
https://github.com/khoih-prog/ESPAsyncTCP
28+
https://github.com/khoih-prog/ESPAsyncWebServer
29+
https://github.com/alanswx/ESPAsyncWiFiManager

src/Config.h

+15-6
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ namespace Config
1111
char mqtt_server[80] = "";
1212
char username[24] = "";
1313
char password[24] = "";
14+
15+
uint16_t sgp30ECo2Base = 0;
16+
uint16_t sgp30TvocBase = 0;
1417

1518
bool save()
1619
{
@@ -19,37 +22,40 @@ namespace Config
1922
json["username"] = username;
2023
json["password"] = password;
2124

25+
json["sgp_eco2_base"] = sgp30ECo2Base;
26+
json["sgp_tvoc_base"] = sgp30TvocBase;
27+
2228
File configFile = LittleFS.open(CONFIG_FILE_PATH, "w");
2329
if (!configFile)
2430
{
25-
Serial.println("Failed to open configurationfile for reading");
31+
Serial.println(F("Failed to open configurationfile for reading"));
2632
return false;
2733
}
2834

2935
serializeJson(json, configFile);
3036
configFile.close();
3137

3238
return true;
33-
}
39+
}
3440

3541
bool load()
3642
{
3743
if (!LittleFS.begin())
3844
{
39-
Serial.println("An Error has occurred while mounting LittleFS");
45+
Serial.println(F("An Error has occurred while mounting LittleFS"));
4046
return false;
4147
}
4248

4349
if (!LittleFS.exists(CONFIG_FILE_PATH))
4450
{
45-
Serial.println("Configuration file does not exist");
51+
Serial.println(F("Configuration file does not exist"));
4652
return false;
4753
}
4854

4955
File configFile = LittleFS.open(CONFIG_FILE_PATH, "r");
5056
if (!configFile)
5157
{
52-
Serial.println("Failed to open configurationfile for reading");
58+
Serial.println(F("Failed to open configurationfile for reading"));
5359
return false;
5460
}
5561

@@ -61,14 +67,17 @@ namespace Config
6167
DeserializationError error = deserializeJson(json, buf.get());
6268
if (error)
6369
{
64-
Serial.println("JSON deserialization failed");
70+
Serial.println(F("JSON deserialization failed"));
6571
return false;
6672
}
6773

6874
strcpy(mqtt_server, json["mqtt_server"]);
6975
strcpy(username, json["username"]);
7076
strcpy(password, json["password"]);
7177

78+
sgp30ECo2Base = json["sgp_eco2_base"].as<uint16_t>();
79+
sgp30TvocBase = json["sgp_tvoc_base"].as<uint16_t>();
80+
7281
return true;
7382
}
7483
} // namespace Config

src/SerialCom.h

+8-37
Original file line numberDiff line numberDiff line change
@@ -5,30 +5,18 @@
55
#include "Types.h"
66

77
namespace SerialCom {
8-
constexpr static const uint8_t PIN_UART_RX = 14; // receive sensor response
9-
constexpr static const uint8_t PIN_UART_TX = 12; // send request
8+
constexpr static const uint8_t PIN_UART_RX = 13; // D2 on Wemos D1 Mini
9+
constexpr static const uint8_t PIN_UART_TX = 16; // UNUSED
1010

1111
SoftwareSerial sensorSerial(PIN_UART_RX, PIN_UART_TX);
1212

13-
const byte init_request[] = {0x11, 0x03, 0x0c, 0x02, 0x1e, 0xc0};
14-
const byte request[] = {0x11, 0x02, 0x0b, 0x01, 0xe1};
15-
1613
uint8_t serialRxBuf[255];
1714
uint8_t rxBufIdx = 0;
1815

1916
void setup() {
2017
sensorSerial.begin(9600);
2118
}
2219

23-
void init() {
24-
sensorSerial.write(init_request, sizeof(init_request));
25-
}
26-
27-
void sendRequest() {
28-
Serial.println("Request sent...");
29-
sensorSerial.write(request, sizeof(request));
30-
}
31-
3220
void clearRxBuf() {
3321
// Clear everything for the next message
3422
memset(serialRxBuf, 0, sizeof(serialRxBuf));
@@ -42,20 +30,17 @@ namespace SerialCom {
4230
*/
4331
const uint16_t pm25 = (serialRxBuf[5] << 8) | serialRxBuf[6];
4432

45-
Serial.printf("Received PM 2.5 reading: %d\n", pm25);
46-
47-
state.measurements[state.measurementIdx] = pm25;
33+
state.measurements += pm25;
4834

4935
state.measurementIdx = (state.measurementIdx + 1) % 5;
5036

5137
if (state.measurementIdx == 0) {
5238
float avgPM25 = 0.0f;
5339

54-
for (uint8_t i = 0; i < 5; ++i) {
55-
avgPM25 += state.measurements[i] / 5.0f;
56-
}
40+
avgPM25 = state.measurements / 5.0f;
5741

5842
state.avgPM25 = avgPM25;
43+
state.measurements = 0;
5944
state.valid = true;
6045

6146
Serial.printf("New Avg PM25: %d\n", state.avgPM25);
@@ -67,8 +52,8 @@ namespace SerialCom {
6752
bool isValidHeader() {
6853
bool headerValid = serialRxBuf[0] == 0x16 && serialRxBuf[1] == 0x11 && serialRxBuf[2] == 0x0B;
6954

70-
if (!headerValid) {
71-
Serial.println("Received message with invalid header.");
55+
if (! headerValid) {
56+
Serial.println(F("Received message with invalid header."));
7257
}
7358

7459
return headerValid;
@@ -93,34 +78,20 @@ namespace SerialCom {
9378
return;
9479
}
9580

96-
Serial.print("Receiving:");
9781
while (sensorSerial.available()) {
9882
serialRxBuf[rxBufIdx++] = sensorSerial.read();
99-
Serial.print(".");
100-
10183
// Without this delay, receiving data breaks for reasons that are beyond me
10284
delay(15);
10385

10486
if (rxBufIdx >= 64) {
10587
clearRxBuf();
10688
}
10789
}
108-
Serial.println("Done.");
10990

11091
if (isValidHeader() && isValidChecksum()) {
11192
parseState(state);
112-
113-
Serial.printf(
114-
"Current measurements: %d, %d, %d, %d, %d\n",
115-
116-
state.measurements[0],
117-
state.measurements[1],
118-
state.measurements[2],
119-
state.measurements[3],
120-
state.measurements[4]
121-
);
12293
} else {
12394
clearRxBuf();
12495
}
12596
}
126-
} // namespace SerialCom
97+
} // namespace SerialCom

src/Types.h

+9-21
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,17 @@
11
#pragma once
22

3-
struct particleSensorState_t
4-
{
3+
struct particleSensorState_t {
54
uint16_t avgPM25 = 0;
6-
uint16_t measurements[5] = {0, 0, 0, 0, 0};
5+
uint16_t measurements = 0;
76
uint8_t measurementIdx = 0;
87
boolean valid = false;
98
};
109

11-
struct aht10SensorState_t
12-
{
13-
float temperature;
14-
float humidity;
15-
};
16-
17-
struct bmp280SensorState_t
18-
{
19-
float temperature;
20-
float altitude;
21-
float pressure;
22-
float waterBoilingPoint;
10+
struct vocSensorState_t {
11+
uint16_t avgTVOC = 0;
12+
uint16_t avgECO2 = 0;
13+
uint32_t sumTVOC = 0;
14+
uint32_t sumECO2 = 0;
15+
uint8_t measurementIdx = 0;
16+
boolean valid = false;
2317
};
24-
25-
struct sgp30SensorState_t
26-
{
27-
uint16_t tvoc;
28-
uint16_t eCo2;
29-
};

0 commit comments

Comments
 (0)