#include <Arduino.h>
#include <WiFi.h>
#include <HTTPClient.h>
const int LM35_PIN = 35;
const int numReadings = 10;
int readings[numReadings];
int readingIndex = 0;
int total = 0;
String wifiSSID = "Wokwi-GUEST";
String wifiPass = "";
String tbHost = "demo.thingsboard.io";
//String tbHost = "thingsboard.cloud";
String tbToken = "hcyTACXCR8DWju7fRzVq";
void connectWifi() {
WiFi.begin(wifiSSID.c_str(), wifiPass.c_str());
while (WiFi.status() != WL_CONNECTED) {
delay(500);
}
}
void sendDataToThingsBoard(String temperature) {
HTTPClient http;
String url = "http://" + tbHost + "/api/v1/" + tbToken + "/telemetry";
String payload = "{\"temperature\":" + temperature + "}";
http.begin(url);
http.addHeader("Content-Type", "application/json");
int httpResponseCode = http.POST(payload);
if (httpResponseCode > 0) {
Serial.println("Data sent to ThingsBoard successfully");
} else {
Serial.println("Error sending data to ThingsBoard");
}
http.end();
}
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
connectWifi();
pinMode(LM35_PIN, INPUT);
analogReadResolution(12);
for (int i = 0; i < numReadings; i++) {
readings[i] = 0;
}
}
void loop() {
// get the ADC value from the temperature sensor
int adcValue = analogRead(LM35_PIN);
// Ubah nilai bacaan menjadi suhu dalam derajat Celsius
// float temperature = (adcVal / 4095.0) * 330.0;
// new calculation:
// ref: https://e2e.ti.com/support/microcontrollers/arm-based-microcontrollers-group/arm-based-microcontrollers/f/arm-based-microcontrollers-forum/831167/ccs-tms570lc4357-adc-result-convert-to-celsius-degree
float temperature = (adcValue / 4096.0) * 330.0;
// print the temperature TANPA FILTER in the Serial Monitor:
Serial.print("Temperature: ");
Serial.print(temperature); // print the temperature in Celsius
Serial.print("°C");
// Serial.print(" ~ ");
// Serial.print(adcValue);
Serial.println(" ~ ");
sendDataToThingsBoard(String(temperature));
delay(1000); // this speeds up the simulation
}