Skip to content

Instantly share code, notes, and snippets.

@kumaraditya303
Last active March 21, 2020 10:15
Show Gist options
  • Save kumaraditya303/321b04400a586fef64a873108af7feaa to your computer and use it in GitHub Desktop.
Save kumaraditya303/321b04400a586fef64a873108af7feaa to your computer and use it in GitHub Desktop.
ESP8266_LPG_Leakage_Email_Sender

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 = "xxxxxxxxx";    // WIFI network name  
    
  • const char *password = "xxxxxxxx"; // WIFI network password  
    
  • 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

/*
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 make sure that your ESP8266 Core is version is 2.4.0 as this code only works for that !
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
const char *recipient="recipient@gmail.com"; //change this to your recipient
uint8_t nSampling = 500; //change as per your need
uint16_t threshold = 200; // change as per your need
Parts list:
NodeMCU :
https://amzn.to/397GzNe
Gas Sensor:
https://amzn.to/2xQlvOi
Jumper Wires:
https://amzn.to/2U9lWMz
Project made and maintained by Kumar Aditya
The source code along with libraries can be found at:
https://github.com/rahuladitya303/ESP8266_LPG_Leakage_Email_Sender
Base64 Encode: https://www.base64encode.org/
*/
#include <ESP8266WiFi.h>
#include "Gsender.h"
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
const char *recipient="recipient@gmail.com"; //change this to your recipient
void setup()
{
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 need
WiFi.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());
}
void loop()
{
if ((WiFi.status() == WL_CONNECTED))
{
float total = 0;
for (int i = 1; i <= nSampling; i++)
{
int x = analogRead(A0);
total += x;
delay(10);
}
total /= nSampling;
Serial.println(total);
if (total >= threshold)
{
Gsender *gsender = Gsender::Instance(); // Getting pointer to class instance
String subject = "Gas Leakage";
if (gsender->Subject(subject)->Send(recipient, "Gas Leakage found!"))
{ // change recipient with your recipient
Serial.println("Message send.");
}
else
{
Serial.print("Error sending message: ");
Serial.println(gsender->getError());
}
delay(30000);
}
}
else if (WiFi.status() != WL_CONNECTED)
{
Serial.print(".");
digitalWrite(2, HIGH);
delay(500);
digitalWrite(2, LOW);
delay(500);
}
}
#include "Gsender.h"
Gsender* Gsender::_instance = 0;
Gsender::Gsender(){}
Gsender* Gsender::Instance()
{
if (_instance == 0)
_instance = new Gsender;
return _instance;
}
Gsender* Gsender::Subject(const char* subject)
{
delete [] _subject;
_subject = new char[strlen(subject)+1];
strcpy(_subject, subject);
return _instance;
}
Gsender* Gsender::Subject(const String &subject)
{
return Subject(subject.c_str());
}
bool Gsender::AwaitSMTPResponse(WiFiClientSecure &client, const String &resp, uint16_t timeOut)
{
uint32_t ts = millis();
while (!client.available())
{
if(millis() > (ts + timeOut)) {
_error = "SMTP Response TIMEOUT!";
return false;
}
}
_serverResponce = client.readStringUntil('\n');
#if defined(GS_SERIAL_LOG_1) || defined(GS_SERIAL_LOG_2)
Serial.println(_serverResponce);
#endif
if (resp && _serverResponce.indexOf(resp) == -1) return false;
return true;
}
String Gsender::getLastResponce()
{
return _serverResponce;
}
const char* Gsender::getError()
{
return _error;
}
bool Gsender::Send(const String &to, const String &message)
{
WiFiClientSecure client;
#if defined(GS_SERIAL_LOG_2)
Serial.print("Connecting to :");
Serial.println(SMTP_SERVER);
#endif
if(!client.connect(SMTP_SERVER, SMTP_PORT)) {
_error = "Could not connect to mail server";
return false;
}
if(!AwaitSMTPResponse(client, "220")) {
_error = "Connection Error";
return false;
}
#if defined(GS_SERIAL_LOG_2)
Serial.println("HELO friend:");
#endif
client.println("HELO friend");
if(!AwaitSMTPResponse(client, "250")){
_error = "identification error";
return false;
}
#if defined(GS_SERIAL_LOG_2)
Serial.println("AUTH LOGIN:");
#endif
client.println("AUTH LOGIN");
AwaitSMTPResponse(client);
#if defined(GS_SERIAL_LOG_2)
Serial.println("EMAILBASE64_LOGIN:");
#endif
client.println(EMAILBASE64_LOGIN);
AwaitSMTPResponse(client);
#if defined(GS_SERIAL_LOG_2)
Serial.println("EMAILBASE64_PASSWORD:");
#endif
client.println(EMAILBASE64_PASSWORD);
if (!AwaitSMTPResponse(client, "235")) {
_error = "SMTP AUTH error";
return false;
}
String mailFrom = "MAIL FROM: <" + String(FROM) + '>';
#if defined(GS_SERIAL_LOG_2)
Serial.println(mailFrom);
#endif
client.println(mailFrom);
AwaitSMTPResponse(client);
String rcpt = "RCPT TO: <" + to + '>';
#if defined(GS_SERIAL_LOG_2)
Serial.println(rcpt);
#endif
client.println(rcpt);
AwaitSMTPResponse(client);
#if defined(GS_SERIAL_LOG_2)
Serial.println("DATA:");
#endif
client.println("DATA");
if(!AwaitSMTPResponse(client, "354")) {
_error = "SMTP DATA error";
return false;
}
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();
String body = "<!DOCTYPE html><html lang=\"en\">" + message + "</html>";
client.println(body);
client.println(".");
if (!AwaitSMTPResponse(client, "250")) {
_error = "Sending message error";
return false;
}
client.println("QUIT");
if (!AwaitSMTPResponse(client, "221")) {
_error = "SMTP QUIT error";
return false;
}
return true;
}
#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>
class Gsender
{
protected:
Gsender();
private:
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";//yoyr email address with encoding
const char* _error = nullptr;
char* _subject = nullptr;
String _serverResponce;
static Gsender* _instance;
bool AwaitSMTPResponse(WiFiClientSecure &client, const String &resp = "", uint16_t timeOut = 10000);
public:
static Gsender* Instance();
Gsender* Subject(const char* subject);
Gsender* Subject(const String &subject);
bool Send(const String &to, const String &message);
String getLastResponce();
const char* getError();
};
#endif // G_SENDER
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment