Jump to content
cubeberg

Facebook Like Counter, 7 Segment display and Cloud9

Recommended Posts

For my first BBB project, I created a Facebook Likes counter.  My wife wrote a romance book recently and has been obsessing about the number of likes on her facebook page.  Using my BBB and a SPI 4 digit 7-segment display (found on ebay a while back) - I wrote a simple Cloud9/Node.js script to regularly update the display.  

post-35-14264691117946_thumb.jpg

(Sorry for the brightness on the display - it's really hard to get a good picture of a display that's scanned)

 

The script is dropped in the startup folder for Cloud9, so it runs on boot and updates every 2 seconds.  I had to use a bit-bang interface for the SPI because the clock polarity for shiftOut for BoneScript was wrong.  I tried fixing the BoneScript function, but couldn't get it working.  Values are retrieved from Facebook's OpenGraph interface in a JSON format - so it's easily parsed.

 

fbLikes_BB.js.txt

 

I will mention - I was having trouble until I installed AdaFruit's BBB IO Python library.  I'm not quite sure if the library fixed something, or if I just fixed something simple in my code.  At this point - I haven't tried wiping my system to see if that's the case.

 

Way over-powered for what it's doing - but my wife loves it, and the BBB still has plenty of power left over to run other processes when I get to it.  It's pretty cool to see it jump up regularly.  For now, it sits under our TV.  I plan to add some other counters later - books sold, etc.

var http = require('http');
var b = require('bonescript');

var likes = "";

var options = {
  hostname: 'graph.facebook.com',
  port: 80,
  path: '/authorjlberg?fields=likes',
  method: 'GET'
};

var dout = "P8_12";
var clk = "P8_14";
var stb = "P8_16";
/*
var dout = "USR0";
var clk = "USR1";
var stb = "USR2";
*/
var SPISPEED = 1;
 function SPI_delay() 
 {
     var v; var i; 
     for (i = 0; i < SPISPEED/2; i++) 
        v++; 
}

function dataSent()
{
    console.log('data sent');
}

var segments= new Array(0x3f, 0x06, 0x5b, 0x4f, 0x66, 0x6d, 0x7d, 0x07, 0x7f, 0x6f, 0x77, 0x7c, 0x58, 0x5e, 0x79, 0x71);
function sendData(data, hold)
{
    b.digitalWrite(stb, b.LOW);
    SPI_delay(SPISPEED);
    console.log('send');
    console.log(data);
    //b.shiftOut(dout,clk,b.gLSBFIRST,data,dataSent());
    
    console.log('bitbang out');
    var c = 0;
    while(c<8)
    {
        //LSB First
        if(data&0x01)
        {
            b.digitalWrite(dout, b.HIGH);
            console.log('high');
        }
        else
        {
            b.digitalWrite(dout, b.LOW);
            console.log('low');
        }
        data>>=1;
        //SPI_delay(SPISPEED);
        b.digitalWrite(clk,b.LOW);
        //SPI_delay(SPISPEED);
        b.digitalWrite(clk,b.HIGH);
        //SPI_delay(SPISPEED);
        c++;
    }
    
    
    if(hold === false)
    {
        SPI_delay(SPISPEED);
        console.log('releasing hold');
        b.digitalWrite(stb, b.HIGH);
        SPI_delay(SPISPEED);
    }
}
function initDisplay()
{
    //console.log('init' + dout);
    b.pinMode(dout, b.OUTPUT);//, 7, 'disabled',  'slow');
    //   console.log('init2');
    b.pinMode(clk, b.OUTPUT);//, 7, 'disabled',  'slow');
    //   console.log('init3');
    b.pinMode(stb, b.OUTPUT);//, 7, 'disabled',  'slow');
    //   console.log('init4');
    b.digitalWrite(dout,b.HIGH);
    b.digitalWrite(clk,b.HIGH);
    b.digitalWrite(stb,b.HIGH);
    

    
    sendData(0x02, false);
    sendData(0x40, false);
    //sendData(0x72, false); //test mode
    sendData(0x8F, false);
    console.log('init done');
}
function displayVal(toOutput)
{
    console.log(toOutput);
    var b = require('bonescript');
    console.log(toOutput.toString()[0]);
    likes = toOutput.toString();
    
    var ones = 0, tens = 0, hund = 0, thou = 0;
    
    console.log(likes.length);
    if(likes.length >3)
        thou=Math.floor(likes /1000)%10;
    if(likes.length >2)
        hund = Math.floor(likes /100)%10;
    if(likes.length > 1)
        tens = Math.floor(likes /10)%10;
    if(likes.length > 0)
        ones = likes % 10;
    
    console.log('sending');
    sendData(0xC0, 1); //begin first digit
    sendData(segments[thou],false);
    sendData(0xC2,1);
    sendData(segments[hund],false);
    sendData(0xC4, 1);
    sendData(segments[tens],false);
    sendData(0xC6, 1);
    sendData(segments[ones],false);
}

//setTimeout(function(){ initDisplay();},100);
initDisplay();
displayVal("0000");

setInterval(function(){

var req = http.request(options, function(res) {
  console.log('STATUS: ' + res.statusCode);
  //console.log('HEADERS: ' + JSON.stringify(res.headers));
  res.setEncoding('utf8');
  res.on('data', function (chunk) {
    console.log('BODY: ' + chunk);
    var fbObject = JSON.parse(chunk);
    displayVal(fbObject.likes);
  });
});

req.on('error', function(e) {
  console.log('problem with request: ' + e.message);
});


// write data to request body

req.end();

},2000);

Share this post


Link to post
Share on other sites

@@Kunal Raunak - do you have the hardware available?  I'm not sure if the displays are still available on ebay or not.

@@cubeberg yes sir i got beaglebone , i will get that display anyhow.. Actually sir u did for facebook like counter,  my professor has given me project to do on number of tweets on twitter so i have to get more display instaed of 4, i think i will get that.. Sir i want your help in this project, it is very difficult for me.. I am not getting where to start and what code to write ..  :( I dont know much about beaglebone also ..Can u sir please give me your social media link(like fb or google +) in the message so that we can chat on this project.. I will be very glad if u guide me.. Eagerly Waiting for your reply :)

Share this post


Link to post
Share on other sites

I believe so.  I did try to use hardware SPI with Python but didn't really get anywhere, but that was early-on.  I believe the python libraries should be further developed at this point.  I don't know anything about getting it started though. Adafruit appears to have a library for the BBB that might be a good starting point.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...