From 39cc68c4d91ffd0322eaeeb6d6cb9ea40fe5c2ec Mon Sep 17 00:00:00 2001 From: James Date: Sat, 27 Apr 2024 18:45:06 +0100 Subject: [PATCH] dev --- ESPBMS.ino | 74 +++++++++++++++++------------------------------------- config.h | 1 + 2 files changed, 24 insertions(+), 51 deletions(-) create mode 100644 config.h diff --git a/ESPBMS.ino b/ESPBMS.ino index 0f7f1a6..e366664 100644 --- a/ESPBMS.ino +++ b/ESPBMS.ino @@ -5,6 +5,7 @@ #include #include #include "ca_cert.h" +#include "config.h" // NTP Client setup WiFiUDP ntpUDP; @@ -36,27 +37,21 @@ void setup() { timeClient.begin(); client.setCACert(ca_cert); client.setHandshakeTimeout(5); + esp_bt_controller_mem_release(ESP_BT_MODE_CLASSIC_BT); 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"; + //https.addHeader("Content-Type","application/x-www-form-urlencoded"); + String url="URL"; + https.end(); 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; } @@ -76,6 +71,7 @@ void loop() { delay(250); } timeClient.update(); + //testClient("www.google.com", 80); client_connect(); for (int i = 0; i < foundDevices.getCount(); i++) { @@ -85,6 +81,8 @@ void loop() { BLEAdvertisedDevice advertisedDevice = foundDevices.getDevice(i); Serial.println("\nFound Device: " + String(advertisedDevice.toString().c_str())); + client_connect(); + std::string targetAddress = "d0:65:de:e5:89:76"; if (advertisedDevice.getAddress().toString() == targetAddress) { Serial.println("Victron device found!"); @@ -531,51 +529,25 @@ void eth_send(const char* format, Args... args) { } void eth_send(const char *data) { - Serial.print("connecting...\r\n"); - - WiFiClient client; - if (!client.connect("ecomotus.home.arpa", 2003)) { - Serial.println("connection failed"); - return; + if(!https.connected()){ + https.end(); + client_connect(); + } + Serial.print("http connecting...\r\n"); + Serial.printf("ETH Send: %s\r\n", data); + int httpCode=https.POST(data); + if(httpCode==200){ + Serial.println("post ok"); + while (client.available()) { + Serial.write(client.read()); + } + }else{ + Serial.print("post fail:"); + Serial.println(httpCode); } - 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()) { - // i=i+1; - Serial.write(client.read()); - // if(i==100){i=0; delay(1);} - } - - Serial.println("closing connection\n"); - client.stop(); } void eth_begin() { WiFi.onEvent(WiFiEvent); ETH.begin(ETH_ADDR, ETH_POWER_PIN, ETH_MDC_PIN, ETH_MDIO_PIN, ETH_TYPE, ETH_CLK_MODE); } - -void testClient(const char * host, uint16_t port) { - Serial.print("\nconnecting to "); - Serial.println(host); - - WiFiClient client; - if (!client.connect(host, port)) { - Serial.println("connection failed"); - return; - } - client.printf("GET / HTTP/1.1\r\nHost: %s\r\n\r\n", host); - while (client.connected() && !client.available()); - //long i; - while (client.available()) { - // i=i+1; - Serial.write(client.read()); - // if(i==100){i=0; delay(1);} - } - - Serial.println("closing connection\n"); - client.stop(); -} diff --git a/config.h b/config.h new file mode 100644 index 0000000..528ba1b --- /dev/null +++ b/config.h @@ -0,0 +1 @@ +#define URL "https://api.ecomotus.co.uk/api/v1/graphite"