Liquefied petroleum gas or liquid petroleum gas LPG, is a flammable mixture of hydrocarbon gases used as fuel in heating appliances, cooking equipment, and vehicles. LPG is a odorless gas but to make it odor, additives are added.
Our gas sensor which is MQ-4 Gas Monitors the LPG sensor output 500 times and takes the average of the sampling to reduce reading noise, and if the value is greater than the threshold value, it sends email to the recipient.
/* ESP8266 LPG Leakage Email Sender Monitors the LPG sensor output 500 times and takes the average of the sampling to reduce reading noise, and if the value is greater than the threshold value, it sends email to the recipient . Before uploading the code make to sure to change in the Gsender header file: const int SMTP_PORT = 465;// STMP port change it if you use other smtp const char* SMTP_SERVER = "smtp.gmail.com";// gmail smtp server change if you use other smtp const char* EMAILBASE64_LOGIN = "YOUR EMAIL ID ENCODED WITH BASE64";// your email must me encoded with base64 const char* EMAILBASE64_PASSWORD = "YOUR PASSWORD ENCODED WITH BASE64";// your password must be encoded with base64 const char* FROM = "youremail@gmail.com";//your email address with encoding Change these lines as per your need: const int sensorPin = A0; //connect sensor A0 output to this pin const char *ssid = "**********"; // WIFI network name const char *password = "*********"; // WIFI network password uint8_t connection_state = 0; // Connected to WIFI or not uint8_t nSampling = 500; //change as per your need uint16_t threshold = 200; // change as per your need Parts list: NodeMCU : https://www.amazon.in/Generic-Nodemcu-Esp8266-Internet-Development/dp/B07262H53W/ref=sr_1_1?keywords=nodemcu&qid=1583588225&sr=8-1 Gas Sensor: https://www.amazon.in/REES52-Arduino-Compatible-Sensor-Methane/dp/B018GW70G0/ref=sr_1_2?crid=2O01SJT84RKL&keywords=mq4+gas+detector+sensor&qid=1583588254&sprefix=mq4+%2Caps%2C390&sr=8-2 Jumper Wires: https://www.amazon.in/Electrobot-Jumper-Wires-120-Pieces/dp/B071VQLQQQ/ref=sr_1_5?keywords=jumper+wire&qid=1583588298&sr=8-5 Project made and maintained by Kumar Aditya The source code along with libraries can be found at: https://github.com/rahuladitya303/programs/tree/master/ESP8266_LPG_Leakage_Email_Sender Base64 Encode: https://www.base64encode.org/*/#include<ESP8266WiFi.h>#include"Gsender.h"constintsensorPin=A0;//connect sensor A0 output to this pinconstchar*ssid="**********";// WIFI network nameconstchar*password="*********";// WIFI network passworduint8_tconnection_state=0;// Connected to WIFI or notuint8_tnSampling=500;//change as per your needuint16_tthreshold=200;// change as per your needvoidsetup(){Serial.begin(115200);pinMode(sensorPin,INPUT);pinMode(16,OUTPUT);pinMode(2,OUTPUT);digitalWrite(2,HIGH);digitalWrite(16,HIGH);Serial.begin(115200);// change as per your needWiFi.begin(ssid,password);Serial.print("\nConnecting to WiFi network");while(WiFi.status()!=WL_CONNECTED){Serial.print(".");digitalWrite(2,HIGH);delay(500);digitalWrite(2,LOW);delay(500);}Serial.println("\nConnected to the WiFi network: "+WiFi.SSID()+" and strength is: "+WiFi.RSSI());}voidloop(){if((WiFi.status()==WL_CONNECTED)){floattotal=0;for(inti=1;i<=nSampling;i++){intx=analogRead(A0);total+=x;delay(10);}total/=nSampling;Serial.println(total);if(total>=threshold){Gsender*gsender=Gsender::Instance();// Getting pointer to class instanceStringsubject="Gas Leakage";if(gsender->Subject(subject)->Send("recipient@gmail.com","Gas Leakage found!")){// change recipient with your recipientSerial.println("Message send.");}else{Serial.print("Error sending message: ");Serial.println(gsender->getError());}delay(300000);}}elseif(WiFi.status()!=WL_CONNECTED){Serial.print(".");digitalWrite(2,HIGH);delay(500);digitalWrite(2,LOW);delay(500);}}
Gsender header file
C Header File
#ifndef G_SENDER#define G_SENDER#define GS_SERIAL_LOG_1 // Print to Serial only server responce//#define GS_SERIAL_LOG_2 // Print to Serial client commands and server responce#include<WiFiClientSecure.h>classGsender{protected:Gsender();private:constintSMTP_PORT=465;// STMP port change it if you use other smtpconstchar*SMTP_SERVER="smtp.gmail.com";// gmail smtp server change if you use other smtpconstchar*EMAILBASE64_LOGIN="YOUR EMAIL ID ENCODED WITH BASE64";// your email must me encoded with base64constchar*EMAILBASE64_PASSWORD="YOUR PASSWORD ENCODED WITH BASE64";// your password must be encoded with base64constchar*FROM="youremail@gmail.com";//yoyr email address with encodingconstchar*_error=nullptr;char*_subject=nullptr;String_serverResponce;staticGsender*_instance;boolAwaitSMTPResponse(WiFiClientSecure&client,constString&resp="",uint16_ttimeOut=10000);public:staticGsender*Instance();Gsender*Subject(constchar*subject);Gsender*Subject(constString&subject);boolSend(constString&to,constString&message);StringgetLastResponce();constchar*getError();};#endif // G_SENDER
Gsender cpp file
C/C++
#include"Gsender.h"Gsender*Gsender::_instance=0;Gsender::Gsender(){}Gsender*Gsender::Instance(){if(_instance==0)_instance=newGsender;return_instance;}Gsender*Gsender::Subject(constchar*subject){delete[]_subject;_subject=newchar[strlen(subject)+1];strcpy(_subject,subject);return_instance;}Gsender*Gsender::Subject(constString&subject){returnSubject(subject.c_str());}boolGsender::AwaitSMTPResponse(WiFiClientSecure&client,constString&resp,uint16_ttimeOut){uint32_tts=millis();while(!client.available()){if(millis()>(ts+timeOut)){_error="SMTP Response TIMEOUT!";returnfalse;}}_serverResponce=client.readStringUntil('\n');#if defined(GS_SERIAL_LOG_1) || defined(GS_SERIAL_LOG_2) Serial.println(_serverResponce);#endifif(resp&&_serverResponce.indexOf(resp)==-1)returnfalse;returntrue;}StringGsender::getLastResponce(){return_serverResponce;}constchar*Gsender::getError(){return_error;}boolGsender::Send(constString&to,constString&message){WiFiClientSecureclient;#if defined(GS_SERIAL_LOG_2)Serial.print("Connecting to :");Serial.println(SMTP_SERVER);#endifif(!client.connect(SMTP_SERVER,SMTP_PORT)){_error="Could not connect to mail server";returnfalse;}if(!AwaitSMTPResponse(client,"220")){_error="Connection Error";returnfalse;}#if defined(GS_SERIAL_LOG_2)Serial.println("HELO friend:");#endifclient.println("HELO friend");if(!AwaitSMTPResponse(client,"250")){_error="identification error";returnfalse;}#if defined(GS_SERIAL_LOG_2)Serial.println("AUTH LOGIN:");#endifclient.println("AUTH LOGIN");AwaitSMTPResponse(client);#if defined(GS_SERIAL_LOG_2)Serial.println("EMAILBASE64_LOGIN:");#endifclient.println(EMAILBASE64_LOGIN);AwaitSMTPResponse(client);#if defined(GS_SERIAL_LOG_2)Serial.println("EMAILBASE64_PASSWORD:");#endifclient.println(EMAILBASE64_PASSWORD);if(!AwaitSMTPResponse(client,"235")){_error="SMTP AUTH error";returnfalse;}StringmailFrom="MAIL FROM: <"+String(FROM)+'>';#if defined(GS_SERIAL_LOG_2)Serial.println(mailFrom);#endifclient.println(mailFrom);AwaitSMTPResponse(client);Stringrcpt="RCPT TO: <"+to+'>';#if defined(GS_SERIAL_LOG_2)Serial.println(rcpt);#endifclient.println(rcpt);AwaitSMTPResponse(client);#if defined(GS_SERIAL_LOG_2)Serial.println("DATA:");#endifclient.println("DATA");if(!AwaitSMTPResponse(client,"354")){_error="SMTP DATA error";returnfalse;}client.println("From: <"+String(FROM)+'>');client.println("To: <"+to+'>');client.print("Subject: ");client.println(_subject);client.println("Mime-Version: 1.0");client.println("Content-Type: text/html; charset=\"UTF-8\"");client.println("Content-Transfer-Encoding: 7bit");client.println();Stringbody="<!DOCTYPE html><html lang=\"en\">"+message+"</html>";client.println(body);client.println(".");if(!AwaitSMTPResponse(client,"250")){_error="Sending message error";returnfalse;}client.println("QUIT");if(!AwaitSMTPResponse(client,"221")){_error="SMTP QUIT error";returnfalse;}returntrue;}
Readme
Markdown
># ESP8266 LPG Leakage Email Sender
>**Monitors the LPG sensor output 500times and takes the average of the sampling to reduce reading noise,and if the value is greater than the threshold value, it sends email to the recipient.**
>**Before uploading the code make to sure to change in the Gsender header file:**
- const int SMTP_PORT=465;// STMP port change it if you use other smtp
- const char* SMTP_SERVER="smtp.gmail.com";// gmail smtp server change if you use other smtp
- const char* EMAILBASE64_LOGIN="YOUR EMAIL ID ENCODED WITH BASE64";// your email must me encoded with base64
- const char* EMAILBASE64_PASSWORD="YOUR PASSWORD ENCODED WITH BASE64";// your password must be encoded with base64
- const char* FROM="youremail@gmail.com";//your email address with encoding
>**Change these lines as per your need:**
- const int sensorPin= A0; //connect sensor A0 output to this pin
- const char *ssid ="xxxxxxxxx"; // WIFI network name
- const char *password ="xxxxxxxx"; // WIFI network password
- uint8_t connection_state=0; // Connected to WIFI or not
- uint8_t nSampling=500; //change as per your need
- uint16_t threshold=200; // change as per your need
>**Parts list:**
- NodeMCU : https://www.amazon.in/Generic-Nodemcu-Esp8266-Internet-Development/dp/B07262H53W/ref=sr_1_1?keywords=nodemcu&qid=1583588225&sr=8-1
- Gas Sensor: https://www.amazon.in/REES52-Arduino-Compatible-Sensor-Methane/dp/B018GW70G0/ref=sr_1_2?crid=2O01SJT84RKL&keywords=mq4+gas+detector+sensor&qid=1583588254&sprefix=mq4+%2Caps%2C390&sr=8-2
- Jumper Wires: https://www.amazon.in/Electrobot-Jumper-Wires-120-Pieces/dp/B071VQLQQQ/ref=sr_1_5?keywords=jumper+wire&qid=1583588298&sr=8-5
>Project made and maintained by ***Kumar Aditya***
>Base64 Encode: https://www.base64encode.org/
>The source code along with libraries can be found at:
https://github.com/rahuladitya303/programs/tree/master/ESP8266_LPG_Leakage_Email_Sender
Comments
Please log in or sign up to comment.