- Esp32
- Male-to-Male jumpers
- Small plastic container or a capsule, open at the bottom
- USB cable
- Breadboard
- Code Editor (VS Code)
- NodeJS
- Arduino IDE
Setting Up Twilio
First of all make an account in Twilio from here https://www.twilio.com/try-twilio.
Then signin to your Twilio account and go to your console
Goto Account Info
Get a free My Twilio phone number
Setting Up Gmailfor sending mail
Go to your google manage account
Goto Security
Make sure you have setup your 2 Step Verification
Then There will be a link for App password
Select app :- Mail
Select device :- Other
Give it a name and click generate
You will get 16 char password in yellow box save it
Making Express Server
Now open your code Editor
Make a folder and open the integrated terminal and use this code
npm init -y
This will initialize the nodejs and then enter this code
npm i express nodemon twilio nodemailer dotenv
make a .env
file and add your folder
TWILIO_ACCOUNT_SID=XXXXXXXXXXX
TWILIO_AUTH_TOKEN=XXXXX
TWILIO_NUMBER=+1XXXX
GMAIL_SENDER=XXXXXX@gmail.com
GMAIL_PASSWORD=xxxxxxxx
Then make index.js
file in it
require("dotenv").config();
const accountSid = process.env.TWILIO_ACCOUNT_SID;
const authToken = process.env.TWILIO_AUTH_TOKEN;
const twilioNumber = process.env.TWILIO_NUMBER;
const gmailSender = process.env.GMAIL_SENDER;
const gmailPass = process.env.GMAIL_PASSWORD;
const client = require("twilio")(accountSid, authToken);
const express = require("express");
var nodemailer = require("nodemailer");
const app = express();
const PORT = 5000;
//--------------------- welcome ------------------------------------
app.get("/", (req, res) => {
res.send("Welcome to Water-Tank water level detector!!");
});
//--------------------- call ------------------------------------
app.get("/call/:number", async (req, res) => {
try {
await client.calls.create({
twiml: "<Response><Say>Water Tank is about to overflow, be quick and turn off the electric motor!</Say></Response>",
to: req.params.number,
from: twilioNumber,
});
res.send({
msg: "Call Sent successfully",
});
} catch (e) {
res.status(500).send({
err: "Something went wrong. Please try again.",
msg: e,
});
}
});
//--------------------- sms ------------------------------------
app.get("/sms/:number", async (req, res) => {
try {
await client.messages.create({
body: "Water Tank is about to overflow, be quick and turn off the electric motor!",
to: req.params.number,
from: twilioNumber,
});
res.send({
msg: "SMS Sent successfully",
});
} catch (e) {
res.status(500).send({
err: "Something went wrong. Please try again.",
msg: e,
});
}
});
//--------------------- mail ------------------------------------
var transporter = nodemailer.createTransport({
service: "gmail",
auth: {
user: gmailSender,
pass: gmailPass,
},
});
app.get("/mail/:receiverMail", async (req, res) => {
try {
const mailOptions = {
from: gmailSender,
to: req.params.receiverMail,
subject: "TURN OFF ELECTRIC MOTOR",
text: "Water Tank is about to overflow, be quick and turn off the electric motor!",
};
transporter.sendMail(mailOptions, (err, info) => {
if (err) {
res.status(500).send({
err: "Something Went Wrong. Try again!",
msg: err,
});
} else {
res.send({
msg: "Mail Sent successfully",
});
}
});
} catch (e) {
res.status(500).send({
err: "Something went wrong. Please try again.",
});
}
});
//--------------------- listen ------------------------------------
app.listen(PORT, () => {
console.log("Server Running at port: " + PORT);
});
Now to test the server run this cmd to your terminal
nodemon index.js
We have successfully created the server and api which can send call / sms / mail
Now deploy it to github repo
Deploy the serverto vercel
you can deploy this to anywhere here I have deployed it to vercel and connect it with github
Goto dashboard and add new project
Select the repo you have created ( click import )
Under configure project
Add environment variables and deploy
After that it should look like this
make a file in folder with
vercel.json
add this to it
{
"version": 2,
"builds": [
{
"src": "./index.js",
"use": "@vercel/node"
}
],
"routes": [
{
"src": "/(.*)",
"dest": "/"
}
]
}
Final file configuration
Esp32configuration for jumpers
- GND ➡️ -ve ( a.k.a Earthing )
- A5 ➡️ ❌ ( a.k.a Touch sensor)
- IO15 ➡️❌` ( a.k.a Analog Sensor)
- -ve ➡️ ❌
Here ❌ is for capsule as you can see from images down
Make sure wires are separated
F inally your setup should look something like this
Code in Arduino IDE
#include <WiFi.h>
#include <HTTPClient.h>
// Declaring pin number
const int waterSensorPin = 4;
const int voltageSensorPin = 32;
// Declaring wifi info
const char* WIFI_SSID = "your wifi name";
const char* WIFI_PASS = "your wifi password";
// Server domain name
String ServerName = "your server link";
// Mobile number along with country code
String OwnerPhNo = "your mobile number";
String MailAdd = "your email address";
// Sensory data
float waterSensorValue = 0;
float voltageValue = 0;
// Counter for confirming correct data
int confirmationCounter = 0;
// Waiting for water tank to change its state from full to not-full
bool tankFull = false;
void sendAlert(){
// Checking the wifi connection and http connection
if(WiFi.status()== WL_CONNECTED){
HTTPClient http;
String serverPath = ServerName + "/mail/" + MailAdd; // Setting Mail
// String serverPath = ServerName + "/sms/" + OwnerPhNo; // Setting SMS
// String serverPath = ServerName + "/call/" + OwnerPhNo; // Setting Call
// Your Domain name with URL path or IP address with path
http.begin(serverPath.c_str());
// Send HTTP GET request
int httpResponseCode = http.GET();
if (httpResponseCode > 0) {
Serial.print("HTTP Response code: ");
Serial.println(httpResponseCode);
String payload = http.getString();
Serial.println(payload);
}
else {
Serial.print("Error code: ");
Serial.println(httpResponseCode);
}
// Free resources
http.end();
}
else {
Serial.println("WiFi Disconnected");
}
}
void setup() {
Serial.begin(115200);
delay(1000);
// Connecting to wifi
WiFi.begin(WIFI_SSID, WIFI_PASS);
Serial.println("\nConnecting to wifi");
while(WiFi.status() != WL_CONNECTED){
Serial.print(".");
delay(100);
}
Serial.println("\nConnected to the WiFi network");
}
void loop() {
// Reading Water-Sensor Value
waterSensorValue = touchRead(waterSensorPin);
// Reading Voltage Value
voltageValue = analogRead(voltageSensorPin);
// Checking if the tank is full
if((waterSensorValue == 0) && (voltageValue < 0.001 || voltageValue >= 0))
{
if (tankFull == false) confirmationCounter++;
else confirmationCounter = 0;
}
else{
tankFull = false;
}
// For safety we will check for n iteration (here 5) that the tank is really full and then send notification to the owner
if(confirmationCounter == 5){
tankFull = true;
Serial.println("Sending Notification...");
sendAlert();
}
delay(500);
}
you can setup mail alert or call alert or sms alert just refer the function sendAlert()
- Esp32 takes input from touch and analog sensors
- If there is no water there will be some touch value and voltage in the wires
- If wires come in contact with water the voltage
V = 0
andtouch = 0
because of grounding and no voltage present in water - The controller detects this change and sends the http request to the server
- then the server sends the notification / sms / call to the user as specified
- It can help in preventing wastage of water
- Can be used as a software and hardware project
- you can make this thing solid and compact
- you can add a battery to it
- you can place it in tank at the top from inside
- use good quality wire for sustainability
Comments
Please log in or sign up to comment.