Monitor Temperature Humidity Using IoT Telegram Bot on ESP32

Monitor temperature of your room, house or office through Telegram app.

Introduction

Today we will develop a project to monitor temperature and humidity using IoT Telegram Bot on ESP32. Yes, you will using your Telegram app to monitor temperature of your room, house or office. Before proceed, please read following tutorial first.

Video

This video shows how to monitor temperature and humidity data using IoT Telegram Bot on ESP32.

Hardware Preparation

This is the list of items used in the video.

Sample Program

This is Arduino sample program to monitor temperature and humidity using IoT Telegram Bot on ESP32. It requires a few additional libraries from Arduino Library Manager:

/*
Project: Monitor Temperature Humidity Using IoT Telegram Bot on ESP32
Board: ESP32 Dev Module (TTGO T-Display ESP32)
Connections:
ESP32 | DHT22
5V – VCC
GND – GND
IO27 – DAT
External libraries:
– UniversalTelegramBot by Brian Lough V1.1.0 (Manager)
– ArduinoJson by Benoit Blanchon V5.13.5 (Manager)
*/
#include <TFT_eSPI.h>
#include <SPI.h>
#include <SimpleDHT.h>
#include <WiFi.h>
#include <WiFiClientSecure.h>
#include <UniversalTelegramBot.h>
// Initialize Wifi connection to the router
char ssid[] = "YourWiFiSSID"; // your network SSID (name)
char password[] = "YourWiFiPassword"; // your network key
// Initialize Telegram BOT
#define BOTtoken "YourTelegramBotToken" // your Bot Token (Get from Botfather)
WiFiClientSecure client;
UniversalTelegramBot bot(BOTtoken, client);
TFT_eSPI tft = TFT_eSPI();
#define DHT22_PIN 27
SimpleDHT22 dht22(DHT22_PIN);
#define ADC_EN 14
#define ADC_PIN 35
#define VREF 1100
#define BUTTON1 35
#define BUTTON2 0
#define BUZZER 13
#define FF17 &FreeSans9pt7b
#define FF21 &FreeSansBold9pt7b
#define ROW1 0,16
#define ROW2 0,38
#define ROW3 0,60
#define ROW4 0,82
#define ROW5 0,104
#define ROW6 0,126
#define NOTE_C4 262
#define NOTE_D4 294
#define NOTE_G4 392
int melody1[] = {NOTE_C4, NOTE_G4};
int melody1Dur[] = {12, 8};
int melody3[] = {NOTE_D4};
int melody3Dur[] = {8};
#define playReadyMelody() playTone(melody1, melody1Dur, 2)
#define playNoteD4() playTone(melody3, melody3Dur, 1)
long previousMillis = 0;
int interval = 2000;
void setup()
{
pinMode(BUTTON1, INPUT_PULLUP);
pinMode(BUTTON2, INPUT_PULLUP);
Serial.begin(115200);
// Attempt to connect to Wifi network:
Serial.print("Connecting Wifi: ");
Serial.println(ssid);
tft.init();
tft.setRotation(1);
tft.fillScreen(TFT_BLACK);
tft.setFreeFont(FF21);
tft.setTextColor(TFT_GREEN);
tft.setCursor(ROW1);
tft.print("Initializing…");
// Set WiFi to station mode and disconnect from an AP if it was Previously
// connected
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(500);
}
Serial.println("");
Serial.println("WiFi connected");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
tft.fillScreen(TFT_BLACK);
tft.setFreeFont(FF21);
tft.setTextColor(TFT_GREEN);
tft.setCursor(ROW1);
tft.print("Telegram Bot");
tft.setTextColor(TFT_WHITE);
tft.setCursor(ROW2);
tft.print("—————————————");
tft.setTextColor(TFT_BLUE);
tft.setCursor(ROW3);
tft.print("Temperature:");
tft.setCursor(ROW5);
tft.print("Humidity:");
tft.setTextColor(TFT_RED);
tft.setFreeFont(FF17);
playReadyMelody();
}
void loop()
{
if (millis() – previousMillis > interval) {
float temperature = 0;
float humidity = 0;
int err = SimpleDHTErrSuccess;
if ((err = dht22.read2(&temperature, &humidity, NULL)) != SimpleDHTErrSuccess) {
Serial.print("Read DHT22 failed, err=");
Serial.println(err);
delay(2000);
return;
}
tft.fillRect(0, 62, 240, 22, TFT_BLACK);
tft.setCursor(ROW4);
tft.print(temperature);
tft.print(" Celsius");
tft.fillRect(0, 106, 240, 22, TFT_BLACK);
tft.setCursor(ROW6);
tft.print(humidity);
tft.print(" %RH");
int newMessages = bot.getUpdates(bot.last_message_received + 1);
if (newMessages) {
playNoteD4();
String chatId = bot.messages[0].chat_id;
String message = bot.messages[0].text;
String statusText = "";
if (message == "STATUS") {
statusText = "Temperature: " + String(temperature) + " °C" +
"\nHumidity: " + String(humidity) + " %RH";
}
else if (message == "/start") {
statusText = "Welcome to Idris Bot. Reply STATUS to get temperature and humidity data. Thank you.";
}
else {
statusText = "Invalid command. Reply STATUS to get temperature and humidity data.";
}
bot.sendMessage(chatId, statusText, "");
}
previousMillis = millis();
}
}
const int FREQUENCY = 2000;
const int CHANNEL = 0;
const int RESOLUTION = 8;
void playTone(int *melody, int *melodyDur, int notesLength)
{
for (int i = 0; i < notesLength; i++) {
ledcAttachPin(BUZZER, CHANNEL);
int noteDuration = 1000 / melodyDur[i];
ledcWriteTone(CHANNEL, melody[i]);
int pauseBetweenNotes = noteDuration * 1.30;
delay(pauseBetweenNotes);
ledcDetachPin(BUZZER);
ledcWrite(CHANNEL, 0);
}
}

Thank You

References:

Thanks for reading this tutorial. If you have any technical inquiries, please post at Cytron Technical Forum.

Please be reminded, this tutorial is prepared for you to try and learn.
You are encouraged to improve the code for better application.

Leave a Comment

Your email address will not be published.

Share this Tutorial

Share on facebook
Share on whatsapp
Share on email
Share on print
Share on twitter
Share on pinterest
Share on facebook
Share on whatsapp
Share on email
Share on print
Share on twitter
Share on pinterest

Latest Tutorial

Raspberry Pi Pico VS Arduino MKRZero
Raspberry Pi Pico VS Teensy LC
A New Challenger in MCU Platform – Raspberry Pi Pico
Raspberry Pi Pico VS Arduino UNO
Raspberry Pi Pico VS micro:bit V2
Scroll to Top