[LinkIt One 教學] MediaTek Cloud Sandbox 控制 LED 並檢視狀態 – 可同時用手機與網頁來操作

照過來照過來,看看如何使用 MediaTek Cloud Sandbox 雲端服務來控制 LED 並檢視狀態 – 手機與網頁都可以同時控制喔。沒有LinkIt One 開發板的朋友,也可以先到 MCS 註冊一個帳號,我們很快就會辦理 LinkIt One 的物聯網研習,歡迎大家報名喔!

[LinkIt One 教學] 連接 LinkIt ONE 到 MediaTek Cloud Sandbox

先來看影片,配音員阿吉老師

這是系統的樣子,我們直接在 pin 13 插一個超白光 LED,錄影效果比較好。三支天線由上而下分別是 GPRS、Wifi/BT 與 GPS。

IMG_0710

這是本範例的介面,請看最下面的 LED_CONTROL 是控制 LED 亮滅,右邊的 LED 則是檢視 LED 狀態。

螢幕快照 2015-01-21 下午9.19.00

程式碼如下:

//#include <b64.h>
#include <HttpClient.h>
#include <LTask.h>
#include <LWiFi.h>
#include <LWiFiClient.h>
#include <LDateTime.h>
#define WIFI_AP "IT Portable Hotspot"
#define WIFI_PASSWORD "12345678"
#define WIFI_AUTH LWIFI_WPA  // choose from LWIFI_OPEN, LWIFI_WPA, or LWIFI_WEP.
#define per 50
#define per1 3
#define DEVICEID "********" // Input your deviceId
#define DEVICEKEY "***************" // Input your deviceKey
#define SITE_URL "api.mediatek.com"

LWiFiClient c;
unsigned int rtc;
unsigned int lrtc;
unsigned int rtc1;
unsigned int lrtc1;
char port[4]="   ";
char connection_info[21]="                    ";
char ip[21]="              ";             
int portnum;
int val = 0;
String tcpdata = String(DEVICEID) + "," + String(DEVICEKEY) + ",0";
String upload_led;
LWiFiClient c2;
HttpClient http(c2);

void setup()
{
  
  LTask.begin();
  LWiFi.begin();
  Serial.begin(115200);
  while(!Serial) delay(1000); /* comment out this line when Serial is not present, ie. run this demo without connect to PC */

  Serial.println("Connecting to AP");
  while (0 == LWiFi.connect(WIFI_AP, LWiFiLoginInfo(WIFI_AUTH, WIFI_PASSWORD)))
  {
    delay(1000);
  }
  
  Serial.println("calling connection");

  while (!c2.connect(SITE_URL, 80))
  {
    Serial.println("Re-Connecting to WebSite");
    delay(1000);
  }
  delay(100);

  pinMode(13, OUTPUT);
  getconnectInfo();
  connectTCP();
}

void getconnectInfo(){
  //calling RESTful API to get TCP socket connection
  c2.print("GET /mcs/v2/devices/");
  c2.print(DEVICEID);
  c2.println("/connections.csv HTTP/1.1");
  c2.print("Host: ");
  c2.println(SITE_URL);
  c2.print("deviceKey: ");
  c2.println(DEVICEKEY);
  c2.println("Connection: close");
  c2.println();
  
  delay(500);

  int errorcount = 0;
  while (!c2.available())
  {
    Serial.println("waiting HTTP response: ");
    Serial.println(errorcount);
    errorcount += 1;
    if (errorcount > 10) {
      c2.stop();
      return;
    }
    delay(100);
  }
  int err = http.skipResponseHeaders();

  int bodyLen = http.contentLength();
  Serial.print("Content length is: ");
  Serial.println(bodyLen);
  Serial.println();
  char c;
  int ipcount = 0;
  int count = 0;
  int separater = 0;
  while (c2)
  {
    int v = c2.read();
    if (v != -1)
    {
      c = v;
      Serial.print(c);
      connection_info[ipcount]=c;
      if(c==',')
      separater=ipcount;
      ipcount++;    
    }
    else
    {
      Serial.println("no more content, disconnect");
      c2.stop();

    }
    
  }
  Serial.print("The connection info: ");
  Serial.println(connection_info);
  int i;
  for(i=0;i<separater;i++)
  {  ip[i]=connection_info[i];
  }
  int j=0;
  separater++;
  for(i=separater;i<21 && j<5;i++)
  {  port[j]=connection_info[i];
     j++;
  }
  Serial.println("The TCP Socket connection instructions:");
  Serial.print("IP: ");
  Serial.println(ip);
  Serial.print("Port: ");
  Serial.println(port);
  portnum = atoi (port);
  Serial.println(portnum);

} //getconnectInfo

