SMS FloodAlerter
An apparatus for your basement that sends you a text when a flood is detected.
Components and supplies
1
Arduino Ethernet Shield 2
1
Arduino UNO
1
Water Sensor
Apps and platforms
1
ThingSpeak API
1
SMS Messaging API
Project description
Code
Arduino SMS Sketch
arduino
Send SMS from Arduino over the Internet using Ethernet Shield
1/* 2Send SMS from Arduino over the Internet using Ethernet Shield 3 4*/ 5 6 7#include <SPI.h> 8 9#include <Ethernet.h> //to use the eithent shield 10 11// Enter a MAC address for your controller below. 12byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; 13 14 15//if DHCP fails, use a static IP 16// within proveder's router LAN!!! 17IPAddress ip(**ENTER IP ADDRESS**); //ENTER YOUR IP ADDRESS HERE!! 18 19// Initialize the Ethernet client library 20EthernetClient client; 21 22//thingspeak serverTS 23char serverTS[] = "api.thingspeak.com"; 24 25//API key for the Thingspeak ThingHTTP already configured 26const String apiKey = "your TS api key"; 27 28//the RECIPIENT phone number the message should be sent to 29const String sendToNumber = "+your number"; 30// 31// digital pins 32/* Arduino uses digital pins 10, 11, 12, and 13 (SPI) 33 to communicate with the W5100 on the ethernet shield. 34 These pins cannot be used for general i/o. 35 36*/ 37int sensor = 7; // the pin that the sensor is atteched to 38bool flood = false; 39int val = 0; 40// 41// output pins to visualize 42// important states: 43int networkOK = 8; // green LED 44int networkDown = 9; // RED LED 45int smsSent = 6; // Yellow LED 46int sentCount=0; 47 48 bool etherNet = false; 49 // 50void setup() 51{ 52 pinMode(sensor, INPUT); // initialize sensor as an input' 53 pinMode(networkOK, OUTPUT); 54 pinMode(networkDown, OUTPUT); 55 pinMode(smsSent, OUTPUT); 56 57 Serial.begin(9600); 58 59 // connect the device to wired LAN 60 setupEthernet(); 61} 62//------------------------------------------------- 63void loop() 64{ 65 if ( etherNet == false ) 66 { 67 Serial.println("Check Ethernet..."); 68 return; 69 } 70 // 71 // device is on the network 72 // continue 73 val = digitalRead(sensor); // read sensor value 74 // check if the sensor is HIGH 75 if (val == HIGH) 76 { 77 // flood occured! 78 // send SMS right away 79 80 // send up to 2 text messages! 81 if (sentCount < 2) 82 { 83 sendSMS(sendToNumber, "My basement is flooded!"); 84 sentCount += 1; 85 86 } 87 if (flood == false) { 88 Serial.println("Basement is flooded!"); 89 flood = true; 90 } 91 92 }// val testing 93 94} 95 96//------------------------------------------------------------ 97// 98void sendSMS(String number,String message) 99{ 100 // Make a TCP connection to remote host 101 if (client.connect(serverTS, 80)) 102 { 103 Serial.println("Connected...."); 104 delay(2000); 105 106 //should look like this... 107 //api.thingspeak.com/apps/thinghttp/send_request?api_key={api key}&number={send to number}&message={text body} 108 109 client.print("GET /apps/thinghttp/send_request?api_key="); 110 client.print(apiKey); 111 client.print("&number="); 112 client.print(number); 113 client.print("&message="); 114 client.print(message); 115 116 Serial.println("msg:"+message); 117 118 client.println(" HTTP/1.1"); 119 client.print("Host: "); 120 client.println(serverTS); 121 delay(11); 122 client.println("Connection: close"); 123 client.println(); 124 } 125 else 126 { 127 Serial.println(F("Connection failed")); 128 } 129 130 String reply =""; 131 // Check for a response from the server, and route it 132 // out the serial port. 133 while (client.connected()) 134 { 135 if ( client.available() ) 136 { 137 char c = client.read(); 138 Serial.print(c); 139 //------------------------------------------- 140 reply = String(reply + c); 141 if(reply.indexOf("><DateSent/><") > 0) 142 { smsSent = HIGH; } 143 144 if (reply.length() > 400) 145 { reply=""; } 146 //-------------------------------------------------- 147 } 148 } 149 Serial.println(); 150 if(smsSent == HIGH) 151 { Serial.println("Text message sent successfully."); } 152 else 153 { 154 Serial.println("!!!!!!!!! no Text message sent"); 155 //Serial.println(reply); 156 Serial.println("=================="); 157 } 158 Serial.println(); 159 client.stop(); 160} 161//--------------------------------------------------- 162// 163void setupEthernet() 164{ 165 etherNet = true; 166 //-------------------------------------------------- 167 // 168 // check for Ethernet cable 169 // 170 if (Ethernet.linkStatus() == LinkOFF) 171 { 172 etherNet = false; 173 Serial.println("Ethernet cable is not connected."); 174 return; 175 } 176 //------------------------------------------------------- 177 // 178 Serial.println("Setting up Ethernet..."); 179 // start the Ethernet connection: 180 if (Ethernet.begin(mac) == 0) { 181 Serial.println(F("Failed to configure Ethernet using DHCP")); 182 // no point in carrying on, so do nothing forevermore: 183 // try to congifure using IP address instead of DHCP: 184 Ethernet.begin(mac, ip); 185 } 186 Serial.print("My IP address: "); 187 Serial.println(Ethernet.localIP()); 188 // give the Ethernet shield a second to initialize: 189 delay(1000); 190} 191
Comments
Only logged in users can leave comments