ZPA_elektromer_UARTtest/ZPA_elektromer_UARTtest.ino

141 lines
3.3 KiB
Arduino
Raw Permalink Normal View History

2019-06-11 23:38:48 +02:00
#include <WiFi.h>
#include <WiFiClient.h>
#include <ArduinoOTA.h>
2019-06-13 15:19:31 +02:00
#include "ZPAElectricity.h"
2019-06-11 23:38:48 +02:00
const char* host = "zpaesp32";
const char* ssid = "...";
const char* password = "...";
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
2019-06-12 00:31:36 +02:00
Serial.println("Booting...");
2019-06-11 23:38:48 +02:00
WiFi.mode(WIFI_STA);
2019-06-12 00:31:36 +02:00
Serial.print("Wifi connect... ");
2019-06-11 23:38:48 +02:00
WiFi.begin(ssid, password);
while (WiFi.waitForConnectResult() != WL_CONNECTED) {
Serial.println("Connection Failed! Rebooting...");
delay(5000);
ESP.restart();
}
2019-06-12 00:31:36 +02:00
Serial.println("Connected");
2019-06-11 23:38:48 +02:00
2019-06-12 00:31:36 +02:00
Serial.print("Configure OTA... ");
2019-06-11 23:38:48 +02:00
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");
2019-06-12 11:25:47 +02:00
Serial.println("Disconnect from wifi");
WiFi.disconnect();
2019-06-11 23:38:48 +02:00
})
.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");
});
2019-06-12 00:31:36 +02:00
Serial.println("OK");
Serial.print("Starting OTA... ");
2019-06-11 23:38:48 +02:00
ArduinoOTA.begin();
2019-06-12 00:31:36 +02:00
Serial.println("OK");
2019-06-11 23:38:48 +02:00
2019-06-12 00:31:36 +02:00
Serial.print("Configure Reading serial port... ");
2019-06-11 23:38:48 +02:00
Serial2.begin(300, SERIAL_7E1, 36, 4);
2019-06-12 00:31:36 +02:00
Serial.println("OK");
2019-06-11 23:38:48 +02:00
Serial.println("Test ZPA elektro");
2019-06-11 23:57:33 +02:00
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();
2019-06-11 23:38:48 +02:00
Serial.println("Ready");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
}
2019-06-13 15:19:31 +02:00
void handlePCSerial() {
2019-06-11 23:38:48 +02:00
if (Serial.available()) {
char r = Serial.read();
if (r == 'R') {
2019-06-13 15:19:31 +02:00
if (ZPAreading()) {
Serial.println(F("Another reading already running"));
return;
}
ZPArequestData();
2019-06-11 23:38:48 +02:00
return;
}
2019-06-11 23:57:33 +02:00
if (r == '1') {
2019-06-13 15:19:31 +02:00
Serial.println(F("Baudrate to 300"));
2019-06-11 23:57:33 +02:00
Serial2.updateBaudRate(300);
}
if (r == '2') {
2019-06-13 15:19:31 +02:00
Serial.println(F("Baudrate to 1200"));
2019-06-11 23:57:33 +02:00
Serial2.updateBaudRate(1200);
}
if (r == '3') {
2019-06-13 15:19:31 +02:00
Serial.println(F("Baudrate to 2400"));
2019-06-11 23:57:33 +02:00
Serial2.updateBaudRate(2400);
}
if (r == '4') {
2019-06-13 15:19:31 +02:00
Serial.println(F("Baudrate to 4800"));
2019-06-11 23:57:33 +02:00
Serial2.updateBaudRate(4800);
}
if (r == '5') {
2019-06-13 15:19:31 +02:00
Serial.println(F("Baudrate to 9600"));
2019-06-11 23:57:33 +02:00
Serial2.updateBaudRate(9600);
}
2019-06-11 23:38:48 +02:00
if (r == 'C') {
2019-06-13 15:19:31 +02:00
ZPAchangeBaudrate('4');
2019-06-11 23:38:48 +02:00
return;
}
Serial2.write(r);
}
2019-06-13 15:19:31 +02:00
}
void loop() {
ArduinoOTA.handle();
handleZPA();
handlePCSerial();
2019-06-11 23:38:48 +02:00
}