Advertisement
Treeloy

Water Warden Ethernet Version

Sep 11th, 2013
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.85 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.  
  11.  
  12.   //Ethernet Shield Version
  13.  
  14.   #include <SPI.h>
  15.   #include <Ethernet.h>    // this allows us to use the Ethernet shield easily
  16.   #include <Twitter.h>
  17.  
  18.   //Twitter info
  19.   // Your Token to Tweet (get it from http://arduino-tweet.appspot.com/)
  20.   Twitter twitter("Your-Token-Here");
  21.  
  22.   // Message to post to twitter in case of a fail
  23.   char msg[] = "Pet Water Warden Error: Please check my water!";
  24.  
  25.   //Network information (Only edit if not using default mode).
  26.   //Default mode is Auto DHCP, your router will assign an ip address.
  27.   //Only fill in ip, gateway, and subnet if not using auto DHCP
  28.   byte mac[] = {
  29.     0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; // this can be made up
  30.   byte ip[] = {
  31.     0, 0, 0, 0 }; // a free IP address on your network
  32.   byte gateway[] = {
  33.     0, 0, 0, 0 }; // the gateway address of your network
  34.   byte subnet[] = {
  35.     0, 0, 0, 0 };  // the subnet mask of your network
  36.  
  37.   //Set output pin 8 to control switchtail/pump
  38.   const int pump = 8;
  39.  
  40.   //Failsafe off
  41.   long timerA = 0;
  42.  
  43. void setup() {
  44.   // initialize serial communication at 9600 bits per second:
  45.   Serial.begin(9600);
  46.  
  47.   //Set pump output pin
  48.   pinMode(pump, OUTPUT);
  49.  
  50.   //Start with pump off
  51.   digitalWrite(pump, LOW);
  52.  
  53.  
  54.  
  55.   //If not using auto DHCP use Ethernet.begin(mac, ip, gateway, subnet);
  56.   Ethernet.begin(mac);  //begins the Ethernet connection and uses automatic DHCP
  57.   Serial.begin(9600);  // starts serial communications so we can debug easier
  58.   delay(1000);        // a 1 second delay to let everything settle down!
  59.  
  60.  
  61.  
  62.   //Test Twitter
  63.   Serial.println("Pet Water Warden testing twitter connection...");  
  64.  
  65.   if (twitter.post("Pet Water Warden is up and running!!")) {  // Twitter that we are up and running
  66.     int status = twitter.wait(&Serial);     // wait for a response from twitter                    
  67.     if (status == 200) {    // if Twitter responds 200
  68.       Serial.println(", Tweet is OK!");  // print success
  69.       Serial.println();  // print a blank line, used for debugging
  70.     }
  71.     else {
  72.       Serial.print("Tweet failed : code ");
  73.       Serial.println(status);  // print error code
  74.       Serial.println();  // print a blank line, used for debugging
  75.     }
  76.   }
  77.   else {
  78.     Serial.println("Connection to Twitter failed.");
  79.   }
  80.  
  81. }
  82.  
  83.  
  84. void loop(){
  85.  
  86.   //Read the input on A0-A1
  87.   //High and Low Sensors
  88.   int sensorLow = analogRead(A1);
  89.   int sensorHigh = analogRead(A0);
  90.  
  91.   //Convert to a voltage
  92.   float voltageLow = sensorLow * (5.0 / 1023.0);
  93.   float voltageHigh = sensorHigh * (5.0 / 1023.0);  
  94.  
  95.   //Sensor States
  96.   int lowState = 0;
  97.   int highState = 0;
  98.  
  99.   //Are the sensors on or off?
  100.   //Write states, Active Low
  101.   if (voltageLow >= 3.5){lowState = 0;}
  102.   else if (voltageLow < 3.5){lowState = 1;}
  103.  
  104.   if (voltageHigh >= 3.5){highState = 0;}
  105.   else if (voltageHigh < 3.5){highState = 1;}
  106.  
  107.   //Turn on the pump?
  108.   if(highState == 1 && lowState == 1){
  109.     digitalWrite(pump, LOW);
  110.     timerA = 0;  
  111.   }else if(highState == 0 && digitalRead(pump) == LOW){
  112.     //FailSafe Timers
  113.     timerA = 0;
  114.     digitalWrite(pump, HIGH);
  115.     timerA = millis();
  116.     Serial.print("Starting timer: ");    
  117.     Serial.println(timerA);
  118.   }
  119.  
  120.   //My pet bowl fills in about 45 sec, adjust to the size
  121.   // of your pet bowl
  122.   if( (millis() - timerA) >= 45000 && timerA != 0){
  123.     digitalWrite(pump, LOW);  
  124.     Serial.println(timerA);
  125.     Serial.println(millis());
  126.     timerA = 0;
  127.     //Either no water left or the pump didn't turn off, bad sensor?
  128.     tweetFail();
  129.     Serial.println("Either no water left or the pump didn't turn off, bad sensor?");
  130.   }
  131.  
  132.  
  133.   //Debug Prints
  134.   Serial.print("Low Sensor: ");
  135.   Serial.println(lowState);
  136.   Serial.print("High Sensor: ");
  137.   Serial.println(highState);
  138.  
  139.   //Check Sensors every 10 sec
  140.   delay(10000);
  141.  
  142.  
  143. }
  144.  
  145.  
  146. void tweetFail(){
  147.     if (twitter.post(msg)) {  // Twitter that we are up and running
  148.     int status = twitter.wait(&Serial);     // wait for a response from twitter                    
  149.     if (status == 200) {    // if Twitter responds 200
  150.       Serial.println(", Tweet is OK!");  // print success
  151.       Serial.println();  // print a blank line, used for debugging
  152.     }
  153.     else {
  154.       Serial.print("Tweet failed : code ");
  155.       Serial.println(status);  // print error code
  156.       Serial.println();  // print a blank line, used for debugging
  157.       //If Tweet fails try again
  158.       tweetFail();
  159.     }
  160.   }
  161.   else {
  162.     Serial.println("Connection to Twitter failed.");
  163.   }
  164. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement