mirror of
https://github.com/airgradienthq/arduino.git
synced 2025-07-18 19:22:08 +02:00
Add device webserver to get measure data at <IPAddress>/measures/current
This commit is contained in:
@ -46,6 +46,7 @@ CC BY-SA 4.0 Attribution-ShareAlike 4.0 International License
|
|||||||
#include <AirGradient.h>
|
#include <AirGradient.h>
|
||||||
#include <Arduino_JSON.h>
|
#include <Arduino_JSON.h>
|
||||||
#include <U8g2lib.h>
|
#include <U8g2lib.h>
|
||||||
|
#include <WebServer.h>
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Application state machine state
|
* @brief Application state machine state
|
||||||
@ -549,6 +550,9 @@ U8G2_SH1106_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0, /* reset=*/U8X8_PIN_NONE);
|
|||||||
/** wifi manager instance */
|
/** wifi manager instance */
|
||||||
WiFiManager wifiManager;
|
WiFiManager wifiManager;
|
||||||
|
|
||||||
|
/** Web server instance */
|
||||||
|
WebServer webServer;
|
||||||
|
|
||||||
static bool wifiHasConfig = false; /** */
|
static bool wifiHasConfig = false; /** */
|
||||||
static int connectCountDown; /** wifi configuration countdown */
|
static int connectCountDown; /** wifi configuration countdown */
|
||||||
static int ledCount; /** For LED animation */
|
static int ledCount; /** For LED animation */
|
||||||
@ -587,6 +591,8 @@ static void sendDataToServer(void);
|
|||||||
static void tempHumPoll(void);
|
static void tempHumPoll(void);
|
||||||
static void co2Poll(void);
|
static void co2Poll(void);
|
||||||
static void showNr(void);
|
static void showNr(void);
|
||||||
|
static void webServerInit(void);
|
||||||
|
static String getServerSyncData(void);
|
||||||
|
|
||||||
/** Init schedule */
|
/** Init schedule */
|
||||||
AgSchedule dispLedSchedule(DISP_UPDATE_INTERVAL, updateDispLedBar);
|
AgSchedule dispLedSchedule(DISP_UPDATE_INTERVAL, updateDispLedBar);
|
||||||
@ -646,6 +652,8 @@ void setup() {
|
|||||||
* Send first data to ping server and get server configuration
|
* Send first data to ping server and get server configuration
|
||||||
*/
|
*/
|
||||||
if (WiFi.status() == WL_CONNECTED) {
|
if (WiFi.status() == WL_CONNECTED) {
|
||||||
|
webServerInit();
|
||||||
|
|
||||||
sendPing();
|
sendPing();
|
||||||
Serial.println(F("WiFi connected!"));
|
Serial.println(F("WiFi connected!"));
|
||||||
Serial.println("IP address: ");
|
Serial.println("IP address: ");
|
||||||
@ -762,6 +770,65 @@ static void co2Poll(void) {
|
|||||||
|
|
||||||
static void showNr(void) { Serial.println("Serial nr: " + getDevId()); }
|
static void showNr(void) { Serial.println("Serial nr: " + getDevId()); }
|
||||||
|
|
||||||
|
void webServerMeasureCurrentGet(void) {
|
||||||
|
webServer.send(200, "application/json", getServerSyncData());
|
||||||
|
}
|
||||||
|
|
||||||
|
void webServerHandler(void* param) {
|
||||||
|
for (;;) {
|
||||||
|
webServer.handleClient();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static void webServerInit(void) {
|
||||||
|
webServer.on("/measures/current", HTTP_GET, webServerMeasureCurrentGet);
|
||||||
|
webServer.begin();
|
||||||
|
|
||||||
|
if (xTaskCreate(webServerHandler, "webserver", 1024 * 4, NULL, 5, NULL) !=
|
||||||
|
pdTRUE) {
|
||||||
|
Serial.println("Create task handle webserver failed");
|
||||||
|
}
|
||||||
|
Serial.println("Webserver init");
|
||||||
|
}
|
||||||
|
|
||||||
|
static String getServerSyncData(void) {
|
||||||
|
JSONVar root;
|
||||||
|
root["wifi"] = WiFi.RSSI();
|
||||||
|
if (co2Ppm >= 0) {
|
||||||
|
root["rco2"] = co2Ppm;
|
||||||
|
}
|
||||||
|
if (pm01 >= 0) {
|
||||||
|
root["pm01"] = pm01;
|
||||||
|
}
|
||||||
|
if (pm25 >= 0) {
|
||||||
|
root["pm02"] = pm25;
|
||||||
|
}
|
||||||
|
if (pm10 >= 0) {
|
||||||
|
root["pm10"] = pm10;
|
||||||
|
}
|
||||||
|
if (pm03PCount >= 0) {
|
||||||
|
root["pm003_count"] = pm03PCount;
|
||||||
|
}
|
||||||
|
if (tvocIndex >= 0) {
|
||||||
|
root["tvoc_index"] = tvocIndex;
|
||||||
|
}
|
||||||
|
if (tvocRawIndex >= 0) {
|
||||||
|
root["tvoc_raw"] = tvocRawIndex;
|
||||||
|
}
|
||||||
|
if (noxIndex >= 0) {
|
||||||
|
root["noxIndex"] = noxIndex;
|
||||||
|
}
|
||||||
|
if (temp >= 0) {
|
||||||
|
root["atmp"] = ag.round2(temp);
|
||||||
|
}
|
||||||
|
if (hum >= 0) {
|
||||||
|
root["rhum"] = hum;
|
||||||
|
}
|
||||||
|
root["boot"] = bootCount;
|
||||||
|
|
||||||
|
return JSON.stringify(root);
|
||||||
|
}
|
||||||
|
|
||||||
static void sendPing() {
|
static void sendPing() {
|
||||||
JSONVar root;
|
JSONVar root;
|
||||||
root["wifi"] = WiFi.RSSI();
|
root["wifi"] = WiFi.RSSI();
|
||||||
@ -1598,41 +1665,7 @@ static void pmPoll(void) {
|
|||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
static void sendDataToServer(void) {
|
static void sendDataToServer(void) {
|
||||||
JSONVar root;
|
String syncData = getServerSyncData();
|
||||||
root["wifi"] = WiFi.RSSI();
|
|
||||||
if (co2Ppm >= 0) {
|
|
||||||
root["rco2"] = co2Ppm;
|
|
||||||
}
|
|
||||||
if (pm01 >= 0) {
|
|
||||||
root["pm01"] = pm01;
|
|
||||||
}
|
|
||||||
if (pm25 >= 0) {
|
|
||||||
root["pm02"] = pm25;
|
|
||||||
}
|
|
||||||
if (pm10 >= 0) {
|
|
||||||
root["pm10"] = pm10;
|
|
||||||
}
|
|
||||||
if (pm03PCount >= 0) {
|
|
||||||
root["pm003_count"] = pm03PCount;
|
|
||||||
}
|
|
||||||
if (tvocIndex >= 0) {
|
|
||||||
root["tvoc_index"] = tvocIndex;
|
|
||||||
}
|
|
||||||
if (tvocRawIndex >= 0) {
|
|
||||||
root["tvoc_raw"] = tvocRawIndex;
|
|
||||||
}
|
|
||||||
if (noxIndex >= 0) {
|
|
||||||
root["noxIndex"] = noxIndex;
|
|
||||||
}
|
|
||||||
if (temp >= 0) {
|
|
||||||
root["atmp"] = ag.round2(temp);
|
|
||||||
}
|
|
||||||
if (hum >= 0) {
|
|
||||||
root["rhum"] = hum;
|
|
||||||
}
|
|
||||||
root["boot"] = bootCount;
|
|
||||||
|
|
||||||
String syncData = JSON.stringify(root);
|
|
||||||
if (agServer.postToServer(getDevId(), syncData)) {
|
if (agServer.postToServer(getDevId(), syncData)) {
|
||||||
resetWatchdog();
|
resetWatchdog();
|
||||||
}
|
}
|
||||||
@ -1664,7 +1697,6 @@ static void tempHumPoll(void) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static void mqtt_event_handler(void *handler_args, esp_event_base_t base,
|
static void mqtt_event_handler(void *handler_args, esp_event_base_t base,
|
||||||
int32_t event_id, void *event_data) {
|
int32_t event_id, void *event_data) {
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user