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.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

190 lines
4.4 KiB
C++

//========== Preprocesor ==========
//---- Required ----
#include <ESP8266WiFi.h>
#include <ArduinoOTA.h>
#include <SoftwareSerial.h>
//------------------
#include <ESPAsyncTCP.h>
#include <ESPAsyncWebServer.h>
#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;
}
}
}
}
}