---
title: "SMS Flood Alerter"
url: "https://maker.wiznet.io/nagamma06/projects/SMS-Flood-Alerter/"
markdown_url: "https://maker.wiznet.io/nagamma06/projects/SMS-Flood-Alerter/md"
type: "UCC: User Created Content"
author: "nagamma06"
author_url: "https://maker.wiznet.io/nagamma06/"
original_author: "nagamma06"
original_url: "https://create.arduino.cc/projecthub/alexhenrylee/sms-floodalerter-2b8ec6?ref=part&ref_id=31780&offset=52"
published: "2021-12-01"
language: "en"
likes: 0
views: 301
comments: 0
source: "WIZnet Makers (https://maker.wiznet.io/)"
---

# SMS Flood Alerter

> SMS Flood Alerter

Original author: nagamma06 (source: https://create.arduino.cc/projecthub/alexhenrylee/sms-floodalerter-2b8ec6?ref=part&ref_id=31780&offset=52)

## Article

## ABOUT THIS PROJECTI.Introduction** Disaster has struck- a boiler leakage has flooded the basement. However, I am currently grocery shopping and am too far away to instantly be aware of the circumstances. OH NO!!! The goal of this project was to develop some form of a communication line between my house and myself to notify me that something's wrong. In order to maximize efficiency, I created this project with these points in mind: 1) I would like some sort of ultra-fast connection via SMS, and for its service to be free. 2) After the disaster occurs, the Arduino sketch should easily connect to the Internet without any special libraries other than the standard Ethernet communication libraries (I.E. More efficient and simpler code). **The**?**product:**?An Arduino-powered water sensor that can quickly notify me through text if something floods at home by connecting to the internet via a WiFi module or an?**EthernetShield**. **II:**?**Important**?**HardwareThe Arduino EthernetShield?**connects the Arduino to the internet in mere minutes.

![Simply plug this module onto the Arduino board and connect it to the wired network with an RJ45 cable.](https://hackster.imgix.net/uploads/attachments/1160372/image_rfJU40W1f3.png?auto=compress%2Cformat&w=680&h=510&fit=max)

*Simply plug this module onto the Arduino board and connect it to the wired network with an RJ45 cable.*

I opted to use a wired connection over WiFi because the distance of the flood to the WiFi router may be too far for a stable wireless connection between the two. The wired connection always is more stable, and not to mention faster. The house powerlines will be able to establish a wired connection from?**The**?**Sensor**?to the provider's router.

### The Sensor:

![Water sensor that is connected to Arduino.](https://hackster.imgix.net/uploads/attachments/1160377/water_GGpNUav4Wd.PNG?auto=compress%2Cformat&w=680&h=510&fit=max)

*Water sensor that is connected to Arduino.*

This sensor is used for water detection and can monitor a multitude of variables such as: 1)?**Rainfall** 2)?**Water level** 3)?**Water leakage** 4)?**Water**?**tank overflow** This sensor can easily convert the water size to an?**analog signal**, whose output analog value can directly be implemented into the program to achieve the desired function. It simply gives the Arduino the signal of whether to text the homeowner or not. **III.SMS** There are many ways to approach sending an SMS from an Arduino using the Internet. However, the easiest method is to split the job between Arduino and an online service like?**ThingSpeak**?which passes the SMS request to a second service?**Twilio**. The latter integrates the Internet with the telephony infrastructure.

### Setup needed:

