initial commit

master
David Zálešák 2 years ago
commit bc7899ed34

1
.gitignore vendored

@ -0,0 +1 @@
config.h

@ -0,0 +1,121 @@
//========== 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;
}
}

@ -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