pagero
Published © MIT

"like-anator"

likes on a Facebook page result in buzzer beeps connected to Spark core in your home .

Full instructions provided915
"like-anator"

Story

Read more

Code

file_3724.java

Java
FacebookLikes.java
package com.mypkg.spark;
import java.net.*;
import java.io.*;
import java.util.*;
import org.apache.http.entity.*;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;

public class FacebookGraph {

	private  static String HOST="http://graph.facebook.com/" ;// all Facebook pages start with this domain
	private static String MYPAGE = "XXXXXXXXX"; // check likes on this page
	/**
	 * @param args
	 */
	public static void main(String[] args) {
	try {
		//SendToSpark();
		// TODO Auto-generated method stub
		int likesOld = getLikes(HOST,MYPAGE);
		int likesNew;
		while ( true)
		{
			Thread.sleep(3000);
			likesNew = getLikes(HOST,MYPAGE);
			if ( likesNew > likesOld)
			{// POST call to SPARK core.
				SendToSpark();
			}
			likesOld=likesNew;
			System.out.println("facebook likes="+likesNew);
		}
		} catch ( Exception exp)
		{
			System.out.println(exp.toString());
	}
	};
	private static void SendToSpark()
	{
		try {
			HttpClient client = new DefaultHttpClient();
			HttpPost post = new HttpPost("https://api.spark.io/v1/devices/0123456789abcdef01234567_YOURDEVICE/buzz?access_token=9876987698769876987698769876987698769876_YOUR_TOKEN");
			
			HttpResponse response = client.execute(post);
			BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
        	String line = "";
        	while ((line = rd.readLine()) != null) {
        		System.out.println(line);
        	}
		}
        catch (Exception exx){
        	System.out.println(exx.toString());
        }
    }
	
	
	private static int getLikes (String HOST, String MYPAGE)
	{
		int likesNum=0;
		try
		{
		URL link = new URL(HOST + MYPAGE);
        Socket s = new Socket(link.getHost(), 80);
        OutputStream theOutput = s.getOutputStream();
        // no auto-flushing
        PrintWriter pw = new PrintWriter(theOutput, false);
        // native line endings are uncertain so add them manually
        pw.print("GET " + link.getFile() + " HTTP/1.1\r\n");
        pw.print("HOST: " + "graph.facebook.com\r\n");
        pw.print("Connection: close\r\n");
        pw.print("Accept: text/plain, text/html, text/*\r\n");
        pw.print("\r\n");
        pw.flush();
        InputStream in = s.getInputStream();
        InputStreamReader isr = new InputStreamReader(in);
        BufferedReader br = new BufferedReader(isr);
//        int c;
        String response;
        while (( response = br.readLine()) != null)
        {
//        	while ((c = br.read()) != -1) {
//          	System.out.print((char) c);
//          	response = response.concat((String)c);
          
//        	} 
        	System.out.println(response);
        	int indx=response.indexOf("likes\":");
        	if (indx > -1)
        	{
        		int nextIndx = response.indexOf("\"link\":");
        		String likesString= response.substring(indx+7, nextIndx-1);
        		likesNum = Integer.valueOf(likesString).intValue();
        		break;
        	}
        }
        s.close();
		} catch (Exception ioe)
		{
			System.out.println(ioe.toString());
		}		
		return likesNum ; 
	}

}

file_3725.txt

C/C++
buzzer.ino
// Define the pins we're going to call pinMode on
int led = D1; // This one is the built-in tiny one to the right of the USB jack
int blinkLed (String number) ;
int buzzer = D5;

// This routine runs only once upon reset
void setup() {
  // Initialize D0 + D5 pin as output
  // It's important you do this here, inside the setup() function rather than outside it or in the loop function.
  //register fucntion "buzz" to be called over REST
  pinMode(led, OUTPUT);
  pinMode(buzzer, OUTPUT);
  Spark.function("buzz", buzzFunc); 
}

// This routine gets called repeatedly, like once every 5-15 milliseconds.
// Spark firmware interleaves background CPU activity associated with WiFi + Cloud activity with your code. 
// Make sure none of your code delays or blocks for too long (like more than 5 seconds), or weird things can happen.
void loop() {

}
// this is the fucntion invoked by the java code running on the laptop
int buzzFunc(String number)
{
    int i = 0;
    digitalWrite(led, HIGH);   // blink on board LED 
    while ( i++<= 6){  //6 short beeps

        digitalWrite(buzzer, HIGH);
        delay(100);
 
        digitalWrite(buzzer, LOW);  
        Delay(100);
    }
    //digitalWrite(led, LOW);   // Turn ON the LED
        return 1;
}

Credits

pagero
1 project • 0 followers
Product Guy & part-time tinkerer - smart devices, firmware, OS, Energy, Enterprise Software, Cloud, SaaS, Marketing, Strategy

Comments