2015.9.7	 
Taipei
Maker	 of	 Things	 
Open	 IoT	 Cloud	 for	 Makers
Innovation of Things
Mokoversity
姓名標示-非商
業性	 4.0	 國際
Lecturer
Jollen, Founder of WoT.City
!
Jollen is a passionate software
developer with a breadth of technical
experience with software
engineering, web technologies,
embedded systems, operating
systems and distributed systems.
!
Jollen specializes in open source
software business model and its
strategy planning. He is also the
founder of Mokoversity and WoT.City.
!
Please Email Jollen through
jollen@jollen.org
1
ARM mbed
The next big OS to rock the
world will be little.
2 Web Trends 2015
Virtual DOM
3 Physical Web
The future of the IoT is URLs.
4 W3C WoT
5 CoAP
WoT Servers: Use Cases
Constrained!
Device
High
Performance
MCU
WiFi MCU SBC
Server !
Farms
Platform
Portfolio
Legacy MCUs
ARM mbed OS、
Neuclio
ESP8266、
NodeMCU、
EMW3165
Intel Edison、
Qualcomm
Dragonboard 410c
IoT Diagram
(Use
Scenario)
WoT
Architecture
IoT Protocols
TD
#1: IoT & WoT Introduction
Web of Things
《ARM mbed Practice: RTOS, IoT Client & IoT Cloud》 Copyright (C) 2015 Moko365 Inc. 僅供 QUALCOMM 內部使⽤用.
the Web of Things reuses existing and well-known
Web standards[1][2] used in the programmable Web
(e.g., REST, HTTP, JSON), semantic Web (e.g., JSON-LD,
Microdata, etc.), the real-time Web (e.g., Websockets)
and the social Web (e.g., oauth or social networks).
!
—Wikipedia
Web of Things
《ARM mbed Practice: RTOS, IoT Client & IoT Cloud》 Copyright (C) 2015 Moko365 Inc. 僅供 QUALCOMM 內部使⽤用.
Web
IoT 使⽤用情境
HTTP
Websocket
CoAP
http://
ws://
coap://
IP
UDP
SMTPDNS HTTP
TCP
CoAP FTP
Ethernet
Layering of Protocols
IoT WoT
連結⽅方式 Bluetooth HTTP
數據管理 Centerlized Decenterlized
Things Objects RESTful Objects
應⽤用開發模式 Native Web (SPA)
硬件概念 Sensor Device
Physical Web
(Sensor Fusion)
Figure: Thinking in WoT. Copyright (C) 2014 Mokoversity Inc.
IoT vs WoT: Methdology
IoT WoT
Connectivity Machine to Machine
LWM2M
Machine to Web
Networking TCP / UDP HTTP / REST / CoAP
Things Sensor Device RESTful Objects
Data Streaming Data Time-Series Data
Discover MQTT Publish / Subscribe
IoT vs WoT: Connectivity
WoT Application
Internet of Things
Web of Things
An Application Layer that simplifies the creation of
Internet of Things applications
* of Things
Thinking in WoT
IoT WoT
连接⽅方式 Bluetooth HTTP
数据管理哲学 Centerlized Decenterlized
Things Objects RESTful Objects
IoT App Native Web (SPA)
硬件设计哲学 Sensor Device
Physical Web
(Sensor Fusion)
IoT & mbed Client - RTOS
ARM mbed
mbed OS
We are creating a modern full-stack operating
system that is designed specifically for ARM
Cortex®-M-based MCUs; the worlds leading 32-
bit microcontrollers that already sell in the
billions.
!
mbed Device Server
Analogous to a Web Server that accepts
connections from mobile phones or web browsers,
a Device Server handles the connections from IoT
devices.
!
mbed Tools
online compiler (Web IDE), SDK, HDK and etc.
mbed OS
mbed Device Server
mbed Tools
《ARM mbed Practice: RTOS, IoT Client & IoT Cloud》 Copyright (C) 2015 Moko365 Inc. 僅供 QUALCOMM 內部使⽤用.
Source: http://mbed.org/technology/os/
mbed OS
mbed™ OS is an operating system for IoT devices
and is especially well-suited to run in energy
constrained environments.
mbed RTOS
Thread
Mutex
Semaphore
Message Queue
Mail
Memory Poll
mbed Libraries
Analog I/O
Digital I/O
Timers
Digital Interface
Real-time Operating System
File System
USB (Device and Host)
Networking
Connectivity Protocol Stack
Bluetooth Low Energy
Cellular
Ethernet
Thread
Wi-Fi
Zigbee IP
Zigbee NAN
6LoWPAN
mbed Memory Sections
RW Data
Program
RAM
Flash
RW Data
Program
ZI = 0
RW Data
Stack
Heap
mbed Threads
RAM
Flash
RW Data
ZI: Timer Stack
ZI: Idle Stack
ZI
Scheduler Stack
Main Thread Stack
Head
When you use the RTOS, before explicitly
initializing any additional thread, you will
have 4 separate stacks:
!
• The stack of the Main Thread (executing
the main function).
!
• The Idle Thread executed each time all
the other threads are waiting for external,
or scheduled events. This is particularly
useful to implement energy saving
strategies. (ie sleep).
!
• The Timer Thread that executes all the
time scheduled tasks (periodic and non
periodic).
!
• The stack of OS Scheduler itself (also
used by the ISRs).
mbed Timer
#include "mbed.h"
Timer t;
int main() {
t.start();
printf("Hello World!n");
t.stop();
printf("The time taken was %f secondsn", t.read());
}
See: https://developer.mbed.org/handbook/Timer
mbed Thread API
#include "mbed.h"
#include "rtos.h"
DigitalOut led1(LED1);
DigitalOut led2(LED2);
void led2_thread(void const *args) {
while (true) {
led2 = !led2;
Thread::wait(1000);
}
}
int main() {
Thread thread(led2_thread);
while (true) {
led1 = !led1;
Thread::wait(500);
}
}
See: https://developer.mbed.org/handbook/RTOS#thread
mbed Thread Priority
See: https://developer.mbed.org/handbook/RTOS#thread
int main() {
Thread thread(led2_thread);
thread.set_priority(osPriorityNormal);
!
while (true) {
led1 = !led1;
Thread::wait(500);
}
}
mbed Thread States
See: https://developer.mbed.org/handbook/RTOS#thread
mbed Mutex
See: https://developer.mbed.org/handbook/RTOS
#include "mbed.h"
#include "rtos.h"
Mutex stdio_mutex;
void notify(const char* name, int state) {
stdio_mutex.lock();
printf("%s: %dnr", name, state);
stdio_mutex.unlock();
}
void test_thread(void const *args) {
while (true) {
notify((const char*)args, 0); Thread::wait(1000);
notify((const char*)args, 1); Thread::wait(1000);
}
}
int main() {
Thread t2(test_thread, (void *)"Th 2");
Thread t3(test_thread, (void *)"Th 3");
test_thread((void *)"Th 1");
}
#include "mbed.h"
#include "rtos.h"
Semaphore two_slots(2);
void test_thread(void const *name) {
while (true) {
two_slots.wait();
printf("%snr", (const char*)name);
Thread::wait(1000);
two_slots.release();
}
}
int main (void) {
Thread t2(test_thread, (void *)"Th 2");
Thread t3(test_thread, (void *)"Th 3");
test_thread((void *)"Th 1");
}
mbed Semaphore
See: https://developer.mbed.org/handbook/RTOS
IoT & mbed Client - IoT
Layering of Protocols
IP
UDP
SMTPDNS HTTP
TCP
CoAP FTP
Ethernet
CoAP
CoAP 並不是要取代 HTTP,它是針對
Constrained Device 的 HTTP 需求。CoAP
(Constrained Application Protocol)是更
簡單且輕量化的 HTTP 技術,簡單的意思是,
CoAP 簡化了 HTTP 的內容,輕量化的意思
是,CoAP 採⽤用 UDP 進⾏行傳輸。
CoAP over UDP
UDP
IP
CoAP
WoT Server
IoT over the Web
Ethernet MAC
Ethernet PHY
Application
Transport
Network
Data Link
Physical
TLS (optional) Presentation / Session
HTTP over UDP
簡單來說,CoAP 可以看做是⼀一個 HTTP over
UDP 的技術。CoAP 是物聯網的重要技術,它
讓 Constrained Device 都能具備 HTTP 的
能⼒力。⼤大部份的 MCU 裝置都是 Constrained
Device,因此,就也像是 MCU + HTTP。
HTTP over TCP
TCP
IP
HTTP 1.1/2.0
WoT Server
the Web
Ethernet MAC
Ethernet PHY
Application
Transport
Network
Data Link
Physical
TLS (optional) Presentation / Session
SPDY
因為 HTTP request/response headers 設
計上的⼀一些缺點,讓 HTTP 的網路傳輸效能無
法提昇。為解決這些問題,Google 便提出了
SPDY 協定。SPDY 協定後來成為 HTTP/2
(HTTP 2.0)的基礎。IETF 在 2015 年 5 ⽉月
正式發佈 HTTP/2 標準(RFC 7540)。
HTTP/2 是基於 TCP 協定,因此要讓物聯網裝
置使⽤用 HTTP over UDP 的話,⺫⽬目前仍必須使
⽤用 HTTP + QUIC + UDP 的堆疊。
HTTP/1.1 over SPDY+TCP
TCP
IP
HTTP
WoT Server
the Web
Ethernet MAC
Ethernet PHY
Application
Transport
Network
Data Link
Physical
SPDY + TLS Presentation / Session
QUIC
HTTP over TCP 的 ACK 會造成的⼀一些負擔,
因此如果讓 HTTP over UDP 的話,就可以解
決這個問題。Google 所提出的 QUIC(Quick
UDP Internet Connection)就是這樣的技
術。QUIC 可以讓 HTTP 基於 UDP 傳輸層,就
是 HTTP + QUIC + UDP。
HTTP over QUIC+UDP
HTTP/2 未來也可能在物聯網應⽤用上,扮演重
要⾓角⾊色。因為 HTTP request/response
headers 設計上的⼀一些缺點,讓 HTTP 的網路
傳輸效能無法提昇。為解決這些問題,Google
便提出了 SPDY 協定。SPDY 協定後來成為
HTTP/2(HTTP 2.0)的基礎。IETF 在
2015 年 5 ⽉月正式發佈 HTTP/2 標準(RFC
7540)。HTTP/2 是基於 TCP 協定,因此要
讓物聯網裝置使⽤用 HTTP over UDP 的話,⺫⽬目
前仍必須使⽤用 HTTP + QUIC + UDP 的堆
疊。
UDP + QUIC
IP
HTTP
WoT Server
IoT over the Web
Ethernet MAC
Ethernet PHY
Application
Transport
Network
Data Link
Physical
SPDY + QUIC Presentation / Session
HTTP over QUIC+UDP
UDP + QUIC
IP
HTTP
WoT Server
IoT over the Web
Ethernet MAC
Ethernet PHY
QUIC
HTTP/2 over QUIC
因為 HTTP/2 標準就是 SPDY 的內容,如果有
意在物聯網裝置上使⽤用 HTTP/2 的特性,就要
採⽤用 HTTP + SPDY + QUIC + UDP 的堆疊。
不過,Google 未來有意將 HTTP/2 over
QUIC 提交給 IETF,到時就能捨棄 HTTP +
SPDY + QUIC + UDP 的做法,畢竟這只是過
渡時期的解決⽅方案。
IP
HTTP/2
WoT Server
IoT over the Web
Ethernet MAC
Ethernet PHY
Application
Transport
Network
Data Link
Physical
QUIC + UDP
Presentation / Session
HTTP/2 over QUIC
CoAP-HTTP Translate
CoAP 並⾮非直接採⽤用 HTTP 標準,⽽而是透過轉
換(translate)的⽅方式將訊息對應成標準的
HTTP。CoAP 採納了 REST 架構,並且也是採
取 request/response 的模式。因此,要將
CoAP 轉換為 HTTP,或是將 HTTP 轉換為
CoAP,其實是⾮非常容易的。實際上,CoAP 只
對 request/response 的部份做轉換,也就是
CoAP 的 request 都能轉換為 HTTP request
headers;response 的部份亦同。
CoAP-HTTP
UDP
IP
CoAP
WoT Server
Ethernet MAC
Ethernet PHY
Application
Transport
Network
Data Link
Physical
TLS (optional) Presentation / Session
TCP
IP
HTTP
WoT Client
the Web
Ethernet MAC
Ethernet PHY
TLS (optional)
IoT over the Web
Transferring HTTP Message
IP
TCP
HTTP
Ethernet
Interface
Web client
IP
TCP
HTTP
Ethernet
Interface
Web server
HTTP message
TCP segment
Transferring CoAP Message
IP
UDP
HTTP
Ethernet
Interface
Web client
IP
UDP
HTTP
Ethernet
Interface
Web server
CoAP message
UDP segment
TCP Connection
B
SYN
A
SYN-ACK
ACK
DATA
ACK
DATA
FIN
.
.
.
IoT/WoT Interoperability
Websocket
HTTP 1.1/2.0
CoAP
IoT
Cloud
IoT Device
IoT Device
IoT Device IoT Device
Mobile &
Client
IoT Proxy
Thread
A secure wireless mesh network for
smart home.
IEEE 802.15.4 MAC
IEEE 802.15.4 PHY
6LoWPAN (IPv6)
IP Routing
UDP + DTLS
Application
Thread
RFC 4944, RFC 4862, RFC 6775
RFC 1058, RFC 2080
RFC 768, RFC 6347, RFC 4279
RFC 4492v RFC 3315, 5007
6LoWPAN
IPv6 over Low power Wireless Personal
Area Networks.
For constrained IoT devices.
UDP
6LoWPAN
CoAP
EXI
TCP
IP
HTTP
HTML
the Web IoT + the Web
TCP/IP Protocol Stack
UDP
6LoWPAN (IPv6)
CoAP
WoT Application
TCP
IP
HTTP
HTML
the Web IoT + the Web
UDP
Ethernet MAC
Ethernet PHY
IEEE 802.15.4 MAC
IEEE 802.15.4 PHY
Application
Transport
Network
Data Link
Physical
Source: http://coap.technology
REST model for small
devices
!
Like HTTP, CoAP is based on the wildly
successful REST model: Servers make
resources available under a URL, and
clients access these resources using
methods such as GET, PUT, POST, and
DELETE.
Source: http://coap.technology
Made for billions of
nodes
!
The Internet of Things will need billions of
nodes, many of which will need to be
inexpensive. CoAP has been designed to
work on microcontrollers with as low as 10
KiB of RAM and 100 KiB of code space (RFC
7228).
Source: http://coap.technology
Existing skills
transfer
!
From a developer point of view, CoAP feels
very much like HTTP. Obtaining a value
from a sensor is not much different from
obtaining a value from a Web API.
Source: http://coap.technology
ARM IoT Tutorial	

