So this weekend I had some spare time and about a month ago I ordered some switches that would toggle if the lever was pushed. I have six pigs and one feeder with four lids. I thought wouldn’t it be fun to put these switches in the feeder and capture when the lid closed.



I decided to use a spark core with wifi, since I have outdoor wifi on my farm. I used the analog ports. Two for each switch, one in AnalogWrite and one in AnalogRead, when the switch goes up a fake timer starts in the spark. When the lid closes the connection is broken again and the information is sent back to one of my servers. The server has all the oauth information so I can post my tweet.

Here is the code on the spark. The ip address has been changed to protect the innocent.

// Define the pins we're going to call pinMode on int output = A0 ; // You'll need to wire an LED to this one to see it blink. int input = A1 ; // This one is the built-in tiny one to the right of the USB jack int output2 = A2 ; // You'll need to wire an LED to this one to see it blink. int input2 = A3 ; int output3 = A4 ; // You'll need to wire an LED to this one to see it blink. int input3 = A5 ; int output4 = A6 ; // You'll need to wire an LED to this one to see it blink. int input4 = A7 ; int feeder1last = 0 ; int feeder1timer = 0 ; int feeder2last = 0 ; int feeder2timer = 0 ; int feeder3last = 0 ; int feeder3timer = 0 ; int feeder4last = 0 ; int feeder4timer = 0 ; TCPClient client ; byte server [ ] = { x , x , x , x } ; // This routine runs only once upon reset void setup ( ) { pinMode ( output , OUTPUT ) ; pinMode ( input , INPUT ) ; analogWrite ( output , 255 ) ; pinMode ( output2 , OUTPUT ) ; pinMode ( input2 , INPUT ) ; analogWrite ( output2 , 255 ) ; pinMode ( output3 , OUTPUT ) ; pinMode ( input3 , INPUT ) ; analogWrite ( output3 , 255 ) ; pinMode ( output4 , OUTPUT ) ; pinMode ( input4 , INPUT ) ; analogWrite ( output4 , 255 ) ; Serial. begin ( 9600 ) ; } void loop ( ) { //Serial.println(analogRead(input4)); if ( analogRead ( input ) >= 4000 && feeder1last == 0 ) { Serial. println ( "Hog Feeder 1 is open" ) ; } if ( analogRead ( input ) >= 4000 ) { feeder1last = 1 ; feeder1timer += 500 ; } if ( analogRead ( input ) < 4000 && feeder1last == 1 ) { feeder1last = 0 ; feeder1timer += 500 ; feeder1timer = feeder1timer / 1000 ; tweet ( 1 , feeder1timer ) ; Serial. print ( "Hog Feeder 1 Closed: " ) ; Serial. println ( feeder1timer ) ; feeder1timer = 0 ; } /*My Guess The difference Here is something in the switch itself*/ if ( analogRead ( input2 ) >= 0 && analogRead ( input2 ) < 100 && feeder2last == 0 ) { Serial. println ( "Hog Feeder 2 is open" ) ; } if ( analogRead ( input2 ) >= 0 && analogRead ( input2 ) < 100 ) { feeder2last = 1 ; feeder2timer += 500 ; } if ( analogRead ( input2 ) > 2000 && feeder2last == 1 ) { feeder2last = 0 ; feeder2timer += 500 ; feeder2timer = feeder2timer / 1000 ; tweet ( 2 , feeder2timer ) ; Serial. print ( "Hog Feeder 2 Closed: " ) ; Serial. println ( feeder2timer ) ; feeder2timer = 0 ; } if ( analogRead ( input3 ) >= 4000 && feeder3last == 0 ) { Serial. println ( "Hog Feeder 3 is open" ) ; } if ( analogRead ( input3 ) >= 4000 ) { feeder3last = 1 ; feeder3timer += 500 ; } if ( analogRead ( input3 ) < 4000 && feeder3last == 1 ) { feeder3last = 0 ; feeder3timer += 500 ; feeder3timer = feeder3timer / 1000 ; tweet ( 3 , feeder3timer ) ; Serial. print ( "Hog Feeder 3 Closed: " ) ; Serial. println ( feeder3timer ) ; feeder3timer = 0 ; } if ( analogRead ( input4 ) >= 4000 && feeder4last == 0 ) { Serial. println ( "Hog Feeder 4 is open" ) ; } if ( analogRead ( input4 ) >= 4000 ) { feeder4last = 1 ; feeder4timer += 500 ; } if ( analogRead ( input4 ) < 4000 && feeder4last == 1 ) { //Serial.println("Pin 4 LOW"); feeder4last = 0 ; feeder4timer += 500 ; feeder4timer = feeder4timer / 1000 ; tweet ( 4 , feeder4timer ) ; Serial. print ( "Hog Feeder 4 Closed: " ) ; Serial. println ( feeder4timer ) ; feeder4timer = 0 ; } delay ( 500 ) ; } void tweet ( int feeder , int ttime ) { char msg [ 40 ] ; sprintf ( msg , "GET /tweet.php?status=%d,%d HTTP/1.0" , feeder , ttime ) ; Serial. println ( msg ) ; TCPClient client ; if ( client. connect ( server , 80 ) ) { client. println ( msg ) ; client. println ( ) ; } if ( client. available ( ) ) { char c = client. read ( ) ; //Serial.print(c); } //msg=""; } // Define the pins we're going to call pinMode on int output = A0; // You'll need to wire an LED to this one to see it blink. int input = A1; // This one is the built-in tiny one to the right of the USB jack int output2 = A2; // You'll need to wire an LED to this one to see it blink. int input2 = A3; int output3 = A4; // You'll need to wire an LED to this one to see it blink. int input3 = A5; int output4 = A6; // You'll need to wire an LED to this one to see it blink. int input4 = A7; int feeder1last=0; int feeder1timer=0; int feeder2last=0; int feeder2timer=0; int feeder3last=0; int feeder3timer=0; int feeder4last=0; int feeder4timer=0; TCPClient client; byte server[] = { x,x,x,x }; // This routine runs only once upon reset void setup() { pinMode(output,OUTPUT); pinMode(input,INPUT); analogWrite(output,255); pinMode(output2,OUTPUT); pinMode(input2,INPUT); analogWrite(output2,255); pinMode(output3,OUTPUT); pinMode(input3,INPUT); analogWrite(output3,255); pinMode(output4,OUTPUT); pinMode(input4,INPUT); analogWrite(output4,255); Serial.begin(9600); } void loop() { //Serial.println(analogRead(input4)); if (analogRead(input) >= 4000 && feeder1last == 0) { Serial.println("Hog Feeder 1 is open"); } if (analogRead(input) >= 4000 ) { feeder1last = 1; feeder1timer += 500; } if (analogRead(input) < 4000 && feeder1last == 1) { feeder1last=0; feeder1timer += 500; feeder1timer = feeder1timer /1000; tweet(1,feeder1timer); Serial.print("Hog Feeder 1 Closed: " ); Serial.println(feeder1timer); feeder1timer=0; } /*My Guess The difference Here is something in the switch itself*/ if (analogRead(input2) >= 0 && analogRead(input2) < 100 && feeder2last == 0) { Serial.println("Hog Feeder 2 is open"); } if (analogRead(input2) >= 0 && analogRead(input2) < 100) { feeder2last = 1; feeder2timer += 500; } if (analogRead(input2) > 2000 && feeder2last == 1) { feeder2last=0; feeder2timer += 500; feeder2timer = feeder2timer /1000; tweet(2,feeder2timer); Serial.print("Hog Feeder 2 Closed: " ); Serial.println(feeder2timer); feeder2timer=0; } if (analogRead(input3) >= 4000 && feeder3last == 0) { Serial.println("Hog Feeder 3 is open"); } if (analogRead(input3) >= 4000 ) { feeder3last = 1; feeder3timer += 500; } if (analogRead(input3) < 4000 && feeder3last == 1) { feeder3last=0; feeder3timer += 500; feeder3timer = feeder3timer /1000; tweet(3,feeder3timer); Serial.print("Hog Feeder 3 Closed: " ); Serial.println(feeder3timer); feeder3timer=0; } if (analogRead(input4) >= 4000 && feeder4last == 0) { Serial.println("Hog Feeder 4 is open"); } if (analogRead(input4) >= 4000 ) { feeder4last = 1; feeder4timer += 500; } if (analogRead(input4) < 4000 && feeder4last == 1) { //Serial.println("Pin 4 LOW"); feeder4last=0; feeder4timer += 500; feeder4timer = feeder4timer/1000; tweet(4,feeder4timer); Serial.print("Hog Feeder 4 Closed: " ); Serial.println(feeder4timer); feeder4timer=0; } delay(500); } void tweet(int feeder,int ttime) { char msg[40]; sprintf(msg, "GET /tweet.php?status=%d,%d HTTP/1.0", feeder,ttime); Serial.println(msg); TCPClient client; if (client.connect(server, 80)) { client.println(msg); client.println(); } if (client.available()) { char c = client.read(); //Serial.print(c); } //msg=""; }

