World cup Led blinker

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 :smiley: )
*/

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

FUN! I live in England no chance… :frowning:

But great Idea thanks for sharing

Why live in England is no chance for that project?!?

Nice - thanks for sharing!

@jeanphilippe It will work I was commenting on the England team who won’t score a goal as they are Pants!!

@controlCloud Don’t say that, I am supporting England too!! :slight_smile:

I considered writing some code that would take crowd sourced twitter data and determine if a goal had been scored. Wonder if it would be any faster?

Joel for England you need a #groan filter :slight_smile:
@jeanphilippe here’s hopping we can do better than I hope for :slight_smile:

Improved version of this hack. Now with an emulated rotating light !!! : http://goo.gl/kO13AO.

By the way, did someone already did such a hack? My data source (www.livescores.com) seems to be the fastest refreshed web page but… still too slow… about 20 seconds…
What was the secret with the Budweiser Red Light?!?

I think that the secret is paying a lot of $$$ for a live sports scores service.

Yes I think this is the only way to get minimal delay. And even with a low latency data source, it is quite difficult to synchronize the LED blinker with the live TV stream. I compared some Swiss providers, the delay can be up to 25 seconds depending of the provider… Not easy to deal with such differences…

Anyway, Paying is cheating :slight_smile: . I’ll keep livescores.com as my prefered datasource.

I bet you could use the Twitter Streaming API and look for “{country} scores!” or “{country} goal” or “x-y #{country}{country}” or something like that?

I’m not sure how people tweet about the world cup, but I bet it’s possible :wink:

I suppose that is a very good idea, but I never used that API and I am not very confident with that stuff… I’ll try that solution as soon as I have a little time to learn how to use the Twitter API.

Thank for your advice!

Here’s the Twitter class - note, it requires you to setup some stuff up on the Twitter side…

https://github.com/electricimp/reference/tree/master/webservices/twitter

Thank you, it seems to be easy to use!