Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Template ID, Device Name and Auth Token are provided by the Blynk.Cloud
- // See the Device Info tab, or Template settings
- #define BLYNK_TEMPLATE_ID "TMPL24lTb8SVL"
- #define BLYNK_DEVICE_NAME "Anti intruder theft system"
- #define BLYNK_AUTH_TOKEN "a3E3Oed_RIDAy6C3sw-Dl15ab-_iSFUo"
- // including all the required library
- #define BLYNK_PRINT Serial
- #include <WiFi.h>
- #include <WiFiClient.h>
- #include <BlynkSimpleEsp32.h>
- #include <Wire.h>
- #include <LiquidCrystal_I2C.h>
- #include <SoftwareSerial.h>
- //Create software serial object to communicate with SIM800L
- SoftwareSerial mySerial(27, 26); //SIM800L Tx & Rx is connected to Arduino #3 & #2
- const int blue_led = 25;
- const int buzzer = 19;
- const int gas_sensor = 32;
- LiquidCrystal_I2C lcd(0x27, 16, 2); // I2C LCD address and dimensions
- int previousDistance = 0;
- int buzzerBeepDuration = 0;
- char auth[] = BLYNK_AUTH_TOKEN;
- // Your WiFi credentials.
- // Set password to "" for open networks.
- char ssid[] = "wifi name";
- char pass[] = "password";
- BlynkTimer timer;
- void setup()
- {
- Serial.begin(9600);
- //Begin serial communication with Arduino and SIM800L
- mySerial.begin(9600);
- pinMode(buzzer, OUTPUT);
- pinMode(blue_led, OUTPUT);
- pinMode(gas_sensor, INPUT);
- lcd.init();
- // Print a message to the LCD.
- lcd.backlight();
- lcd.setCursor(0, 0);
- lcd.print("IOT GAS SYSTEM!");
- lcd.blink();
- delay(2000);
- lcd.setCursor(0, 1);
- lcd.print("Initializing...");
- lcd.blink();
- delay(2000);
- Blynk.begin(auth, ssid, pass);
- }
- void loop()
- {
- Blynk.run();
- // Get the current time in milliseconds
- unsigned long currentTime = millis();
- unsigned long previousTime;
- unsigned long real_time = currentTime / 1000;
- // Check if 1 second (1000 milliseconds) has elapsed
- if (currentTime - previousTime >= 1000)// Update previousTime with the current time
- {
- previousTime = currentTime;
- currentTime = real_time;
- Blynk.virtualWrite(V1, real_time);// Log the real time in seconds to Blynk's virtual pin 1
- }
- // Read gas sensor value
- int gasValue = analogRead(gas_sensor);
- // Map the gas sensor value to a meaningful range (adjust as needed)
- int gasPercentage = map(gasValue, 1840, 4095, 0, 100);
- if (gasPercentage < 0) {gasPercentage = 0;}
- Blynk.virtualWrite(V0, gasPercentage);
- Serial.print(real_time);
- Serial.print("sec ");
- Serial.print("Gas Percentage: ");
- Serial.print(gasPercentage);
- Serial.println("%");
- lcd.clear();
- lcd.setCursor(0, 0);
- lcd.print("Monitoring now:");
- lcd.setCursor(0, 1);
- lcd.print("Gas Level: "); // Update distance value on LCD
- lcd.print(gasPercentage);
- lcd.print("%");
- lcd.blink();
- // if (gasPercentage > 35)
- // {
- // // If distance is less than 35%, turn on the LED and adjust the buzzer duration
- // digitalWrite(blue_led, HIGH);
- // digitalWrite(buzzer, HIGH);
- // }
- // else // If distance is greater than or equal to 30 cm, turn off the LED and reset the buzzer duration
- // {
- // digitalWrite(blue_led, LOW);
- // digitalWrite(buzzer, LOW);
- // }
- if (gasPercentage > 50)
- {
- mySerial.println("AT"); //Once the handshake test is successful, it will back to OK
- updateSerial();
- mySerial.println("AT+CMGF=1"); // Configuring TEXT mode
- updateSerial();
- mySerial.println("AT+CMGS=\"+2348096283289\"");//change ZZ with country code and xxxxxxxxxxx with phone number to sms
- updateSerial();
- mySerial.print("Good day sir! Gas threshold is above 50%."); //text content
- updateSerial();
- mySerial.write(26);
- digitalWrite (blue_led, HIGH );
- delay (2000);
- Serial.println("SMS sent succesfully!");
- for(int a =0;a<8;a++)
- {
- tone (buzzer,31,500);
- digitalWrite (blue_led, HIGH );
- delay(500);
- noTone(buzzer);
- digitalWrite (blue_led,LOW);
- delay(500);
- }
- }
- delay(100); // Delay for stability
- }
- void updateSerial()
- {
- delay(500);
- while (Serial.available())
- {
- mySerial.write(Serial.read());//Forward what Serial received to Software Serial Port
- }
- while(mySerial.available())
- {
- Serial.write(mySerial.read());//Forward what Software Serial received to Serial Port
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement