commit 92d6218a582a5abc218aaa9503c366375ace702b Author: David Zalesak Date: Thu Dec 15 13:39:26 2022 +0100 initial commit 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/ESPsiton.ino b/ESPsiton.ino new file mode 100644 index 0000000..a14d048 --- /dev/null +++ b/ESPsiton.ino @@ -0,0 +1,189 @@ +//========== Preprocesor ========== + +//---- Required ---- +#include +#include +#include +//------------------ +#include +#include + +#include "Tasker.h" + +#include "config.h" + +//========== Create objects ========== + +AsyncWebServer server(80); +Tasker tasker; +SoftwareSerial mySerial(5, 4); + +//========== Init variables ========== + +String metrics = ""; +byte nodeID; +int napeti; +int proud; +int vykon; +int teplota; +int strida; +unsigned long vyroba; +byte byteRec; +byte delka; +byte cs; +byte prijem[40]; +bool dataOK = false; + + +//================================= + + +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(4, OUTPUT); + digitalWrite(4, LOW); + pinMode(5, INPUT); + + pinMode(LED_PIN, OUTPUT); + digitalWrite(LED_PIN, HIGH); + + //========== Serial setup ========== + + mySerial.begin(9600); + + //========== Tasks init ========== + + tasker.setInterval(otaHandle, 1000); + + //========== Web server setup ========== + + if (METRICS_EXPORT) { + server.on("/metrics", HTTP_GET, [](AsyncWebServerRequest * request) { + tasker.setTimeout(tripleLedBlink, 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(); + + readSiton(); +} + +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); +} +void tripleLedBlink() { + digitalWrite(LED_PIN, LOW); + delay(50); + digitalWrite(LED_PIN, HIGH); + delay(50); + digitalWrite(LED_PIN, LOW); + delay(50); + digitalWrite(LED_PIN, HIGH); + delay(50); + digitalWrite(LED_PIN, LOW); + delay(50); + digitalWrite(LED_PIN, HIGH); +} +void readSiton() { + //=========================================================================== + //cte ze serial portu data ve formatu EasyTransfer + if (mySerial.available() >= 0x25) { + delay(10); + byteRec = mySerial.read(); // Precte byte + if (byteRec == 0x06) { + byteRec = mySerial.read(); + if (byteRec == 0x85) { + ledBlink(); + delka = mySerial.read(); + cs = delka; + for (byte i = 0; i < delka ; i++) { + byteRec = mySerial.read(); + prijem[i] = byteRec; + cs ^= byteRec; //kontrolni soucet + } + //Serial.println(); + byteRec = mySerial.read(); + //Serial.println(byteRec, HEX); + //Serial.print(cs, HEX); + if (cs == byteRec) { //kontrolni soucet OK data jsou platna + dataOK = true; + nodeID = prijem[0]; + napeti = word(prijem[5], prijem[4]); + proud = word(prijem[7], prijem[6]); + vykon = word(prijem[9], prijem[8]); + teplota = word(prijem[11], prijem[10]); + strida = word(prijem[13], prijem[12]); + + vyroba = (long)prijem[23] << 24; + vyroba += (long)prijem[22] << 16; + vyroba += (long)prijem[21] << 8; + vyroba += (long)prijem[20]; + + metrics = "dataOK "; + metrics += dataOK; + metrics += "\nnodeID "; + metrics += nodeID; + metrics += "\nnapeti "; + metrics += napeti; + metrics += "\nproud "; + metrics += proud; + metrics += "\nvykon "; + metrics += vykon; + metrics += "\nteplota "; + metrics += teplota; + metrics += "\nstrida "; + metrics += strida; + metrics += "\nvyroba "; + metrics += vyroba; + } else { + dataOK = false; + metrics = "dataOK "; + metrics += dataOK; + } + } + } + } +} diff --git a/config.h.example b/config.h.example new file mode 100644 index 0000000..b406423 --- /dev/null +++ b/config.h.example @@ -0,0 +1,7 @@ +#define STASSID "SSID" +#define STAPSK "heslo" + +//Light on power on, blink on request +#define LED_PIN 2 + +#define METRICS_EXPORT 1