3
3
#include < ArduinoOTA.h>
4
4
#include < AsyncMqttClient.h>
5
5
#include < ArduinoJson.h>
6
+ #include < StreamUtils.h>
6
7
#include < SPIFFS.h>
7
8
#include < WiFiSettings.h>
8
9
#include < MHZ.h>
9
10
#include < Adafruit_GFX.h>
10
11
#include < Adafruit_SSD1306.h>
11
12
#include < Adafruit_I2CDevice.h>
13
+ #include < ESPAsyncWebServer.h>
12
14
#include < SoftwareSerial.h>
13
15
14
16
#include " config.h"
@@ -30,7 +32,7 @@ extern "C"
30
32
#define DEVICE_ID (Sprintf(" %06" PRIx64, ESP.getEfuseMac() >> 24 )) // unique device ID
31
33
#define uS_TO_S_FACTOR 1000000 // Conversion factor for micro seconds to seconds
32
34
33
- String version = " 1.1.3 " ;
35
+ String version = " 1.2.0 " ;
34
36
35
37
AsyncMqttClient mqttClient;
36
38
@@ -40,6 +42,7 @@ TimerHandle_t wifiReconnectTimer;
40
42
41
43
MHZ co2Sensor (&Serial2, MHZ14A);
42
44
Adafruit_SSD1306 display (SCREEN_WIDTH, SCREEN_HEIGHT, &Wire);
45
+ AsyncWebServer server (80 );
43
46
44
47
// states
45
48
bool isUpdating = false ;
@@ -134,7 +137,8 @@ const char *getMqttTopic(String part)
134
137
return topic.c_str ();
135
138
}
136
139
137
- void sendInfo ()
140
+ // TODO change to StaticJsonDocument<>
141
+ DynamicJsonDocument getInfoJson ()
138
142
{
139
143
DynamicJsonDocument doc (1024 );
140
144
doc[" version" ] = version;
@@ -157,10 +161,15 @@ void sendInfo()
157
161
co2Meter[" temperature" ] = lastTemperature;
158
162
co2Meter[" ppm" ] = lastCo2Value > 0 ? lastCo2Value : 0 ;
159
163
160
- String JS;
161
- serializeJson (doc, JS);
164
+ return doc;
165
+ }
166
+
167
+ void sendInfo ()
168
+ {
169
+ StringStream stream;
170
+ serializeJson (getInfoJson (), stream);
162
171
163
- mqttClient.publish (getMqttTopic (" out/info" ), 1 , false , JS .c_str ());
172
+ mqttClient.publish (getMqttTopic (" out/info" ), 1 , false , stream. str () .c_str ());
164
173
165
174
lastInfoSend = millis ();
166
175
}
@@ -290,27 +299,27 @@ void connectToWifi()
290
299
WiFiSettings.connect (true , 30 );
291
300
}
292
301
293
- void setupTimers ()
302
+ void setupWebserver ()
294
303
{
295
- mqttReconnectTimer = xTimerCreate (" mqttTimer" , pdMS_TO_TICKS (2000 ), pdFALSE, (void *)0 , reinterpret_cast <TimerCallbackFunction_t>(connectToMqtt));
296
- wifiReconnectTimer = xTimerCreate (" wifiTimer" , pdMS_TO_TICKS (2000 ), pdFALSE, (void *)1 , reinterpret_cast <TimerCallbackFunction_t>(connectToWifi));
297
- }
304
+ server.on (" /" , HTTP_GET, [](AsyncWebServerRequest *request)
305
+ { request->send (200 , " text/plain" , " Hi! This is a sample response." ); });
298
306
299
- void setupDisplay ( )
300
- {
301
- display. begin (SSD1306_SWITCHCAPVCC, 0x3C );
307
+ server. on ( " /api/info " , HTTP_GET, [](AsyncWebServerRequest *request )
308
+ {
309
+ auto infoJson = getInfoJson ( );
302
310
303
- delay (1000 );
311
+ StringStream stream;
312
+ auto size = serializeJson (infoJson, stream);
304
313
305
- display. clearDisplay ( );
314
+ request-> send (stream, " application/json " , size); } );
306
315
307
- // demo output
308
- display.setTextColor (WHITE);
309
- display.setTextSize (1 );
310
- display.setCursor (0 , 0 );
311
- display.print (" >> co2 meter <<" );
316
+ server.begin ();
317
+ }
312
318
313
- display.display ();
319
+ void setupTimers ()
320
+ {
321
+ mqttReconnectTimer = xTimerCreate (" mqttTimer" , pdMS_TO_TICKS (2000 ), pdFALSE, (void *)0 , reinterpret_cast <TimerCallbackFunction_t>(connectToMqtt));
322
+ wifiReconnectTimer = xTimerCreate (" wifiTimer" , pdMS_TO_TICKS (2000 ), pdFALSE, (void *)1 , reinterpret_cast <TimerCallbackFunction_t>(connectToWifi));
314
323
}
315
324
316
325
void setupOTA ()
@@ -391,6 +400,50 @@ void setupOTA()
391
400
.begin ();
392
401
}
393
402
403
+ void setupWifiSettings ()
404
+ {
405
+ WiFi.onEvent (onWiFiEvent);
406
+
407
+ WiFiSettings.secure = true ;
408
+ WiFiSettings.hostname = " co2-meter-" ; // will auto add device ID
409
+ WiFiSettings.password = PASSWORD;
410
+
411
+ // Set callbacks to start OTA when the portal is active
412
+ WiFiSettings.onPortal = []()
413
+ {
414
+ isPortalActive = true ;
415
+
416
+ Serial.println (" WiFi config portal active" );
417
+
418
+ setupOTA ();
419
+ };
420
+ WiFiSettings.onPortalWaitLoop = []()
421
+ {
422
+ ArduinoOTA.handle ();
423
+ };
424
+ WiFiSettings.onConfigSaved = []()
425
+ {
426
+ ESP.restart ();
427
+ };
428
+ }
429
+
430
+ void setupDisplay ()
431
+ {
432
+ display.begin (SSD1306_SWITCHCAPVCC, 0x3C );
433
+
434
+ delay (1000 );
435
+
436
+ display.clearDisplay ();
437
+
438
+ // demo output
439
+ display.setTextColor (WHITE);
440
+ display.setTextSize (1 );
441
+ display.setCursor (0 , 0 );
442
+ display.print (" >> co2 meter <<" );
443
+
444
+ display.display ();
445
+ }
446
+
394
447
void detect_wakeup_reason ()
395
448
{
396
449
esp_sleep_wakeup_cause_t wakeup_reason = esp_sleep_get_wakeup_cause ();
@@ -437,30 +490,8 @@ void setup()
437
490
// SPIFFS.format(); // TODO reset config on connection MQTT fail
438
491
439
492
setupTimers ();
440
-
441
- WiFi.onEvent (onWiFiEvent);
442
-
443
- WiFiSettings.secure = true ;
444
- WiFiSettings.hostname = " co2-meter-" ; // will auto add device ID
445
- WiFiSettings.password = PASSWORD;
446
-
447
- // Set callbacks to start OTA when the portal is active
448
- WiFiSettings.onPortal = []()
449
- {
450
- isPortalActive = true ;
451
-
452
- Serial.println (" WiFi config portal active" );
453
-
454
- setupOTA ();
455
- };
456
- WiFiSettings.onPortalWaitLoop = []()
457
- {
458
- ArduinoOTA.handle ();
459
- };
460
- WiFiSettings.onConfigSaved = []()
461
- {
462
- ESP.restart ();
463
- };
493
+ setupWifiSettings ();
494
+ setupWebserver ();
464
495
465
496
// define custom settings
466
497
mqtt_host = WiFiSettings.string (" mqtt_host" , " 192.168.1.1" );
0 commit comments