updated code
This commit is contained in:
parent
0c6447f309
commit
6c4a7c4df1
49
examples/esp_influx/esp_influx.ino
Normal file
49
examples/esp_influx/esp_influx.ino
Normal file
@ -0,0 +1,49 @@
|
|||||||
|
#include <SPI.h>
|
||||||
|
#include <Ethernet.h>
|
||||||
|
#include <InfluxdbClient.h>
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
@ -1,10 +1,52 @@
|
|||||||
#include "InfluxdbClient.h"
|
#include "InfluxdbClient.h"
|
||||||
|
#include <Ethernet.h>
|
||||||
|
|
||||||
|
|
||||||
IRHasbroTag::InfluxdbClient() {}
|
InfluxdbClient::InfluxdbClient() {}
|
||||||
|
|
||||||
|
InfluxdbClient::~InfluxdbClient() {}
|
||||||
|
|
||||||
|
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";
|
||||||
|
}
|
||||||
|
|
||||||
IRHasbroTag::~InfluxdbClient() {
|
|
||||||
}
|
|
||||||
|
|
||||||
void IRHasbroTag::begin() {
|
|
||||||
}
|
}
|
@ -1,19 +1,28 @@
|
|||||||
#ifndef InfluxdbClient_H
|
#ifndef InfluxdbClient_H
|
||||||
#define InfluxdbClient_H
|
#define InfluxdbClient_H
|
||||||
|
|
||||||
|
#include <Ethernet.h>
|
||||||
|
#include <Arduino.h>
|
||||||
|
|
||||||
|
|
||||||
|
// 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 {
|
class InfluxdbClient {
|
||||||
public:
|
public:
|
||||||
explicit InfluxdbClient(); // Constructor
|
explicit InfluxdbClient(); // Constructor
|
||||||
~IRHasbroTag();
|
~InfluxdbClient();
|
||||||
|
|
||||||
|
|
||||||
void begin();
|
void begin();
|
||||||
// Functions
|
String Send_db(EthernetClient client, char* data);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
// Instances
|
// Instances
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // InfluxdbClient_H
|
#endif // InfluxdbClient_H
|
Loading…
Reference in New Issue
Block a user