Hello, I am new to Arduino and does not have software background. I need a system for my water tank.
Objective: When ever water level in the underground tank reaches to certain low level, circuit/system should send sms alert to predefine mobile number. But it should not keep sending sms and flood the inbox.
Can you please help me with this task and how it can be achielved using arduino ultrasonic sensor or any switch.
a float with a switch is probably the simplest way to sense the water level
when the switch is triggered you send the SMS and set a flag to indicate it has been sent.
you clear the flag when the water level drops
e.g. in pseudo code
IF(waterLevelIsHeigh AND messageSent is FALSE)
send SMS
messageSent=TRUE
ELSE
messageSent=FALSE
waterLevelIsHeigh is the level switch returns TRUE if the level is too heigh
you should probably put some hysteresis on the switch high/low test otherwise you may get it switching on/off multiple times at changeover, in particular if there are waves in the tank (it would also remove any switch bounce)
IF(waterLevelIsHeigh AND messageSent is FALSE)
send SMS
messageSent=TRUE
ELSE
messageSent=FALSE
because the ELSE will be triggered when the level is high and the messageSent is true - immediately causing another message to be sent.
I try to avoid using ELSE clauses with compound IF clauses. This style of code makes the relationships clearer
IF(waterLevelIsHeigh ) {
IF messageSent is FALSE) {
send SMS
messageSent=TRUE
}
}
ELSE {
messageSent=FALSE
}
Also, I suspect there should be a completely separate test for setting messageSent back to FALSE - for example when the water level reaches a different threshold.
Finally, but not really relevant to the code, I think the OP is concerned with a LOW water level.
Thanks, but is this the full code ? Can the following be alter..... I can place a switch or can place two wires in the tank at low level. When this wries gets open (when there is no water) send the sms.
Arduino Program:
int PIN = 7;// attach sensor to pin 7
int sensor = 0;
void setup()
{
pinMode (PIN,INPUT);
Serial.begin(9600); // gsm baud rate
delay(5000);
}
void loop()
{
sensor = digitalRead (PIN); // read whether water is presented or not
if (sensor == LOW){ // if water is not presented send a message
Serial.println("AT");
delay(1000);
Serial.println("AT+CMGF=1"); // send SMS in text mode
delay(1000);
Serial.println("AT+CMGS="+01XXXX""); //CHANGE TO Number , you'd like to receive message
delay(1000);
Serial.print("Water level is low in underground tank (attention required)"); // content of the message
Serial.write(26); // ctrl+z ASCII code
delay(18000000); // Wait for 5 hrs before next reading
}
}
Best use a proper float switch, which is designed to be submerged all the time and work well. Two wires may work but not reliable in the long run due to corrosion and debris (may stick to the wires so you don't realise it's empty).
When posting code, please add code tags - the </> button in the full editor.
somairm:
Thanks, but is this the full code ? Can the following be alter..... I can place a switch or can place two wires in the tank at low level. When this wries gets open (when there is no water) send the sms.
in several projects where water levels in tanks was required a 0 to 5volt 1KHz square wave was generated (using timer interrupts) which via a suitable circuit applied an AC signal to an electrode. When the generated signal was high (5V output) the state of the other electrode was read to determine level.
1 kHz is at the very low end where this works.
I'm using this trick, target frequencies are 3-300 kHz - so you have to accurately measure times as short as 1-2 microseconds at the high end of your range. Higher frequencies also don't work (and not just because you can't time them properly - the liquid doesn't react linearly any more).
If the height between full and empty is less than 4-5m, you could use a hc-sr04 range sensor. I grabbed some on ebay with brackets for a few dollars for this purpose.
just buy two float sensor's like this float sensor and treat it like a button then simply use a int or boolean for a lock, i made you a sketch based on your code which should work as is.
/*-----( Setup function )-----*/
void setup() {
Serial.begin(9600);
pinMode(8, INPUT_PULLUP);
pinMode(9, INPUT_PULLUP);
}
/*-----( Loop function )-----*/
void loop() {
boolean run_cycle = true;
const int lowerSersor = digitalRead(8); // lower float sensor on digital pin 8
const int topSensor = digitalRead(9); // top float sensor on digital pin 9
if (lowerSersor == LOW) { // float sensor has dropped down - tank empty
// Toggle on
if (run_cycle) {
sendSMS();
run_cycle = false;
}
}
if (topSensor == LOW) { // tank now full again - this float sensor is upside down so it float when the tank is full
// Toggle off
if (run_cycle == false) {
run_cycle = true;
}
}
}
void sendSMS() {
// Some code here to send a sms
Serial.println("AT");
delay(1000);
Serial.println("AT+CMGF=1"); // send SMS in text mode
delay(1000);
Serial.println("AT+CMGS=\"+01XXXX\""); //CHANGE TO Number , you'd like to receive message
delay(1000);
Serial.print("Water level is low in underground tank (attention required)"); // content of the message
Serial.write(26); // ctrl+z ASCII code
}
Just as a note though, i have never used a gsm module to send a sms so im unsure of your setup for communication i.e. Serial.begin(9600); and Serial.println("AT"); but if it's like a esp8266 it and you're using a uno it should be:
#include "SoftwareSerial.h"
SoftwareSerial Serial1(6, 7); // RX, TX on digital pins 6 and 7
/*-----( Setup function )-----*/
void setup() {
Serial1.begin(9600); // initialize serial for SMS module
Serial.begin(115200); // debugging open serial monitor and set to 115200 baud rate for debugging text
pinMode(8, INPUT_PULLUP);
pinMode(9, INPUT_PULLUP);
}
/*-----( Loop function )-----*/
void loop() {
boolean run_cycle = true;
const int lowerSersor = digitalRead(8); // lower float sensor on digital pin 8
const int topSensor = digitalRead(9); // top float sensor on digital pin 9
if (lowerSersor == LOW) { // float sensor has dropped down - tank empty
// Toggle on
if (run_cycle) {
sendSMS();
run_cycle = false;
Serial.println(F("tank low, sending sms")); // Serial.println(F saves the text in the flash to save space
}
}
if (topSensor == LOW) { // tank now full again - this float sensor is upside down so it float when the tank is full
// Toggle off
if (run_cycle == false) {
run_cycle = true;
Serial.println(F("tank full"));
}
}
}
void sendSMS() {
// Some code here to send a sms
Serial1.println(F("AT"));
delay(1000);
Serial1.println(F("AT+CMGF=1")); // send SMS in text mode
delay(1000);
Serial1.println(F("AT+CMGS=\"+01XXXX\"")); //CHANGE TO Number , you'd like to receive message
delay(1000);
Serial1.print(F("Water level is low in underground tank (attention required)")); // content of the message
Serial1.write(26); // ctrl+z ASCII code
Serial.println(F("SMS sent !"));
}
But just look at the sms example for your module and copy that as im unsure as i said.
Thanks for the code. I am not fanatic to use GSM module, if you have better idea i.e Internet of thing, to send message to mobile phone please let me know.
Here is the link of GSM module i have . Thsi is the same GSM i am using.... so I think I can use the same code which this guy used in his program. If not then can you please rewrite the code for me.
Yes Twillo is just a web hook and therefore you just need to access the url, which i have used before, i have not used a Yun before though but there is nothing special about the codes. But the first script uses the normal Serial as is just like your SMS script. and the second uses software serial to send over tr and tx as i assumed the wifi SMS module was external.
In your code you use two sensors ? top and low sensors ? connected to pin 8 and pin 9 of uno.
I think if i use only one floot sensor at the bottom of tank, as the floot is up it mean tank is full and as soon the floot is down it mean tank is empty, this will serve my purpose as i do not need indication that tank is full.
/-----( Loop function )-----/
void loop() {
boolean run_cycle = true;
const int lowerSersor = digitalRead(8); // lower float sensor on digital pin 8
const int topSensor = digitalRead(9); // top float sensor on digital pin 9
if (lowerSersor == LOW) { // float sensor has dropped down - tank empty
// Toggle on
if (run_cycle) {
sendSMS();
run_cycle = false;
}
}
if (topSensor == LOW) { // tank now full again - this float sensor is upside down so it float when the tank is full
// Toggle off
if (run_cycle == false) {
run_cycle = true;
}
}
}
void sendSMS() {
// Some code here to send a sms
Serial.println("AT");
delay(1000);
Serial.println("AT+CMGF=1"); // send SMS in text mode
delay(1000);
Serial.println("AT+CMGS="+01XXXX""); //CHANGE TO Number , you'd like to receive message
delay(1000);
Serial.print("Water level is low in underground tank (attention required)"); // content of the message
Serial.write(26); // ctrl+z ASCII code
}
For just one sensor just check if the bottom float sensor low or high - up or down.
/*-----( Setup function )-----*/
void setup() {
Serial.begin(9600);
pinMode(8, INPUT_PULLUP);
}
/*-----( Loop function )-----*/
void loop() {
boolean run_cycle = true;
const int lowerSersor = digitalRead(8); // lower float sensor on digital pin 8
if (lowerSersor == LOW) { // float sensor has dropped down - tank empty
// Toggle on
if (run_cycle) {
sendSMS();
run_cycle = false;
}
}
if (lowerSensor == HIGH) { // tank now full
// Toggle off
if (run_cycle == false) {
run_cycle = true;
}
}
}
void sendSMS() {
// Some code here to send a sms
Serial.println("AT");
delay(1000);
Serial.println("AT+CMGF=1"); // send SMS in text mode
delay(1000);
Serial.println("AT+CMGS=\"+01XXXX\""); //CHANGE TO Number , you'd like to receive message
delay(1000);
Serial.print("Water level is low in underground tank (attention required)"); // content of the message
Serial.write(26); // ctrl+z ASCII code
}
look up hysteresis. the reason you need to have an upper level sensor and a lower level sensor is to prevent the pump from starting and stopping too frequently.
as I would anticipate your project,
one upper level sensor to tell the pump to shut off and to indicate your tank is at maximum.
one lower START level to start the pump.
one EMPTY sensor to send your alarm report.
as a note, you can save the start time, the run time, the stop time, etc, in memory and if you can remote access, you can read that data.
Personally, I would add some additional sensors at the pump.
1 for water level high enough to pump
1 for no water available at pump
1 for pump running
the pins are free. the software can handle them and the sensors are very low cost.
Oh, I am not attaching a pump with this project... there is a separate circuit for starting and switching off the pump.
I will install a new float sensor in to the tank and the lower heigh, just to get notification that now water level in the underground tank is low..... so that I can order a new payload i.e water from water company..... or I will add their mobile number so that they will receive sms and send the water tanker at my home.