Introduction: Water Alarm

After our washing machine ruined our Kitchen floors I decided to see if I could make an alarm to detect the water.

Step 1: Redback

Started with this little wifi arduino device. It is sold by Linksprite.com I was able to get a good deal on one from Ebay.

You can use a wifi shield or any wifi board that will work on Arduino.

I did struggle for a while getting the Redback to work at all as the documentation is non existent. They provide a few tutorial/examples but no real docs for the code.

Wired up an LED on pin 9 as that's the one that the Redback uses to show it's online, then a red led on analog pin 2 but using in the code as a digital pin.

This sensor has digital and analog outputs. I'm only using the digital.

Basically we get logged into router, then wait till the water sensor is HIGH and then go into a loop where we send the email to a PHP page on my server that will send the text message.

The PHP code is pretty simple I used a "magic number" as a security measure. The PHP will not send email without the proper number. This litte trick lets you send SMS message via ATT you sould be able to do the same for any wireless carrier

PHP code

<?
$magic=$_GET[magic];
$message=$_GET[message];
$message=urlDecode(stripSlashes($message));
if ($magic=="0000"){
$ok=mail("myphonenumber@txt.att.net","Water Detected",
			$message , "FROM:water@mydomain.com");
			
echo "email sent\n ".$message;			
echo $ok;
}
?>

Replace the 0000 with a code you want

Replace myphonenumber with your cell number

in the FROM use whatever email you want

This code is saved on a webserver.

Before using the Arduino code you will need the libraries for the RedBack from
Redback forum

/*Code (C)2013 Bret Lanius
bret@bretlanius.com http://bretlanius.com
Hardware is running on Redback from linksprite.com
using Water sensor bought on ebay
*/
#include 
#define WIRELESS_MODE_INFRA	1
#define WIRELESS_MODE_ADHOC	2
// Wireless configuration parameters ----------------------------------------
unsigned char local_ip[] = {192,168,1,35};	// IP address of WiShield
unsigned char gateway_ip[] = {192,168,1,254};	// router or gateway IP address
unsigned char subnet_mask[] = {255,255,255,0};	// subnet mask for the local network
const prog_char ssid[] PROGMEM = {"YourSSID"};		// max 32 bytes
unsigned char security_type = 3;	// 0 - open; 1 - WEP; 2 - WPA; 3 - WPA2
// WPA/WPA2 passphrase
const prog_char security_passphrase[] PROGMEM = {"yourpassword for router"};	// max 64 characters
// WEP 128-bit keys
// sample HEX keys
prog_uchar wep_keys[] PROGMEM = {	0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d,	// Key 0
					0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,	0x00,	// Key 1
					0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,	0x00,	// Key 2
					0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,	0x00	// Key 3
								};
// setup the wireless mode
// infrastructure - connect to AP
// adhoc - connect to another WiFi device
unsigned char wireless_mode = WIRELESS_MODE_INFRA;
unsigned char ssid_len;
unsigned char security_passphrase_len;
//---------------------------------------------------------------------------
const byte waterPin=7;
const byte redPin=A2;
const byte waterAnalogPin=0;
const byte resetPin=5;
int beenhere=false;
void printData(char* data, int len) {
  // Print the data returned by the server
  // Note that the data is not null-terminated, may be broken up into smaller packets, and 
  // includes the HTTP header. 
  while (len-- > 0) {
    Serial.print(*(data++));
  } 
}
uint8 bret_ip[]={198,xx,xx,xx};
GETrequest getAlert(bret_ip, 80, "www.yourserver.com", "/wateralert.php?magic=0000&message=Water%20Detected");
void setup() {
 
  Serial.begin(57600);
  Serial.println("StartUp");
  WiServer.init(NULL);
  WiServer.enableVerboseMode(true);
  Serial.println("Wifi enabled");
  pinMode(waterPin,INPUT);
  pinMode(redPin,OUTPUT);
  pinMode(resetPin,INPUT_PULLUP);
  digitalWrite(redPin,LOW);
  getAlert.setReturnFunc(printData);
 
}
void loop() {
  if (beenhere==true){
    Serial.println("Back in main loop");
    beenhere=false;
  }
  int water=digitalRead(waterPin);  
  
    if (water==LOW){
      Serial.println("ALERT");
     
      alert();
   }
 
  delay(10);
  WiServer.server_task();
}
void noAlert(){
  noTone(6);
  digitalWrite(redPin,LOW);
  beenhere=true;
}
void reset(){
  Serial.println("Silenced");
  noTone(6);
  delay(500);
 
  while(digitalRead(resetPin)==HIGH){
    digitalWrite(redPin,HIGH);
    delay(500);
    digitalWrite(redPin,LOW);
    delay(500);
  }
  Serial.println("Clearing alarm");
  delay(100);
 
  noAlert();
  WiServer.server_task();
}
void alert(){
      tone(6,3000);
      delay(500);
      Serial.println("sending email");
      getAlert.submit();
    
        while(digitalRead(resetPin)==HIGH){
          WiServer.server_task();
          digitalWrite(redPin,HIGH);
          delay(50);
          digitalWrite(redPin,LOW);
          delay(50);
        }
        
       reset();
   }