M2M Communications with mbed Eric Liang # Date:2015/06/16
Outline M2M Communication for ptt 地震文 MQTT HTTP CoAP mbed (client ) + Raspberry pi (broker) HTTP Mbed (client ) + MCS (MediaTek Cloud Sandbox ) CoAP Mbed ( CoAP Server ) + Firefox ( Copper client )
M2M Communication for PTT 地震文 mbde Arch Pro + MPU 6050 mbed K64F + FXOS8700 Raspberry Pi 2
MQTT 輕量級訊息佇列 ( message queueing ) 傳輸協定 非同步訊息(事件) 通訊模式 Low overhead ( 2 bytes header) 適合應用於低頻寬網路 支持各種流行編程語言(包括 C,Java,Ruby,Python...)& Client 容易使用 發佈 (Publish) / 訂閱 (Subscribe) 模式,簡化應用程序的開發 發佈 和 訂閱 傳送資料透過 標題 (Topics) 訊息佇列( message queues ) 底層協定為TCP/IP 提供三種不同消息傳遞等級,讓消息能按需到達目的地,適應在不穩定工作的網絡傳輸 (Wireless Network disruptions)
MQTT (Cont’d) Refer to: “temperature” = “22.5” subscribe “temperature” Client C “temperature” = “22.5” subscribe “temperature” Broker “temperature” = “22.5” subscribe “temperature” publish “temperature” “22.5” Client A Client B Refer to: http://isplanet.dyndns-office.com:8081/wiki/index.php/Message_Queuing_Telemetry_Transport
MQTT Client + mbed
MQTT Client + mbed (Cont’d)
MQTT Client + mbed (Cont’d)
MQTT + RPi Install the mosquitto 1.4.1 on Raspberry Pi Run the Broker Server Subscribe – by Python-mosquitto Refer to http://isplanet.dyndns-office.com:8081/wiki/index.php/Message_Queuing_Telemetry_Transport
MQTT Demo ( mbed + RPi ) Topic: sen/g-sensor Message: Name X Y Z G Eric 0.0012 0.002 0.0001 0.001
HTTP ( Cont’d ) Web Open IoT Cloud Architecture Physical Web: WoT ( Web of Things ) + REST API https://www.mokoversity.com/post/%E5%BE%9E%20Google%20%E7%9A%84%20Physical%20Web%20%E8%A8%88%E7%95%AB%E8%A7%A3%E8%AE%80%20IoT%20%E6%8A%80%E8%A1%93%E8%B6%A8%E5%8B%A2
HTTP Apache / IIS / nginx / Tomcat / Node.js GET / POST / PUT / DELETE 200 OK / 201 Create / 400 Bad Request / 401 Unauthorized / 404 Not Found 500 Internal Server Error / 501 Not Implemented
HTTP Client + mbed
HTTP Client + mbed / MCS mediaTek Cloud Sandbox mbed HTTP Client Demo
CoAP ( Constrained Application Protocol ) M2M Protocol CoAP over UDP ( Sever default port: 5683 ) The smallest size is 4 Bytes ( Header only ) Suit for Small Device ( 256KB Flash 32KB RAM CPU 20MHz) Request / Response ( like HTTP ) Resource Discover ( by URI: .well-known/core ) GET /.well-known/core Response: <sensors/temp>;sz=512;title=Temperature Sensor;ct=50
CoAP Header Ver: CoAP version, 01 ( like HTTP1.0 / HTTP1.1 ) T: Confirmable (0), Non-confirmable (1), Acknowledgement (2), or Reset (3) TKL: Token Length (0-8 bytes) Code: Request / Reply Code. 2.xx, 4.xx, 5.xx Message ID: Used to detect message duplication. Token: is used to match a response with a request. Options: like HTTP Header. Host: xx, URI, Request parameter or content-type 11111111: (0xFF) indicates the end of options and the start of the payload. Payload: Data Content
CoAP Request /Response Code 0.01 GET 0.02 POST 0.03 PUT 0.04 DELETE Response 2.04 Changed 2.05 Content (HTTP 200 OK ) 4.00 Bad Request ( HTTP 400 ) 4.01 Unauthorized ( HTTP 401) 5.00 Internal Server Error ( HTTP 500 ) 5.01 Not Implemented ( HTTP 501 )
CoAP Option Format 1’s Option Delta = 11 => Uri-Path (11), \sen\temperature 2’s Option Delta = 1, => 1+11 Content-Format (12), text/plain, application/json 3’s Option Delta = 3, => 3+1+11 Uri-Query (15), ?value1=1&value2=2
Example of CoAP CoAP Client “GET” the temperature value from Server CoAP URI: coap://www.server.com/temperature GET Reply
CoAP ( WebIOPi + Copper ) Installer the WebIOPi on Rpi Download the WebIOPi (WebIOPi-0.7.1.tar.gz) http://sourceforge.net/projects/webiopi/files/ $ tar xvzf WebIOPi-x.y.z.tar.gz $ cd WebIOPi-x.y.z $ sudo ./setup.sh $ sudo webiopi -d -c /etc/webiopi/config Web’s id:password: webiopi/raspberry Copper ( Mozilla Plug-in Module ) Refer to http://blog.csdn.net/xukai871105/article/details/20799551
WebIOPi + Copper ( Demo) Set the Pin4 as digital out http://ip:port/GPIO/4/function/out Set GPIO 4 by CoAP protocol coap://ip:5683/GPIO/4/value/1
WebIOPi + Copper ( Cont’d)