https://www.youtube.com/watch?v=4bSr5x5gKvA
Ready for
integration
!
Since HTTP and CoAP share the REST
model, they can easily be connected using
application-agnostic cross-protocol
proxies. A Web client may not even notice
that it just accessed a sensor resource!
ARM mbed Priactice: CoAP
CoapPDU *pdu = new CoapPDU();
!
pdu->setType(CoapPDU::COAP_CONFIRMABLE);
pdu->setCode(CoapPDU::COAP_GET);
pdu->setToken((uint8_t*)"3210",4);
pdu->setMessageID(0x0005);
pdu->setURI((char*)"test",4);
!
// send packet 
ret = send(sockfd,pdu->getPDUPointer(),pdu->getPDULength(),0);
cont…
// receive packet
ret = recvfrom(sockfd,&buffer,BUF_LEN,0,
(sockaddr*)&recvAddr,&recvAddrLen);
!
CoapPDU *recvPDU = new CoapPDU((uint8_t*)buffer,ret);
!
if(recvPDU->validate()) {
recvPDU->getURI(uriBuffer,URI_BUF_LEN,&recvURILen);
...
}
IoT & mbed Cloud - Device
Server
Analogous to a Web Server that accepts connections from
mobile phones or web browsers, a Device Server handles
the connections from Internet of Things (IoT) devices.
A Device Server is a key enabler for cloud service providers, operators and
enterprises to access the IoT growth market with production deployments,
bringing end node devices in to the world of web services.
《ARM mbed Practice: RTOS, IoT Client & IoT Cloud》 Copyright (C) 2015 Moko365 Inc. 僅供 QUALCOMM 內部使⽤用.
Source: http://mbed.org/technology/device-server/
《ARM mbed Practice: RTOS, IoT Client & IoT Cloud》 Copyright (C) 2015 Moko365 Inc. 僅供 QUALCOMM 內部使⽤用.
CoAP Server Architecture
Websocket
HTTP 1.1/2.0
CoAP
www.mokoversity.com
IoT
Cloud
IoT Device
IoT Device
IoT Proxy
1
2
IoT Device
2
3
Mobile &
Client
3
RESTful
UDP
M2M
Wireless Sensor Network
HTTP in Uniform way
Simple Cache
CoAP Features
IoT Device
IoT Device
IoT Proxy
1
2
IoT Device
2
3
Constrained Environment
3
Websocket
HTTP 1.1/2.0
CoAP
CoAP Portfolio
IoT
Cloud
IoT Device
IoT Device
IoT Proxy
(IoT Gateway)
1
2
IoT Device
2
3
Mobile &
Client
The Web of Things Protocol
3
Arch Pro
Arch Pro
Arch Pro
Seeeduino Cloud
http://50.128.14.32/1/jollenchen/sensor/dust/a
IoT Device Mobile
HTTP 1.1/2.0HTTP 1.1/2.0
Light-weight
Web server
Web 
Frontend
Internet
Device Server: HTTP
IoT Device Mobile
HTTP 1.1/2.0
Streaming Data
Physical Object
Web 
Frontend
Real-Time Data
Broker
ws://wot.city/object/jollenchen/sensor/dust/a
Device Server: WebSocket
Device Server Architecture
Websocket
HTTP 1.1/2.0
CoAP
IoT
Cloud
IoT Device
IoT Device
IoT Device IoT Device
Mobile &
Client
IoT Proxy
Architecture of 6LoWPAN
IoT Device
Mobile
Physical 
Object Web 
Frontend
Broker
IoT Device Proxy
(Gateway / Router)
IPv6, CoAP
6LoWPAN, CoAP
6LoWPAN, CoAP
IPv6, HTTP, HTML5
HTTP/CoAP/MQTT
HTTP
The Web protocol.
!
CoAP
The Web of Things 
Protocol.
!
MQTT
The TCP Protocol.
HTTP CoAP MQTT
Type document oriented document oriented message oriented
Purpose server farms
constrained
devices
lightweight M2M
communications
Transport over TCP over UDP over TCP
Model Client/Server Client/Server Client/Server
Resolver URI REST Message
Interoperate one-to-one many-to-many
Architecture request/response request/response publish/subscribe
HTTP/CoAP/MQTT
MQTT Clients and Broker
Source: http://www.eclipse.org/community/eclipse_newsletter/2014/february/article2.php
ARM mbed Practice
mbed Tools
Digital Interface
Networking
HTTPD & REST API
Websocket
Device Server
《ARM mbed Practice: RTOS, IoT Client & IoT Cloud》 Copyright (C) 2015 Moko365 Inc. 僅供 QUALCOMM 內部使⽤用.
Digital Interface
#include "mbed.h"
!
DigitalOut myled1(LED1);
!
int main() {
wait(0.8);

while(1) { 
myled1 = 0;
wait(0.2);
myled1 = 1;
}
}
Networking
#include "mbed.h"
#include "EthernetInterface.h"
!
int main(void) {
// Ethernet Interface
EthernetInterface eth;
!
// use DHCP
eth->init();
!
//eth->init(“192.168.21.81”, "255.255.255.0", "192.168.21.2" );
!
if (eth->connect()) 
return -1;
!
printf("IP Address is %srn", eth->getIPAddress());
}
HTTPD
httpd = new HTTPD;
!
httpd->attach("/1/mbed/lpc1768/sensor/dust/sen12291p", &callback_api);
!
httpd->attach("/", "/local/");
!
httpd->start(80);
REST APIs over mbed
int main() {
printf("HTTP Server...rn");
!
eth = new EthernetInterface;
eth->init("192.168.21.81", "255.255.255.0", "192.168.21.2" );
!
if (eth->connect()) return -1;
!
printf("IP Address is %srn", eth->getIPAddress());
httpd = new HTTPD;
httpd->attach("/1/mbed/lpc1768/sensor/dust/sen12291p", &callback_api);
httpd->attach("/", "/local/");
httpd->start(80);
printf("httpd readyrn");
led2 = 1; 
}
mbed WebSocket Server
IoT 扮演 Websocket server 的話,會有幾個技
術問題:

 •
 ARM mbed 要管理 client 端的