Tweet.php:

<?php require ( 'twitter-php/src/twitter.class.php' ) ; $consumerKey = "xxx" ; $consumerSecret = "xxx" ; $accessToken = "xxx" ; $accessTokenSecret = "xxx" ; $twitter = new Twitter ( $consumerKey , $consumerSecret , $accessToken , $accessTokenSecret ) ; $status = split ( "," , $_GET [ 'status' ] ) ; error_log ( "Tweet: " . $status [ 0 ] ) ; $twitter -> send ( "Feeder " . $status [ 0 ] . " just closed in " . $status [ 1 ] . " Seconds" ) ; ?> <?php require('twitter-php/src/twitter.class.php'); $consumerKey="xxx"; $consumerSecret="xxx"; $accessToken="xxx"; $accessTokenSecret="xxx"; $twitter = new Twitter($consumerKey, $consumerSecret, $accessToken, $accessTokenSecret); $status = split(",",$_GET['status']); error_log("Tweet: " . $status[0]); $twitter->send("Feeder " . $status[0] . " just closed in " . $status[1] . " Seconds"); ?>

The codes is messy, but it’s ok for what I need it for. Another note I used a miny boost for the usb port and at first to up convert to 5 volts. The two double A batteries. didn’t last very long. I ended up switching to a solar panel and an electric fence battery I will keep you posted on how it’s working.

Twitter Account: https://twitter.com/myhungryhogs

–John “The Bacon Man” Hass