ZPA_elektromer_UARTtest/ZPA_elektromer_UARTtest.ino

172 lines
4.4 KiB
Arduino
Raw Normal View History

2019-06-11 23:38:48 +02:00
#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);
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");
})
.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-12 01:04:16 +02:00
// BCC is calculated as follows (source https://github.com/lvzon/dsmr-p1-parser/blob/master/doc/IEC-62056-21-notes.md):
// The BCC is calculated over the bytes after STX up to and including the ETX byte
// To calculate this BCC, take the first byte XOR 0xff
// XOR this value with the second byte, and so forth up to and including the last byte (include 0x03 ETX !!),
// and XOR the final value with 0xff.
2019-06-12 11:23:03 +02:00
uint8_t localBCC;
2019-06-12 01:04:16 +02:00
2019-06-11 23:38:48 +02:00
void loop() {
2019-06-12 01:04:16 +02:00
2019-06-11 23:38:48 +02:00
ArduinoOTA.handle();
if (Serial2.available()) {
char r = Serial2.read();
if (r == 0x02) {
Serial.print("<STX>");
2019-06-12 11:23:03 +02:00
localBCC = 0xFF; // Set checksum to 0xFF (first byte is XORed with 0xFF)
2019-06-11 23:38:48 +02:00
return;
}
if (r == 0x03) {
2019-06-11 23:57:33 +02:00
Serial.println("<ETX>");
Serial.print("Checksum <BCC>: ");
2019-06-12 01:04:16 +02:00
Serial.print(Serial2.read());
Serial.println();
Serial.print("Calculated <BCC>: ");
2019-06-12 11:23:03 +02:00
localBCC = localBCC ^ r; // Include last byte (ETX) to checksum
localBCC = localBCC ^ 0xFF; // Final value XORed with 0xFF
2019-06-12 01:04:16 +02:00
Serial.print(localBCC);
Serial.println();
2019-06-11 23:57:33 +02:00
Serial.println();
Serial.println("Changing back baudrate to 300");
Serial2.updateBaudRate(300);
2019-06-11 23:38:48 +02:00
return;
}
2019-06-12 01:04:16 +02:00
// Calculate BCC checksum
2019-06-12 11:23:03 +02:00
localBCC = localBCC ^ r;
2019-06-11 23:38:48 +02:00
Serial.write(r);
}
if (Serial.available()) {
char r = Serial.read();
if (r == 'R') {
Serial.println("Requesting data");
Serial2.print("/?!\r\n");
return;
}
2019-06-11 23:57:33 +02:00
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);
}
2019-06-11 23:38:48 +02:00
if (r == 'C') {
2019-06-11 23:57:33 +02:00
Serial.println("Change baudrate");
2019-06-11 23:38:48 +02:00
Serial2.write(0x06);
2019-06-11 23:57:33 +02:00
Serial2.print("040\r\n");
Serial2.flush();
Serial2.updateBaudRate(4800);
2019-06-11 23:38:48 +02:00
return;
}
Serial2.write(r);
}
}