Advertisement
Ogomegbunam

ESP32/SIM800L T2

Oct 13th, 2023
15
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.18 KB | None | 0 0
  1. // Template ID, Device Name and Auth Token are provided by the Blynk.Cloud
  2. // See the Device Info tab, or Template settings
  3. #define BLYNK_TEMPLATE_ID "TMPL24lTb8SVL"
  4. #define BLYNK_DEVICE_NAME "Anti intruder theft system"
  5. #define BLYNK_AUTH_TOKEN "a3E3Oed_RIDAy6C3sw-Dl15ab-_iSFUo"
  6.  
  7. // including all the required library
  8. #define BLYNK_PRINT Serial
  9. #include <WiFi.h>
  10. #include <WiFiClient.h>
  11. #include <BlynkSimpleEsp32.h>
  12. #include <Wire.h>
  13. #include <LiquidCrystal_I2C.h>
  14.  
  15. #include <SoftwareSerial.h>
  16. //Create software serial object to communicate with SIM800L
  17. SoftwareSerial mySerial(27, 26); //SIM800L Tx & Rx is connected to Arduino #3 & #2
  18.  
  19. const int blue_led = 25;
  20. const int buzzer = 19;
  21. const int gas_sensor = 32;
  22.  
  23. LiquidCrystal_I2C lcd(0x27, 16, 2); // I2C LCD address and dimensions
  24.  
  25. int previousDistance = 0;
  26. int buzzerBeepDuration = 0;
  27.  
  28. char auth[] = BLYNK_AUTH_TOKEN;
  29.  
  30. // Your WiFi credentials.
  31. // Set password to "" for open networks.
  32. char ssid[] = "wifi name";
  33. char pass[] = "password";
  34.  
  35. BlynkTimer timer;
  36.  
  37. void setup()
  38. {
  39. Serial.begin(9600);
  40. //Begin serial communication with Arduino and SIM800L
  41. mySerial.begin(9600);
  42. pinMode(buzzer, OUTPUT);
  43. pinMode(blue_led, OUTPUT);
  44. pinMode(gas_sensor, INPUT);
  45. lcd.init();
  46. // Print a message to the LCD.
  47. lcd.backlight();
  48. lcd.setCursor(0, 0);
  49. lcd.print("IOT GAS SYSTEM!");
  50. lcd.blink();
  51. delay(2000);
  52. lcd.setCursor(0, 1);
  53. lcd.print("Initializing...");
  54. lcd.blink();
  55. delay(2000);
  56. Blynk.begin(auth, ssid, pass);
  57.  
  58. }
  59.  
  60. void loop()
  61. {
  62. Blynk.run();
  63. // Get the current time in milliseconds
  64. unsigned long currentTime = millis();
  65. unsigned long previousTime;
  66. unsigned long real_time = currentTime / 1000;
  67. // Check if 1 second (1000 milliseconds) has elapsed
  68. if (currentTime - previousTime >= 1000)// Update previousTime with the current time
  69. {
  70. previousTime = currentTime;
  71. currentTime = real_time;
  72. Blynk.virtualWrite(V1, real_time);// Log the real time in seconds to Blynk's virtual pin 1
  73. }
  74.  
  75. // Read gas sensor value
  76. int gasValue = analogRead(gas_sensor);
  77. // Map the gas sensor value to a meaningful range (adjust as needed)
  78. int gasPercentage = map(gasValue, 1840, 4095, 0, 100);
  79. if (gasPercentage < 0) {gasPercentage = 0;}
  80. Blynk.virtualWrite(V0, gasPercentage);
  81. Serial.print(real_time);
  82. Serial.print("sec ");
  83. Serial.print("Gas Percentage: ");
  84. Serial.print(gasPercentage);
  85. Serial.println("%");
  86. lcd.clear();
  87. lcd.setCursor(0, 0);
  88. lcd.print("Monitoring now:");
  89. lcd.setCursor(0, 1);
  90. lcd.print("Gas Level: "); // Update distance value on LCD
  91. lcd.print(gasPercentage);
  92. lcd.print("%");
  93. lcd.blink();
  94.  
  95. // if (gasPercentage > 35)
  96. // {
  97. // // If distance is less than 35%, turn on the LED and adjust the buzzer duration
  98. // digitalWrite(blue_led, HIGH);
  99. // digitalWrite(buzzer, HIGH);
  100. // }
  101. // else // If distance is greater than or equal to 30 cm, turn off the LED and reset the buzzer duration
  102. // {
  103. // digitalWrite(blue_led, LOW);
  104. // digitalWrite(buzzer, LOW);
  105. // }
  106. if (gasPercentage > 50)
  107. {
  108.  
  109. mySerial.println("AT"); //Once the handshake test is successful, it will back to OK
  110. updateSerial();
  111. mySerial.println("AT+CMGF=1"); // Configuring TEXT mode
  112. updateSerial();
  113. mySerial.println("AT+CMGS=\"+2348096283289\"");//change ZZ with country code and xxxxxxxxxxx with phone number to sms
  114. updateSerial();
  115. mySerial.print("Good day sir! Gas threshold is above 50%."); //text content
  116. updateSerial();
  117. mySerial.write(26);
  118. digitalWrite (blue_led, HIGH );
  119. delay (2000);
  120. Serial.println("SMS sent succesfully!");
  121. for(int a =0;a<8;a++)
  122. {
  123. tone (buzzer,31,500);
  124. digitalWrite (blue_led, HIGH );
  125. delay(500);
  126. noTone(buzzer);
  127. digitalWrite (blue_led,LOW);
  128. delay(500);
  129. }
  130. }
  131. delay(100); // Delay for stability
  132.  
  133. }
  134. void updateSerial()
  135. {
  136. delay(500);
  137. while (Serial.available())
  138. {
  139. mySerial.write(Serial.read());//Forward what Serial received to Software Serial Port
  140. }
  141. while(mySerial.available())
  142. {
  143. Serial.write(mySerial.read());//Forward what Software Serial received to Serial Port
  144. }
  145. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement