Request Sensor Readings Using ESP32 via Telegram.

Request Sensor Readings Using ESP32 via Telegram.

In this post you will see how to request ESP32 Sensor readings using telegram, i.e. we are going to build a system in which we request the sensor readinngs like current temperature and humidity from our phone using telegram messenger.

This is a very  simple but very effective project.we can read the temperature and humidity of any place from anywhere in the world through our phone.

we just need to send a message  from telegram bot.

Components Requried

1. ESP32

2.DHT22 Temperature and Humidity Sensor

3.1kohm resistor

Code


#ifdef ESP32 #include <WiFi.h> #else #include <ESP8266WiFi.h> #endif #include <WiFiClientSecure.h> #include <UniversalTelegramBot.h> // Universal Telegram Bot Library written by Brian Lough: https://github.com/witnessmenow/Universal-Arduino-Telegram-Bot #include <ArduinoJson.h> #include "DHT.h" #define DHTPIN 15 // Digital pin connected to the DHT sensor // Uncomment whatever type you're using! //#define DHTTYPE DHT11 // DHT 11 #define DHTTYPE DHT22 // DHT 22 (AM2302), AM2321 //#define DHTTYPE DHT21 // DHT 21 (AM2301) DHT dht(DHTPIN, DHTTYPE); // Replace with your network credentials const char* ssid = "xxxxxxxxxxx"; const char* password = "xxxxxxxxx"; // Use @myidbot to find out the chat ID of an individual or a group // Also note that you need to click "start" on a bot before it can // message you #define CHAT_ID "xxxxxxx" // Initialize Telegram BOT #define BOTtoken "xxxxxxxxxxxxxxxxxxx" // your Bot Token (Get from Botfather) WiFiClientSecure client; UniversalTelegramBot bot(BOTtoken, client); //Checks for new messages every 1 second. int botRequestDelay = 1000; unsigned long lastTimeBotRan; // Serial.println(F("DHTxx test!")); // Get BME280 sensor readings and return them as a String variable String getReadings(){ float temperature, humidity; temperature = dht.readTemperature(); humidity = dht.readHumidity(); String message = "Temperature: " + String(temperature) + " ºC \n"; message += "Humidity: " + String (humidity) + " % \n"; return message; } //Handle what happens when you receive new messages void handleNewMessages(int numNewMessages) { Serial.println("handleNewMessages"); Serial.println(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 != CHAT_ID){ bot.sendMessage(chat_id, "Unauthorized user", ""); continue; } // Print the received message String text = bot.messages[i].text; Serial.println(text); String from_name = bot.messages[i].from_name; text.toUpperCase(); delay(10); if (text == "START") { String welcome = "Welcome, " + from_name + ".\n"; welcome += "Use the below command to get current readings.\n\n"; welcome += "Readings \n"; bot.sendMessage(chat_id, welcome, ""); } if (text == "READINGS") { String readings = getReadings(); bot.sendMessage(chat_id, readings, ""); } } } void setup() { Serial.begin(115200); dht.begin(); // Connect to Wi-Fi WiFi.mode(WIFI_STA); WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(1000); Serial.println("Connecting to WiFi.."); } // Print ESP32 Local IP Address Serial.println(WiFi.localIP()); } void loop() { if (millis() > lastTimeBotRan + botRequestDelay) { int numNewMessages = bot.getUpdates(bot.last_message_received + 1); while(numNewMessages) { Serial.println("got response"); handleNewMessages(numNewMessages); numNewMessages = bot.getUpdates(bot.last_message_received + 1); } lastTimeBotRan = millis(); } }

Video Tutorial

Leave a Reply

Your email address will not be published. Required fields are marked *

Back To Top