Skip to content

Commit 41b731d

Browse files
committed
feat: add rssi to web page, fix sleep and work interval
1 parent 64dc907 commit 41b731d

File tree

2 files changed

+39
-11
lines changed

2 files changed

+39
-11
lines changed

arduino/arduino.ino

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@ SoftwareSerial sensorSerial(sensor_receiver_pin, sensor_transmitter_pin);
1313
LiquidCrystal_I2C lcd(0x27, 16, 2);
1414
Sds011SensorHandler sensorHandler(sensorSerial);
1515

16+
unsigned long measurement_start_time = 0;
17+
bool is_work_mode_active = false;
18+
19+
String pm25Output = "";
20+
String pm10Output = "";
21+
1622
void setup() {
1723
Serial.begin(115200);
1824
sensorSerial.begin(9600);
@@ -24,16 +30,33 @@ void setup() {
2430
lcd.print("Starting...");
2531
sensorHandler.sendQueryReportModeCommand();
2632
delay(1000);
33+
sensorHandler.sendActiveReportModeCommand();
34+
delay(1000);
2735
}
2836

2937
void loop() {
30-
sensorHandler.sendWorkModeCommand();
31-
delay(45000); //Wait for sensor stabilization
32-
sensorHandler.sendQueryDataCommand();
33-
delay(1000);
34-
readMeasurementData();
35-
sensorHandler.sendSleepModeCommand();
36-
delay(60000);
38+
if (millis() - measurement_start_time > 60000) {
39+
sensorHandler.sendQueryReportModeCommand();
40+
delay(1000);
41+
sensorHandler.sendSleepModeCommand();
42+
lcd.clear();
43+
lcd.setCursor(0, 0);
44+
lcd.print(pm25Output);
45+
lcd.setCursor(0, 1);
46+
lcd.print(pm10Output + "slp");
47+
delay(60000);
48+
measurement_start_time = millis();
49+
is_work_mode_active = false;
50+
} else {
51+
if (is_work_mode_active == false) {
52+
sensorHandler.sendWorkModeCommand();
53+
delay(1000);
54+
sensorHandler.sendActiveReportModeCommand();
55+
delay(1000);
56+
is_work_mode_active = true;
57+
}
58+
readMeasurementData();
59+
}
3760
}
3861

3962
String getDateTime() {
@@ -53,8 +76,8 @@ uint8_t esp32_data_head = 0xAA;
5376

5477
void readMeasurementData() {
5578
PmResult pmResult = sensorHandler.readPmResult();
56-
String pm25Output = "PM2.5:" + String(pmResult.pm25) + "ug/m3";
57-
String pm10Output = "PM10:" + String(pmResult.pm10) + "ug/m3";
79+
pm25Output = "PM2.5:" + String(pmResult.pm25) + "ug/m3";
80+
pm10Output = "PM10:" + String(pmResult.pm10) + "ug/m3";
5881

5982
lcd.clear();
6083
lcd.setCursor(0, 0);

esp32/esp32.ino

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ IPAddress gateway(192, 168, 0, 1);
1414
IPAddress subnet(255, 255, 255, 0);
1515

1616
WiFiServer server(80);
17+
long rssi = 0;
18+
1719
volatile bool isDataReady = false;
1820
uint8_t data_head = 0xAA;
1921

@@ -112,14 +114,17 @@ float* getSensorData() {
112114
}
113115

114116
void loop() {
117+
rssi = WiFi.RSSI();
115118
WiFiClient client = server.available();
116119

117120
if (isDataReady) {
118121
waitForData();
119122
float* data = getSensorData();
120-
htmlData = String(String(data[0]) + "<br>" + String(data[1]));
123+
htmlData = String("Wifi strength: " + String(rssi) + "<br>" + String(data[0]) + "<br>" + String(data[1]));
121124
jsonData = String("{\"pm2.5\":") + String(data[0]) + "," + String("\"pm10\":") + String(data[1]) + String("}");
122-
Serial.println("Received data:\n" + String(data[0]) + "\n" + String(data[1]));
125+
Serial.println("Received data:");
126+
Serial.println(data[0]);
127+
Serial.println(data[1]);
123128
}
124129

125130
if (client) {

0 commit comments

Comments
 (0)