#include <Wire.h>
#include <LiquidCrystal.h>
#include <Arduino.h>
#include <WiFi.h>
#include <HTTPClient.h>
#include <LiquidCrystal_I2C.h>
String wifiSSID = "Wokwi-GUEST";
String wifiPassword = "";
String tbHost = "demo.thingsboard.io";
String tbToken = "q7MNmn4vBq9Eu2l9Pkti";
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"}";
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();
}
const float BETA = 3950;
float suhu = 0.0;
// Analog input pin for the NTC thermistor
const int NTC_PIN = 34;
// Buzzer control pin
const int BUZZER_PIN = 12;
// Temperature threshold to trigger the alarm (adjust as needed)
const float TEMPERATURE_THRESHOLD = 70.0;
// LCD settings
LiquidCrystal lcd(15,2,4,5,18,19);
float bacaSensorNTC(){
//Baca nilai sensor menggunakan input analog
int analogValue = analogRead(NTC_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;
}
void setup() {
Serial.begin(115200);
connectWifi();
lcd.begin(16, 2);
lcd.print("Temperature:");
pinMode(BUZZER_PIN, OUTPUT);
ledcSetup(0, 5000, 8); // LEDC channel 0, 5000 Hz PWM, 8-bit resolution
}
void loop() {
suhu = bacaSensorNTC();
String temp = String(suhu);
lcd.setCursor(0, 1);
lcd.print(suhu);
lcd.print("C");
if (suhu < TEMPERATURE_THRESHOLD) {
// Sound the buzzer (if using an active buzzer)
// For a passive buzzer, this part depends on the circuit configuration
tone(BUZZER_PIN, 1000); // You can adjust the frequency for a different sound
} else {
noTone(BUZZER_PIN);
}
sendDataToThingsBoard(temp);
delay(1000); // Wait for 1 second before updating temperature
}