ZPA_elektromer_UARTtest/ZPA_elektromer_UARTtest.ino
2019-06-12 00:31:36 +02:00

155 lines
3.7 KiB
C++

#include <WiFi.h>
#include <WiFiClient.h>
#include <ArduinoOTA.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");
})
.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 loop() {
// put your main code here, to run repeatedly:
ArduinoOTA.handle();
if (Serial2.available()) {
char r = Serial2.read();
if (r == 0x02) {
Serial.print("<STX>");
return;
}
if (r == 0x03) {
Serial.println("<ETX>");
Serial.print("Checksum <BCC>: ");
Serial.println(Serial2.read());
Serial.println();
Serial.println("Changing back baudrate to 300");
Serial2.updateBaudRate(300);
return;
}
Serial.write(r);
}
if (Serial.available()) {
char r = Serial.read();
if (r == 'R') {
Serial.println("Requesting data");
Serial2.print("/?!\r\n");
return;
}
if (r == '1') {
Serial.println("Baudrate to 300");
Serial2.updateBaudRate(300);
}
if (r == '2') {
Serial.println("Baudrate to 1200");
Serial2.updateBaudRate(1200);
}
if (r == '3') {
Serial.println("Baudrate to 2400");
Serial2.updateBaudRate(2400);
}
if (r == '4') {
Serial.println("Baudrate to 4800");
Serial2.updateBaudRate(4800);
}
if (r == '5') {
Serial.println("Baudrate to 9600");
Serial2.updateBaudRate(9600);
}
if (r == 'C') {
Serial.println("Change baudrate");
Serial2.write(0x06);
Serial2.print("040\r\n");
Serial2.flush();
Serial2.updateBaudRate(4800);
return;
}
Serial2.write(r);
}
}