Hi there,
I was asked to post some code that makes a Led (or whaterver you want) blink when a goal is scored during the World Cup in Brazil. You should have a look at my first prototype here: http://youtu.be/avO4OA0P0Zc. As in the video, I first tried to give a random effect on the red LED strip but this is not the best effect… The code below rises the Output during 5 seconds then releases it.
I tried a few websites as data source, but livescores.com seems to be the most responsive. With the TV latency in Switzerland, the red LED strip start blinking 5-6 seconds after the goal, which is acceptable for me.
Agent Code:
`
function AgentGetScore(val)
{
local request = http.get(“http://www.livescores.com/”,{});
local response = request.sendsync();
local responsebody=response.body;
local ex = regexp("<td class=“fs”> ([0-9]) - ([0-9]) ");
local res = ex.capture(responsebody);
if(res)
{
local TeamA = responsebody.slice(res[1].begin,res[1].end);
local TeamB = responsebody.slice(res[2].begin,res[2].end);
local Score = [TeamA,TeamB];
//server.log("TeamA "+TeamA+" - "+TeamB+" TeamB");
device.send("score",Score);
}
else
server.log("No datas");
}
device.on(“GetScore”, AgentGetScore); `
Device code:
`
/* Jean-Philippe Rey - 2014
World Cup Led blinker (or whatever you want to blink )
*/
Output <- hardware.pin9;
Output.configure(DIGITAL_OUT);
local Output_old;
local time=0;
local t_refresh=0;
local refresh_rate=3; // [seconds]
local goal_turnoff_time;
local TeamA_old=“0”;
local TeamB_old=“0”;
function setOutput(State) {
Output.write(State);
}
function main(){
time+=0.5;
if(time>(t_refresh))
{
t_refresh=time+refresh_rate;
agent.send("GetScore",1);
//server.log("Refresh");
}
if(goal_turnoff_time>time)
setOutput(0);
else
setOutput(1);
imp.wakeup(0.5, main);
}
function get_score(score){
if( (score[0]!=TeamA_old) || (score[1]!=TeamB_old) )
{
//new score!
TeamA_old=score[0];
TeamB_old=score[1];
Output_old=Output.read();
server.log(“GOAL!!!”);
server.log(score[0]+" - "+score[1]);
goal_turnoff_time=time+5;
}
}
agent.on(“score”,get_score);
main();
`
I hope that could be useful for someone.
Have fun!
Jean-Philippe Rey