Using Telegram bot and ESP32 to get temperature and humidity readings from sensor (DHT11) remotely

Hi all, the items you need for this project:

  • ESP32 microcontroller
  • DHT11 sensor ( DHT22 also can, but need to change in the Arduino code, do take note of the sensor model)
  • Some Jumper wires
  • Telegram account ( for setting up of bot account in Telegram, please refer to my previous post : https://coding-engineer.com/2024/04/14/using-telegram-bot-to-control-esp32-microcontroller-leds/). You need to use your Bot Token ( bot account) and Chat ID ( your personal Telegram account) in the code.
  • Mobile phone with internet connection

Schematic diagram:

Connections from ESP32 to DHT11 are : Gnd to Gnd, Signal/data to IO pin 25, VCC to 5V. Different sensor model or manufacturer might have different pin order. Please take note.

Video for the demo:

Arduino IDE code:

Points to take note:

  • Need to install “DHT sensor library by Adafruit”. There is a dependency library call “Adafruit Unified Sensor”, which will prompt you to install also. Do take note.
  • I am using DHT11 with 3 pins : Gnd, Data, VCC. If you are using DHT22, then change accordingly in the code.
  • You need to use your Bot Token ( bot account) and Chat ID ( your personal Telegram account) in the code. For the steps to get this 2 credentials. refer to my previous post ( link listed above).
  • Temperature is in Degree Celsius and Humidity in %. To change temperature to Degree Fahrenheit, input true as argument for dht.readTemperature() function.
#include <WiFi.h>
#include <WiFiClientSecure.h>
#include <UniversalTelegramBot.h>
#include <ArduinoJson.h>

// Library is installed from "DHT sensor library by Adafruit". 
// There is a dependency library call "Adafruit Unified Sensor", which will prompt you to install also. Do take note.
#include "DHT.h"

#define DHTPIN 25
#define DHTTYPE DHT11    // Do take note of your sensor model, if it's DHT22 , then change accordingly

// setting up the dht object
DHT dht(DHTPIN, DHTTYPE);

// Enter your Wi-Fi network SSID and password accordingly:
const char* ssid = "xxxxxxx";
const char* password = "xxxxxxxx";

// Enter your Telegram bot token and chat ID accordingly:
#define botToken "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
//your human telegram account ID
#define chatID "xxxxxxxxxxxx"   
 
WiFiClientSecure client;
UniversalTelegramBot bot(botToken, client);

// Checks for new messages every 1 second:
int loop_delay = 4000;
unsigned long lastRun = 0;


// Function to get sensor data and send to bot via telegram chat
// Function to get sensor data and send to bot via telegram chat
void GetSensorReadings(int numNewMessages)
{

  String message; // Used for assembling messages to send
  Serial.println("Handling New Message");
  Serial.println("Message : " + String(numNewMessages));

  for (int i = 0; i < numNewMessages; i++)
    {
      // Chat ID of the requester:
      String chat_id = String(bot.messages[i].chat_id);
      if (chat_id != chatID)
      {
        bot.sendMessage(chat_id, "Unauthorized user", " ");
        continue;
      }

        // Print the received message:
        String user_text = bot.messages[i].text;
        Serial.println(user_text);

        String your_name = bot.messages[i].from_name;
        Serial.println("test1");

    if (user_text == "/start")
        {
            message = "Hello, " + your_name + ".\n";
            message += "Choose from the following commands:\n\n";
            message += "Send /temp for temperature\n";
            message += "Send /hum for humidity\n";
            bot.sendMessage(chat_id, message);
        }

    if (user_text == "/temp"){

            // get BMP180 temperature
            //int chk = DHT11.read(dht11_Pin);
            message = "Temperature (deg C): ";
            message += String(dht.readTemperature());
            bot.sendMessage(chat_id, message);
            Serial.println("test2");
            Serial.println(message);
        }

    if (user_text == "/hum"){

            // Get humidity:
            //int chk = DHT11.read(dht11_Pin);
            message = "Humidity (%): ";
            //message += String((float)DHT11.humidity, 2);
            message += String(dht.readHumidity());
            bot.sendMessage(chat_id, message);
            Serial.println(message);
      }

    }
}

// setup and loop functions starts here
// setup and loop functions starts here

void setup() {

  Serial.begin(9600);

  dht.begin();

 

    // Connect to Wi-Fi:
    WiFi.mode(WIFI_STA);
    WiFi.begin(ssid, password);

    // Add  root certificate for api.telegram.org:
    client.setCACert(TELEGRAM_CERTIFICATE_ROOT);
    
    while (WiFi.status() != WL_CONNECTED)
    {
        delay(1000);
        Serial.println("Connecting to Wi-Fi…");
    }
    Serial.println(WiFi.localIP());

}

void loop() {
 
  if (millis() > lastRun + loop_delay)

{       // A counter for the number of message the bot received from you
        int numNewMessages = bot.getUpdates(bot.last_message_received + 1);
        while (numNewMessages)
        {
            Serial.println("Received message");
            GetSensorReadings(numNewMessages);
            numNewMessages = bot.getUpdates(bot.last_message_received + 1);
        }
        lastRun = millis();
}


}