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 <TinyGPSPlus.h>
- #define WIFI_SSID "Hotel Net"
- #define WIFI_PASSWORD "0549084774"
- #define BOT_TOKEN "6630308049:AAHCPpjtIkrZspQyhFJNgFkNdtWAIyv3fns"
- #define CHAT_ID "-1002087168221"
- #define Rpi_ESP32_GPS_Pin 21
- #define ESP32_Rpi_Door_Pin 22
- const unsigned long BOT_MTBS = 100;
- unsigned long bot_lasttime;
- char google_maps_link[70];
- String google_maps_link_string = "";
- String Longitude = "39.241978";
- String Latitude = "21.495031";
- int Rpi_ESP32_GPS_Value = 0;
- TinyGPSPlus gps;
- WiFiClientSecure secured_client;
- UniversalTelegramBot bot(BOT_TOKEN, secured_client);
- void setup() {
- setup_input_output();
- connect_wifi_telegram();
- bot.sendMessage(CHAT_ID, "Bot Online");
- }
- void loop() {
- Rpi_ESP32_GPS_Value = digitalRead(Rpi_ESP32_GPS_Pin);
- if (Rpi_ESP32_GPS_Value) {
- bot.sendMessage(CHAT_ID, google_maps_link_string);
- }
- readMsgsFromTelegram();
- read_GPS_receiver();
- }//end of Loop
- void setup_input_output() {
- Serial.begin(115200);
- Serial2.begin(9600);
- pinMode(Rpi_ESP32_GPS_Pin, INPUT);
- pinMode(ESP32_Rpi_Door_Pin, OUTPUT);
- digitalWrite(ESP32_Rpi_Door_Pin, LOW);
- }
- void read_GPS_receiver() {
- while (Serial2.available() > 0) {
- if (gps.encode(Serial2.read())) {
- if (gps.location.isValid()) {
- Latitude = String(gps.location.lat(), 6);
- Longitude = String(gps.location.lng(), 6);
- Serial.print(Latitude);
- Serial.print(",");
- Serial.print(Longitude);
- Serial.println();
- google_maps_link_string = "http://maps.google.com/maps?&z=15&mrt=yp&t=k&q=" + Latitude + "+" + Longitude;
- google_maps_link_string.toCharArray(google_maps_link, 70);
- }
- else {
- //Serial.println(F("INVALID"));
- google_maps_link_string = "http://maps.google.com/maps?&z=15&mrt=yp&t=k&q=21.462718+39.200071";
- }
- }
- }
- if (millis() > 5000 && gps.charsProcessed() < 10) {
- Serial.println(F("No GPS detected: check wiring."));
- while (true);
- }
- }
- void connect_wifi_telegram() {
- 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);
- }
- void readMsgsFromTelegram() {
- 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 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 == "/gps") {
- bot.sendMessage(chat_id, google_maps_link_string);
- }
- if (text == "/password123") {
- digitalWrite(ESP32_Rpi_Door_Pin, HIGH);
- bot.sendMessage(chat_id, "Password Correct Door is now Open");
- delay(2000);
- digitalWrite(ESP32_Rpi_Door_Pin, LOW);
- }
- if (text == "/start")
- {
- String welcome = "Welcome to Universal Arduino Telegram Bot library, " + from_name + ".\n";
- welcome += "This is Chat Action Bot example.\n\n";
- bot.sendMessage(chat_id, welcome);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement