this is the code i use
agent:
`function HttpGetWrapper (url, headers) {
local request = http.get(url, headers);
local response = request.sendsync();
return response;
}
function start_time(likes)
{
// register the HTTP handler
local resp = HttpGetWrapper("https://graph.facebook.com/CAVEEducation?fref=ts",{ "Content-Type" : "text/xml" });
//server.log(resp.body);
local data = http.jsondecode(resp.body)
server.log(data["likes"]);
//server.log(data["cover"]["source"]);
// Send the device a 'pong' message immediately
local likes = data["likes"];
device.send("pong", likes);
//device.send("pong", data);
}
// When we get a ‘ping’ message from the device, call start_time()
device.on(“ping”, start_time);
`
////////////////////////////////////////////////////////////////////
Device:
`hardware.pin1.configure(DIGITAL_OUT);
hardware.pin2.configure(DIGITAL_OUT);
hardware.pin5.configure(DIGITAL_OUT);
local seven_seg_digits = [
0xFC, // = 0
0x60, // = 1
0xDA, // = 2
0xF2, // = 3
0x66, // = 4
0xB6, // = 5
0xBE, // = 6
0xE0, // = 7
0xFE, // = 8
0xE6 // = 9
];
function sevenSegWrite(digit)
{
//hardware.pin1.write(0);
shiftOut(hardware.pin2, hardware.pin5, 0, seven_seg_digits[digit]);
//hardware.pin1.write(1);
}
function shiftOut(data_pin, clock_pin, constant, data)
{
if (constant == 1)
{
for (local i = 8 ; i > 0 ; i–)
{
clock_pin.write(0);
data_pin.write(data & 0x80);
data = data << 1;
clock_pin.write(1);
}
}
else
{
for (local i = 0 ; i < 8 ; i++)
{
clock_pin.write(0);
data_pin.write(data & 0x01);
data = data >> 1;
clock_pin.write(1);
}
}
}
function ping()
{
// Send a ‘ping’ message to the server with the current millis counter
agent.send(“ping”, hardware.millis());
}
function return_from_imp(likes)
{
hardware.pin1.write(0);
sevenSegWrite(likes/1000);
sevenSegWrite((likes%1000)/100);
sevenSegWrite((likes%100)/10);
sevenSegWrite(likes%10);
hardware.pin1.write(1);
server.log(likes);
// Wake up in 5 seconds and ping again
imp.wakeup(5.0, ping);
}
// When we get a ‘pong’ message from the agent, call return_from_imp()
agent.on(“pong”, return_from_imp);
// Start the ping-pong
ping();`