• 沒有找到結果。

LinkIt ONE補充資料

N/A
N/A
Protected

Academic year: 2021

Share "LinkIt ONE補充資料"

Copied!
16
0
0

加載中.... (立即查看全文)

全文

(1)

MediaTek LinkIt One 硬體規格包括藍牙、GPS、GSM/GPRS 與 Wi-Fi 等,正面的腳

位配置比照 Arduino UNO,因此相關的擴充板都可以用,但數位輸出為 3.3V。

如何管控 LinkIt ONE 的電池?

要讓 LinkIt ONE 改用電池供電運作,必須先撥動 LinkIt ONE 電路板上的一個開關,從 USB(指 USB 埠

供電)檔位打到 BAT 檔案(BAT 為 Battery 的縮寫),而在此之前必須先把 LinkIt ONE 配套的鋰電池

接上。

使用 LBattery 可取得電池狀態,只有兩個方法,level 回傳電量百分比,回傳值可能是 0、33、66、100;

呼叫 isCharging 得知是否正在充電中。底下程式碼,每隔五秒就把狀態送出到序列埠。

#include <LBattery.h>

#define BAUDRATE 19200

void setup(){

Serial.begin(BAUDRATE);

}

void loop(){

Serial.print("Battery level is ");

Serial.println(LBattery.level());

Serial.print("Charging: ");

Serial.println(LBattery.isCharging() ? "yes" : "no");

delay(5000);

}

註:測試前先切到 BAT 電源供電,開啟序列埠監控視窗,如果沒插電池,LBattery.isCharging()仍會回傳

true,而 LBattery.level()回傳 100,LinkIt ONE 官方配屬的鋰電池為

3.7V

(典型鋰電池供電電壓)。

板子上有三個指撥開關,

非常重要

1:上下兩端標示著 SPI 與 SD

,請切到

SPI(上),代表 SPI 功能相關腳位由你使用。若切到 SD,代表

想使用外插的

Micro SD 卡,那麼腳位 D11、D12、D13 就不能運作,而內建 LED 連接到腳位 D13。

2:上下兩端標示著 MS 與 UART

,請切到

UART(下),代表你想燒錄程式。MS(Mass Storage)代表不

會執行程式,板子裡的

Flash 儲存媒體會成為類似於隨身碟/記憶卡的模式,可用於韌體更新。

3:上下兩端標示著 USB 與 BAT

,請切到

USB(上),從 USB 線/電腦端汲取電源。若是 BAT 則從電

池供電,若連接電腦可進行充電。

(2)

MediaTek Cloud Sandbox 介紹 (需先免費註冊)

MediaTek Cloud Sandbox (MCS) 提供您打造準備商業化的穿戴式和物聯網裝置,所需要的數據和裝置管理

服務。它支持您專心的開發產品原型,不必煩惱建立一套自己的雲端基礎設施。

您可以在我們提供的強大的網頁儀表板介面上,看到使用我們提供的 RESTful API 從裝置蒐集回來的圖

形化的資料。之後,您可以通過從儀表板發出命令控制您的穿戴式和物聯網裝置。此外,我們提供附屬

的手機應用程式讓您能查看蒐集到的數據,並從任何地方控制您的裝置。

以下是您可以透過 MediaTek Cloud Sandbox 做的事情:

• 定義專屬於您的穿戴式和物聯網產品原型以及個人化裝置。

• 隨時在裝置和 MCS 平台間推播各式數據點,例如 GPS 資料點、溫度、或是濕度。

• 遠端控制您的裝置。

• 圖形化的資料點介面。

• 當資料點超過特定觸發條件預設範圍時,執行動作。

• 管理所有的裝置。

• 使用附屬的手機應用程式遠端控制裝置。

• 建立產品原型,裝置,和蒐集來的資料報表。

• 空中(Over the air)韌體更新。

• 使用者權限管理。

產品原型是您開發裝置的藍圖。您創建的每個裝置都會有一個

