From 9191e2edd2b29e0fde448a1b64dbb37d512e4880 Mon Sep 17 00:00:00 2001 From: James Date: Sat, 27 Apr 2024 16:29:55 +0100 Subject: [PATCH] dev --- ESPBMS.ino | 97 ++++++++++++++++++++++++++++++++++++++++------------- ca_cert.h | 23 +++++++++++++ victron.ino | 14 ++++---- 3 files changed, 103 insertions(+), 31 deletions(-) create mode 100644 ca_cert.h diff --git a/ESPBMS.ino b/ESPBMS.ino index b465737..6b3b3af 100644 --- a/ESPBMS.ino +++ b/ESPBMS.ino @@ -1,5 +1,14 @@ #include "BLEDevice.h" #include +#include +#include +#include +#include +#include "ca_cert.h" + +// NTP Client setup +WiFiUDP ntpUDP; +NTPClient timeClient(ntpUDP, "pool.ntp.org"); static BLEUUID serviceUUID("0000ff00-0000-1000-8000-00805f9b34fb"); //xiaoxiang bms service static BLEUUID charUUID_rx("0000ff01-0000-1000-8000-00805f9b34fb"); //xiaoxiang bms rx id @@ -13,17 +22,44 @@ typedef struct byte dataLen; } bmsPacketHeaderStruct; -void setup() { - Serial.begin(115200); - eth_begin(); - BLEDevice::init(""); // Initialize BLE device -} - char currentName[128]; bool gotBasicInfo; bool gotCellInfo; static bool eth_ready = false; +WiFiClientSecure client; +HTTPClient https; + +void setup() { + Serial.begin(115200); + eth_begin(); + timeClient.begin(); + client.setCACert(ca_cert); + client.setHandshakeTimeout(5); + BLEDevice::init(""); // Initialize BLE device +} + +bool client_connect(){ + //https.setTimeout(preferences.getInt("http_timeout")); + //https.setConnectTimeout(preferences.getInt("http_timeout")); + https.addHeader("Content-Type","application/x-www-form-urlencoded"); + String url="https://jhodges.co.uk/test.php"; + if(!https.begin(client, url)) { // HTTPS + Serial.println("connect fail"); + return false; + } + Serial.println("connect ok"); + int httpCode=https.POST("TEST"); + if(httpCode==200){ + Serial.println("post ok"); + }else{ + Serial.print("post fail:"); + Serial.println(httpCode); + return false; + } + return true; +} + void loop() { Serial.printf("\r\n\r\n===============================\r\n\r\n"); @@ -39,6 +75,8 @@ void loop() { Serial.println("Wait for eth..."); delay(250); } + timeClient.update(); + client_connect(); for (int i = 0; i < foundDevices.getCount(); i++) { delay(1000); @@ -311,8 +349,8 @@ bool processBasicInfo(byte *data, unsigned int dataLen){ int32_t Watts = Volts * Amps / 1000000; // W - Serial.printf("Remaining Capacity: %4.2fAh\n", ((float)(data[4] * 256 + data[5]))/100); - Serial.printf("Nominal Capacity: %4.2fAh\n", ((float)(data[6] * 256 + data[7]))/100); + //Serial.printf("Remaining Capacity: %4.2fAh\n", ((float)(data[4] * 256 + data[5]))/100); + //Serial.printf("Nominal Capacity: %4.2fAh\n", ((float)(data[6] * 256 + data[7]))/100); uint32_t CapacityRemainAh = ((uint16_t)two_ints_into16(data[4], data[5])) * 10; uint8_t CapacityRemainPercent = ((uint8_t)data[19]); @@ -324,14 +362,14 @@ bool processBasicInfo(byte *data, unsigned int dataLen){ uint16_t BalanceCodeHigh = (two_ints_into16(data[14], data[15])); uint8_t MosfetStatus = ((byte)data[20]); - Serial.printf(">>>RC.%s.Voltage %f\r\n",currentName, (float)Volts / 1000); - Serial.printf(">>>RC.%s.Amps %f\r\n",currentName, (float)Amps / 1000); - Serial.printf(">>>RC.%s.Watts %f\r\n",currentName, (float)Watts); - Serial.printf(">>>RC.%s.Capacity_Remain_Ah %f\r\n",currentName, (float)CapacityRemainAh / 1000); - Serial.printf(">>>RC.%s.Capacity_Remain_Wh %f\r\n",currentName, ((float)(CapacityRemainAh) / 1000) * ((float)(Volts) / 1000)); - Serial.printf(">>>RC.%s.Capacity_Remain_Percent %d\r\n",currentName, CapacityRemainPercent); - Serial.printf(">>>RC.%s.Temp1 %f\r\n",currentName, (float)Temp1 / 10); - Serial.printf(">>>RC.%s.Temp2 %f\r\n",currentName, (float)Temp2 / 10); + eth_send("test.RC.%s.Voltage %f",currentName, (float)Volts / 1000); + eth_send("test.RC.%s.Amps %f",currentName, (float)Amps / 1000); + eth_send("test.RC.%s.Watts %f",currentName, (float)Watts); + eth_send("test.RC.%s.Capacity_Remain_Ah %f",currentName, (float)CapacityRemainAh / 1000); + eth_send("test.RC.%s.Capacity_Remain_Wh %f",currentName, ((float)(CapacityRemainAh) / 1000) * ((float)(Volts) / 1000)); + eth_send("test.RC.%s.Capacity_Remain_Percent %d",currentName, CapacityRemainPercent); + eth_send("test.RC.%s.Temp1 %f",currentName, (float)Temp1 / 10); + eth_send("test.RC.%s.Temp2 %f",currentName, (float)Temp2 / 10); /* Serial.printf("%s Balance Code Low: 0x%x\r\n",currentName, BalanceCodeLow); Serial.printf("%s Balance Code High: 0x%x\r\n",currentName, BalanceCodeHigh); @@ -365,13 +403,13 @@ bool processCellInfo(byte *data, unsigned int dataLen) _cellMin = CellVolt; } - Serial.printf(">>>RC.%s.Cell.%d.Voltage %f\r\n",currentName, i+1,(float)CellVolt/1000); + eth_send("test.RC.%s.Cell.%d.Voltage %f",currentName, i+1,(float)CellVolt/1000); } - Serial.printf(">>>RC.%s.Max_Cell_Voltage %f\r\n",currentName, (float)_cellMax / 1000); - Serial.printf(">>>RC.%s.Min_Cell_Voltage %f\r\n",currentName, (float)_cellMin / 1000); - Serial.printf(">>>RC.%s.Difference_Cell_Voltage %f\r\n",currentName, (float)(_cellMax - _cellMin) / 1000); - Serial.printf(">>>RC.%s.Average_Cell_Voltage %f\r\n",currentName, (float)(_cellSum / NumOfCells) / 1000); + eth_send("test.RC.%s.Max_Cell_Voltage %f",currentName, (float)_cellMax / 1000); + eth_send("test.RC.%s.Min_Cell_Voltage %f",currentName, (float)_cellMin / 1000); + eth_send("test.RC.%s.Difference_Cell_Voltage %f",currentName, (float)(_cellMax - _cellMin) / 1000); + eth_send("test.RC.%s.Average_Cell_Voltage %f",currentName, (float)(_cellSum / NumOfCells) / 1000); gotCellInfo=true; @@ -483,15 +521,26 @@ void WiFiEvent(WiFiEvent_t event) { } } +// Overloaded template function for formatted sending +template +void eth_send(const char* format, Args... args) { + constexpr std::size_t bufferSize = 1024; // Define a reasonable default buffer size + std::array buffer; + snprintf(buffer.data(), buffer.size(), format, args...); + eth_send(buffer.data()); // Call the original eth_send function +} + void eth_send(const char *data) { - Serial.print("\nconnecting..."); + Serial.print("connecting...\r\n"); WiFiClient client; - if (!client.connect("10.6.0.1", 2003)) { + if (!client.connect("ecomotus.home.arpa", 2003)) { Serial.println("connection failed"); return; } - client.print(data); + unsigned long epochTime = timeClient.getEpochTime(); + client.printf("%s %lu\r\n", data, epochTime); + Serial.printf("ETH Send: %s %lu\r\n", data, epochTime); while (client.connected() && !client.available()); //long i; while (client.available()) { diff --git a/ca_cert.h b/ca_cert.h new file mode 100644 index 0000000..c77eccc --- /dev/null +++ b/ca_cert.h @@ -0,0 +1,23 @@ +#ifndef CA_CERT_H +#define CA_CERT_H +const char* ca_cert = \ + "-----BEGIN CERTIFICATE-----\n" \ + "MIIDHTCCAgWgAwIBAgIUWLAb5lCXs4G6QxCaV78EtsRQgkEwDQYJKoZIhvcNAQEL\n" \ + "BQAwHTEbMBkGA1UEAwwSYXBpLmVjb21vdHVzLmNvLnVrMCAXDTIyMTAxOTE1Mjgx\n" \ + "N1oYDzIxMjIwOTI1MTUyODE3WjAdMRswGQYDVQQDDBJhcGkuZWNvbW90dXMuY28u\n" \ + "dWswggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQCwXfttIwX1y1tSeaiZ\n" \ + "0LtnQ3q9xosglFsXyoLcctJmOf+zgdHbNNxMH8CRbm4Z3ZQ4ghBoL/1RHaERl5aA\n" \ + "U7oVxr4MPHn6fWsYrLlaXLIcmL6ZS91woTKLejhf6D991sH2Jt0xVDhqerimnF4p\n" \ + "Ut1U6rY6Lw7aUAUUldChhzRUAkAcMHApWwzxElAM+KFFleLq63AESkT21xYOO+WG\n" \ + "2hLTQB+hDcBvN9IQ4Ud1V7AQ/MDKzVvJsn/z+KnslbH246l1w6haJk230UbPizau\n" \ + "fAWl63O2/xIxPJzWBXJeUuvi+lTqCf+ZVPBU6chpyL4xX+I7vCTBoKmpaI+qAF9F\n" \ + "2C4HAgMBAAGjUzBRMB0GA1UdDgQWBBTtxYrL2Cg9PVp4wx6MllB/cKXXYjAfBgNV\n" \ + "HSMEGDAWgBTtxYrL2Cg9PVp4wx6MllB/cKXXYjAPBgNVHRMBAf8EBTADAQH/MA0G\n" \ + "CSqGSIb3DQEBCwUAA4IBAQBAai8ewCT3Q2CgBMxvDLKQx7YRBNlv1gbUtYq88rvK\n" \ + "iz6yzGmbPP1Ax5LCv0oRtRdnrz0h2F80tBibS2mJ2tqsLd3277yMN81mHB0qVIrR\n" \ + "tq9aTzjGHUXgXmcezEgkTLTfISebvCB8jdR7cjvFUaTUKH3MLR3jNAAqU6WLVY6Q\n" \ + "wCYLKRhTU+aYkDeObOu2fsoph8FwR9gB9D4K0/W78UTiOQxLFJmCqubooNtGLrph\n" \ + "dz1hmIkYSKH3pdhE3kZwNilYVjyfxq3UFkh2/2J0Fz7vB7eaJE6PptcPJ2KgxTMO\n" \ + "i7QEQ+jNru8B20F4DrbvEa0IY5wv9mywugBsXg5rcfjs\n" \ + "-----END CERTIFICATE-----\n"; +#endif diff --git a/victron.ino b/victron.ino index ab856f5..903eeb0 100644 --- a/victron.ino +++ b/victron.ino @@ -185,12 +185,12 @@ void decodeVictron(BLEAdvertisedDevice advertisedDevice) { return; } - Serial.printf(">>>RC.MPPT.1.Battery_Volts %f\r\n",batteryVoltage); - Serial.printf(">>>RC.MPPT.1.Battery_Amps %f\r\n",batteryCurrent); - Serial.printf(">>>RC.MPPT.1.Battery_Watts %f\r\n",batteryVoltage*batteryCurrent); - Serial.printf(">>>RC.MPPT.1.Solar_Watts %f\r\n",inputPower); - Serial.printf(">>>RC.MPPT.1.Output_Current %f\r\n",outputCurrent); - Serial.printf(">>>RC.MPPT.1.Yield %f\r\n",todayYield); - Serial.printf(">>>RC.MPPT.1.State %d\r\n",deviceState); + eth_send("test.RC.MPPT.1.Battery_Volts %f",batteryVoltage); + eth_send("test.RC.MPPT.1.Battery_Amps %f",batteryCurrent); + eth_send("test.RC.MPPT.1.Battery_Watts %f",batteryVoltage*batteryCurrent); + eth_send("test.RC.MPPT.1.Solar_Watts %f",inputPower); + eth_send("test.RC.MPPT.1.Output_Current %f",outputCurrent); + eth_send("test.RC.MPPT.1.Yield %f",todayYield); + eth_send("test.RC.MPPT.1.State %d",deviceState); } }