Whatsapp has made communication very easy. We can send calls, messages, and video calls from anywhere in the world. All we need is Wi-Fi or mobile data. There is no charge. We can receive notifications from a gas sensor on our mobile phones through WhatsApp. This tutorial will show how.
Working principle:
We will use an ESP8266 as the main processor for the project. , we know that ESP8266 has its own Wi-Fi module. Through this Wi-Fi module, ESP8266 can connect itself to a Wi-Fi network.
The ESP8266 will be connected to a Wi-Fi network. We will connect the MQ-2 to the ESP8266. The MQ-2 sensor can detect various types of gases. Gas from stoves, alcohol fumes, and smoke are among them.
There will be a pre-defined WhatsApp number. When the sensor detects any gas or smoke, our project will send an alert message to that number.
To send messages on WhatsApp, we will use an API called CallMeBot.
What will we learn from this project?
- How to send Whatspp message from ESP8266.
- How to read MQ-2 sensor data with ESP8266.
Components needed |
Quantity |
ESP8266 NodeMCU V2 Development Board with CP2102 |
1 |
Gas Sensor Module (MQ-2) |
1 |
Breadboard(830 point) |
1 |
Male to male jumpers |
3 |
Micro USB cable |
1 |
App and API needed:
- CallMeBot
Circuit connection:
Arduino UNO |
MQ-2 |
3V |
VCC |
GND |
GND |
A0 |
A0 |
My setup looked like this in real life:
App installation:I assume, whatsapp is installed in your mobile. If not, download and install it from here.
API Key generation: To use CallMeBot, follow the steps described below:
Save +34 644 44 21 48 in your phone’s contact list। You can save by any name.
Now send this message from whatsapp to the number above “I allow callmebot to send me messages” ।
You will receive a reply. It will include your API key. You’ll have to write the API key in your programme.
Is it your first project with ESP8266? If yes, you have to finish some tasks before. You have to perform board installation. You can see the procedure here.
Library installation: Open Arduino IDE. Then search and install UrlEncode library.
Code:
Write the following programe in Arduino IDE:
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <WiFiClient.h>
#include <UrlEncode.h>
const char* ssid = "Your SSID";
const char* password = "Your password";
// +international_country_code + phone number
// Bangladesh +88, example: +8801....
String phoneNumber = "Your phone number";
String apiKey = "API KEY";
void sendMessage(String message){
// Data to send with HTTP POST
String url = "http://api.callmebot.com/whatsapp.php?phone=" + phoneNumber + "&apikey=" + apiKey + "&text=" + urlEncode(message);
WiFiClient client;
HTTPClient http;
http.begin(client, url);
// Specify content-type header
http.addHeader("Content-Type", "application/x-www-form-urlencoded");
// Send HTTP POST request
int httpResponseCode = http.POST(url);
if (httpResponseCode == 200){
Serial.print("Message sent successfully");
}
else{
Serial.println("Error sending the message");
Serial.print("HTTP response code: ");
Serial.println(httpResponseCode);
}
// Free resources
http.end();
}
void setup() {
Serial.begin(115200);
WiFi.begin(ssid, password);
Serial.println("Connecting");
while(WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.print("Connected to WiFi network with IP Address: ");
Serial.println(WiFi.localIP());
// Send Message to WhatsAPP
// sendMessage("Hello from ESP8266!");
}
void loop() {
// read the input on analog pin 0:
int sensorValue = analogRead(A0);
// Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V):
float voltage = sensorValue * (5.0 / 1023.0);
// print out the value you read:
// Serial.println(voltage);
Serial.println(sensorValue);
if(sensorValue>=700)
{
sendMessage("GAS!");
}
}
You have to select NodeMCU 1.0 (ESP12E-Module) from the Board option at the time of uploading the code.
Open the serial monitor in 115200 baud rate. You will see the analog value of A0 pin. In my setup, this value was below 300 under normal condition.
In presence of gas, this value increases. It can be up to 1024.
According to my code, you’ll receive an whatsapp message when the value is 700 or more.
You can test the circuit keeping gas lighter, candle, hand sanitizer etc. in front of the sensor.
Top Comments