唯一的 DeviceId 和 DeviceKey

,此訊息當您

在呼叫 MCS 所提供的 API 時將會需要用到,須複製貼上配合程式碼。

資料通道

一個 資料通道 代表由 MCS 所儲存的由裝置的感應元件蒐集而來的資料或是透過 MCS 傳送給裝置的

指令,這些資料通道可以分為以下幾類:

顯示器

:此類型的資料通道是專門儲存和顯示由裝置的感應元件蒐集而來的資料。例如從裝置感應元

件上傳而來的溫度,MCS 會將此資料以時間序列方式儲存。

控制器

:此類型的資料通道是專門用來傳遞指令至裝置,以控制裝置內元件的狀態。例如控制燈的開

或關。

(3)

綜合型顯示控制器

:此類型的資料型態能同時為顯示器和控制器。例如冷氣機的溫度顯示器,並且同

時能控制冷氣的開關或是調整溫度。

資料型態

:每個資料通道都能用來傳輸以下的九種種資料型態的一種

開/關

— 此類型的資料型態用來表示裝置的兩種狀態,使用者能選擇開啟或是關閉裝置的狀態。例如

一盞燈的開或關。

類別

— 此類型的資料型態能用來表示一個任意的類別。您能定義任何您想要的類別和此類別相對的

內容。例如您能用來儲存星期,月份,或是風扇的狀態(關、慢速、中速、高速)。

整數

— 此類型的資料型態能用來表示任意的整數,例如某一個使用者一天走了多少步的數值。

浮點數

— 此類型的資料型態能用來表示任意的浮點數,例如氣溫。

字串

— 此類型的資料型態用來表示字串,例如裝置回傳的訊息。

十六進位數

— 此類型的資料型態用來表示十六進位數值,例如 LED 燈的顯示顏色。

GPS

— 此類型的資料型態用來表示地理位置,包含經度、緯度、和高度。

GPIO

— 此類型的資料型態用來表示 GPIO 的數位訊號。例如在 Pin 4 位置為 High 的訊號狀態。

PWM

— 此類型的資料型態用來表示傳遞到 GPIO 的 PWM 數位訊號, 例如在 Pin 3 位置的 level 15

訊號。

類比

— 此類型的資料型態所代表的是一個整數,其數值的區間需由使用者自行定義。類比控制器適

合需要微調或是比例控制等應用,像是音量控制器。

觸發條件和動作

此功能可讓您定義觸發電子郵件或基於雲的通報標準,您身為裝置的擁有者,除了會無條件的收到通知

外,還能透過設定使用者權限來使其他使用者也收到相同的通知。

觸發器可用於以下情況:

當某一個資料通道回傳的資料超過或是低於預設值,將會觸發通知條件,並且通知有權限的使用者。

同時,資料值仍然會被保存。

當控制器型態的資料通道的值被改變時,執行觸發動作。

因為數位輸出僅 3.3V,所以若接 LED 會比原來 Arduino(5V)暗,原因是配合穿戴裝置,需較省電,若是

接繼電器 Relay 需注意輸入電壓僅 3.3V 喔!linkit ONE 雖和 Arduino 類似,但功能主要是放在穿戴、雲端、

藍芽、Wi-Fi、GPS、GSM。

API 使用限制  裝置數量和使用資料點數量將會被記綠以及圖形化。  限制每個免費開發者帳戶只能有 10 個測試裝置。  限制每個免費開發者帳戶的測試裝置每個月只能有 10 萬個資料點。  限制每個免費開發者帳戶的測試裝置每天只能有 100 封觸發通知郵件。

(4)

#include <HttpClient.h> #include <LTask.h> #include <LWiFi.h> #include <LWiFiClient.h> #include <LDateTime.h> //雲端帳號 chenlung,密碼 1331**** #define WIFI_AP "chenlung88" //AP 名稱