void uploadstatus(){
  //calling RESTful API to upload datapoint to MCS to report LED status
  Serial.println("calling connection");
  LWiFiClient c2;  

  while (!c2.connect(SITE_URL, 80))
  {
    Serial.println("Re-Connecting to WebSite");
    delay(1000);
  }
  delay(100);
  if(digitalRead(13)==1)
  upload_led = "LED,,on";
  else
  upload_led = "LED,,off";
  int thislength = upload_led.length();
  HttpClient http(c2);
  c2.print("POST /mcs/v2/devices/");
  c2.print(DEVICEID);
  c2.println("/datapoints.csv HTTP/1.1");
  c2.print("Host: ");
  c2.println(SITE_URL);
  c2.print("deviceKey: ");
  c2.println(DEVICEKEY);
  c2.print("Content-Length: ");
  c2.println(thislength);
  c2.println("Content-Type: text/csv");
  c2.println("Connection: close");
  c2.println();
  c2.println(upload_led);
  
  delay(500);

  int errorcount = 0;
  while (!c2.available())
  {
    Serial.print("waiting HTTP response: ");
    Serial.println(errorcount);
    errorcount += 1;
    if (errorcount > 10) {
      c2.stop();
      return;
    }
    delay(100);
  }
  int err = http.skipResponseHeaders();

  int bodyLen = http.contentLength();
  Serial.print("Content length is: ");
  Serial.println(bodyLen);
  Serial.println();
  while (c2)
  {
    int v = c2.read();
    if (v != -1)
    {
      Serial.print(char(v));
    }
    else
    {
      Serial.println("no more content, disconnect");
      c2.stop();

    }
    
  }
}



void connectTCP(){
  //establish TCP connection with TCP Server with designate IP and Port
  c.stop();
  Serial.println("Connecting to TCP");
  Serial.println(ip);
  Serial.println(portnum);
  while (0 == c.connect(ip, portnum))
  {
    Serial.println("Re-Connecting to TCP");    
    delay(1000);
  }  
  Serial.println("send TCP connect");
  c.println(tcpdata);
  c.println();
  Serial.println("waiting TCP response:");
} //connectTCP

void heartBeat(){
  Serial.println("send TCP heartBeat");
  c.println(tcpdata);
  c.println();
    
} //heartBeat

void loop()
{
  //Check for TCP socket command from MCS Server 
  String tcpcmd="";
  while (c.available())
   {
      int v = c.read();
      if (v != -1)
      {
        Serial.print((char)v);
        tcpcmd += (char)v;
        if (tcpcmd.substring(52).equals("1")){
          digitalWrite(13, HIGH);
          Serial.print("Switch LED ON ");
          tcpcmd="";
        }else if(tcpcmd.substring(52).equals("0")){  
          digitalWrite(13, LOW);
          Serial.print("Switch LED OFF");
          tcpcmd="";
        }
      }
   }

  LDateTime.getRtc(&rtc);
  if ((rtc - lrtc) >= per) {
    heartBeat();
    lrtc = rtc;
  }
  //Check for report datapoint status interval
  LDateTime.getRtc(&rtc1);
  if ((rtc1 - lrtc1) >= per1) {
    uploadstatus();
    lrtc1 = rtc1;
  }
  
}
LinkItOne_LED_MCS

About CAVEDU 阿吉 - 雜工 (2521 Articles)
CAVEDU 教育團隊 打雜
Contact: Website

8 Comments on [LinkIt One 教學] MediaTek Cloud Sandbox 控制 LED 並檢視狀態 – 可同時用手機與網頁來操作

  1. compile就有問題了, 是MCS改了API還是LinkIt改了SDK?

    mcs_test.ino:28:19: error: no matching function for call to ‘HttpClient::HttpClient(LWiFiClient&)’
    mcs_test.ino:28:19: note: candidates are:
    In file included from mcs_test.ino:1:0:
    D:\Program Files (x86)\Arduino\libraries\Bridge\src/HttpClient.h:26:5: note: HttpClient::HttpClient()
    HttpClient();
    ^
    D:\Program Files (x86)\Arduino\libraries\Bridge\src/HttpClient.h:26:5: note: candidate expects 0 arguments, 1 provided
    D:\Program Files (x86)\Arduino\libraries\Bridge\src/HttpClient.h:24:7: note: HttpClient::HttpClient(const HttpClient&)
    class HttpClient : public Process {
    ^

  2. 感謝, 我找到HttpClient, 已compile好, 但測試有問題, 問題是出在getconnectInfo()
    “The TCP Socket connection instructions:” 前面取得port的那幾行121-126, 不知道是LinkIt處理memory有問題還是怎樣, 如果直接跑code, line 129印出來的ip會是空白的,
    如果拿掉121-126, 印出來才會是正確的ip.
    是否因為sample方便所以沒有用substring方法?

  3. 我試Modem Port做為Com Port時可以成功啟動,但是Debug Port不能將程式碼寫入板子,請問會是哪裡有問題呢QAQ 謝謝

Leave a comment

Your email address will not be published.


*