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 Pins -----------
- #define Door_PIN 35 // ✓
- #define GAS_PIN 34 // ✓
- #define Flame_PIN 39 // ✓
- #define Relay_PIN 23 // ✓
- #define Camera_PIN 18 // ✓ changed from 16 to 18
- int GasActivationValue = 3000;
- int FlameActivationValue = 4000;
- int MotionValue;
- int GAS_Value;
- int Flame_Value;
- int door_opened_flag = 0;
- int take_photo_flag = 0;
- int counter = 0;
- ///////////////////////////////////// Door Sensor Interval
- unsigned long prevMillis = 0;
- const long interval = 15000;// for fan 15 Second
- ///////////////////////////////////// Door Sensor Interval
- ///////////////////////////////////// Taking Photo Interval
- unsigned long prevMillis2 = 0;
- const long interval2 = 5000;
- ///////////////////////////////////// Taking Photo Interval
- ///////////////////////////////////// For GPS and GSM
- String s = "www.google.com/maps/dir/";
- unsigned long intervalGSM = 10000;
- static const uint32_t GPSBaud = 9600;
- unsigned long previousMillis = 0;
- int data_counter;
- const size_t BUFSIZE = 300;
- char f_buffer[BUFSIZE];
- float *f_buf = (float*)f_buffer;
- TinyGPSPlus gps;// The TinyGPSPlus object
- ///////////////////////////////////// For GPS and GSM
- volatile bool buttonPressedFlag = false;
- #define WIFI_SSID "Thamer"
- #define WIFI_PASSWORD "123451234"
- #define BOT_TOKEN "6836575708:AAF9ZuMQoycwyrg-ZrJe3AevoUM5gmuSIZs"
- #define CHAT_ID "-4059146311"
- WiFiClientSecure secured_client;
- UniversalTelegramBot bot(BOT_TOKEN, secured_client);
- void setup() {
- Serial.begin(115200);
- Serial.println("-.-");
- pinMode(Door_PIN, INPUT);
- pinMode(GAS_PIN, INPUT);
- pinMode(Flame_PIN, INPUT);
- pinMode(Relay_PIN, OUTPUT);
- pinMode(Camera_PIN, OUTPUT);
- digitalWrite(Relay_PIN, 0);
- digitalWrite(Camera_PIN, 0);
- //attachInterrupt(Door_PIN, interuptButtonPressed, FALLING);
- 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(5000);
- setup_GPS_GSM_Modem();
- Serial.println("delay finished");
- }
- int checkGAS() {
- int value1 = analogRead(GAS_PIN);
- //Serial.print("GAS value: ");
- //Serial.println(value1);
- return value1;
- }
- int checkFlame() {
- int value2 = analogRead(Flame_PIN);
- //Serial.print("Flame value: ");
- //Serial.println(value2);
- return value2;
- }
- /*
- void interuptButtonPressed() {
- Serial.println("Door was opened!!");
- int button = digitalRead(Door_PIN);
- if (button == HIGH)
- {
- buttonPressedFlag = true;
- }
- return;
- }
- void handleButtonPressed() {
- bot.sendMessage(CHAT_ID, "Door was opened!!");
- //turn ON FAN
- digitalWrite(Relay_PIN, 1);
- prevMillis = millis();
- door_opened_flag = 1;
- buttonPressedFlag = false;
- }
- */
- void loop() {
- MotionValue = analogRead(Door_PIN);
- Serial.print("MotionValue: ");
- Serial.println(MotionValue);
- if (MotionValue > 10) {
- bot.sendMessage(CHAT_ID, "Motion Detected!!");
- //turn ON FAN
- digitalWrite(Relay_PIN, 1);
- prevMillis = millis();
- door_opened_flag = 1;
- }
- if (door_opened_flag) {
- if (millis() - prevMillis >= interval) {
- Serial.println("15 seconds have passed");
- bot.sendMessage(CHAT_ID, "Fan is turning OFF Now");
- //turn OFF FAN
- digitalWrite(Relay_PIN, 0);
- door_opened_flag = 0;
- }
- }
- if (take_photo_flag) {
- if (millis() - prevMillis2 >= interval2) {
- prevMillis2 = millis();
- counter = counter + 1;
- Serial.println("5 seconds have passed");
- //bot.sendMessage(CHAT_ID, "Take Photo");
- //turn ON Camera
- digitalWrite(Camera_PIN, 1);
- delay(1000);
- digitalWrite(Camera_PIN, 0);
- if (counter >= 13) {
- take_photo_flag = 0;
- counter = 0;
- }
- }
- }
- /*
- if ( buttonPressedFlag ) {
- handleButtonPressed();
- }
- */
- delay(50);
- GAS_Value = checkGAS();
- if (GAS_Value > GasActivationValue) {
- sendSMS("GAS detected " + String(GAS_Value));
- delay(1000);
- bot.sendMessage(CHAT_ID, "GAS detected " + String(GAS_Value));
- prevMillis2 = millis();
- take_photo_flag = 1;
- digitalWrite(Camera_PIN, 1);
- delay(1000);
- digitalWrite(Camera_PIN, 0);
- }
- delay(50);
- Flame_Value = checkFlame();
- if (Flame_Value < FlameActivationValue) {
- sendSMS("Flame detected " + String(Flame_Value));
- delay(1000);
- bot.sendMessage(CHAT_ID, "Flame detected " + String(Flame_Value));
- prevMillis2 = millis();
- take_photo_flag = 1;
- digitalWrite(Camera_PIN, 1);
- delay(1000);
- digitalWrite(Camera_PIN, 0);
- }
- delay(50);
- }//end of Loop
- void setup_GPS_GSM_Modem() {
- Serial2.begin(GPSBaud);
- Serial.println("Starting...");
- Serial2.println("\r");
- Serial2.println("AT\r");
- delay(10);
- Serial2.println("\r");
- Serial2.println("AT+GPS=1\r");
- delay(100);
- Serial2.println("AT+CREG=2\r");
- delay(6000);
- //Serial2.print("AT+CREG?\r");
- Serial2.println("AT+CGATT=1\r");
- delay(6000);
- Serial2.println("AT+CGDCONT=1,\"IP\",\"WWW\"\r");
- delay(6000);
- // Serial2.println("AT+LOCATION=1\r");
- Serial2.println("AT+CGACT=1,1\r");
- delay(6000);
- //Initialize ends
- //Initialize GPS
- Serial2.println("\r");
- Serial2.println("AT+GPS=1\r");
- delay(1000);
- //Serial2.println("AT+GPSMD=1\r"); // Change to only GPS mode from GPS+BDS, set to 2 to revert to default.
- Serial2.println("AT+GPSRD=10\r");
- delay(100);
- // set SMS mode to text mode
- Serial2.println("AT+CMGF=1\r");
- delay(1000);
- //Serial2.println("AT+LOCATION=2\r");
- Serial.println("GSM GPS Setup Executed");
- }
- void sendSMS(String s) {
- Serial.println("Sending SMS Message");
- Serial2.println("AT+CMGF=1\r");
- delay(1000);
- Serial2.println("AT+CNMI=2,2,0,0,0\r");
- delay(1000);
- Serial2.print("AT+CMGS=\"+966580224645\"\r");//Replace this with your mobile number
- delay(1000);
- Serial2.print(s);
- Serial2.write(0x1A);
- delay(1000);
- }
Add Comment
Please, Sign In to add comment