Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <WiFi.h>
- #include <WiFiClientSecure.h>
- #include <UniversalTelegramBot.h>
- #include <ArduinoJson.h>
- #include <ESP32Servo.h>
- // -------- Define Pins -----------
- #define SERVO_PIN1 26
- #define SERVO_PIN2 27
- const int trigPin = 22;
- const int echoPin = 23;
- long duration;
- int distance;
- #define LED 21
- #define buzzer 25
- Servo servoMotor1;
- Servo servoMotor2;
- const unsigned long BOT_MTBS = 1000; // mean time between scan messages
- unsigned long bot_lasttime; // last time messages' scan has been done
- // Wifi network station credentials
- #define WIFI_SSID "Zain-E6878-606A"
- #define WIFI_PASSWORD "DEMDEFJ09QT"
- #define BOT_TOKEN "6847705271:AAEbKxAIQ7TD2MJBw1YbGFrARi0OrHR8ENg"
- #define CHAT_ID "-1002124008517"
- WiFiClientSecure secured_client;
- UniversalTelegramBot bot(BOT_TOKEN, secured_client);
- //////////////////////////////////////////////////////////
- void handleNewMessages(int numNewMessages)
- {
- Serial.println("handleNewMessages");
- Serial.println(String(numNewMessages));
- for (int i = 0; i < numNewMessages; i++)
- {
- String chat_id = bot.messages[i].chat_id;
- String text = bot.messages[i].text;
- String from_name = bot.messages[i].from_name;
- if (from_name == "")
- from_name = "Guest";
- if (text == "/send_test_action")
- {
- bot.sendChatAction(chat_id, "typing");
- delay(4000);
- bot.sendMessage(chat_id, "Did you see the action message?");
- }
- if (text == "/open_door1") {
- servoMotor1.write(120);
- delay(15);
- bot.sendMessage(CHAT_ID, "door1 is open ");
- }
- if (text == "/close_door1") {
- servoMotor1.write(0);
- delay(15);
- bot.sendMessage(CHAT_ID, "door1 is open ");
- }
- if (text == "/close_door2") {
- servoMotor2.write(120);
- delay(15);
- bot.sendMessage(CHAT_ID, "door2 is close ");
- }
- if (text == "/open_door2") {
- servoMotor2.write(0);
- delay(15);
- bot.sendMessage(CHAT_ID, "door2 is open ");
- }
- if (text.equals("/options")) {
- String keyboardJson = "[[\"/open_door1\"],[\"/close_door1\"],[\"/open_door2\"],[\"/close_door2\"]]";
- bot.sendMessageWithReplyKeyboard(chat_id, "Choose from one of the following options", "", keyboardJson, true);
- }
- if (text == "/start")
- {
- String welcome = "Welcome to Universal Arduino Telegram Bot library, " + from_name + ".\n";
- welcome += "This is Chat Action Bot example.\n\n";
- welcome += "/options : returns a custom reply keyboard\n";
- bot.sendMessage(chat_id, welcome);
- }
- }
- }
- //////////////////////////////////////////////////////////
- void setup() {
- Serial.begin(115200);
- Serial.println();
- pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
- pinMode(echoPin, INPUT); // Sets the echoPin as an Input
- servoMotor1.attach(SERVO_PIN1);
- servoMotor2.attach(SERVO_PIN2);
- pinMode(LED, OUTPUT);
- pinMode(buzzer, OUTPUT);
- // 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());
- Serial.print("Retrieving time: ");
- configTime(0, 0, "pool.ntp.org"); // get UTC time via NTP
- time_t now = time(nullptr);
- while (now < 24 * 3600)
- {
- Serial.print(".");
- delay(100);
- now = time(nullptr);
- }
- Serial.println(now);
- bot.sendMessage(CHAT_ID, "Bot started up", "");
- Serial.println("delay finished");
- }
- void loop() {
- digitalWrite(trigPin, LOW);
- delayMicroseconds(2);
- digitalWrite(trigPin, HIGH);
- delayMicroseconds(10);
- digitalWrite(trigPin, LOW);
- duration = pulseIn(echoPin, HIGH);
- distance = duration * 0.034 / 2;
- Serial.println(distance);
- delay(10);
- if (distance < 50 && distance > 0) {
- digitalWrite(LED, HIGH);
- digitalWrite(buzzer, HIGH );
- bot.sendMessage(CHAT_ID, "human detector " + String(distance));
- }
- else {
- digitalWrite(LED, 0 );
- digitalWrite(buzzer, 0);
- }
- ///////////////////////////////////////////
- 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();
- }
- ///////////////////////////////////////////
- }//end of Loop
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement