#include #include #include #include "DHT.h" #define DHTPIN D0 // Uncomment whatever type you're using! //#define DHTTYPE DHT11 // DHT 11 #define DHTTYPE DHT22 // DHT 22 (AM2302), AM2321 //#define DHTTYPE DHT21 // DHT 21 (AM2301) // Wifi network station credentials #define WIFI_SSID "Color" #define WIFI_PASSWORD "YourPass" // Telegram BOT Token (Get from Botfather) #define BOT_TOKEN "YourToken" #define CHAT_ID "YourID" WiFiClientSecure secured_client; UniversalTelegramBot bot(BOT_TOKEN, secured_client); DHT dht(DHTPIN, DHTTYPE); const unsigned long BOT_MTBS = 1000; // mean time between scan messages unsigned long bot_lasttime; // last time messages' scan has been done float temperatureC; float temperatureF; float humidity; int btn; int led = D10; int lastButtonState = 0; void setup() { pinMode(led, OUTPUT); Serial.begin(9600); Serial.println(F("Ousiafb's XIO_ESP32")); dht.begin(); // attempt to connect to Wifi network: Serial.print("Connecting to Wifi SSID "); Serial.print(WIFI_SSID); WiFi.begin(WIFI_SSID, WIFI_PASSWORD); secured_client.setCACert(TELEGRAM_CERTIFICATE_ROOT); // Add root certificate for api.telegram.org while (WiFi.status() != WL_CONNECTED) { Serial.print("."); delay(500); } Serial.print("\nWiFi connected. IP address: "); Serial.println(WiFi.localIP()); } void loop() { btn = digitalRead(D1); buttonSateChange(); humidity = dht.readHumidity(); // Read temperature as Celsius (the default) temperatureC = dht.readTemperature(); // Read temperature as Fahrenheit (isFahrenheit = true) temperatureF = dht.readTemperature(true); if (millis() - bot_lasttime > BOT_MTBS) { 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); } bot_lasttime = millis(); } } void buttonSateChange() { // compare the buttonState to its previous state if (btn != lastButtonState) { // if the state has changed, increment the counter if (btn == HIGH) { // if the current state is HIGH then the button went from off to on: String msg = "Someone just opened the door"; bot.sendMessage(CHAT_ID, msg, ""); Serial.println("opened"); } else if (btn == LOW) { // if the current state is LOW then the button went from on to off: String msg = "Someone just closed the door"; bot.sendMessage(CHAT_ID, msg, ""); Serial.println("closed"); } // Delay a little bit to avoid bouncing delay(50); } // save the current state as the last state, for next time through the loop lastButtonState = btn; } void handleNewMessages(int numNewMessages) { Serial.print("handleNewMessages "); Serial.println(numNewMessages); for (int i = 0; i < numNewMessages; i++) { String chat_id = String(bot.messages[i].chat_id); if (chat_id != CHAT_ID ) { bot.sendMessage(chat_id, "Unauthorized user", ""); } else { String text = bot.messages[i].text; String from_name = bot.messages[i].from_name; if (from_name == "") from_name = "Guest"; if (text == "/tempC") { String msg = "Temperature is "; msg += msg.concat(temperatureC); msg += "C"; bot.sendMessage(chat_id, msg, ""); } if (text == "/tempF") { String msg = "Temperature is "; msg += msg.concat(temperatureF); msg += "F"; bot.sendMessage(chat_id, msg, ""); } if (text == "/humidity") { String msg = "Humidity is "; msg += msg.concat(humidity); msg += "%"; bot.sendMessage(chat_id, msg, ""); } if (text == "/ButtonState") { String Door = ""; String msg = "DoorState: "; if (btn == 1) { Door = "Ay Caramba! You didn't close the door"; } else if (btn == 0) { Door = "From what I can sense the door is closed"; } msg += msg.concat(Door); bot.sendMessage(chat_id, msg, ""); } if (text == "/ON") { String msg = "Turning led ON :"; bot.sendMessage(chat_id, msg, ""); digitalWrite(led, HIGH); } if (text == "/OFF") { String msg = "Turning led OFF :"; bot.sendMessage(chat_id, msg, ""); digitalWrite(led, LOW); } if (text == "/start") { String welcome = "Ousiafb XIO_ESP32.\n"; welcome += "/tempC : Temperature in celcius \n"; welcome += "/tempF : Temperature in faranthit\n"; welcome += "/humidity : Humidity\n"; welcome += "/ButtonState : 0 or 1\n"; welcome += "/ON or /OFF to turn led OFF\n"; bot.sendMessage(chat_id, welcome, "Markdown"); } } } }