#include <Arduino.h>
#include <WiFi.h>
#include <HTTPClient.h>
#include <LiquidCrystal_I2C.h>
//#include "DHTesp.h"
LiquidCrystal_I2C lcd = LiquidCrystal_I2C(0x27, 16, 2);
String wifiSSID = "Wokwi-GUEST";
String wifiPassword = "";
String tbHost = "demo.thingsboard.io";
String tbToken = "dzLSI2CCfJ784A8nWwBp";
//const int DHT_PIN = 15;
//DHTesp dhtSensor;
#define DEBUG
#ifdef DEBUG
#define SERIAL_BEGIN Serial.begin(115200)
#define debug(p) Serial.print(p)
#define debugln(p) Serial.println(p)
#else
#define SERIAL_BEGIN
#define debug(p)
#define debugln(p)
#endif
//============================================================================================
//Pin yang dipakai di ESP32
#define NTC_ANALOG_PIN 34 //ke pin input sensor ntc
//============================================================================================
//============================================================================================
//Konfigurasi sensor NTC
const float BETA = 3950;
float suhu = 0.0;
//============================================================================================
//1. Implementasi interface sensor NTC
//Fungsi untuk membaca nilai sensor NTC
float bacaSensorNTC(){
//Baca nilai sensor menggunakan input analog
int analogValue = analogRead(NTC_ANALOG_PIN);
//Kalibrasi untuk mengkonversi nilai analog menjadi nilai suhu celsius
float celsius = 1 / (log(1 / (4095. / analogValue - 1)) / BETA + 1.0 / 298.15) - 273.15;
return celsius;
}
//============================================================================================
//DHTesp dhtSensor;
void connectWifi() {
WiFi.begin(wifiSSID.c_str(), wifiPassword.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() {
Serial.begin(115200);
connectWifi();
// dhtSensor.setup(DHT_PIN, DHTesp::DHT22);
lcd.init();
lcd.backlight();
}
void loop() {
//TempAndHumidity data = dhtSensor.getTempAndHumidity();
suhu = bacaSensorNTC();
//Tampilkan ke Serial monitor
debugln("[NTC] Temperature: " + String(suhu) + " ℃");
delay(10); // this speeds up the simulation
String temp = String(suhu,2);
// String hum = String(data.humidity,1);
// Serial.println("Temp: " + String(temp) + "°C");
// Serial.println("Humidity: " + String(hum) + "%");
// Serial.println("---");
lcd.setCursor(0, 0);
lcd.print("Temperature: " + temp);
//lcd.setCursor(0, 1);
//lcd.print("Humidity: " + hum);
sendDataToThingsBoard(temp);
delay(1000); // Wait for a new reading from the sensor (DHT22 has ~0.5Hz sample rate)
}