SMS Flood Alerter
SMS Flood Alerter
0
Project description
ABOUT THIS PROJECT

The Sensor:
Setup needed:
A?Twilio account. Register at?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); }