connections

 •
 需要更多的記億體來維護 client
connections

 •
 需要實作 Data push 的演算法

 •
 要考量 error handling 與 exception
handling
WebSocket Broker
WebSocket Broker 的使⽤用案例:
!

 1.
佈署專⽤用的 Websocket broker server

 2.
ARM mbed 將 data 即時推送(push)
到 Websocket broker server

 3.
⽤用⼾戶(user)與 Websocket broker
server 建⽴立 Websocket connection

 4.
⽤用⼾戶接收 Websocket broker server 的
即時資料
WoT.City
Copyright (C) 2015 WoT.City, Inc. All rights reserved.
About us.
!
Jollen, Founder
jollen@wotcity.com
+
ARM
mbed
Available Now
ARM mbed with WoT
+
Coming Soon
Intel Edison with WoT
+
Under Development
Dragonboard 410c with WoT
+
Launch Soon
NodeMCU/ESP8266 with WoT
Constrained
IoT Devices
ws://wot.city
Websocket
Client 1
Client 2
...
1 to 1
n to n
Sender Client
Broker / Load Balancer
(Data Channel) Viewer Client
WebSocket Broker Server
Available Now
Constrained
IoT Devices
coap://wot.city
Websocket & HTTP
Client 1
Client 2
...
1 to 1
n to n
Sender Client
Broker / Load Balancer
(Data Channel) Viewer Client
CoAP Broker Server
Available Now
CoAP
WoT Platform Website
WoT Open Source
Contact
Angel List
https://wotcity.com/zh-tw
https://github.com/wotcity
hello@wotcity.com
https://angel.co/wot-city
(C) 2015 wotcity.com

