#include <WiFi.h>
#include <UniversalTelegramBot.h>
#include <WiFiClientSecure.h>
const char* ssid = "Wokwi-GUEST";
const char* password = "";
// Note: Fixed space in BOT_TOKEN
#define BOT_TOKEN "8329398768:AAFvhOIetejbojWmLuuPSepTnzurgCwFNa4"
WiFiClientSecure client;
UniversalTelegramBot bot(BOT_TOKEN, client);
const int ntcPin = 34;
// Forward declaration of the function
void handleNewMessages(int numNewMessages);
// --- NTC Thermistor Calculation ---
// Note: Fixed syntax, missing operators, and the misplaced 'return' statement
float calculateTemperature(int adcValue) {
// Assuming a 10k resistor in a voltage divider
float voltage = adcValue * (3.3 / 4095.0);
float resistance = (10000.0 * 3.3 / voltage) - 10000.0;
// Steinhart-Hart Equation to calculate temperature in Celsius
float temperature = (1.0 / (0.001129148 + (0.000234125 * log(resistance)) + (0.0000000876741 * pow(log(resistance), 3)))) - 273.15;
return temperature;
}
// --- SETUP ---
// Note: All setup code is now inside the setup() function's braces
void setup() {
Serial.begin(115200);
WiFi.begin(ssid, password);
Serial.print("Connecting to WiFi");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println(" Connected to WiFi");
client.setInsecure();
pinMode(ntcPin, INPUT);
Serial.println("Bot setup completed");
}
// --- LOOP ---
// Note: All loop code is now inside the loop() function's braces
void loop() {
// Note: Fixed missing '=' and typo 'numNewMassages'
int numNewMessages = bot.getUpdates(bot.last_message_received + 1);
if (numNewMessages) {
handleNewMessages(numNewMessages);
}
delay(1000); // Added a delay to prevent getting spammed with updates
}
// --- HANDLE NEW MESSAGES ---
// Note: Fixed typo 'handleNesMessages' and 'numNestlessages'
void handleNewMessages(int numNewMessages) {
// Note: Fixed incorrect for loop syntax
for (int i = 0; i < numNewMessages; i++) {
// Note: Fixed missing '='
String chat_id = bot.messages[i].chat_id;
// Note: Fixed missing '=' and typo 'hot'
String text = bot.messages[i].text;
Serial.println("Message received: " + text);
// Note: Fixed incorrect comparison 'text/temp"'
if (text == "/temp") {
// Note: Fixed missing '='
int adcValue = analogRead(ntcPin);
float temperature = calculateTemperature(adcValue);
String message = "Temperature: " + String(temperature, 2) + "°C";
bot.sendMessage(chat_id, message, "Markdown");
Serial.println(message);
} else {
bot.sendMessage(chat_id, "Invalid command. Use /temp to get the temperature.", "Markdown");
}
}
}