Advertisement
Ogomegbunam

ESP32/SIM800L T1

Oct 13th, 2023
16
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.01 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(17, 16); //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(115200);
  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.  
  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. Blynk.virtualWrite(V1, currentTime / 1000);// Log the real time in seconds to Blynk's virtual pin 1
  72. }
  73.  
  74. // Read gas sensor value
  75. int gasValue = analogRead(gas_sensor);
  76. // Map the gas sensor value to a meaningful range (adjust as needed)
  77. int gasPercentage = map(gasValue, 1840, 4095, 0, 100);
  78. if (gasPercentage < 0) {gasPercentage = 0;}
  79. Blynk.virtualWrite(V0, gasPercentage);
  80. Serial.println("Gas Percentage: ");
  81. Serial.print(gasPercentage);
  82. lcd.clear();
  83. lcd.setCursor(0, 0);
  84. lcd.print("Monitoring now:");
  85. lcd.setCursor(0, 1);
  86. lcd.print("Gas Level: "); // Update distance value on LCD
  87. lcd.print(gasPercentage);
  88. lcd.print("%");
  89. lcd.blink();
  90.  
  91. // if (gasPercentage > 35)
  92. // {
  93. // // If distance is less than 35%, turn on the LED and adjust the buzzer duration
  94. // digitalWrite(blue_led, HIGH);
  95. // digitalWrite(buzzer, HIGH);
  96. // }
  97. // else // If distance is greater than or equal to 30 cm, turn off the LED and reset the buzzer duration
  98. // {
  99. // digitalWrite(blue_led, LOW);
  100. // digitalWrite(buzzer, LOW);
  101. // }
  102. if (gasPercentage > 50)
  103. {
  104.  
  105. mySerial.println("AT"); //Once the handshake test is successful, it will back to OK
  106. updateSerial();
  107. mySerial.println("AT+CMGF=1"); // Configuring TEXT mode
  108. updateSerial();
  109. mySerial.println("AT+CMGS=\"+2348135786537\"");//change ZZ with country code and xxxxxxxxxxx with phone number to sms
  110. updateSerial();
  111. mySerial.print("Good day sir! Gas threshold is above 50%."); //text content
  112. updateSerial();
  113. mySerial.write(26);
  114. digitalWrite (blue_led, HIGH );
  115. delay (2000);
  116. for(int a =0;a<8;a++)
  117. {
  118. tone (buzzer,31,500);
  119. digitalWrite (blue_led, HIGH );
  120. delay(500);
  121. noTone(buzzer);
  122. digitalWrite (blue_led,LOW);
  123. delay(500);
  124. }
  125. delay (2000);
  126. }
  127. delay(100); // Delay for stability
  128.  
  129. }
  130. void updateSerial()
  131. {
  132. delay(500);
  133. while (Serial.available())
  134. {
  135. mySerial.write(Serial.read());//Forward what Serial received to Software Serial Port
  136. }
  137. while(mySerial.available())
  138. {
  139. Serial.write(mySerial.read());//Forward what Software Serial received to Serial Port
  140. }
  141. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement