Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <TinyGPSPlus.h>
- #include <WiFi.h>
- #include <WiFiClientSecure.h>
- #include <UniversalTelegramBot.h>
- #include <ArduinoJson.h>
- #include <NTPClient.h>
- #include <WiFiUdp.h>
- #define commandToESP32Camera 22
- #define triggerPin 23
- // Define NTP Client to get time
- WiFiUDP ntpUDP;
- NTPClient timeClient(ntpUDP);
- // Variables to save date and time
- String formattedDate;
- String dayStamp;
- String timeStamp;
- char google_maps_link[70];
- String google_maps_link_string = "";
- String Longitude = "1";
- String Latitude = "1";
- unsigned long dataMillis = 0;
- int count = 0;
- 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 "yaseen1421"
- #define WIFI_PASSWORD "yaseen1421"
- // Telegram BOT Token (Get from Botfather)
- #define BOT_TOKEN "6890911094:AAGIdkiGi-Uwdw1S0d4yZh4uqBMvZ3iac2U"
- // Use @myidbot (IDBot) 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 "-1002022958868"
- TinyGPSPlus gps;
- 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 == "/Test") {
- bot.sendMessage(CHAT_ID, "Bot Test [OK]");
- }
- 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);
- }
- }
- }
- void setup() {
- Serial.begin(9600);
- Serial2.begin(9600);
- pinMode(triggerPin, INPUT_PULLUP);
- pinMode(commandToESP32Camera, OUTPUT);
- digitalWrite(commandToESP32Camera, HIGH);
- // 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");
- delay(1000);
- Serial.println("delay finished");
- // Initialize a NTPClient to get time
- timeClient.begin();
- // Set offset time in seconds to adjust for your timezone, for example:
- // GMT +1 = 3600
- // GMT +8 = 28800
- // GMT -1 = -3600
- // GMT 0 = 0
- timeClient.setTimeOffset(10800);
- }
- void loop() {
- read_GPS_receiver();
- //Weapon Out => triggerState = 0
- //Weapon In => triggerState = 1
- int triggerState = digitalRead(triggerPin);
- Serial.println(triggerState);
- while (!timeClient.update()) {
- timeClient.forceUpdate();
- }
- // The formattedDate comes with the following format:
- // 2018-05-28T16:00:13Z
- // We need to extract date and time
- formattedDate = timeClient.getFormattedDate();
- Serial.println(formattedDate);
- // Extract date
- int splitT = formattedDate.indexOf("T");
- dayStamp = formattedDate.substring(0, splitT);
- Serial.print("DATE: ");
- Serial.println(dayStamp);
- // Extract time
- timeStamp = formattedDate.substring(splitT + 1, formattedDate.length() - 1);
- Serial.print("HOUR: ");
- Serial.println(timeStamp);
- /*
- 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();
- }
- */
- if (triggerState == 0) {
- //Send Command to ESP32-Cam to take Photo
- digitalWrite(commandToESP32Camera, LOW);
- //Send GPS Location
- String textToSend = "Weapon is OUT\n\n";
- textToSend += "DATE: ";
- textToSend += dayStamp;
- textToSend += "\nTime: ";
- textToSend += timeStamp;
- textToSend += "\n\n";
- textToSend += google_maps_link;
- bot.sendMessage(CHAT_ID, textToSend);
- delay(5000);
- }
- else {
- digitalWrite(commandToESP32Camera, HIGH);
- }
- }
- 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"));
- Latitude = "1.23";
- Longitude = "1.23";
- 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);
- }
- }
- }
- if (millis() > 5000 && gps.charsProcessed() < 10) {
- Serial.println(F("No GPS detected: check wiring."));
- while (true);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement