You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
122 lines
2.8 KiB
Arduino
122 lines
2.8 KiB
Arduino
2 years ago
|
//========== Preprocesor ==========
|
||
|
|
||
|
//---- Required ----
|
||
|
#include <ESP8266WiFi.h>
|
||
|
#include <ArduinoOTA.h>
|
||
|
//------------------
|
||
|
#include <ESPAsyncTCP.h>
|
||
|
#include <ESPAsyncWebServer.h>
|
||
|
|
||
|
#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;
|
||
|
}
|
||
|
}
|