小惡魔取讚數



前言

研究動機:

        互聯網盛行,有廠商把未上市的imp(可以結合雲端的單晶片)送來請我們測試,因此想利用       來讀取網路資料並轉換成我們可使用的資訊

目的與背景:

        facebook流行,因緣際會下取得imp,因此想利用這塊單晶片取得facebook粉絲專業按讚數並       顯示於七段顯示器上

設計概念

硬體設備

1.          imp單晶片


2.          七段顯示器x4


3.          74HC595 IC x4


 

imp為主要控制端,藉由imp取得網頁資料並連結控制選項。而主要的資料庫則是放在imp               端並且使用七段顯示器顯示。


預期功能

        能取得facebook粉絲專業的按讚數,並顯示於4個七段顯示器上

程式碼-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();


Comments