Advertisement
Treeloy

Water Warden WiFi Version

Aug 13th, 2013
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.03 KB | None | 0 0
  1.   /************************************************
  2.   * Author: Eloy Salinas
  3.   * Project: Pet Water Warden
  4.   * Company: Make Magazine // RadioShack
  5.   * Date: 8/8/13
  6.   * Version: 1.0
  7.   * http://makezine.com/projects/petwaterwarden/
  8.   *************************************************/
  9.   //Twitter capabilities thanks to http://arduino-tweet.appspot.com/
  10.   //Wifi Shield Twitter thanks to https://github.com/gauravg11/arduinolibs
  11.  
  12.   #include <SPI.h>
  13.   #include <WiFi.h>
  14.   #include <Twitter.h>
  15.  
  16.   //Twitter info
  17.   // Your Token to Tweet (get it from http://arduino-tweet.appspot.com/)
  18.   Twitter twitter("Your-Token-Here");
  19.  
  20.   // Message to post to twitter in case of a fail
  21.   char msg[] = "Pet Water Warden Error: Please check my water!";
  22.  
  23.   //Network information
  24.   char ssid[] = "SSID";     //  your network SSID (name)
  25.   char pass[] = "Password";  // your network password
  26.   int status = WL_IDLE_STATUS;     // the Wifi radio's status
  27.  
  28.   //Set output pin 8 to control switchtail/pump
  29.   const int pump = 8;
  30.  
  31.   //Failsafe off
  32.   long timerA = 0;
  33.  
  34. void setup() {
  35.   // initialize serial communication at 9600 bits per second:
  36.   Serial.begin(9600);
  37.  
  38.   //Set pump output pin
  39.   pinMode(pump, OUTPUT);
  40.  
  41.   //Start with pump off
  42.   digitalWrite(pump, LOW);
  43.  
  44.   // check for the presence of the shield:
  45.   if (WiFi.status() == WL_NO_SHIELD) {
  46.     Serial.println("WiFi shield not present");
  47.     // don't continue:
  48.     while(true);
  49.   }
  50.  
  51.   // attempt to connect to Wifi network:
  52.   while ( status != WL_CONNECTED) {
  53.     Serial.print("Attempting to connect to WPA SSID: ");
  54.     Serial.println(ssid);
  55.     // Connect to WPA/WPA2 network:    
  56.     status = WiFi.begin(ssid, pass);
  57.    
  58.     // wait 10 seconds for connection:
  59.     delay(10000);
  60.   }
  61.  
  62.   printWifiData();
  63.  
  64.  
  65.  
  66.   //Test Twitter
  67.   Serial.println("Pet Water Warden testing twitter connection...");  
  68.  
  69.   if (twitter.post("Pet Water Warden is up and running!!")) {  // Twitter that we are up and running
  70.     int status = twitter.wait(&Serial);     // wait for a response from twitter                    
  71.     if (status == 200) {    // if Twitter responds 200
  72.       Serial.println(", Tweet is OK!");  // print success
  73.       Serial.println();  // print a blank line, used for debugging
  74.     }
  75.     else {
  76.       Serial.print("Tweet failed : code ");
  77.       Serial.println(status);  // print error code
  78.       Serial.println();  // print a blank line, used for debugging
  79.     }
  80.   }
  81.   else {
  82.     Serial.println("Connection to Twitter failed.");
  83.   }
  84.  
  85. }
  86.  
  87.  
  88. void loop(){
  89.  
  90.   //Read the input on A0-A1
  91.   //High and Low Sensors
  92.   int sensorLow = analogRead(A1);
  93.   int sensorHigh = analogRead(A0);
  94.  
  95.   //Convert to a voltage
  96.   float voltageLow = sensorLow * (5.0 / 1023.0);
  97.   float voltageHigh = sensorHigh * (5.0 / 1023.0);  
  98.  
  99.   //Sensor States
  100.   int lowState = 0;
  101.   int highState = 0;
  102.  
  103.   //Are the sensors on or off?
  104.   //Write states, Active Low
  105.   if (voltageLow >= 3.5){lowState = 0;}
  106.   else if (voltageLow < 3.5){lowState = 1;}
  107.  
  108.   if (voltageHigh >= 3.5){highState = 0;}
  109.   else if (voltageHigh < 3.5){highState = 1;}
  110.  
  111.   //Turn on the pump?
  112.   if(highState == 1 && lowState == 1){
  113.     digitalWrite(pump, LOW);
  114.     timerA = 0;  
  115.   }else if(highState == 0 && digitalRead(pump) == LOW){
  116.     //FailSafe Timers
  117.     timerA = 0;
  118.     digitalWrite(pump, HIGH);
  119.     timerA = millis();
  120.     Serial.print("Starting timer: ");    
  121.     Serial.println(timerA);
  122.   }
  123.  
  124.   //My pet bowl fills in about 45 sec, adjust to the size
  125.   // of your pet bowl
  126.   if( (millis() - timerA) >= 45000 && timerA != 0){
  127.     digitalWrite(pump, LOW);  
  128.     Serial.println(timerA);
  129.     Serial.println(millis());
  130.     timerA = 0;
  131.     //Either no water left or the pump didn't turn off, bad sensor?
  132.     tweetFail();
  133.     Serial.println("Either no water left or the pump didn't turn off, bad sensor?");
  134.   }
  135.  
  136.  
  137.   //Debug Prints
  138.   Serial.print("Low Sensor: ");
  139.   Serial.println(lowState);
  140.   Serial.print("High Sensor: ");
  141.   Serial.println(highState);
  142.  
  143.   //Check Sensors 10 sec
  144.   delay(10000);
  145.  
  146.  
  147. }
  148.  
  149.  
  150. void printWifiData() {
  151.   // print your WiFi shield's IP address:
  152.   IPAddress ip = WiFi.localIP();
  153.     Serial.print("IP Address: ");
  154.   Serial.println(ip);
  155.  
  156.   // print the SSID of the network you're attached to:
  157.   Serial.print("SSID: ");
  158.   Serial.println(WiFi.SSID());
  159.  
  160. }
  161.  
  162. void tweetFail(){
  163.     if (twitter.post(msg)) {  // Twitter that we are up and running
  164.     int status = twitter.wait(&Serial);     // wait for a response from twitter                    
  165.     if (status == 200) {    // if Twitter responds 200
  166.       Serial.println(", Tweet is OK!");  // print success
  167.       Serial.println();  // print a blank line, used for debugging
  168.     }
  169.     else {
  170.       Serial.print("Tweet failed : code ");
  171.       Serial.println(status);  // print error code
  172.       Serial.println();  // print a blank line, used for debugging
  173.       //If Tweet fails try again
  174.       tweetFail();
  175.     }
  176.   }
  177.   else {
  178.     Serial.println("Connection to Twitter failed.");
  179.   }
  180. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement