From 6c4a7c4df1818c3b03a5bb4e241adda1b006677f Mon Sep 17 00:00:00 2001 From: locker98 Date: Wed, 1 Jan 2025 15:16:41 -0500 Subject: [PATCH] updated code --- examples/esp_influx/esp_influx.ino | 49 ++++++++++++++++++++++++++++ src/InfluxdbClient.cpp | 52 +++++++++++++++++++++++++++--- src/InfluxdbClient.h | 19 ++++++++--- 3 files changed, 110 insertions(+), 10 deletions(-) create mode 100644 examples/esp_influx/esp_influx.ino diff --git a/examples/esp_influx/esp_influx.ino b/examples/esp_influx/esp_influx.ino new file mode 100644 index 0000000..188186d --- /dev/null +++ b/examples/esp_influx/esp_influx.ino @@ -0,0 +1,49 @@ +#include +#include +#include + +InfluxdbClient db; +// Network configuration +byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; // MAC Address +IPAddress ip(10, 10, 1, 176); // IP Address + + + + +int cpu_usage = 0; // Example value +int memory_usage = 0; // Example value +char server_data[200];// = "system_metrics,host=local cpu_usage=40,memory_usage=100"; + +// Initialize Ethernet client +EthernetClient client; + +void setup() { + Serial.begin(115200); + Ethernet.init(5); + db.begin(); + while (!Serial) { + ; // Wait for Serial Monitor to open + } + + // Start Ethernet connection + Ethernet.begin(mac, ip); + + // Give the Ethernet shield a second to initialize + delay(1000); + Serial.println("Ethernet setup completed"); +} + +void loop() { + Serial.println("sending"); + + cpu_usage +=3; // Example value + memory_usage += 1; // Example value + + sprintf(server_data, "system_metrics,host=local cpu_usage=%d,memory_usage=%d", cpu_usage, memory_usage); + + String result = db.Send_db(client, server_data); + + + Serial.println(result); + delay(2000); +} diff --git a/src/InfluxdbClient.cpp b/src/InfluxdbClient.cpp index e132a50..2b17eb9 100644 --- a/src/InfluxdbClient.cpp +++ b/src/InfluxdbClient.cpp @@ -1,10 +1,52 @@ #include "InfluxdbClient.h" +#include -IRHasbroTag::InfluxdbClient() {} +InfluxdbClient::InfluxdbClient() {} -IRHasbroTag::~InfluxdbClient() { -} +InfluxdbClient::~InfluxdbClient() {} -void IRHasbroTag::begin() { -} +void InfluxdbClient::begin() {} + +String InfluxdbClient::Send_db(EthernetClient client, char* data) { + String buff; + + if (client.connect(INFLUXDB_HOST, INFLUXDB_PORT)) { + Serial.println("Connected to InfluxDB server"); + + // Construct the HTTP POST request + client.println("POST /api/v2/write?org=" INFLUXDB_ORG "&bucket=" INFLUXDB_BUCKET "&precision=s HTTP/1.1"); + client.print("Host: "); + client.print(INFLUXDB_HOST); + client.print(":"); + client.println(INFLUXDB_PORT); + //client.println("User-Agent: curl/8.5.0"); + //client.println("Accept: */*"); + client.print("Authorization: Token "); + client.println(INFLUXDB_TOKEN); + client.println("Content-Type: text/plain; charset=utf-8"); + client.println("Connection: close"); + client.print("Content-Length: "); + client.println(strlen(data)); + client.println(); // End of headers + client.println(data); // Data payload + + + // Wait for server response + while (client.connected() || client.available()) { + if (client.available()) { + char c = client.read(); + buff += String(c); // Print the response + } + } + + // Close the connection + client.stop(); + + return buff; + } + else { + return "Connection failed"; + } + +} \ No newline at end of file diff --git a/src/InfluxdbClient.h b/src/InfluxdbClient.h index b2165e4..fa32a3d 100644 --- a/src/InfluxdbClient.h +++ b/src/InfluxdbClient.h @@ -1,19 +1,28 @@ #ifndef InfluxdbClient_H #define InfluxdbClient_H +#include +#include + + +// InfluxDB Configuration +#define INFLUXDB_HOST "10.10.1.2" +#define INFLUXDB_PORT 8086 +#define INFLUXDB_TOKEN "m7gkPochobk-OxyKjdytRtKdtFMM68QTaEhIDoMpcXdT-a66nEOa8ezWULehE5Hjn9i26LnczlEQHuM5gN1rVw==" +#define INFLUXDB_ORG "tomato" +#define INFLUXDB_BUCKET "SolarESP32" + class InfluxdbClient { public: explicit InfluxdbClient(); // Constructor - ~IRHasbroTag(); - + ~InfluxdbClient(); void begin(); - // Functions + String Send_db(EthernetClient client, char* data); private: // Instances - }; -#endif // InfluxdbClient_H +#endif // InfluxdbClient_H \ No newline at end of file