#include <WiFi.h>
#include <HTTPClient.h>
#define WIFI_AP "Wokwi-GUEST"
#define WIFI_PASS ""
#define THINGSBOARD_SERVER "thingsboard.cloud"
#define THINGSBOARD_TOKEN "mkzvx3x3vd3tsxilqihv"
const int LM35Pin = 34;
const int numReadings = 10;
int readings[numReadings];
int readingIndex = 0;
int total = 0;
void setup() {
Serial.begin(9600);
pinMode(LM35Pin, INPUT);
analogReadResolution(12);
for (int i = 0; i < numReadings; i++) {
readings[i] = 0;
}
Serial.print("Connecting to WiFi");
WiFi.begin(WIFI_AP, WIFI_PASS);
while (WiFi.status() != WL_CONNECTED) {
delay(100);
Serial.print(".");
}
Serial.println(" Connected!");
Serial.println(" Starting!");
delay(2000);
}
void loop() {
int sensorValue = analogRead(LM35Pin);
float temperatureC = (sensorValue / 4095.0) * 330.0;
Serial.print("Temperature: ");
Serial.print(temperatureC);
Serial.print("°C");
Serial.println(" ~ ");
// Kirim data ke ThingsBoard
sendDataToThingsBoard(temperatureC);
delay(1000);
}
void sendDataToThingsBoard(float temperature) {
WiFiClient client;
HTTPClient http;
// URL untuk mengirim data ke ThingsBoard
String url = "http://" + String(THINGSBOARD_SERVER) + "/api/v1/" + String(THINGSBOARD_TOKEN) + "/telemetry";
// Buat payload JSON untuk dikirim ke ThingsBoard
String payload = "{\"temperatureC\":" + String(temperature) + "}";
// Kirim data ke ThingsBoard
http.begin(client, url);
http.addHeader("Content-Type", "application/json");
int httpResponseCode = http.POST(payload);
if (httpResponseCode > 0) {
Serial.print("HTTP Response code: ");
Serial.println(httpResponseCode);
} else {
Serial.print("Error sending data to ThingsBoard. HTTP Response code: ");
Serial.println(httpResponseCode);
}
http.end();
}