From bc7899ed349691cc12f4f8220355c8b563a2ca08 Mon Sep 17 00:00:00 2001 From: David Zalesak Date: Thu, 15 Dec 2022 13:15:22 +0100 Subject: [PATCH] initial commit --- .gitignore | 1 + ESPelektromer.ino | 121 ++++++++++++++++++++++++++++++++++++++++++++++ config.h.example | 10 ++++ 3 files changed, 132 insertions(+) create mode 100644 .gitignore create mode 100644 ESPelektromer.ino create mode 100644 config.h.example diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..0e56cf2 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +config.h diff --git a/ESPelektromer.ino b/ESPelektromer.ino new file mode 100644 index 0000000..9f028dc --- /dev/null +++ b/ESPelektromer.ino @@ -0,0 +1,121 @@ +//========== Preprocesor ========== + +//---- Required ---- +#include +#include +//------------------ +#include +#include + +#include "Tasker.h" + +#include "config.h" + +//========== Create objects ========== + +AsyncWebServer server(80); +Tasker tasker; + +//========== Init variables ========== + +int electricityTotal = 0; +int gasTotal = 0; +String metrics = "electricity 0\ngas 0"; +unsigned long lastElectricityInterruptTime; +unsigned long lastGasInterruptTime; + +//================================= + + +void setup() { +//========== Wi-Fi setup ========== + + WiFi.mode(WIFI_STA); + WiFi.begin(STASSID, STAPSK); + while (WiFi.waitForConnectResult() != WL_CONNECTED) { + //Connection Failed! Rebooting... + delay(5000); + ESP.restart(); + } + +//========== OTA setup ========== + + // Port defaults to 8266 + // ArduinoOTA.setPort(8266); + + // Hostname defaults to esp8266-[ChipID] + // ArduinoOTA.setHostname("myesp8266"); + + // No authentication by default + // ArduinoOTA.setPassword("admin"); + + // Password can be set with it's md5 value as well + // MD5(admin) = 21232f297a57a5a743894a0e4a801fc3 + // ArduinoOTA.setPasswordHash("21232f297a57a5a743894a0e4a801fc3"); + ArduinoOTA.begin(); + SPIFFS.begin(); + +//========== Pin setup ========== + + pinMode(ELECTRICITY, INPUT); + pinMode(GAS, INPUT); + attachInterrupt(digitalPinToInterrupt(ELECTRICITY), electricityIn, FALLING); + attachInterrupt(digitalPinToInterrupt(GAS), gasIn, FALLING); + pinMode(LED_PIN, OUTPUT); + digitalWrite(LED_PIN, HIGH); + +//========== Tasks init ========== + + tasker.setInterval(otaHandle, 1000); + +//========== Web server setup ========== + + if(METRICS_EXPORT){ + server.on("/metrics", HTTP_GET, [](AsyncWebServerRequest *request){ + //tasker.setTimeout(ledBlink, 10); + request->send(200, "text/plain; charset=utf-8", metrics); + }); + server.onNotFound(notFound); + server.begin(); + } +} + +// the loop routine runs over and over again forever: +void loop() { + tasker.loop(); +} + +void otaHandle(){ + ArduinoOTA.handle(); +} + +void notFound(AsyncWebServerRequest *request) { + request->send(404, "text/plain", "Not found"); +} +void ledBlink() { + digitalWrite(LED_PIN, LOW); + delay(200); + digitalWrite(LED_PIN, HIGH); +} +ICACHE_RAM_ATTR void electricityIn() { + unsigned long interruptTime = millis(); + if (interruptTime - lastElectricityInterruptTime > 100) { + electricityTotal++; + metrics = "electricity "; + metrics += electricityTotal; + metrics += "\ngas "; + metrics += gasTotal; + lastElectricityInterruptTime = interruptTime; + } +} +ICACHE_RAM_ATTR void gasIn() { + unsigned long interruptTime = millis(); + if (interruptTime - lastGasInterruptTime > 12000) { + gasTotal++; + metrics = "electricity "; + metrics += electricityTotal; + metrics += "\ngas "; + metrics += gasTotal; + lastGasInterruptTime = interruptTime; + } +} diff --git a/config.h.example b/config.h.example new file mode 100644 index 0000000..2ea3d30 --- /dev/null +++ b/config.h.example @@ -0,0 +1,10 @@ +#define STASSID "SSID" +##define STAPSK "heslo" + +#define ELECTRICITY 2 +#define GAS 0 + +//Light on power on, blink on request +#define LED_PIN 1 + +#define METRICS_EXPORT 1