Skip to content

Commit

Permalink
Merge pull request #6 from oleksiikutuzov/dev
Browse files Browse the repository at this point in the history
Fix reboots by moving from AsyncWebServer
  • Loading branch information
oleksiikutuzov committed Aug 2, 2022
2 parents c8f9690 + 642025a commit 7142a95
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 39 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
.DS_Store
build/
*.bin
*.code-workspace
*.code-workspace
.vscode/c_cpp_properties.json
4 changes: 2 additions & 2 deletions .vscode/arduino.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
{
"cSpell.words": [
"Adafruit",
"Berman",
"Erriez",
"Grafana",
"haxfleisch",
"HOMEKIT",
"homespan",
"Hypfer",
"kasik",
"microcontroller",
"NEOPIXEL",
"nhomekit",
"Testpoint",
"VINDRIKTNING",
"webserver",
Expand Down
11 changes: 7 additions & 4 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
]
}
2 changes: 1 addition & 1 deletion air_quality/DEV_Sensors.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
66 changes: 36 additions & 30 deletions air_quality/air_quality.ino
Original file line number Diff line number Diff line change
Expand Up @@ -49,43 +49,49 @@
* ╚═════════════════════════════╝
*/

#include <HomeSpan.h>
#define REQUIRED VERSION(1, 5, 1)

#include "DEV_Sensors.hpp"
#include <ErriezMHZ19B.h>
#include <SoftwareSerial.h>
#include <Adafruit_NeoPixel.h>
#include "SerialCom.hpp"
#include "Types.hpp"
#include <ESPAsyncWebServer.h>
#include <AsyncElegantOTA.h>
#include <Adafruit_NeoPixel.h>
#include <WiFiClient.h>
#include <WebServer.h>
#include <ElegantOTA.h>
#include <ErriezMHZ19B.h>
#include <HomeSpan.h>
#include <SoftwareSerial.h>

#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();
Expand All @@ -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 = "<html><body>Rebooting! Will return to configuration page in 10 seconds.<br><br>";
content += "<meta http-equiv = \"refresh\" content = \"10; url = /\" />";
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

0 comments on commit 7142a95

Please sign in to comment.