safwan092

Untitled

Nov 13th, 2023
13
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.64 KB | None | 0 0
  1. #include <WiFi.h>
  2. #include <WiFiClientSecure.h>
  3. #include <UniversalTelegramBot.h>
  4. #include <ArduinoJson.h>
  5. #include <TinyGPSPlus.h>
  6.  
  7. // -------- Define Pins -----------
  8. #define Door_PIN 35 // ✓
  9. #define GAS_PIN 34 // ✓
  10. #define Flame_PIN 39 // ✓
  11. #define Relay_PIN 23 // ✓
  12. #define Camera_PIN 18 // ✓ changed from 16 to 18
  13.  
  14. int GasActivationValue = 3000;
  15. int FlameActivationValue = 4000;
  16.  
  17. int MotionValue;
  18. int GAS_Value;
  19. int Flame_Value;
  20. int door_opened_flag = 0;
  21. int take_photo_flag = 0;
  22. int counter = 0;
  23.  
  24. ///////////////////////////////////// Door Sensor Interval
  25. unsigned long prevMillis = 0;
  26. const long interval = 15000;// for fan 15 Second
  27. ///////////////////////////////////// Door Sensor Interval
  28.  
  29. ///////////////////////////////////// Taking Photo Interval
  30. unsigned long prevMillis2 = 0;
  31. const long interval2 = 5000;
  32. ///////////////////////////////////// Taking Photo Interval
  33.  
  34. ///////////////////////////////////// For GPS and GSM
  35. String s = "www.google.com/maps/dir/";
  36. unsigned long intervalGSM = 10000;
  37. static const uint32_t GPSBaud = 9600;
  38. unsigned long previousMillis = 0;
  39. int data_counter;
  40. const size_t BUFSIZE = 300;
  41. char f_buffer[BUFSIZE];
  42. float *f_buf = (float*)f_buffer;
  43. TinyGPSPlus gps;// The TinyGPSPlus object
  44. ///////////////////////////////////// For GPS and GSM
  45.  
  46. volatile bool buttonPressedFlag = false;
  47.  
  48. #define WIFI_SSID "Thamer"
  49. #define WIFI_PASSWORD "123451234"
  50. #define BOT_TOKEN "6836575708:AAF9ZuMQoycwyrg-ZrJe3AevoUM5gmuSIZs"
  51. #define CHAT_ID "-4059146311"
  52.  
  53. WiFiClientSecure secured_client;
  54. UniversalTelegramBot bot(BOT_TOKEN, secured_client);
  55.  
  56.  
  57. void setup() {
  58. Serial.begin(115200);
  59.  
  60. Serial.println("-.-");
  61.  
  62. pinMode(Door_PIN, INPUT);
  63. pinMode(GAS_PIN, INPUT);
  64. pinMode(Flame_PIN, INPUT);
  65. pinMode(Relay_PIN, OUTPUT);
  66. pinMode(Camera_PIN, OUTPUT);
  67.  
  68. digitalWrite(Relay_PIN, 0);
  69. digitalWrite(Camera_PIN, 0);
  70.  
  71. //attachInterrupt(Door_PIN, interuptButtonPressed, FALLING);
  72.  
  73. Serial.print("Connecting to Wifi SSID ");
  74. Serial.print(WIFI_SSID);
  75. WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
  76. secured_client.setCACert(TELEGRAM_CERTIFICATE_ROOT); // Add root certificate for api.telegram.org
  77. while (WiFi.status() != WL_CONNECTED)
  78. {
  79. Serial.print(".");
  80. delay(500);
  81. }
  82. Serial.print("\nWiFi connected. IP address: ");
  83. Serial.println(WiFi.localIP());
  84.  
  85. Serial.print("Retrieving time: ");
  86. configTime(0, 0, "pool.ntp.org"); // get UTC time via NTP
  87. time_t now = time(nullptr);
  88. while (now < 24 * 3600)
  89. {
  90. Serial.print(".");
  91. delay(100);
  92. now = time(nullptr);
  93. }
  94. Serial.println(now);
  95.  
  96. bot.sendMessage(CHAT_ID, "Bot started up", "");
  97. delay(5000);
  98. setup_GPS_GSM_Modem();
  99. Serial.println("delay finished");
  100. }
  101.  
  102. int checkGAS() {
  103. int value1 = analogRead(GAS_PIN);
  104. //Serial.print("GAS value: ");
  105. //Serial.println(value1);
  106. return value1;
  107. }
  108. int checkFlame() {
  109. int value2 = analogRead(Flame_PIN);
  110. //Serial.print("Flame value: ");
  111. //Serial.println(value2);
  112. return value2;
  113. }
  114. /*
  115. void interuptButtonPressed() {
  116. Serial.println("Door was opened!!");
  117. int button = digitalRead(Door_PIN);
  118. if (button == HIGH)
  119. {
  120. buttonPressedFlag = true;
  121. }
  122. return;
  123. }
  124. void handleButtonPressed() {
  125. bot.sendMessage(CHAT_ID, "Door was opened!!");
  126. //turn ON FAN
  127. digitalWrite(Relay_PIN, 1);
  128. prevMillis = millis();
  129. door_opened_flag = 1;
  130. buttonPressedFlag = false;
  131. }
  132. */
  133. void loop() {
  134.  
  135. MotionValue = analogRead(Door_PIN);
  136. Serial.print("MotionValue: ");
  137. Serial.println(MotionValue);
  138.  
  139. if (MotionValue > 10) {
  140. bot.sendMessage(CHAT_ID, "Motion Detected!!");
  141. //turn ON FAN
  142. digitalWrite(Relay_PIN, 1);
  143. prevMillis = millis();
  144. door_opened_flag = 1;
  145. }
  146. if (door_opened_flag) {
  147. if (millis() - prevMillis >= interval) {
  148. Serial.println("15 seconds have passed");
  149. bot.sendMessage(CHAT_ID, "Fan is turning OFF Now");
  150. //turn OFF FAN
  151. digitalWrite(Relay_PIN, 0);
  152. door_opened_flag = 0;
  153. }
  154. }
  155.  
  156.  
  157. if (take_photo_flag) {
  158. if (millis() - prevMillis2 >= interval2) {
  159. prevMillis2 = millis();
  160. counter = counter + 1;
  161. Serial.println("5 seconds have passed");
  162. //bot.sendMessage(CHAT_ID, "Take Photo");
  163. //turn ON Camera
  164. digitalWrite(Camera_PIN, 1);
  165. delay(1000);
  166. digitalWrite(Camera_PIN, 0);
  167. if (counter >= 13) {
  168. take_photo_flag = 0;
  169. counter = 0;
  170. }
  171. }
  172. }
  173. /*
  174. if ( buttonPressedFlag ) {
  175. handleButtonPressed();
  176. }
  177. */
  178. delay(50);
  179. GAS_Value = checkGAS();
  180. if (GAS_Value > GasActivationValue) {
  181. sendSMS("GAS detected " + String(GAS_Value));
  182. delay(1000);
  183. bot.sendMessage(CHAT_ID, "GAS detected " + String(GAS_Value));
  184. prevMillis2 = millis();
  185. take_photo_flag = 1;
  186. digitalWrite(Camera_PIN, 1);
  187. delay(1000);
  188. digitalWrite(Camera_PIN, 0);
  189. }
  190. delay(50);
  191. Flame_Value = checkFlame();
  192. if (Flame_Value < FlameActivationValue) {
  193. sendSMS("Flame detected " + String(Flame_Value));
  194. delay(1000);
  195. bot.sendMessage(CHAT_ID, "Flame detected " + String(Flame_Value));
  196. prevMillis2 = millis();
  197. take_photo_flag = 1;
  198. digitalWrite(Camera_PIN, 1);
  199. delay(1000);
  200. digitalWrite(Camera_PIN, 0);
  201. }
  202. delay(50);
  203.  
  204. }//end of Loop
  205.  
  206.  
  207. void setup_GPS_GSM_Modem() {
  208. Serial2.begin(GPSBaud);
  209.  
  210. Serial.println("Starting...");
  211. Serial2.println("\r");
  212. Serial2.println("AT\r");
  213. delay(10);
  214.  
  215. Serial2.println("\r");
  216. Serial2.println("AT+GPS=1\r");
  217.  
  218. delay(100);
  219. Serial2.println("AT+CREG=2\r");
  220. delay(6000);
  221.  
  222. //Serial2.print("AT+CREG?\r");
  223. Serial2.println("AT+CGATT=1\r");
  224. delay(6000);
  225.  
  226. Serial2.println("AT+CGDCONT=1,\"IP\",\"WWW\"\r");
  227. delay(6000);
  228.  
  229. // Serial2.println("AT+LOCATION=1\r");
  230. Serial2.println("AT+CGACT=1,1\r");
  231. delay(6000);
  232.  
  233. //Initialize ends
  234. //Initialize GPS
  235. Serial2.println("\r");
  236. Serial2.println("AT+GPS=1\r");
  237. delay(1000);
  238.  
  239. //Serial2.println("AT+GPSMD=1\r"); // Change to only GPS mode from GPS+BDS, set to 2 to revert to default.
  240. Serial2.println("AT+GPSRD=10\r");
  241. delay(100);
  242.  
  243. // set SMS mode to text mode
  244. Serial2.println("AT+CMGF=1\r");
  245. delay(1000);
  246. //Serial2.println("AT+LOCATION=2\r");
  247. Serial.println("GSM GPS Setup Executed");
  248. }
  249.  
  250.  
  251. void sendSMS(String s) {
  252. Serial.println("Sending SMS Message");
  253. Serial2.println("AT+CMGF=1\r");
  254. delay(1000);
  255. Serial2.println("AT+CNMI=2,2,0,0,0\r");
  256. delay(1000);
  257. Serial2.print("AT+CMGS=\"+966580224645\"\r");//Replace this with your mobile number
  258. delay(1000);
  259. Serial2.print(s);
  260. Serial2.write(0x1A);
  261. delay(1000);
  262. }
Add Comment
Please, Sign In to add comment