A?**Twilio account**. Register at?[https://www.twilio.com/try-twilio.](https://www.twilio.com/try-twilio)After signing up, verify the Twilio number. A?**Thingspeak**?account. Register at?<https://thingspeak.com/users/sign_up> The TS account must point to the?**Twilio**?account. CODE:

```
/*
Send SMS from Arduino over the Internet using Ethernet Shield

*/


#include <SPI.h>

#include <Ethernet.h> //to use the eithent shield

// Enter a MAC address for your controller below.
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };


//if DHCP fails, use a static IP
// within proveder's router LAN!!!
IPAddress ip(**ENTER IP ADDRESS**); //ENTER YOUR IP ADDRESS HERE!!

// Initialize the Ethernet client library
EthernetClient client;

//thingspeak serverTS
char serverTS[] = "api.thingspeak.com";

//API key for the Thingspeak ThingHTTP already configured
const String apiKey = "your TS api key";

//the RECIPIENT phone number the message should be sent to
const String sendToNumber = "+your number";
//
// digital pins
/* Arduino uses digital pins 10, 11, 12, and 13 (SPI) 
   to communicate with the W5100 on the ethernet shield.
   These pins cannot be used for general i/o. 
   
*/
int sensor = 7;              // the pin that the sensor is atteched to
bool flood = false;            
int val = 0;
// 
// output pins to visualize 
// important states:
int networkOK = 8;           // green LED
int networkDown = 9;         // RED LED
int smsSent = 6;             // Yellow LED
int sentCount=0;

 bool etherNet = false;
 //
void setup()
{
   pinMode(sensor, INPUT);    // initialize sensor as an input'
   pinMode(networkOK, OUTPUT);
   pinMode(networkDown, OUTPUT);
   pinMode(smsSent, OUTPUT);
  
  Serial.begin(9600);

  // connect the device to wired LAN
  setupEthernet();
}
//-------------------------------------------------
void loop()
{
  if ( etherNet == false )
  {
     Serial.println("Check Ethernet...");
     return;
  }
  //
  // device is on the network
  // continue
   val = digitalRead(sensor);   // read sensor value
   // check if the sensor is HIGH
   if (val == HIGH) 
   {           
     // flood occured!
     // send SMS right away

     // send up to 2 text messages!
     if (sentCount < 2)
     {
        sendSMS(sendToNumber, "My basement is flooded!");
        sentCount += 1;
       
     }
     if (flood == false) {
       Serial.println("Basement is flooded!"); 
       flood = true;        
     } 
         
  }// val testing
 
}

//------------------------------------------------------------
//
void sendSMS(String number,String message)
{
  // Make a TCP connection to remote host
  if (client.connect(serverTS, 80))
  {
    Serial.println("Connected....");
    delay(2000);
 
    //should look like this...
    //api.thingspeak.com/apps/thinghttp/send_request?api_key={api key}&number={send to number}&message={text body}

    client.print("GET /apps/thinghttp/send_request?api_key=");
    client.print(apiKey);
    client.print("&number=");
    client.print(number);
    client.print("&message=");
    client.print(message);

     Serial.println("msg:"+message);
     
    client.println(" HTTP/1.1");
    client.print("Host: ");
    client.println(serverTS);
    delay(11);
    client.println("Connection: close");
    client.println();
  }
  else
  {
    Serial.println(F("Connection failed"));
  } 

  String reply ="";
  // Check for a response from the server, and route it
  // out the serial port.
  while (client.connected())
  {
    if ( client.available() )
    {
      char c = client.read();
      Serial.print(c);
      //-------------------------------------------
      reply = String(reply + c);
      if(reply.indexOf("><DateSent/><") > 0)
      { smsSent = HIGH; }
             
      if (reply.length() > 400)
      { reply="";  }  
      //--------------------------------------------------    
    }
  }
  Serial.println();
  if(smsSent == HIGH)
  { Serial.println("Text message sent successfully.");  }
  else
  {
     Serial.println("!!!!!!!!! no Text message sent");
     //Serial.println(reply);  
     Serial.println("==================");   
  }
  Serial.println();
  client.stop();
}
//---------------------------------------------------
//
void setupEthernet()
{
    etherNet = true;
    //--------------------------------------------------
   //
    // check for Ethernet cable
    //
    if (Ethernet.linkStatus() == LinkOFF)
    {
      etherNet = false;
      Serial.println("Ethernet cable is not connected.");
      return;
    }
     //-------------------------------------------------------
    //
    Serial.println("Setting up Ethernet...");
    // start the Ethernet connection:
     if (Ethernet.begin(mac) == 0) {
       Serial.println(F("Failed to configure Ethernet using DHCP"));
       // no point in carrying on, so do nothing forevermore:
       // try to congifure using IP address instead of DHCP:
        Ethernet.begin(mac, ip);
     }
     Serial.print("My IP address: ");
     Serial.println(Ethernet.localIP());
      // give the Ethernet shield a second to initialize:
     delay(1000);
}
```

---

Source: https://maker.wiznet.io/nagamma06/projects/SMS-Flood-Alerter/