#define WIFI_PASSWORD "8642****" //AP 密碼 #define WIFI_AUTH LWIFI_WPA //加密方式為 WPA #define intervalBeat 60 //重新與 MCS 連線時間,單位為秒 #define intervalUpload 1 //上傳資料給 MCS 的時間,單位為秒 #define DEVICEID "D13htjmu" //測試裝置 Id,每次創建都不同

#define DEVICEKEY "GOm8F0bzh9cP3Vcu" //測試裝置 Key,每次創建都不同 #define SITE_URL "api.mediatek.com"

LWiFiClient client; LWiFiClient client2; HttpClient http(client2);

unsigned int rtcBeat; //用於重新與 MCS 連線時間 unsigned int lrtcBeat;

unsigned int rtcUpload; //用於上傳資料給 MCS 的時間 unsigned int lrtcUpload;

char port[4]={0}; //埠號(字元陣列) int portnum; //埠號(數值)

char connection_info[21]={0}; //連線資料 char ip[21]={0}; //IP 位址 int val = 0;

String tcpdata = String(DEVICEID) + "," + String(DEVICEKEY) + ",0"; //MCS 裝置資料 String upload_led; //上傳給 MCS 資料

String tcpcmd_led_on = "LED_Control,1"; //MCS 傳回點亮 LED 燈資料 String tcpcmd_led_off = "LED_Control,0"; //MCS 傳回熄滅 LED 燈資料

