diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index f4b78e0..698109b 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -41,7 +41,7 @@ jobs: uses: ArminJo/arduino-test-compile@v3 with: sketch-names: "air_quality.ino" - arduino-board-fqbn: "esp32:esp32:esp32:PSRAM=disabled,PartitionScheme=default,CPUFreq=240,FlashMode=qio,FlashFreq=80,FlashSize=4M,UploadSpeed=115200,LoopCore=1,EventsCore=1,DebugLevel=none" + arduino-board-fqbn: "esp32:esp32:esp32:PSRAM=disabled,PartitionScheme=default,CPUFreq=240,FlashMode=qio,FlashFreq=80,FlashSize=4M,UploadSpeed=115200,LoopCore=1,EventsCore=0,DebugLevel=none" platform-url: https://raw.githubusercontent.com/espressif/arduino-esp32/gh-pages/package_esp32_index.json required-libraries: HomeSpan,EspSoftwareSerial # sketches-exclude: WhistleSwitch 50Hz diff --git a/.gitignore b/.gitignore index d636667..1e118e6 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ .DS_Store build/ *.bin -*.code-workspace \ No newline at end of file +*.code-workspace +.vscode/c_cpp_properties.json diff --git a/.vscode/arduino.json b/.vscode/arduino.json index 161bac7..30782a0 100644 --- a/.vscode/arduino.json +++ b/.vscode/arduino.json @@ -1,7 +1,7 @@ { - "configuration": "PSRAM=disabled,PartitionScheme=default,CPUFreq=240,FlashMode=qio,FlashFreq=80,FlashSize=4M,UploadSpeed=115200,LoopCore=1,EventsCore=1,DebugLevel=none", + "configuration": "PSRAM=disabled,PartitionScheme=default,CPUFreq=240,FlashMode=qio,FlashFreq=80,FlashSize=4M,UploadSpeed=115200,LoopCore=1,EventsCore=0,DebugLevel=none", "board": "esp32:esp32:esp32", - "port": "/dev/tty.usbserial-1410", + "port": "/dev/tty.usbserial-01DFA3CE", "sketch": "air_quality/air_quality.ino", "output": "build", "postbuild": "cp ./build/air_quality.ino.bin ./esp32_air_quality.bin", diff --git a/.vscode/settings.json b/.vscode/settings.json index a43e4f1..36ade92 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,14 +1,17 @@ { "cSpell.words": [ "Adafruit", + "Berman", "Erriez", "Grafana", "haxfleisch", + "HOMEKIT", "homespan", "Hypfer", "kasik", "microcontroller", "NEOPIXEL", + "nhomekit", "Testpoint", "VINDRIKTNING", "webserver", diff --git a/.vscode/tasks.json b/.vscode/tasks.json index 63191cd..3f75b7e 100644 --- a/.vscode/tasks.json +++ b/.vscode/tasks.json @@ -4,10 +4,13 @@ "version": "2.0.0", "tasks": [ { - "label": "Erase Flash", - "type": "shell", - "command": "esptool.py -p /dev/cu.usbserial-1410 erase_flash", - "problemMatcher": [] + "label": "ESP32: Build", + "command": "${command:arduino.verify}", + "problemMatcher": [], + "group": { + "kind": "build", + "isDefault": true + } } ] } \ No newline at end of file diff --git a/air_quality/DEV_Sensors.hpp b/air_quality/DEV_Sensors.hpp index 21a7e23..40d73bd 100644 --- a/air_quality/DEV_Sensors.hpp +++ b/air_quality/DEV_Sensors.hpp @@ -12,7 +12,7 @@ #define HOMEKIT_CO2_TRIGGER 1350 // co2 level, at which HomeKit alarm will be triggered #define NEOPIXEL_PIN 16 // Pin to which NeoPixel strip is connected #define NUMPIXELS 1 // Number of pixels -#define BRIGHTNESS_DEFAULT 10 // Default (dimmed) brightness +#define BRIGHTNESS_DEFAULT 9 // Default (dimmed) brightness #define BRIGHTNESS_MAX 150 // maximum brightness of CO2 indicator led #define BRIGHTNESS_THRESHOLD 500 // TODO calibrate Threshold value of dimmed brightness #define ANALOG_PIN 35 // Analog pin, to which light sensor is connected diff --git a/air_quality/air_quality.ino b/air_quality/air_quality.ino index dca65fb..a29f5e3 100644 --- a/air_quality/air_quality.ino +++ b/air_quality/air_quality.ino @@ -49,43 +49,49 @@ * ╚═════════════════════════════╝ */ -#include +#define REQUIRED VERSION(1, 5, 1) + #include "DEV_Sensors.hpp" -#include -#include -#include #include "SerialCom.hpp" #include "Types.hpp" -#include -#include +#include +#include +#include +#include +#include +#include +#include -#define BUTTON_PIN 0 -#define LED_STATUS_PIN 26 +#define BUTTON_PIN 0 +#define LED_STATUS_PIN 26 -AsyncWebServer server(80); +#define BOOT_REASON_MESSAGE_SIZE 150 -DEV_CO2Sensor *CO2; // GLOBAL POINTER TO STORE SERVICE +WebServer server(80); + +DEV_CO2Sensor *CO2; // GLOBAL POINTER TO STORE SERVICE DEV_AirQualitySensor *AQI; // GLOBAL POINTER TO STORE SERVICE void setup() { Serial.begin(115200); - homeSpan.setControlPin(BUTTON_PIN); // Set button pin - homeSpan.setStatusPin(LED_STATUS_PIN); // Set status led pin - homeSpan.setLogLevel(0); // set log level - homeSpan.setPortNum(88); // change port number for HomeSpan so we can use port 80 for the Web Server - homeSpan.setStatusAutoOff(10); // turn off status led after 10 seconds of inactivity - homeSpan.setWifiCallback(setupWeb); // need to start Web Server after WiFi is established - homeSpan.reserveSocketConnections(3); // reserve 3 socket connections for Web Server - // homeSpan.enableAutoStartAP(); + homeSpan.setControlPin(BUTTON_PIN); // Set button pin + homeSpan.setStatusPin(LED_STATUS_PIN); // Set status led pin + homeSpan.setLogLevel(0); // set log level + homeSpan.setPortNum(88); // change port number for HomeSpan so we can use port 80 for the Web Server + homeSpan.setStatusAutoOff(10); // turn off status led after 10 seconds of inactivity + homeSpan.setWifiCallback(setupWeb); // need to start Web Server after WiFi is established + homeSpan.reserveSocketConnections(3); // reserve 3 socket connections for Web Server + homeSpan.enableWebLog(10, "pool.ntp.org", "UTC", "myLog"); // enable Web Log + homeSpan.enableAutoStartAP(); // enable auto start AP homeSpan.begin(Category::Bridges, "HomeSpan Air Sensor Bridge"); new SpanAccessory(); new Service::AccessoryInformation(); new Characteristic::Identify(); - new Characteristic::FirmwareRevision("1.2"); + new Characteristic::FirmwareRevision("1.3"); new SpanAccessory(); new Service::AccessoryInformation(); @@ -101,42 +107,42 @@ void setup() { } void loop() { - homeSpan.poll(); + server.handleClient(); } void setupWeb() { Serial.print("Starting Air Quality Sensor Server Hub...\n\n"); - server.on("/metrics", HTTP_GET, [](AsyncWebServerRequest *request) { + server.on("/metrics", HTTP_GET, []() { double airQuality = AQI->pm25->getVal(); double co2 = CO2->co2Level->getVal(); if (co2 >= 400) { // exclude when it is still warming up // double lightness = neopixelAutoBrightness(); double uptime = esp_timer_get_time() / (6 * 10e6); double heap = esp_get_free_heap_size(); - String airQualityMetric = "# HELP air_quality PM2.5 Density\nair_quality{device=\"air_sensor\",location=\"home\"} " + String(airQuality); - String CO2Metric = "# HELP co2 Carbon Dioxide\ncarbon_dioxide{device=\"air_sensor\",location=\"home\"} " + String(co2); - String uptimeMetric = "# HELP uptime Sensor uptime\nuptime{device=\"air_sensor\",location=\"home\"} " + String(int(uptime)); - String heapMetric = "# HELP heap Available heap memory\nheap{device=\"air_sensor\",location=\"home\"} " + String(int(heap)); + String airQualityMetric = "# HELP air_quality PM2.5 Density\nhomekit_air_quality{device=\"air_sensor\",location=\"home\"} " + String(airQuality); + String CO2Metric = "# HELP co2 Carbon Dioxide\nhomekit_carbon_dioxide{device=\"air_sensor\",location=\"home\"} " + String(co2); + String uptimeMetric = "# HELP uptime Sensor uptime\nhomekit_uptime{device=\"air_sensor\",location=\"home\"} " + String(int(uptime)); + String heapMetric = "# HELP heap Available heap memory\nhomekit_heap{device=\"air_sensor\",location=\"home\"} " + String(int(heap)); // String lightnessMetric = "# HELP lightness Lightness\nlightness{device=\"air_sensor\",location=\"home\"} " + String(lightness); Serial.println(airQualityMetric); Serial.println(CO2Metric); Serial.println(uptimeMetric); Serial.println(heapMetric); - request->send(200, "text/plain", airQualityMetric + "\n" + CO2Metric + "\n" + uptimeMetric + "\n" + heapMetric); + server.send(200, "text/plain", airQualityMetric + "\n" + CO2Metric + "\n" + uptimeMetric + "\n" + heapMetric); } }); - server.on("/reboot", HTTP_GET, [](AsyncWebServerRequest *request) { + server.on("/reboot", HTTP_GET, []() { String content = "Rebooting! Will return to configuration page in 10 seconds.

"; content += ""; - request->send(200, "text/html", content); + server.send(200, "text/html", content); ESP.restart(); }); - AsyncElegantOTA.begin(&server); // Start AsyncElegantOTA + ElegantOTA.begin(&server); // Start AsyncElegantOTA server.begin(); Serial.println("HTTP server started"); -} // setupWeb +} // setupWeb \ No newline at end of file