//https://www.electronicwings.com/esp32/pir-sensor-interfacing-with-esp32
//https://esp32io.com/tutorials/esp32-servo-motor
//https://wokwi.com/projects/323706614646309460
//https://randomnerdtutorials.com/telegram-control-esp32-esp8266-nodemcu-outputs/
//https://randomnerdtutorials.com/telegram-esp32-motion-detection-arduino/
#include <ESP32Servo.h>
#include <WiFi.h>
#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>
const int servoPin = 26;
const int PIR_SENSOR_OUTPUT_PIN = 27; /* PIR sensor O/P pin */
Servo servo;
// Replace with your network credentials
const char* ssid = "Wokwi-GUEST";
const char* password = "";
// Initialize Telegram BOT
#define BOTtoken "6701738664:AAEArb4EUlI9D6litNG4uVjX1E12SiEjaVg" // your Bot Token (Get from Botfather)
// 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 "1295163847"
WiFiClientSecure client;
UniversalTelegramBot bot(BOTtoken, client);
// Checks for new messages every 1 second.
int botRequestDelay = 1000;
unsigned long lastTimeBotRan;
int state = 0;
// 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;
if (text == "/start") {
String welcome = "Welcome, " + from_name + ".\n";
welcome += "Use the following commands to control your outputs.\n\n";
welcome += "/open to turn servo 0 degree \n";
welcome += "/lock to turn servo 90 degree \n";
welcome += "/state to request current servo state \n";
bot.sendMessage(chat_id, welcome, "");
}
if (text == "/open") {
bot.sendMessage(chat_id, "Servo state set to 0", "");
state = 0;
servo.write(0);
delay(100);
}
if (text == "/lock") {
bot.sendMessage(chat_id, "Servo state set to 90", "");
state = 1;
servo.write(90);
delay(100);
}
if (text == "/state") {
if (state == 1){
bot.sendMessage(chat_id, "Door is Locked", "");
}
else{
bot.sendMessage(chat_id, "Door is Opened", "");
}
return;
}
}
}
void setup() {
pinMode(PIR_SENSOR_OUTPUT_PIN, INPUT);
servo.attach(servoPin, 500, 2400);
Serial.begin(115200); /* Define baud rate for serial communication */
// Connect to Wi-Fi
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
client.setCACert(TELEGRAM_CERTIFICATE_ROOT); // Add root certificate for api.telegram.org
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();
}
// int sensor_output;
// sensor_output = digitalRead(PIR_SENSOR_OUTPUT_PIN);
// if( sensor_output == LOW )
// {
// Serial.print("No object in sight\n\n");
// delay(1000);
// servo.write(0);
// delay(100);
// }
// else
// {
// Serial.print("Object detected\n\n");
// delay(1000);
// servo.write(90);
// delay(100);
// }
}