This commit is contained in:
James 2024-04-27 18:45:06 +01:00
parent 4f555200a5
commit 39cc68c4d9
2 changed files with 24 additions and 51 deletions

View File

@ -5,6 +5,7 @@
#include <WiFiClientSecure.h>
#include <HTTPClient.h>
#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();
}

1
config.h Normal file
View File

@ -0,0 +1 @@
#define URL "https://api.ecomotus.co.uk/api/v1/graphite"