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>
- // Wifi network station credentials
- #define WIFI_SSID "iPhone"
- #define WIFI_PASSWORD "12333333"
- // Telegram BOT Token (Get from Botfather)
- #define BOT_TOKEN "5768226421:AAGsAirC4AgKH7MZCFevZ1cpPIaIyMHviYg"
- String chat_id = "824758478";
- const unsigned long BOT_MTBS = 1000; // mean time between scan messages
- String stringMessage = "";
- String lonGPS = "";
- String latGPS = "";
- char Buf[70];
- WiFiClientSecure secured_client;
- UniversalTelegramBot bot(BOT_TOKEN, secured_client);
- unsigned long bot_lasttime; // last time messages' scan has been done
- bool Start = false;
- #define Sensor_input 36
- int sensor_Aout = 0;
- TinyGPSPlus gps;
- void setup() {
- Serial.begin(9600);
- Serial2.begin(9600);
- pinMode(Sensor_input, INPUT);
- delay(3000);
- // 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, "Attention");
- }
- void loop() {
- sensor_Aout = analogRead(Sensor_input);
- //Serial.print("Gas Sensor: ");
- //Serial.println(sensor_Aout);
- while (Serial2.available() > 0)
- if (gps.encode(Serial2.read()))
- displayInfo();
- if (millis() > 5000 && gps.charsProcessed() < 10) {
- Serial.println(F("No GPS detected: check wiring."));
- while (true);
- }
- if (sensor_Aout > 1800) {
- Serial.println("Gas");
- //http://maps.google.com/maps?&z=15&mrt=yp&t=k&q=20.123456+20.123456
- //Send Alert Telegram Message
- //String message = "Alert Gas Detected !! \n Sensor Value: " + String(sensor_Aout) + ".\n";
- //message += "http://maps.google.com/maps?&z=15&mrt=yp&t=k&q=" + String(lat_long) + "+" + String(lng_long) + ".\n";
- bot.sendMessage(chat_id, Buf);
- delay(10000);
- }
- else {
- //Serial.println("No Gas");
- }
- }//end of LOOP
- void displayInfo() {
- Serial.print(F("Location: "));
- if (gps.location.isValid()) {
- lonGPS = String(gps.location.lng(), 6);
- latGPS = String(gps.location.lat(), 6);
- Serial.print("Lat: ");
- Serial.print(latGPS);
- Serial.print(F(","));
- Serial.print("Lng: ");
- Serial.print(lonGPS);
- Serial.println();
- stringMessage = "http://www.google.com/maps/place/" + latGPS + "," + lonGPS;
- stringMessage.toCharArray(Buf, 70);
- }
- else {
- Serial.print(F("INVALID"));
- stringMessage = "http://www.google.com/maps/place/21.861307,39.066032";
- stringMessage.toCharArray(Buf, 70);
- }
- }
Add Comment
Please, Sign In to add comment