#include <WiFi.h> // Thêm thư viện wifi
#include <HTTPClient.h>
WiFiClient client;
String thingsboardAddress = "http://demo.thingsboard.io:80"; // Replace with your ThingsBoard server address
String accessToken = "MZYqo7bsN4bG76hBFjUQ"; // Replace with your ThingsBoard device access token
String telemetryEndpoint = "/api/v1/" + accessToken + "/telemetry";
HTTPClient http;
void setup() {
Serial.begin(115200);
WiFi.disconnect();
WiFi.begin("Wokwi-GUEST"); // Replace with your WiFi credentials
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
Serial.println("Connected to WiFi");
}
void loop() {
if (WiFi.status() == WL_CONNECTED) {
if (client.connect("demo.thingsboard.io", 80)) { // Replace with your ThingsBoard server address
float temperature = random(20, 25);
float humidity = random(55, 70);
float light = random(0, 1000);
float ph = random(0, 14);
String payload = "{\"temperature\":" + String(temperature) + ",\"humidity\":" + String(humidity) + ",\"light\":" + String(light) + ",\"ph\":" + String(ph)+"}";
Serial.println("Sending data to ThingsBoard: " + payload);
http.begin(thingsboardAddress + telemetryEndpoint);
http.addHeader("Content-Type", "application/json");
int httpCode = http.POST(payload);
if (httpCode > 0) {
String response = http.getString();
Serial.println("Server response: " + response);
} else {
Serial.println("HTTP request failed");
}
http.end();
delay(1000);
}
}
}