[LinkIt ONE 教學] 取得 Facebook 粉絲頁的 JSON 物件

各位朋友新年快樂,這篇文章要介紹如何使用 LinkIt ONE 來取得 Facebook 粉絲頁底層的 JSON 格式,您可以參考我們日前用 App Inventor 做的版本

LinkIt ONE 的 Wifi / BT 天線是整合在同一支,只要接上板子即可,如下圖。

2015-01-03 16.36.20

在程式中有一個叫做 c 的 LWiFiClient 物件,它負責管理網路相關的動作,get 與 post 等等

LWiFiClient c;

接著在 loop() 中,就透過 c.read() 把指定 URL (再次使用 CAVEDU Facebook 專頁的 open graph) 的字元逐一顯示出來。如下圖的最後一行  { “id” = …  開始就是了。有了這些資料之後就可以玩很多東西了。

while (c)
{
int v = c.read();

if (v != -1)
{
Serial.print((char)v);
}…   //以下省略

 

 

linkit_002

完整的程式碼如下,歡迎您也入手一片 LinkIt ONE 與我們互相討論喔~

LinkIt ONE 取得 Facebook JSON object
/*

Web client

This sketch connects to a website
using Wi-Fi functionality on MediaTek LinkIt platform.

Change the macro WIFI_AP, WIFI_PASSWORD, WIFI_AUTH and SITE_URL accordingly.

created 13 July 2010
by dlf (Metodo2 srl)
modified 31 May 2012
by Tom Igoe
modified 20 Aug 2014
by MediaTek Inc.
*/

#include <LTask.h>
#include <LWiFi.h>
#include <LWiFiClient.h>

#define WIFI_AP "XXX" //your WIFI AP
#define WIFI_PASSWORD "YYY" //WIFI PWD#define WIFI_AUTH LWIFI_WPA // choose from LWIFI_OPEN, LWIFI_WPA, or LWIFI_WEP.
#define SITE_URL "graph.facebook.com" // note that "caveeducation" is part of the HTTP request rather than host name.

LWiFiClient c;

void setup()
{
LTask.begin();
LWiFi.begin();
Serial.begin(115200);

while(!Serial)delay(100);

// keep retrying until connected to AP
Serial.println("Connecting to AP");
while (0 == LWiFi.connect(WIFI_AP, LWiFiLoginInfo(WIFI_AUTH, WIFI_PASSWORD)))
{
delay(1000);
}

// keep retrying until connected to website
Serial.println("Connecting to WebSite");
while (0 == c.connect(SITE_URL, 80))
{
Serial.println("Re-Connecting to WebSite");
delay(1000);
}

// send HTTP request, ends with 2 CR/LF
Serial.println("send HTTP GET request");
c.println("GET /caveeducation HTTP/1.1");
c.println("Host: " SITE_URL);
c.println("Connection: close");
c.println();

// waiting for server response
Serial.println("waiting HTTP response:");
while (!c.available())
{
delay(100);
}
}

boolean disconnectedMsg = false;

void loop()
{
// Make sure we are connected, and dump the response content to Serial
while (c)
{
int v = c.read();
if (v != -1)
{
Serial.print((char)v);
}
else
{
Serial.println("no more content, disconnect");
c.stop();
while (1)
{
delay(1);
}
}
}

if (!disconnectedMsg)
{
Serial.println("disconnected by server");
disconnectedMsg = true;
}
delay(500);
}

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *