#include <LiquidCrystal_I2C.h>
#include <Wire.h>
#include <WiFi.h>
#include <UniversalTelegramBot.h>
#include <ArduinoJson.h>
#include <WiFiClientSecure.h>
// Inisialisasi pin untuk LCD
LiquidCrystal_I2C lcd(0x27, 16, 2);
// Pin analog untuk sensor NTC
const int ntcPin1 = 35;
const int ntcPin2 = 32;
unsigned long displayStartTime = 0, wifiRead = 0;
int displayState = 0;
const char* ssid = "Wokwi-GUEST";
const char* pw = "";
// Telegram BOT
#define BOTtoken "7371004070:AAFHlpd_OBfGfNzACMK1zRqXD5rmmHUzOBs"
#define CHAT_ID "1523471432"
// #define BOTtoken "7299414967:AAER90giRcYXo9Ezl6Pitq2X2I8JGu1KF1o"
//#define BOTtoken "6628232016:AAHl9M9mLf6jcfCn2t3vmPpj0CsCmvgOVl8"
//#define CHAT_ID "1290007560"
//#define CHAT_ID2 "1523471432"
WiFiClient espClient;
WiFiClientSecure client;
UniversalTelegramBot bot(BOTtoken, client);
// Cek Pesan Setiap 1 detik
int interval = 1000;
unsigned long waktu_terakhir = 0, Waktubaca = 0;
bool wifiConnected = false;
void setup() {
Serial.begin(115200); // Initialize serial communication
// Menggunakan WiFiManager untuk menghubungkan WiFi
lcd.init();
lcd.begin(16, 2);
lcd.backlight();
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Delta Temp");
lcd.setCursor(0,1);
lcd.print("Wifi Connecting");
// Hubungkan ke Wi-Fi
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, pw);
client.setCACert(TELEGRAM_CERTIFICATE_ROOT);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Sedang menghubungkan ke WiFi..");
}
wifiConnected = true;
// IP Address
Serial.println(WiFi.localIP());
Serial.print("Atur Waktu: ");
configTime(0, 0, "pool.ntp.org");
time_t now = time(nullptr);
while (now < 24 * 3600)
{
Serial.print(".");
delay(100);
now = time(nullptr);
}
Serial.println(now);
}
void loop() {
// Membaca nilai ADC dari sensor NTC
int adcValue1 = analogRead(ntcPin1);
int adcValue2 = analogRead(ntcPin2);
// Menghitung suhu dari nilai ADC
float temp1 = calculateTemperature1(adcValue1);
float temp2 = calculateTemperature2(adcValue2);
Serial.println(temp1);
Serial.println(temp2);
// Menghitung perbedaan suhu menggunakan fungsi yang sudah dibuat
float diffTemp = calculateDiffTemp(temp1, temp2);
float diffTempF = calculateDiffTempF(temp1, temp2);
Serial.println(String(diffTemp) + " C");
Serial.println(String(diffTempF) + " F");
// Menampilkan perbedaan suhu di baris kedua LCD
if (millis() - displayStartTime >= 4000) {
displayState = (displayState + 1) % 2;
displayStartTime = millis();
lcd.clear();
}
switch (displayState) {
case 0:
lcd.setCursor(0, 0);
lcd.print("Delta Temp");
lcd.setCursor(0, 1);
lcd.print(diffTemp);
lcd.setCursor(5, 1);
lcd.print((char)223); // Simbol derajat
lcd.print("C");
lcd.setCursor(8, 1);
lcd.print(diffTempF);
lcd.setCursor(14, 1);
lcd.print((char)223);
lcd.print("F");
break;
case 1:
lcd.setCursor(0, 0);
lcd.print("T1 = ");
lcd.print(temp1);
lcd.setCursor(12, 0);
lcd.print((char)223); // Simbol derajat
lcd.print("C");
lcd.setCursor(0, 1);
lcd.print("T2 = ");
lcd.print(temp2);
lcd.setCursor(12, 1);
lcd.print((char)223); // Simbol derajat
lcd.print("C");
break;
}
if (wifiConnected) {
cek_pesan();
} else {
lcd.setCursor(0, 0);
lcd.print("Delta Temp");
lcd.setCursor(0,1);
lcd.print("Wifi Connecting");
}
delay(200);
}
// Cek Pesan Terbaru
void cek_pesan() {
if (millis() > waktu_terakhir + interval) {
int banyakPesan = bot.getUpdates(bot.last_message_received + 1);
while (banyakPesan) {
Serial.println("got response");
handleNewMessages(banyakPesan);
banyakPesan = bot.getUpdates(bot.last_message_received + 1);
}
waktu_terakhir = millis();
}
}
// Memproses pesan yang diterima
void handleNewMessages(int numNewMessages) {
for (int i = 0; i < numNewMessages; i++) {
// Cek Chat ID
String chat_id = String(bot.messages[i].chat_id);
if (chat_id != CHAT_ID) {
bot.sendMessage(chat_id, "Unauthorized user", "");
continue;
}
// Terima pesan dari telegram
String text = bot.messages[i].text;
Serial.println(text);
String from_name = bot.messages[i].from_name;
if (text == "/start") {
String welcome = "Welcome, " + from_name + ".\n";
welcome += "Use the following commands to get delta temp radiator data unit.\n\n";
welcome += "/temp_x (x = unit name)\n";
bot.sendMessage(chat_id, welcome, "");
}
if (text == "/temp_EX3018") {
String test = getReadings();
bot.sendMessage(chat_id, test);
}
}
}
float calculateTemperature1(int adcValue1) {
// Temperature calculation logic
const float referenceVoltage = 3.3;
const float referenceResistor = 10000; // the 'other' resistor
const float beta = 3950; // Beta value (Typical Value)
const float nominalTemperature = 32.2; // Nominal temperature for calculating the temperature coefficient
const float nominalResistance = 6277.59; // Resistance value at nominal temperature
float voltage = (adcValue1 * referenceVoltage) / 4095.0; // Calculate voltage
float resistance = (voltage * referenceResistor) / (referenceVoltage - voltage); // Calculate thermistor resistance with updated configuration
Serial.print("Resistance: ");
Serial.print(resistance);
Serial.println(" ohms");
// Calculate temperature using the Beta parameter equation
float tempK = 1 / (((log(resistance / nominalResistance)) / beta) + (1 / (nominalTemperature + 273.15)));
float temperature = tempK - 273.15; // Get temperature in Celsius
if (temperature >= 65){
const float referenceVoltage = 3.3;
const float referenceResistor = 10000; // the 'other' resistor
const float beta = 3950; // Beta value (Typical Value)
const float nominalTemperature = 100; // Nominal temperature for calculating the temperature coefficient
const float nominalResistance = 270; // Resistance value at nominal temperature
float voltage = (adcValue1 * referenceVoltage) / 4095.0; // Calculate voltage
float resistance = (voltage * referenceResistor) / (referenceVoltage - voltage); // Calculate thermistor resistance with updated configuration
Serial.print("Resistance: ");
Serial.print(resistance);
Serial.println(" ohms");
// Calculate temperature using the Beta parameter equation
float tempK = 1 / (((log(resistance / nominalResistance)) / beta) + (1 / (nominalTemperature + 273.15)));
float temperature = tempK - 273.15; // Get temperature in Celsius
return temperature;
}
return temperature;
}
float calculateTemperature2(int adcValue2) {
// Temperature calculation logic
const float referenceVoltage = 3.3;
const float referenceResistor = 10000 ; // the 'other' resistor
const float beta = 3950; // Beta value (Typical Value)
const float nominalTemperature = 32.2; // Nominal temperature for calculating the temperature coefficient
const float nominalResistance = 6277.59; // Resistance value at nominal temperature
float voltage = (adcValue2 * referenceVoltage) / 4095.0; // Calculate voltage
float resistance = (voltage * referenceResistor) / (referenceVoltage - voltage); // Calculate thermistor resistance with updated configuration
Serial.print("Resistance: ");
Serial.print(resistance);
Serial.println(" ohms");
// Calculate temperature using the Beta parameter equation
float tempK = 1 / (((log(resistance / nominalResistance)) / beta) + (1 / (nominalTemperature + 273.15)));
float temperature = tempK - 273.15; // Get temperature in Celsius
if (temperature >= 65){
const float referenceVoltage = 3.3;
const float referenceResistor = 10000; // the 'other' resistor
const float beta = 3950; // Beta value (Typical Value)
const float nominalTemperature = 100; // Nominal temperature for calculating the temperature coefficient
const float nominalResistance = 270; // Resistance value at nominal temperature
float voltage = (adcValue2 * referenceVoltage) / 4095.0; // Calculate voltage
float resistance = (voltage * referenceResistor) / (referenceVoltage - voltage); // Calculate thermistor resistance with updated configuration
Serial.print("Resistance: ");
Serial.print(resistance);
Serial.println(" ohms");
// Calculate temperature using the Beta parameter equation
float tempK = 1 / (((log(resistance / nominalResistance)) / beta) + (1 / (nominalTemperature + 273.15)));
float temperature = tempK - 273.15; // Get temperature in Celsius
return temperature;
}
return temperature;
}
// Fungsi untuk menghitung selisih temperatur dalam Celcius
float calculateDiffTemp(float temp1, float temp2) {
return fabs(temp1 - temp2);
}
// Fungsi untuk menghitung selisih temperatur dalam Fahrenheit
float calculateDiffTempF(float temp1, float temp2) {
float diffTemp = calculateDiffTemp(temp1, temp2);
return (diffTemp * 1.8) + 32;
}
// Fungsi utama untuk mendapatkan pembacaan dan mengembalikan pesan string
String getReadings() {
if (millis()-Waktubaca >= 170){
Waktubaca = millis();
int adcValue1 = analogRead(ntcPin1);
int adcValue2 = analogRead(ntcPin2);
// Menghitung suhu dari nilai ADC
float temp1 = calculateTemperature1(adcValue1);
float temp2 = calculateTemperature2(adcValue2);
Serial.println(temp1);
Serial.println(temp2);
// Menghitung perbedaan suhu menggunakan fungsi yang sudah dibuat
float diffTemp = calculateDiffTemp(temp1, temp2);
float diffTempF = calculateDiffTempF(temp1, temp2);
Serial.println(diffTemp);
Serial.println(diffTempF);
String message = "Unit EX3018 \n";
message += "Radiator Temp Status: \n";
message += "T1: " + String(temp1) + " ºC \n";
message += "T2: " + String(temp2) + " ºC \n";
message += "Delta: " + String(diffTemp) + " ºC ; " + String(diffTempF) + " ºF \n";
return message;
}
}