void setup() { LTask.begin(); LWiFi.begin(); Serial.begin(9600); //while(!Serial) delay(1000); //除錯用,需開啟監視方可繼續執行

1.

左邊是透過 MCS 用手機透過

Wi-Fi 控制 LED。

2.

參閱碁峰課本 P4-10~4-29

3. 建議使用手機當 AP,方便觀察

是否有正確連接裝置。

4. 該程式會一直反覆偵測是否連

線,所以當按下去會等 1 秒才

會有方應喔!切勿按太快,因

為有時會當掉…

(5)

Serial.println("Connecting to AP");

while (0 == LWiFi.connect(WIFI_AP, LWiFiLoginInfo(WIFI_AUTH, WIFI_PASSWORD))) { //每秒連線一次 delay(1000); } Serial.println("calling connection"); while (!client2.connect(SITE_URL, 80)) { //每秒連接 MCS 網站一次 Serial.println("Re-Connecting to WebSite"); delay(1000); } delay(100); pinMode(13, OUTPUT); getconnectInfo(); //取得連線資訊 connectTCP(); //建立 TCP 連線 } void getconnectInfo(){ //以 RESTful API 方式建立連線 client2.print("GET /mcs/v2/devices/"); client2.print(DEVICEID); client2.println("/connections.csv HTTP/1.1"); client2.print("Host: "); client2.println(SITE_URL); client2.print("deviceKey: "); client2.println(DEVICEKEY); client2.println("Connection: close"); client2.println(); delay(500); int errorcount = 0; //計數器 while (!client2.available()) { Serial.println("waiting HTTP response: "); Serial.println(errorcount); errorcount += 1; if (errorcount > 10) { //超過 2 秒 client2.stop(); return; } delay(200); }

int err = http.skipResponseHeaders(); //忽略表頭部分 int bodyLen = http.contentLength(); //傳送資料長度 Serial.print("Content length is: ");

Serial.println(bodyLen); Serial.println(); char ch; int ipcount = 0; int count = 0; int separater = 0;

while(client2) { //讀取 MCS 傳送過來的資料:IP 及 PORT int v = client2.read();

if (v != -1) { ch = v;

Serial.print(ch);

connection_info[ipcount]=ch;

if(ch==',') //逗點前為 IP,逗點後為 PORT separater=ipcount;

ipcount++; } else {

Serial.println("no more content, disconnect"); client2.stop();

} }

Serial.print("The connection info: "); Serial.println(connection_info);

(6)

int i; for(int i=0;i<separater;i++) { //取得 IP ip[i]=connection_info[i]; } int j=0; separater++;

for(i=separater;i<21 && j<5;i++) { //取得 PORT 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); } void connectTCP(){ //建立 TCP 連線 client.stop(); //關閉原有連線 Serial.println("Connecting to TCP"); Serial.println(ip); Serial.println(portnum);

while (client.connect(ip, portnum) == 0) { Serial.println("Re-Connecting to TCP"); delay(1000); } Serial.println("send TCP connect"); client.println(tcpdata); //傳送 MCS 裝置資料 client.println(); Serial.println("waiting TCP response:"); } void loop() { //檢查 MCS 的傳送資料 String tcpcmd=""; //存 MCS 的傳送資料 while(client.available()) { int v = client.read(); if (v != -1) { Serial.print((char)v); tcpcmd += (char)v; if(tcpcmd.substring(40).equals(tcpcmd_led_on)){ //點亮 LED 燈 digitalWrite(13, HIGH); Serial.print("Switch LED ON "); tcpcmd="";

} else if(tcpcmd.substring(40).equals(tcpcmd_led_off)){ //熄滅 LED 燈 digitalWrite(13, LOW);

Serial.print("Switch LED OFF"); tcpcmd=""; } } } //每 60 秒重新連結 MCS 網頁一次 LDateTime.getRtc(&rtcBeat); //取得目前時間

if((rtcBeat - lrtcBeat) >= intervalBeat) { //大於等於設定時間 heartBeat();

lrtcBeat = rtcBeat; //記錄前次連線時間 }

//每 1 秒傳送資料給 MCS 一次 LDateTime.getRtc(&rtcUpload);

if ((rtcUpload - lrtcUpload) >= intervalUpload) { uploadstatus();

(7)

} } void heartBeat(){ //連結 MCS 網頁 Serial.println("send TCP heartBeat"); client.println(tcpdata); client.println(); } void uploadstatus(){ //傳送資料給 MCS Serial.println(); Serial.println("calling connection"); while (!client2.connect(SITE_URL, 80)) { Serial.println("Re-Connecting to WebSite"); delay(1000); } delay(100); if(digitalRead(13)==1) { //LED 燈為點亮狀態 upload_led = "LED_Display,,1"; } else { //LED 燈為熄滅狀態 upload_led = "LED_Display,,0"; }

int thislength = upload_led.length(); client2.print("POST /mcs/v2/devices/"); client2.print(DEVICEID); client2.println("/datapoints.csv HTTP/1.1"); client2.print("Host: "); client2.println(SITE_URL); client2.print("deviceKey: "); client2.println(DEVICEKEY); client2.print("Content-Length: "); client2.println(thislength); client2.println("Content-Type: text/csv"); client2.println("Connection: close"); client2.println(); client2.println(upload_led); //傳送資料給 MCS delay(500); int errorcount = 0; while (!client2.available()) { Serial.print("waiting HTTP response: "); Serial.println(errorcount); errorcount += 1; if (errorcount > 10) { client2.stop(); return; } delay(200); }

int err = http.skipResponseHeaders(); int bodyLen = http.contentLength(); Serial.print("Content length is: "); Serial.println(bodyLen); Serial.println(); while(client2) { int v = client2.read(); if (v != -1) { Serial.print(char(v)); } else {

Serial.println("no more content, disconnect"); client2.stop();

} } }

(8)

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

#define WIFI_AP "chenlung88" //AP 名稱

#define WIFI_PASSWORD "8642****" //AP 密碼 #define WIFI_AUTH LWIFI_WPA //加密方式為 WPA #define intervalBeat 60 //重新與 MCS 連線時間,單位為秒 #define intervalUpload 3 //上傳資料給 MCS 的時間,單位為秒 #define DEVICEID "DAKqUHoa" //測試裝置 Id

#define DEVICEKEY "YNx7ixwh6npAFq0m" //測試裝置 Key #define SITE_URL "api.mediatek.com"

LWiFiClient client; LWiFiClient client2; HttpClient http(client2);

unsigned int rtcBeat; //用於重新與 MCS 連線時間 unsigned int lrtcBeat;

unsigned int rtcUpload; //用於上傳資料給 MCS 的時間 unsigned int lrtcUpload;

char port[4]={0}; //埠號(字元陣列) int portnum; //埠號(數值)

char connection_info[21]={0}; //連線資料 char ip[21]={0}; //IP 位址 int valueLight = 0;

String tcpdata = String(DEVICEID) + "," + String(DEVICEKEY) + ",0"; //MCS 裝置資料 String upload_data; //上傳給 MCS 資料

const int analogPin = A0; void setup() { LTask.begin(); LWiFi.begin(); pinMode(13, OUTPUT); digitalWrite(13, LOW); Serial.begin(9600); //while(!Serial) delay(1000); //除錯用 Serial.println("Connecting to AP");

while (0 == LWiFi.connect(WIFI_AP, LWiFiLoginInfo(WIFI_AUTH, WIFI_PASSWORD))) { //每秒連線一次 delay(1000);

(9)

Serial.println("calling connection"); while (!client2.connect(SITE_URL, 80)) { //每秒連接 MCS 網站一次 Serial.println("Re-Connecting to WebSite"); delay(1000); } delay(100); getconnectInfo(); //取得連線資訊 connectTCP(); //建立 TCP 連線 } void getconnectInfo(){ //以 RESTful API 方式建立連線 client2.print("GET /mcs/v2/devices/"); client2.print(DEVICEID); client2.println("/connections.csv HTTP/1.1"); client2.print("Host: "); client2.println(SITE_URL); client2.print("deviceKey: "); client2.println(DEVICEKEY); client2.println("Connection: close"); client2.println(); delay(500); int errorcount = 0; //計數器 while (!client2.available()) { Serial.println("waiting HTTP response: "); Serial.println(errorcount); errorcount += 1; if (errorcount > 10) { //超過 2 秒 client2.stop(); return; } delay(200); }

int err = http.skipResponseHeaders(); //忽略表頭部分 int bodyLen = http.contentLength(); //傳送資料長度 Serial.print("Content length is: ");

Serial.println(bodyLen); Serial.println(); char ch; int ipcount = 0; int count = 0; int separater = 0;

while(client2) { //讀取 MCS 傳送過來的資料:IP 及 PORT int v = client2.read();

if (v != -1) { ch = v;

Serial.print(ch);

connection_info[ipcount]=ch;

if(ch==',') //逗點前為 IP,逗點後為 PORT separater=ipcount;

ipcount++; } else {

Serial.println("no more content, disconnect"); client2.stop();

} }

Serial.print("The connection info: "); Serial.println(connection_info); int i; for(int i=0;i<separater;i++) { //取得 IP ip[i]=connection_info[i]; } int j=0;

(10)

separater++;

for(i=separater;i<21 && j<5;i++) { //取得 PORT 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); } void connectTCP(){ //建立 TCP 連線 client.stop(); //關閉原有連線 Serial.println("Connecting to TCP"); Serial.println(ip); Serial.println(portnum);

while (client.connect(ip, portnum) == 0) { Serial.println("Re-Connecting to TCP"); delay(1000); } Serial.println("send TCP connect"); client.println(tcpdata); //傳送 MCS 裝置資料 client.println(); Serial.println("waiting TCP response:"); } void loop() { valueLight = analogRead(analogPin); if(valueLight < 250) { digitalWrite(13, HIGH); } else { digitalWrite(13, LOW); } //每 60 秒重新連結 MCS 網頁一次 LDateTime.getRtc(&rtcBeat); //取得目前時間

if((rtcBeat - lrtcBeat) >= intervalBeat) { //大於等於設定時間 heartBeat();

lrtcBeat = rtcBeat; //記錄前次連線時間 }

//每 3 秒傳送資料給 MCS 一次 LDateTime.getRtc(&rtcUpload);

if ((rtcUpload - lrtcUpload) >= intervalUpload) { uploadstatus(); lrtcUpload = rtcUpload; } delay(1000); } void heartBeat(){ //連結 MCS 網頁 Serial.println("send TCP heartBeat"); client.println(tcpdata); client.println(); } void uploadstatus(){ //傳送資料給 MCS Serial.println(); Serial.println("calling connection"); while (!client2.connect(SITE_URL, 80)) { Serial.println("Re-Connecting to WebSite"); delay(1000); }

(11)

delay(100); if(digitalRead(13)==1) { //LED 燈為點亮狀態 upload_data = "LED_Display,,1"; } else { //LED 燈為熄滅狀態 upload_data = "LED_Display,,0"; }

upload_data = upload_data + "\nLight_Value,," + String(valueLight); int thislength = upload_data.length();

client2.print("POST /mcs/v2/devices/"); client2.print(DEVICEID); client2.println("/datapoints.csv HTTP/1.1"); client2.print("Host: "); client2.println(SITE_URL); client2.print("deviceKey: "); client2.println(DEVICEKEY); client2.print("Content-Length: "); client2.println(thislength); client2.println("Content-Type: text/csv"); client2.println("Connection: close"); client2.println(); client2.println(upload_data); //傳送資料給 MCS delay(500); int errorcount = 0; while (!client2.available()) { Serial.print("waiting HTTP response: "); Serial.println(errorcount); errorcount += 1; if (errorcount > 10) { client2.stop(); return; } delay(200); }

int err = http.skipResponseHeaders(); int bodyLen = http.contentLength(); Serial.print("Content length is: "); Serial.println(bodyLen); Serial.println(); while(client2) { int v = client2.read(); if (v != -1) { Serial.print(char(v)); } else {

Serial.println("no more content, disconnect"); client2.stop(); } } }

註:該程式 LED 可改接 3.3V 繼電器 Relay,直接控制 110V

家電,但缺點是若太暗會一直觸發,並持續自動發 MAIL 通

知,應修改為遇臨界點才觸發通知,即目前點亮,若偵測還

是太暗要點亮,則應不用持續啟動繼電器與通知。

此程式可再增加強制控制 LED 與繼電器的開關控制,試試!

(12)

※戶外測試

#include <LGPS.h> #include <HttpClient.h> #include <LTask.h> #include <LWiFi.h> #include <LWiFiClient.h> #include <LDateTime.h>

#define WIFI_AP "chenlung88" //AP 名稱

#define WIFI_PASSWORD "86423063" //AP 密碼 #define WIFI_AUTH LWIFI_WPA //加密方式為 WPA #define intervalBeat 60 //重新與 MCS 連線時間,單位為秒 #define intervalUpload 10 //上傳資料給 MCS 的時間,單位為秒 #define DEVICEID "DMLqpAsa" //測試裝置 Id

#define DEVICEKEY "nmYamADDZaS9wgmu" //測試裝置 Key #define SITE_URL "api.mediatek.com"

gpsSentenceInfoStruct info; //GPS 所有資訊 char buff[256], monobuf[20];

double latitude, longitude, altitude; //緯度,經度,高度 int satellite; //衛星數

char buffer_latitude[8], buffer_longitude[8], buffer_altitude[7], buffer_satellite[3]; LWiFiClient client;

LWiFiClient client2; HttpClient http(client2);

unsigned int rtcBeat; //用於重新與 MCS 連線時間 unsigned int lrtcBeat;

unsigned int rtcUpload; //用於上傳資料給 MCS 的時間 unsigned int lrtcUpload;

char port[4]={0}; //埠號(字元陣列) int portnum; //埠號(數值)

char connection_info[21]={0}; //連線資料 char ip[21]={0}; //IP 位址

String tcpdata = String(DEVICEID) + "," + String(DEVICEKEY) + ",0"; //MCS 裝置資料 void setup() {

LTask.begin(); LWiFi.begin();

LGPS.powerOn(); //啟動 GPS

while (0 == LWiFi.connect(WIFI_AP, LWiFiLoginInfo(WIFI_AUTH, WIFI_PASSWORD))) { //每秒連線一次 delay(1000); } while (!client2.connect(SITE_URL, 80)) { //每秒連接 MCS 網站一次 delay(1000); } delay(100); getconnectInfo(); //取得連線資訊

(13)

connectTCP(); //建立 TCP 連線 } void getconnectInfo(){ //取得連線資訊 //以 RESTful API 方式建立連線 client2.print("GET /mcs/v2/devices/"); client2.print(DEVICEID); client2.println("/connections.csv HTTP/1.1"); client2.print("Host: "); client2.println(SITE_URL); client2.print("deviceKey: "); client2.println(DEVICEKEY); client2.println("Connection: close"); client2.println(); delay(500); int errorcount = 0; //計數器 while (!client2.available()) { errorcount += 1; if (errorcount > 10) { //超過 2 秒 client2.stop(); return; } delay(200); }

int err = http.skipResponseHeaders(); //忽略表頭部分 int bodyLen = http.contentLength(); //傳送資料長度 char ch;

int ipcount = 0; int count = 0; int separater = 0;

while(client2) { //讀取 MCS 傳送過來的資料:IP 及 PORT int v = client2.read();

if (v != -1) { ch = v;

connection_info[ipcount]=ch;

if(ch==',') //逗點前為 IP,逗點後為 PORT separater=ipcount; ipcount++; } else { client2.stop(); } } int i; for(int i=0;i<separater;i++) { //取得 IP ip[i]=connection_info[i]; } int j=0; separater++;

for(i=separater;i<21 && j<5;i++) { //取得 PORT port[j]=connection_info[i]; j++; } portnum = atoi(port); } void connectTCP(){ //建立 TCP 連線 client.stop(); //關閉原有連線

while (client.connect(ip, portnum) == 0) { delay(1000);

}

client.println(tcpdata); //傳送 MCS 裝置資料 client.println();

(14)

}

void loop() {

//每 60 秒重新連結 MCS 網頁一次

LDateTime.getRtc(&rtcBeat); //取得目前時間

if((rtcBeat - lrtcBeat) >= intervalBeat) { //大於等於設定時間 heartBeat();

lrtcBeat = rtcBeat; //記錄前次連線時間 }

LDateTime.getRtc(&rtcUpload); //每 10 秒傳送資料給 MCS 一次 if ((rtcUpload - lrtcUpload) >= intervalUpload) {

LGPS.getData(&info); //取得 GPS 資訊 parseGPGGA((const char*)info.GPGGA); //解析 GPS 資料 uploadGPS(); //上傳資料給 MCS lrtcUpload = rtcUpload; } } void heartBeat(){ //連結 MCS 網頁 client.println(tcpdata); client.println(); }

void parseGPGGA(const char* GPGGAstr) { //解析 GPS 資料 int tmp; if(GPGGAstr[0] == '$') { //第一個字元為「$」才是 GPS 資料 tmp = getComma(2, GPGGAstr); //取得第 3 個欄位資料:緯度 latitude = getDoubleNumber(&GPGGAstr[tmp])/100.0; //緯度整數部分 int latitude_int=floor(latitude); double latitude_decimal=(latitude-latitude_int)*100.0/60.0; //緯度小數部分 latitude=latitude_int+latitude_decimal; tmp = getComma(4, GPGGAstr); //取得第 5 個欄位資料:經度 longitude = getDoubleNumber(&GPGGAstr[tmp])/100.0; //經度整數部分 int longitude_int=floor(longitude); double longitude_decimal=(longitude-longitude_int)*100.0/60.0; //經度小數部分 longitude=longitude_int+longitude_decimal; tmp = getComma(9, GPGGAstr); //取得第 10 個欄位資料:高度值 altitude = getDoubleNumber(&GPGGAstr[tmp]); tmp = getComma(7, GPGGAstr); //取得第 8 個欄位資料:衛星數 satellite = getIntNumber(&GPGGAstr[tmp]); } }

static unsigned char getComma(unsigned char num,const char *str) { //取得第 num 個逗點欄位起始位置 unsigned char i,j = 0;

int len=strlen(str); for(i = 0;i < len;i ++) { if(str[i] == ',') j++; if(j == num) return i + 1; }

return 0; //若無逗點則傳回 0 }

static double getDoubleNumber(const char *s) { //傳回浮點數 getMonoValue(s);

return atof(monobuf); }

static int getIntNumber(const char *s) { //傳回整數 getMonoValue(s);

(15)

}

static void getString(const char *s) { //傳回字元陣列 getMonoValue(s);

}

void getMonoValue(const char *s) { //配合 getComma 取得單一欄資料 unsigned char i;

i=getComma(1, s); i = i - 1;

strncpy(monobuf, s, i); //欄位資料置於整體變數 monobuf monobuf[i] = 0;

}

void uploadGPS() {

if(latitude>-90 && latitude<90 && longitude>0 && longitude<360) { sprintf(buffer_latitude, "%.4f", latitude);

sprintf(buffer_longitude, "%.4f", longitude); }

sprintf(buffer_satellite, "%d", satellite); sprintf(buffer_altitude, "%.1f", altitude);

String upload_GPS = "GPS,," + String(buffer_latitude) + "," + String(buffer_longitude) + ",0\n"; //將資料加入上傳字串

upload_GPS+="latitude,,"+String(buffer_latitude)+"\n"+"longitude,,"+String(buffer_longitude)+"\n"; //加入經緯度資料

upload_GPS += "satellite,," + String(buffer_satellite) + "\n" + "altitude,," + String(buffer_altitude); //加入衛星數及高度

//上傳資料給 MCS

int GPS_length = upload_GPS.length(); while (!client2.connect(SITE_URL, 80)) { delay(1000); } delay(100); client2.print("POST /mcs/v2/devices/"); client2.print(DEVICEID); client2.println("/datapoints.csv HTTP/1.1"); client2.print("Host: "); client2.println(SITE_URL); client2.print("deviceKey: "); client2.println(DEVICEKEY); client2.print("Content-Length: "); client2.println(GPS_length); client2.println("Content-Type: text/csv"); client2.println("Connection: close"); client2.println(); client2.println(upload_GPS); delay(500); int errorcount = 0; while (!client2.available()) { errorcount += 1; if (errorcount > 10) { client2.stop(); return; } delay(200); } }

(16)

參考文獻

相關文件

• 系統此時除了檢查您的檔案與資料夾權限正確與否,並且也 檢查您的SERVER PHP版本、是否安裝了GD LIB與GD的版

1〃先期篩檢受詴者同意書 P12-13「您提供 腫瘤檢體進行 heregulin 檢測,或您符合 資格參加本詴驗的先期篩檢,不頇支付任 何費用。P8 之後可能會再次進行您組織

在數位系統中,若有一個以上通道的數位信號需要輸往單一的接收端,數位系統通常會使用到一種可提供選擇資料的裝置,透過選擇線上的編碼可以決定輸入端

主持人: C 工藝 0304 您的產業是工藝產業,是採學徒制,C 工藝 0305 您也提到進入 你們這份產業的人員,不僅是創意和知識而已,也有包含對生活的態度,對 文化學習的謙卑。請問 C 設計

隨身碟是一種攜帶方便的電腦儲存裝置。若浩南有一個容量為 8GB

當 Bundle 啟動後會將自身所提供的服務註冊到 Service Registry 中,如圖 2-12,Service Registry 會對部署在 OSGi Framework 的 Bundles 發送新加入 Bundle 的 Service

其防護原理是以櫃內層流配合 HEPA FILTER 的使用來提供

因此 SCP 心電圖在院際交換的時候受到限制。近來,DICOM(補充文件 30)提出一維的生物醫學訊號標準,如:血壓、心電圖。使用