ZPA_elektromer_UARTtest/ZPA_elektromer_UARTtest.ino

141 lines
3.3 KiB
C++

#include <WiFi.h>
#include <WiFiClient.h>
#include <ArduinoOTA.h>
#include "ZPAElectricity.h"
const char* host = "zpaesp32";
const char* ssid = "...";
const char* password = "...";
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
Serial.println("Booting...");
WiFi.mode(WIFI_STA);
Serial.print("Wifi connect... ");
WiFi.begin(ssid, password);
while (WiFi.waitForConnectResult() != WL_CONNECTED) {
Serial.println("Connection Failed! Rebooting...");
delay(5000);
ESP.restart();
}
Serial.println("Connected");
Serial.print("Configure OTA... ");
ArduinoOTA
.onStart([]() {
String type;
if (ArduinoOTA.getCommand() == U_FLASH)
type = "sketch";
else // U_SPIFFS
type = "filesystem";
// NOTE: if updating SPIFFS this would be the place to unmount SPIFFS using SPIFFS.end()
Serial.println("Start updating " + type);
})
.onEnd([]() {
Serial.println("\nEnd");
Serial.println("Disconnect from wifi");
WiFi.disconnect();
})
.onProgress([](unsigned int progress, unsigned int total) {
Serial.printf("Progress: %u%%\r", (progress / (total / 100)));
})
.onError([](ota_error_t error) {
Serial.printf("Error[%u]: ", error);
if (error == OTA_AUTH_ERROR) Serial.println("Auth Failed");
else if (error == OTA_BEGIN_ERROR) Serial.println("Begin Failed");
else if (error == OTA_CONNECT_ERROR) Serial.println("Connect Failed");
else if (error == OTA_RECEIVE_ERROR) Serial.println("Receive Failed");
else if (error == OTA_END_ERROR) Serial.println("End Failed");
});
Serial.println("OK");
Serial.print("Starting OTA... ");
ArduinoOTA.begin();
Serial.println("OK");
Serial.print("Configure Reading serial port... ");
Serial2.begin(300, SERIAL_7E1, 36, 4);
Serial.println("OK");
Serial.println("Test ZPA elektro");
Serial.println("================");
Serial.println();
Serial.println("R - Request (sends /?!<CR><LF>)");
Serial.println("C - Change baudrate to 4800 (sends <ACK>040<CR><LF>)");
Serial.println();
Serial.println("Manual baudrate");
Serial.println("1 - 300b");
Serial.println("2 - 1200b");
Serial.println("3 - 2400b");
Serial.println("4 - 4800b");
Serial.println("5 - 9600b");
Serial.println();
Serial.println();
Serial.println("Ready");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
}
void handlePCSerial() {
if (Serial.available()) {
char r = Serial.read();
if (r == 'R') {
if (ZPAreading()) {
Serial.println(F("Another reading already running"));
return;
}
ZPArequestData();
return;
}
if (r == '1') {
Serial.println(F("Baudrate to 300"));
Serial2.updateBaudRate(300);
}
if (r == '2') {
Serial.println(F("Baudrate to 1200"));
Serial2.updateBaudRate(1200);
}
if (r == '3') {
Serial.println(F("Baudrate to 2400"));
Serial2.updateBaudRate(2400);
}
if (r == '4') {
Serial.println(F("Baudrate to 4800"));
Serial2.updateBaudRate(4800);
}
if (r == '5') {
Serial.println(F("Baudrate to 9600"));
Serial2.updateBaudRate(9600);
}
if (r == 'C') {
ZPAchangeBaudrate('4');
return;
}
Serial2.write(r);
}
}
void loop() {
ArduinoOTA.handle();
handleZPA();
handlePCSerial();
}