Maker of Things - the open IoT cloud for makers chapter.

  • 1.
    2015.9.7 Taipei Maker of Things Open IoT Cloud for Makers Innovation of Things Mokoversity 姓名標示-非商 業性 4.0 國際
  • 2.
    Lecturer Jollen, Founder ofWoT.City ! Jollen is a passionate software developer with a breadth of technical experience with software engineering, web technologies, embedded systems, operating systems and distributed systems. ! Jollen specializes in open source software business model and its strategy planning. He is also the founder of Mokoversity and WoT.City. ! Please Email Jollen through jollen@jollen.org
  • 3.
    1 ARM mbed The nextbig OS to rock the world will be little.
  • 4.
    2 Web Trends2015 Virtual DOM
  • 5.
    3 Physical Web Thefuture of the IoT is URLs.
  • 6.
  • 7.
  • 8.
  • 9.
    Constrained! Device High Performance MCU WiFi MCU SBC Server! Farms Platform Portfolio Legacy MCUs ARM mbed OS、 Neuclio ESP8266、 NodeMCU、 EMW3165 Intel Edison、 Qualcomm Dragonboard 410c IoT Diagram (Use Scenario) WoT Architecture IoT Protocols TD
  • 10.
    #1: IoT &WoT Introduction
  • 11.
    Web of Things 《ARMmbed Practice: RTOS, IoT Client & IoT Cloud》 Copyright (C) 2015 Moko365 Inc. 僅供 QUALCOMM 內部使⽤用.
  • 12.
    the Web ofThings reuses existing and well-known Web standards[1][2] used in the programmable Web (e.g., REST, HTTP, JSON), semantic Web (e.g., JSON-LD, Microdata, etc.), the real-time Web (e.g., Websockets) and the social Web (e.g., oauth or social networks). ! —Wikipedia Web of Things 《ARM mbed Practice: RTOS, IoT Client & IoT Cloud》 Copyright (C) 2015 Moko365 Inc. 僅供 QUALCOMM 內部使⽤用.
  • 13.
  • 14.
  • 15.
  • 16.
    IoT WoT 連結⽅方式 BluetoothHTTP 數據管理 Centerlized Decenterlized Things Objects RESTful Objects 應⽤用開發模式 Native Web (SPA) 硬件概念 Sensor Device Physical Web (Sensor Fusion) Figure: Thinking in WoT. Copyright (C) 2014 Mokoversity Inc. IoT vs WoT: Methdology
  • 17.
    IoT WoT Connectivity Machineto Machine LWM2M Machine to Web Networking TCP / UDP HTTP / REST / CoAP Things Sensor Device RESTful Objects Data Streaming Data Time-Series Data Discover MQTT Publish / Subscribe IoT vs WoT: Connectivity
  • 18.
  • 19.
    Internet of Things Webof Things An Application Layer that simplifies the creation of Internet of Things applications * of Things
  • 20.
    Thinking in WoT IoTWoT 连接⽅方式 Bluetooth HTTP 数据管理哲学 Centerlized Decenterlized Things Objects RESTful Objects IoT App Native Web (SPA) 硬件设计哲学 Sensor Device Physical Web (Sensor Fusion)
  • 21.
    IoT & mbedClient - RTOS
  • 22.
    ARM mbed mbed OS Weare creating a modern full-stack operating system that is designed specifically for ARM Cortex®-M-based MCUs; the worlds leading 32- bit microcontrollers that already sell in the billions. ! mbed Device Server Analogous to a Web Server that accepts connections from mobile phones or web browsers, a Device Server handles the connections from IoT devices. ! mbed Tools online compiler (Web IDE), SDK, HDK and etc.
  • 23.
    mbed OS mbed DeviceServer mbed Tools 《ARM mbed Practice: RTOS, IoT Client & IoT Cloud》 Copyright (C) 2015 Moko365 Inc. 僅供 QUALCOMM 內部使⽤用.
  • 24.
    Source: http://mbed.org/technology/os/ mbed OS mbed™OS is an operating system for IoT devices and is especially well-suited to run in energy constrained environments.
  • 25.
  • 26.
    mbed Libraries Analog I/O DigitalI/O Timers Digital Interface Real-time Operating System File System USB (Device and Host) Networking
  • 27.
    Connectivity Protocol Stack BluetoothLow Energy Cellular Ethernet Thread Wi-Fi Zigbee IP Zigbee NAN 6LoWPAN
  • 28.
    mbed Memory Sections RWData Program RAM Flash RW Data Program ZI = 0 RW Data Stack Heap
  • 29.
    mbed Threads RAM Flash RW Data ZI:Timer Stack ZI: Idle Stack ZI Scheduler Stack Main Thread Stack Head When you use the RTOS, before explicitly initializing any additional thread, you will have 4 separate stacks: ! • The stack of the Main Thread (executing the main function). ! • The Idle Thread executed each time all the other threads are waiting for external, or scheduled events. This is particularly useful to implement energy saving strategies. (ie sleep). ! • The Timer Thread that executes all the time scheduled tasks (periodic and non periodic). ! • The stack of OS Scheduler itself (also used by the ISRs).
  • 30.
    mbed Timer #include "mbed.h" Timert; int main() { t.start(); printf("Hello World!n"); t.stop(); printf("The time taken was %f secondsn", t.read()); } See: https://developer.mbed.org/handbook/Timer
  • 31.
    mbed Thread API #include"mbed.h" #include "rtos.h" DigitalOut led1(LED1); DigitalOut led2(LED2); void led2_thread(void const *args) { while (true) { led2 = !led2; Thread::wait(1000); } } int main() { Thread thread(led2_thread); while (true) { led1 = !led1; Thread::wait(500); } } See: https://developer.mbed.org/handbook/RTOS#thread
  • 32.
    mbed Thread Priority See:https://developer.mbed.org/handbook/RTOS#thread int main() { Thread thread(led2_thread); thread.set_priority(osPriorityNormal); ! while (true) { led1 = !led1; Thread::wait(500); } }
  • 33.
    mbed Thread States See:https://developer.mbed.org/handbook/RTOS#thread
  • 34.
    mbed Mutex See: https://developer.mbed.org/handbook/RTOS #include"mbed.h" #include "rtos.h" Mutex stdio_mutex; void notify(const char* name, int state) { stdio_mutex.lock(); printf("%s: %dnr", name, state); stdio_mutex.unlock(); } void test_thread(void const *args) { while (true) { notify((const char*)args, 0); Thread::wait(1000); notify((const char*)args, 1); Thread::wait(1000); } } int main() { Thread t2(test_thread, (void *)"Th 2"); Thread t3(test_thread, (void *)"Th 3"); test_thread((void *)"Th 1"); }
  • 35.
    #include "mbed.h" #include "rtos.h" Semaphoretwo_slots(2); void test_thread(void const *name) { while (true) { two_slots.wait(); printf("%snr", (const char*)name); Thread::wait(1000); two_slots.release(); } } int main (void) { Thread t2(test_thread, (void *)"Th 2"); Thread t3(test_thread, (void *)"Th 3"); test_thread((void *)"Th 1"); } mbed Semaphore See: https://developer.mbed.org/handbook/RTOS
  • 36.
    IoT & mbedClient - IoT
  • 37.
    Layering of Protocols IP UDP SMTPDNSHTTP TCP CoAP FTP Ethernet
  • 38.
    CoAP CoAP 並不是要取代 HTTP,它是針對 ConstrainedDevice 的 HTTP 需求。CoAP (Constrained Application Protocol)是更 簡單且輕量化的 HTTP 技術,簡單的意思是, CoAP 簡化了 HTTP 的內容,輕量化的意思 是,CoAP 採⽤用 UDP 進⾏行傳輸。
  • 39.
    CoAP over UDP UDP IP CoAP WoTServer IoT over the Web Ethernet MAC Ethernet PHY Application Transport Network Data Link Physical TLS (optional) Presentation / Session
  • 40.
    HTTP over UDP 簡單來說,CoAP可以看做是⼀一個 HTTP over UDP 的技術。CoAP 是物聯網的重要技術,它 讓 Constrained Device 都能具備 HTTP 的 能⼒力。⼤大部份的 MCU 裝置都是 Constrained Device,因此,就也像是 MCU + HTTP。
  • 41.
    HTTP over TCP TCP IP HTTP1.1/2.0 WoT Server the Web Ethernet MAC Ethernet PHY Application Transport Network Data Link Physical TLS (optional) Presentation / Session
  • 42.
    SPDY 因為 HTTP request/responseheaders 設 計上的⼀一些缺點,讓 HTTP 的網路傳輸效能無 法提昇。為解決這些問題,Google 便提出了 SPDY 協定。SPDY 協定後來成為 HTTP/2 (HTTP 2.0)的基礎。IETF 在 2015 年 5 ⽉月 正式發佈 HTTP/2 標準(RFC 7540)。 HTTP/2 是基於 TCP 協定,因此要讓物聯網裝 置使⽤用 HTTP over UDP 的話,⺫⽬目前仍必須使 ⽤用 HTTP + QUIC + UDP 的堆疊。
  • 43.
    HTTP/1.1 over SPDY+TCP TCP IP HTTP WoTServer the Web Ethernet MAC Ethernet PHY Application Transport Network Data Link Physical SPDY + TLS Presentation / Session
  • 44.
    QUIC HTTP over TCP的 ACK 會造成的⼀一些負擔, 因此如果讓 HTTP over UDP 的話,就可以解 決這個問題。Google 所提出的 QUIC(Quick UDP Internet Connection)就是這樣的技 術。QUIC 可以讓 HTTP 基於 UDP 傳輸層,就 是 HTTP + QUIC + UDP。
  • 45.
    HTTP over QUIC+UDP HTTP/2未來也可能在物聯網應⽤用上,扮演重 要⾓角⾊色。因為 HTTP request/response headers 設計上的⼀一些缺點,讓 HTTP 的網路 傳輸效能無法提昇。為解決這些問題,Google 便提出了 SPDY 協定。SPDY 協定後來成為 HTTP/2(HTTP 2.0)的基礎。IETF 在 2015 年 5 ⽉月正式發佈 HTTP/2 標準(RFC 7540)。HTTP/2 是基於 TCP 協定,因此要 讓物聯網裝置使⽤用 HTTP over UDP 的話,⺫⽬目 前仍必須使⽤用 HTTP + QUIC + UDP 的堆 疊。
  • 46.
    UDP + QUIC IP HTTP WoTServer IoT over the Web Ethernet MAC Ethernet PHY Application Transport Network Data Link Physical SPDY + QUIC Presentation / Session HTTP over QUIC+UDP UDP + QUIC IP HTTP WoT Server IoT over the Web Ethernet MAC Ethernet PHY QUIC
  • 47.
    HTTP/2 over QUIC 因為HTTP/2 標準就是 SPDY 的內容,如果有 意在物聯網裝置上使⽤用 HTTP/2 的特性,就要 採⽤用 HTTP + SPDY + QUIC + UDP 的堆疊。 不過,Google 未來有意將 HTTP/2 over QUIC 提交給 IETF,到時就能捨棄 HTTP + SPDY + QUIC + UDP 的做法,畢竟這只是過 渡時期的解決⽅方案。
  • 48.
    IP HTTP/2 WoT Server IoT overthe Web Ethernet MAC Ethernet PHY Application Transport Network Data Link Physical QUIC + UDP Presentation / Session HTTP/2 over QUIC
  • 49.
    CoAP-HTTP Translate CoAP 並⾮非直接採⽤用HTTP 標準,⽽而是透過轉 換(translate)的⽅方式將訊息對應成標準的 HTTP。CoAP 採納了 REST 架構,並且也是採 取 request/response 的模式。因此,要將 CoAP 轉換為 HTTP,或是將 HTTP 轉換為 CoAP,其實是⾮非常容易的。實際上,CoAP 只 對 request/response 的部份做轉換,也就是 CoAP 的 request 都能轉換為 HTTP request headers;response 的部份亦同。
  • 50.
    CoAP-HTTP UDP IP CoAP WoT Server Ethernet MAC EthernetPHY Application Transport Network Data Link Physical TLS (optional) Presentation / Session TCP IP HTTP WoT Client the Web Ethernet MAC Ethernet PHY TLS (optional) IoT over the Web
  • 51.
    Transferring HTTP Message IP TCP HTTP Ethernet Interface Webclient IP TCP HTTP Ethernet Interface Web server HTTP message TCP segment
  • 52.
    Transferring CoAP Message IP UDP HTTP Ethernet Interface Webclient IP UDP HTTP Ethernet Interface Web server CoAP message UDP segment
  • 53.
  • 54.
    IoT/WoT Interoperability Websocket HTTP 1.1/2.0 CoAP IoT Cloud IoTDevice IoT Device IoT Device IoT Device Mobile & Client IoT Proxy
  • 55.
    Thread A secure wirelessmesh network for smart home. IEEE 802.15.4 MAC IEEE 802.15.4 PHY 6LoWPAN (IPv6) IP Routing UDP + DTLS Application Thread RFC 4944, RFC 4862, RFC 6775 RFC 1058, RFC 2080 RFC 768, RFC 6347, RFC 4279 RFC 4492v RFC 3315, 5007
  • 56.
    6LoWPAN IPv6 over Lowpower Wireless Personal Area Networks. For constrained IoT devices. UDP 6LoWPAN CoAP EXI TCP IP HTTP HTML the Web IoT + the Web
  • 57.
    TCP/IP Protocol Stack UDP 6LoWPAN(IPv6) CoAP WoT Application TCP IP HTTP HTML the Web IoT + the Web UDP Ethernet MAC Ethernet PHY IEEE 802.15.4 MAC IEEE 802.15.4 PHY Application Transport Network Data Link Physical
  • 59.
    Source: http://coap.technology REST modelfor small devices ! Like HTTP, CoAP is based on the wildly successful REST model: Servers make resources available under a URL, and clients access these resources using methods such as GET, PUT, POST, and DELETE.
  • 60.
    Source: http://coap.technology Made forbillions of nodes ! The Internet of Things will need billions of nodes, many of which will need to be inexpensive. CoAP has been designed to work on microcontrollers with as low as 10 KiB of RAM and 100 KiB of code space (RFC 7228).
  • 61.
    Source: http://coap.technology Existing skills transfer ! Froma developer point of view, CoAP feels very much like HTTP. Obtaining a value from a sensor is not much different from obtaining a value from a Web API.
  • 62.
    Source: http://coap.technology ARM IoTTutorial https://www.youtube.com/watch?v=4bSr5x5gKvA Ready for integration ! Since HTTP and CoAP share the REST model, they can easily be connected using application-agnostic cross-protocol proxies. A Web client may not even notice that it just accessed a sensor resource!
  • 63.
    ARM mbed Priactice:CoAP CoapPDU *pdu = new CoapPDU(); ! pdu->setType(CoapPDU::COAP_CONFIRMABLE); pdu->setCode(CoapPDU::COAP_GET); pdu->setToken((uint8_t*)"3210",4); pdu->setMessageID(0x0005); pdu->setURI((char*)"test",4); ! // send packet ret = send(sockfd,pdu->getPDUPointer(),pdu->getPDULength(),0);
  • 64.
    cont… // receive packet ret= recvfrom(sockfd,&buffer,BUF_LEN,0, (sockaddr*)&recvAddr,&recvAddrLen); ! CoapPDU *recvPDU = new CoapPDU((uint8_t*)buffer,ret); ! if(recvPDU->validate()) { recvPDU->getURI(uriBuffer,URI_BUF_LEN,&recvURILen); ... }
  • 65.
    IoT & mbedCloud - Device Server
  • 66.
    Analogous to aWeb Server that accepts connections from mobile phones or web browsers, a Device Server handles the connections from Internet of Things (IoT) devices. A Device Server is a key enabler for cloud service providers, operators and enterprises to access the IoT growth market with production deployments, bringing end node devices in to the world of web services. 《ARM mbed Practice: RTOS, IoT Client & IoT Cloud》 Copyright (C) 2015 Moko365 Inc. 僅供 QUALCOMM 內部使⽤用.
  • 67.
    Source: http://mbed.org/technology/device-server/ 《ARM mbedPractice: RTOS, IoT Client & IoT Cloud》 Copyright (C) 2015 Moko365 Inc. 僅供 QUALCOMM 內部使⽤用.
  • 68.
    CoAP Server Architecture Websocket HTTP1.1/2.0 CoAP www.mokoversity.com IoT Cloud IoT Device IoT Device IoT Proxy 1 2 IoT Device 2 3 Mobile & Client 3
  • 69.
    RESTful UDP M2M Wireless Sensor Network HTTPin Uniform way Simple Cache CoAP Features IoT Device IoT Device IoT Proxy 1 2 IoT Device 2 3 Constrained Environment 3
  • 70.
    Websocket HTTP 1.1/2.0 CoAP CoAP Portfolio IoT Cloud IoTDevice IoT Device IoT Proxy (IoT Gateway) 1 2 IoT Device 2 3 Mobile & Client The Web of Things Protocol 3 Arch Pro Arch Pro Arch Pro Seeeduino Cloud
  • 71.
    http://50.128.14.32/1/jollenchen/sensor/dust/a IoT Device Mobile HTTP1.1/2.0HTTP 1.1/2.0 Light-weight Web server Web Frontend Internet Device Server: HTTP
  • 72.
    IoT Device Mobile HTTP1.1/2.0 Streaming Data Physical Object Web Frontend Real-Time Data Broker ws://wot.city/object/jollenchen/sensor/dust/a Device Server: WebSocket
  • 73.
    Device Server Architecture Websocket HTTP1.1/2.0 CoAP IoT Cloud IoT Device IoT Device IoT Device IoT Device Mobile & Client IoT Proxy
  • 74.
    Architecture of 6LoWPAN IoTDevice Mobile Physical Object Web Frontend Broker IoT Device Proxy (Gateway / Router) IPv6, CoAP 6LoWPAN, CoAP 6LoWPAN, CoAP IPv6, HTTP, HTML5
  • 75.
    HTTP/CoAP/MQTT HTTP The Web protocol. ! CoAP TheWeb of Things Protocol. ! MQTT The TCP Protocol.
  • 76.
    HTTP CoAP MQTT Typedocument oriented document oriented message oriented Purpose server farms constrained devices lightweight M2M communications Transport over TCP over UDP over TCP Model Client/Server Client/Server Client/Server Resolver URI REST Message Interoperate one-to-one many-to-many Architecture request/response request/response publish/subscribe HTTP/CoAP/MQTT
  • 77.
    MQTT Clients andBroker Source: http://www.eclipse.org/community/eclipse_newsletter/2014/february/article2.php
  • 78.
  • 79.
    mbed Tools Digital Interface Networking HTTPD& REST API Websocket Device Server 《ARM mbed Practice: RTOS, IoT Client & IoT Cloud》 Copyright (C) 2015 Moko365 Inc. 僅供 QUALCOMM 內部使⽤用.
  • 80.
    Digital Interface #include "mbed.h" ! DigitalOutmyled1(LED1); ! int main() { wait(0.8); while(1) { myled1 = 0; wait(0.2); myled1 = 1; } }
  • 81.
    Networking #include "mbed.h" #include "EthernetInterface.h" ! intmain(void) { // Ethernet Interface EthernetInterface eth; ! // use DHCP eth->init(); ! //eth->init(“192.168.21.81”, "255.255.255.0", "192.168.21.2" ); ! if (eth->connect()) return -1; ! printf("IP Address is %srn", eth->getIPAddress()); }
  • 82.
    HTTPD httpd = newHTTPD; ! httpd->attach("/1/mbed/lpc1768/sensor/dust/sen12291p", &callback_api); ! httpd->attach("/", "/local/"); ! httpd->start(80);
  • 83.
    REST APIs overmbed int main() { printf("HTTP Server...rn"); ! eth = new EthernetInterface; eth->init("192.168.21.81", "255.255.255.0", "192.168.21.2" ); ! if (eth->connect()) return -1; ! printf("IP Address is %srn", eth->getIPAddress()); httpd = new HTTPD; httpd->attach("/1/mbed/lpc1768/sensor/dust/sen12291p", &callback_api); httpd->attach("/", "/local/"); httpd->start(80); printf("httpd readyrn"); led2 = 1; }
  • 84.
    mbed WebSocket Server IoT扮演 Websocket server 的話,會有幾個技 術問題: • ARM mbed 要管理 client 端的 connections • 需要更多的記億體來維護 client connections • 需要實作 Data push 的演算法 • 要考量 error handling 與 exception handling
  • 85.
    WebSocket Broker WebSocket Broker的使⽤用案例: ! 1. 佈署專⽤用的 Websocket broker server 2. ARM mbed 將 data 即時推送(push) 到 Websocket broker server 3. ⽤用⼾戶(user)與 Websocket broker server 建⽴立 Websocket connection 4. ⽤用⼾戶接收 Websocket broker server 的 即時資料
  • 86.
    WoT.City Copyright (C) 2015WoT.City, Inc. All rights reserved. About us. ! Jollen, Founder jollen@wotcity.com
  • 87.
  • 88.
  • 89.
  • 90.
  • 91.
    Constrained IoT Devices ws://wot.city Websocket Client 1 Client2 ... 1 to 1 n to n Sender Client Broker / Load Balancer (Data Channel) Viewer Client WebSocket Broker Server Available Now
  • 92.
    Constrained IoT Devices coap://wot.city Websocket &HTTP Client 1 Client 2 ... 1 to 1 n to n Sender Client Broker / Load Balancer (Data Channel) Viewer Client CoAP Broker Server Available Now CoAP
  • 93.
    WoT Platform Website WoTOpen Source Contact Angel List https://wotcity.com/zh-tw https://github.com/wotcity hello@wotcity.com https://angel.co/wot-city (C) 2015 wotcity.com