SlideShare a Scribd company logo
1 of 16
Download to read offline
電子工程系應 用 電 子 組
電 腦 遊 戲 設 計 組
使用HTTP RESTful
API控制IO
吳錫修
November 20, 2015
shapethefuture
電子工程系
應 用 電 子 組
電 腦 遊 戲 設 計 組
 Bridge函式庫提供AVR及Linux處理器之間的介接
 Bridge函式庫提供RESTful web service作業,透過REST樣式之
URL字串命令,來指示操作動作與回應訊息
REST API
2
shapethefuture
電子工程系
應 用 電 子 組
電 腦 遊 戲 設 計 組
 範例:File/Examples/Bridge/Bridge
 展示如何透過HTTP REST calls方式來存取Arduino Yún控制板之IO;
我們可以參照Bridge程式,使用REST設計風格來創建自己的Web
API
 Possible commands created in this shetch:
 "/arduino/digital/13" -> digitalRead(13)
 "/arduino/digital/13/1" -> digitalWrite(13, HIGH)
 "/arduino/analog/2/123" -> analogWrite(2, 123)
 "/arduino/analog/2" -> analogRead(2)
 "/arduino/mode/13/input" -> pinMode(13, INPUT)
 "/arduino/mode/13/output" -> pinMode(13, OUTPUT)
 線上解說
http://www.arduino.cc/en/Tutorial/Bridge
Bridge範例
3
shapethefuture
電子工程系
應 用 電 子 組
電 腦 遊 戲 設 計 組
 登入Arduino Yún,確認網路連線正常
測試Bridge範例 1/3
4
shapethefuture
電子工程系
應 用 電 子 組
電 腦 遊 戲 設 計 組
 使用sswuyun.local/arduino/digital/13/0關閉Arduino Yún D13
LED
測試Bridge範例 2/3
5
shapethefuture
電子工程系
應 用 電 子 組
電 腦 遊 戲 設 計 組
 使用sswuyun.local/arduino/digital/13/1開啟Arduino Yún D13
LED
測試Bridge範例 3/3
6
直接指定Arduino Yún IP可加快連線速度
shapethefuture
電子工程系
應 用 電 子 組
電 腦 遊 戲 設 計 組
#include <Bridge.h>
#include <YunServer.h>
#include <YunClient.h>
YunServer server; //建立YunServer物件
void setup() {
pinMode(13, OUTPUT);
digitalWrite(13, LOW);
Bridge.begin(); //啟動Bridge,約需2秒時間
digitalWrite(13, HIGH); //點亮LED表示完成Bridge啟動作業
server.listenOnLocalhost();//若要從internet連接,須註解此行
server.begin(); //啟動YunServer
}
解析Bridge範例程式碼 1/10
7
shapethefuture
電子工程系
應 用 電 子 組
電 腦 遊 戲 設 計 組
void loop() {
YunClient client = server.accept(); //取出用戶連線URL字串
if (client) {
process(client); //開始處理,解析URL
client.stop(); //中止連線
}
delay(50); //間隔50ms再重複作業
}
解析Bridge範例程式碼 2/10
8
shapethefuture
電子工程系
應 用 電 子 組
電 腦 遊 戲 設 計 組
void process(YunClient client) { //處理REST CALL
String command = client.readStringUntil(‘/’); //解析第⼀層
if (command == "digital") {
digitalCommand(client); //處理數位IO指令
}
if (command == "analog") {
analogCommand(client); //處理類比IO指令
}
if (command == "mode") {
modeCommand(client); //處理接腳IO設定指令
}
}
解析Bridge範例程式碼 3/10
9
shapethefuture
電子工程系
應 用 電 子 組
電 腦 遊 戲 設 計 組
void digitalCommand(YunClient client) {
int pin, value;
pin = client.parseInt(); //讀取腳位編號
//若下⼀個字元為'/',表示URL格式如同"/digital/13/1"
if (client.read() == '/') {
value = client.parseInt(); //讀取設定值
digitalWrite(pin, value); //執行digitalWrite
//在用戶端顯示作業訊息
client.print(F("Pin D"));
client.print(pin);
client.print(F(" set to "));
client.println(value);
}
解析Bridge範例程式碼 4/10
10
shapethefuture
電子工程系
應 用 電 子 組
電 腦 遊 戲 設 計 組
else {
value = digitalRead(pin); //執行digitalRead
//在用戶端顯示作業訊息
client.print(F("Pin D"));
client.print(pin);
client.print(F(" reads "));
client.println(value);
}
}
解析Bridge範例程式碼 5/10
11
shapethefuture
電子工程系
應 用 電 子 組
電 腦 遊 戲 設 計 組
void analogCommand(YunClient client) {
int pin, value;
pin = client.parseInt(); //讀取腳位編號
//若下⼀個字元為'/',表示URL格式如同"/analog/5/120"
if (client.read() == '/') {
value = client.parseInt(); //讀取設定值
analogWrite(pin, value); //執行analogWrite
//在用戶端顯示作業訊息
client.print(F("Pin D"));
client.print(pin);
client.print(F(" set to analog "));
client.println(value);
}
解析Bridge範例程式碼 6/10
12
shapethefuture
電子工程系
應 用 電 子 組
電 腦 遊 戲 設 計 組
else {
value = analogRead(pin); //執行analogRead
//在用戶端顯示作業訊息
client.print(F("Pin A"));
client.print(pin);
client.print(F(" reads analog "));
client.println(value);
}
}
解析Bridge範例程式碼 7/10
13
shapethefuture
電子工程系
應 用 電 子 組
電 腦 遊 戲 設 計 組
void modeCommand(YunClient client) {
int pin;
pin = client.parseInt(); //讀取腳位編號
//若下⼀個字元不是‘/’,表示URL不符合"/mode/13/input"格式
if (client.read() != '/') {
client.println(F("error"));
return;
}
String mode = client.readStringUntil(‘r’); //讀取腳位設定
if (mode == "input") {
pinMode(pin, INPUT); //執行pinMode
//在用戶端顯示作業訊息
解析Bridge範例程式碼 8/10
14
shapethefuture
電子工程系
應 用 電 子 組
電 腦 遊 戲 設 計 組
client.print(F("Pin D"));
client.print(pin);
client.print(F(" configured as INPUT!"));
return;
}
if (mode == "output") {
pinMode(pin, OUTPUT); //執行pinMode
//在用戶端顯示作業訊息
client.print(F("Pin D"));
client.print(pin);
client.print(F(" configured as OUTPUT!"));
return;
}
解析Bridge範例程式碼 9/10
15
shapethefuture
電子工程系
應 用 電 子 組
電 腦 遊 戲 設 計 組
//若不是“input”或“output”,則表示指令錯誤
client.print(F("error: invalid mode "));
client.print(mode);
}
解析Bridge範例程式碼 10/10
16

More Related Content

What's hot

MicroPython簡介
MicroPython簡介 MicroPython簡介
MicroPython簡介 Max Lai
 
How to write Platform Devices and Drivers with FPGA via GPMC
How to write Platform Devices and Drivers with FPGA via GPMCHow to write Platform Devices and Drivers with FPGA via GPMC
How to write Platform Devices and Drivers with FPGA via GPMCBo-Yi Wu
 
Web + Arduino 實在有夠潮 ( 課程簡報 )
Web + Arduino 實在有夠潮 ( 課程簡報 ) Web + Arduino 實在有夠潮 ( 課程簡報 )
Web + Arduino 實在有夠潮 ( 課程簡報 ) Web Arduino
 
使用 Arduino 控制 ESP8266 的各種方式
使用 Arduino 控制 ESP8266 的各種方式使用 Arduino 控制 ESP8266 的各種方式
使用 Arduino 控制 ESP8266 的各種方式Kenson Chiang
 
Raspberry Pi 溫濕度發報機
Raspberry Pi 溫濕度發報機Raspberry Pi 溫濕度發報機
Raspberry Pi 溫濕度發報機艾鍗科技
 
瞻營全電子_六足機器人(二)
瞻營全電子_六足機器人(二)瞻營全電子_六足機器人(二)
瞻營全電子_六足機器人(二)CAVEDU Education
 
LinkIt 7697 開發平台簡介 (Traditional Chinese)
LinkIt 7697 開發平台簡介 (Traditional Chinese)LinkIt 7697 開發平台簡介 (Traditional Chinese)
LinkIt 7697 開發平台簡介 (Traditional Chinese)Bear Wang
 
[ZigBee 嵌入式系統] ZigBee Architecture 與 TI Z-Stack Firmware
[ZigBee 嵌入式系統] ZigBee Architecture 與 TI Z-Stack Firmware[ZigBee 嵌入式系統] ZigBee Architecture 與 TI Z-Stack Firmware
[ZigBee 嵌入式系統] ZigBee Architecture 與 TI Z-Stack FirmwareSimen Li
 

What's hot (20)

AMA 認證簡介
AMA 認證簡介AMA 認證簡介
AMA 認證簡介
 
Arduino基礎IO控制
Arduino基礎IO控制Arduino基礎IO控制
Arduino基礎IO控制
 
MicroPython簡介
MicroPython簡介 MicroPython簡介
MicroPython簡介
 
AMA 中級術科實作IV
AMA 中級術科實作IVAMA 中級術科實作IV
AMA 中級術科實作IV
 
How to write Platform Devices and Drivers with FPGA via GPMC
How to write Platform Devices and Drivers with FPGA via GPMCHow to write Platform Devices and Drivers with FPGA via GPMC
How to write Platform Devices and Drivers with FPGA via GPMC
 
Arduino序列通訊
Arduino序列通訊Arduino序列通訊
Arduino序列通訊
 
nodeMCU IOT教學02 - Lua語言
nodeMCU IOT教學02 - Lua語言nodeMCU IOT教學02 - Lua語言
nodeMCU IOT教學02 - Lua語言
 
AMA 中級術科實作 I
AMA 中級術科實作 IAMA 中級術科實作 I
AMA 中級術科實作 I
 
Arduino程式開發工具
Arduino程式開發工具Arduino程式開發工具
Arduino程式開發工具
 
Arduino感測應用
Arduino感測應用Arduino感測應用
Arduino感測應用
 
AMA 中級術科實作II
AMA 中級術科實作IIAMA 中級術科實作II
AMA 中級術科實作II
 
nodeMCU IOT教學03 - NodeMCU導論
nodeMCU IOT教學03 - NodeMCU導論nodeMCU IOT教學03 - NodeMCU導論
nodeMCU IOT教學03 - NodeMCU導論
 
Web + Arduino 實在有夠潮 ( 課程簡報 )
Web + Arduino 實在有夠潮 ( 課程簡報 ) Web + Arduino 實在有夠潮 ( 課程簡報 )
Web + Arduino 實在有夠潮 ( 課程簡報 )
 
使用 Arduino 控制 ESP8266 的各種方式
使用 Arduino 控制 ESP8266 的各種方式使用 Arduino 控制 ESP8266 的各種方式
使用 Arduino 控制 ESP8266 的各種方式
 
Raspberry Pi 溫濕度發報機
Raspberry Pi 溫濕度發報機Raspberry Pi 溫濕度發報機
Raspberry Pi 溫濕度發報機
 
瞻營全電子_六足機器人(二)
瞻營全電子_六足機器人(二)瞻營全電子_六足機器人(二)
瞻營全電子_六足機器人(二)
 
LinkIt 7697 開發平台簡介 (Traditional Chinese)
LinkIt 7697 開發平台簡介 (Traditional Chinese)LinkIt 7697 開發平台簡介 (Traditional Chinese)
LinkIt 7697 開發平台簡介 (Traditional Chinese)
 
[ZigBee 嵌入式系統] ZigBee Architecture 與 TI Z-Stack Firmware
[ZigBee 嵌入式系統] ZigBee Architecture 與 TI Z-Stack Firmware[ZigBee 嵌入式系統] ZigBee Architecture 與 TI Z-Stack Firmware
[ZigBee 嵌入式系統] ZigBee Architecture 與 TI Z-Stack Firmware
 
Arduino藍牙傳輸應用
Arduino藍牙傳輸應用Arduino藍牙傳輸應用
Arduino藍牙傳輸應用
 
Arduino導論
Arduino導論Arduino導論
Arduino導論
 

Viewers also liked

Arduino Yún使用網頁顯示監測資料
Arduino Yún使用網頁顯示監測資料Arduino Yún使用網頁顯示監測資料
Arduino Yún使用網頁顯示監測資料吳錫修 (ShyiShiou Wu)
 
Unity遊戲程式設計(04) 2D運動與碰撞處理I
Unity遊戲程式設計(04) 2D運動與碰撞處理IUnity遊戲程式設計(04) 2D運動與碰撞處理I
Unity遊戲程式設計(04) 2D運動與碰撞處理I吳錫修 (ShyiShiou Wu)
 
Unity遊戲程式設計(03) 2D動畫製作及應用
Unity遊戲程式設計(03) 2D動畫製作及應用Unity遊戲程式設計(03) 2D動畫製作及應用
Unity遊戲程式設計(03) 2D動畫製作及應用吳錫修 (ShyiShiou Wu)
 
Unity遊戲程式設計(09) 3D物件與光源設定
Unity遊戲程式設計(09) 3D物件與光源設定Unity遊戲程式設計(09) 3D物件與光源設定
Unity遊戲程式設計(09) 3D物件與光源設定吳錫修 (ShyiShiou Wu)
 
RESTful API的设计与开发
RESTful API的设计与开发RESTful API的设计与开发
RESTful API的设计与开发Ace Lee
 
Refactoring for software design smells - icse 2014 tutorial
Refactoring for software design smells - icse 2014 tutorialRefactoring for software design smells - icse 2014 tutorial
Refactoring for software design smells - icse 2014 tutorialGanesh Samarthyam
 

Viewers also liked (20)

使用Arduino Yún內建Web伺服器
使用Arduino Yún內建Web伺服器使用Arduino Yún內建Web伺服器
使用Arduino Yún內建Web伺服器
 
Arduino Yún使用網頁顯示監測資料
Arduino Yún使用網頁顯示監測資料Arduino Yún使用網頁顯示監測資料
Arduino Yún使用網頁顯示監測資料
 
mBot組裝與測試
mBot組裝與測試mBot組裝與測試
mBot組裝與測試
 
Construct 2的Particles物件
Construct 2的Particles物件Construct 2的Particles物件
Construct 2的Particles物件
 
Unity遊戲程式設計(04) 2D運動與碰撞處理I
Unity遊戲程式設計(04) 2D運動與碰撞處理IUnity遊戲程式設計(04) 2D運動與碰撞處理I
Unity遊戲程式設計(04) 2D運動與碰撞處理I
 
Unity遊戲程式設計(03) 2D動畫製作及應用
Unity遊戲程式設計(03) 2D動畫製作及應用Unity遊戲程式設計(03) 2D動畫製作及應用
Unity遊戲程式設計(03) 2D動畫製作及應用
 
Unity遊戲程式設計(09) 3D物件與光源設定
Unity遊戲程式設計(09) 3D物件與光源設定Unity遊戲程式設計(09) 3D物件與光源設定
Unity遊戲程式設計(09) 3D物件與光源設定
 
mBot藍牙控制
mBot藍牙控制mBot藍牙控制
mBot藍牙控制
 
Construct 2基本操作
Construct 2基本操作Construct 2基本操作
Construct 2基本操作
 
mBot 教學7 聲光控制應用
mBot 教學7 聲光控制應用mBot 教學7 聲光控制應用
mBot 教學7 聲光控制應用
 
mBot 教學3 開發mBot應用程式
mBot 教學3 開發mBot應用程式mBot 教學3 開發mBot應用程式
mBot 教學3 開發mBot應用程式
 
RESTful API的设计与开发
RESTful API的设计与开发RESTful API的设计与开发
RESTful API的设计与开发
 
mBot 教學5 超音波感測應用
mBot 教學5 超音波感測應用mBot 教學5 超音波感測應用
mBot 教學5 超音波感測應用
 
mBot 教學4 移動控制
mBot 教學4 移動控制mBot 教學4 移動控制
mBot 教學4 移動控制
 
mBot 教學8 巡跡控制應用
mBot 教學8 巡跡控制應用mBot 教學8 巡跡控制應用
mBot 教學8 巡跡控制應用
 
mBot 教學6 光感測器與LED應用
mBot 教學6 光感測器與LED應用mBot 教學6 光感測器與LED應用
mBot 教學6 光感測器與LED應用
 
mBot 教學1 組裝與測試
mBot 教學1 組裝與測試mBot 教學1 組裝與測試
mBot 教學1 組裝與測試
 
mBot 教學2 mBlock積木式設計程式
mBot 教學2 mBlock積木式設計程式mBot 教學2 mBlock積木式設計程式
mBot 教學2 mBlock積木式設計程式
 
Construct 2進階操作
Construct 2進階操作Construct 2進階操作
Construct 2進階操作
 
Refactoring for software design smells - icse 2014 tutorial
Refactoring for software design smells - icse 2014 tutorialRefactoring for software design smells - icse 2014 tutorial
Refactoring for software design smells - icse 2014 tutorial
 

Similar to Arduino Yún使用Http restful api控制io

HTML+COIMOTION 開發跨平台 app
HTML+COIMOTION 開發跨平台 appHTML+COIMOTION 開發跨平台 app
HTML+COIMOTION 開發跨平台 appBen Lue
 
[GDG Kaohsiung DevFest 2023] 以 Compose 及 Kotlin Multiplatform 打造多平台應用程式
[GDG Kaohsiung DevFest 2023] 以 Compose 及 Kotlin Multiplatform 打造多平台應用程式[GDG Kaohsiung DevFest 2023] 以 Compose 及 Kotlin Multiplatform 打造多平台應用程式
[GDG Kaohsiung DevFest 2023] 以 Compose 及 Kotlin Multiplatform 打造多平台應用程式Shengyou Fan
 
PHP 應用之一 socket funion : 偽 WEB Server
PHP 應用之一 socket funion : 偽 WEB ServerPHP 應用之一 socket funion : 偽 WEB Server
PHP 應用之一 socket funion : 偽 WEB Server志賢 黃
 
美团点评技术沙龙13-前端工程化开发方案app-proto介绍
美团点评技术沙龙13-前端工程化开发方案app-proto介绍美团点评技术沙龙13-前端工程化开发方案app-proto介绍
美团点评技术沙龙13-前端工程化开发方案app-proto介绍美团点评技术团队
 
ASP.NET Core 2.1設計新思維與新發展
ASP.NET  Core 2.1設計新思維與新發展ASP.NET  Core 2.1設計新思維與新發展
ASP.NET Core 2.1設計新思維與新發展江華 奚
 
181201_CoAP_coding365
181201_CoAP_coding365181201_CoAP_coding365
181201_CoAP_coding365Peter Yi
 
容器驅動開發 - .NET Conf 2017 @ 台中
容器驅動開發 - .NET Conf 2017 @ 台中容器驅動開發 - .NET Conf 2017 @ 台中
容器驅動開發 - .NET Conf 2017 @ 台中Andrew Wu
 
利用Signalr打造即時通訊@Tech day geek
利用Signalr打造即時通訊@Tech day geek利用Signalr打造即時通訊@Tech day geek
利用Signalr打造即時通訊@Tech day geekJohnson Gau
 
Re Introduce Web Development
Re Introduce Web DevelopmentRe Introduce Web Development
Re Introduce Web Developmentfinian lau
 
构建ActionScript游戏服务器,支持超过15000并发连接
构建ActionScript游戏服务器,支持超过15000并发连接 构建ActionScript游戏服务器,支持超过15000并发连接
构建ActionScript游戏服务器,支持超过15000并发连接 Renaun Erickson
 
ASP.NET Core MVC 2.2從開發到測試 - Development & Unit Testing
ASP.NET Core MVC 2.2從開發到測試 - Development & Unit TestingASP.NET Core MVC 2.2從開發到測試 - Development & Unit Testing
ASP.NET Core MVC 2.2從開發到測試 - Development & Unit Testing江華 奚
 
Java技术讲座 网络编程
Java技术讲座 网络编程Java技术讲座 网络编程
Java技术讲座 网络编程xujie
 
以HTML5和COIMOTION打造跨平台App
以HTML5和COIMOTION打造跨平台App以HTML5和COIMOTION打造跨平台App
以HTML5和COIMOTION打造跨平台AppBen Lue
 
高性能并发Web服务器实现核心内幕
高性能并发Web服务器实现核心内幕高性能并发Web服务器实现核心内幕
高性能并发Web服务器实现核心内幕ideawu
 
Real World ASP.NET MVC
Real World ASP.NET MVCReal World ASP.NET MVC
Real World ASP.NET MVCjeffz
 
ASP.NET Core 3.0 新功能
ASP.NET Core 3.0 新功能ASP.NET Core 3.0 新功能
ASP.NET Core 3.0 新功能Edward Kuo
 
常用开发工具介绍
常用开发工具介绍常用开发工具介绍
常用开发工具介绍haozes
 
JdonFramework中文
JdonFramework中文JdonFramework中文
JdonFramework中文banq jdon
 
Bluemix Node-Red Part I
Bluemix Node-Red Part IBluemix Node-Red Part I
Bluemix Node-Red Part IJoseph Chang
 

Similar to Arduino Yún使用Http restful api控制io (20)

HTML+COIMOTION 開發跨平台 app
HTML+COIMOTION 開發跨平台 appHTML+COIMOTION 開發跨平台 app
HTML+COIMOTION 開發跨平台 app
 
[GDG Kaohsiung DevFest 2023] 以 Compose 及 Kotlin Multiplatform 打造多平台應用程式
[GDG Kaohsiung DevFest 2023] 以 Compose 及 Kotlin Multiplatform 打造多平台應用程式[GDG Kaohsiung DevFest 2023] 以 Compose 及 Kotlin Multiplatform 打造多平台應用程式
[GDG Kaohsiung DevFest 2023] 以 Compose 及 Kotlin Multiplatform 打造多平台應用程式
 
PHP 應用之一 socket funion : 偽 WEB Server
PHP 應用之一 socket funion : 偽 WEB ServerPHP 應用之一 socket funion : 偽 WEB Server
PHP 應用之一 socket funion : 偽 WEB Server
 
美团点评技术沙龙13-前端工程化开发方案app-proto介绍
美团点评技术沙龙13-前端工程化开发方案app-proto介绍美团点评技术沙龙13-前端工程化开发方案app-proto介绍
美团点评技术沙龙13-前端工程化开发方案app-proto介绍
 
ASP.NET Core 2.1設計新思維與新發展
ASP.NET  Core 2.1設計新思維與新發展ASP.NET  Core 2.1設計新思維與新發展
ASP.NET Core 2.1設計新思維與新發展
 
181201_CoAP_coding365
181201_CoAP_coding365181201_CoAP_coding365
181201_CoAP_coding365
 
容器驅動開發 - .NET Conf 2017 @ 台中
容器驅動開發 - .NET Conf 2017 @ 台中容器驅動開發 - .NET Conf 2017 @ 台中
容器驅動開發 - .NET Conf 2017 @ 台中
 
利用Signalr打造即時通訊@Tech day geek
利用Signalr打造即時通訊@Tech day geek利用Signalr打造即時通訊@Tech day geek
利用Signalr打造即時通訊@Tech day geek
 
Re Introduce Web Development
Re Introduce Web DevelopmentRe Introduce Web Development
Re Introduce Web Development
 
构建ActionScript游戏服务器,支持超过15000并发连接
构建ActionScript游戏服务器,支持超过15000并发连接 构建ActionScript游戏服务器,支持超过15000并发连接
构建ActionScript游戏服务器,支持超过15000并发连接
 
ASP.NET Core MVC 2.2從開發到測試 - Development & Unit Testing
ASP.NET Core MVC 2.2從開發到測試 - Development & Unit TestingASP.NET Core MVC 2.2從開發到測試 - Development & Unit Testing
ASP.NET Core MVC 2.2從開發到測試 - Development & Unit Testing
 
Java技术讲座 网络编程
Java技术讲座 网络编程Java技术讲座 网络编程
Java技术讲座 网络编程
 
以HTML5和COIMOTION打造跨平台App
以HTML5和COIMOTION打造跨平台App以HTML5和COIMOTION打造跨平台App
以HTML5和COIMOTION打造跨平台App
 
高性能并发Web服务器实现核心内幕
高性能并发Web服务器实现核心内幕高性能并发Web服务器实现核心内幕
高性能并发Web服务器实现核心内幕
 
Real World ASP.NET MVC
Real World ASP.NET MVCReal World ASP.NET MVC
Real World ASP.NET MVC
 
Arduino Yún使用Http client
Arduino Yún使用Http clientArduino Yún使用Http client
Arduino Yún使用Http client
 
ASP.NET Core 3.0 新功能
ASP.NET Core 3.0 新功能ASP.NET Core 3.0 新功能
ASP.NET Core 3.0 新功能
 
常用开发工具介绍
常用开发工具介绍常用开发工具介绍
常用开发工具介绍
 
JdonFramework中文
JdonFramework中文JdonFramework中文
JdonFramework中文
 
Bluemix Node-Red Part I
Bluemix Node-Red Part IBluemix Node-Red Part I
Bluemix Node-Red Part I
 

More from 吳錫修 (ShyiShiou Wu)

Unity遊戲程式設計 - 2D Platformer遊戲
Unity遊戲程式設計 - 2D Platformer遊戲Unity遊戲程式設計 - 2D Platformer遊戲
Unity遊戲程式設計 - 2D Platformer遊戲吳錫修 (ShyiShiou Wu)
 
Unity遊戲程式設計 - 2D移動與碰撞處理II
Unity遊戲程式設計 - 2D移動與碰撞處理IIUnity遊戲程式設計 - 2D移動與碰撞處理II
Unity遊戲程式設計 - 2D移動與碰撞處理II吳錫修 (ShyiShiou Wu)
 
Unity遊戲程式設計 - 2D運動與碰撞處理I
Unity遊戲程式設計 - 2D運動與碰撞處理IUnity遊戲程式設計 - 2D運動與碰撞處理I
Unity遊戲程式設計 - 2D運動與碰撞處理I吳錫修 (ShyiShiou Wu)
 
Unity遊戲設計- 2D動畫製作及應用
Unity遊戲設計-  2D動畫製作及應用Unity遊戲設計-  2D動畫製作及應用
Unity遊戲設計- 2D動畫製作及應用吳錫修 (ShyiShiou Wu)
 

More from 吳錫修 (ShyiShiou Wu) (20)

Vuforia AR影片程式設計
Vuforia AR影片程式設計Vuforia AR影片程式設計
Vuforia AR影片程式設計
 
micro:bit亮度感測應用
micro:bit亮度感測應用micro:bit亮度感測應用
micro:bit亮度感測應用
 
Vuforia AR 同時追踨多張辨識圖
Vuforia AR同時追踨多張辨識圖Vuforia AR同時追踨多張辨識圖
Vuforia AR 同時追踨多張辨識圖
 
micro:bit開關控制應用
micro:bit開關控制應用micro:bit開關控制應用
micro:bit開關控制應用
 
Vuforia AR 應用程式設計入門
Vuforia AR應用程式設計入門Vuforia AR應用程式設計入門
Vuforia AR 應用程式設計入門
 
Vuforia AR 應用程式準備作業
Vuforia AR應用程式準備作業Vuforia AR應用程式準備作業
Vuforia AR 應用程式準備作業
 
micro:bit LED顯示控制
micro:bit LED顯示控制micro:bit LED顯示控制
micro:bit LED顯示控制
 
IDE for micro:bit
IDE for micro:bitIDE for micro:bit
IDE for micro:bit
 
Microbit 1 introduction
Microbit 1 introductionMicrobit 1 introduction
Microbit 1 introduction
 
使用Makeblock App學習mBot程式設計
使用Makeblock App學習mBot程式設計使用Makeblock App學習mBot程式設計
使用Makeblock App學習mBot程式設計
 
使用M部落App學習mBot程式設計
使用M部落App學習mBot程式設計使用M部落App學習mBot程式設計
使用M部落App學習mBot程式設計
 
nodeMCU IOT教學03 - NodeMCU導論
nodeMCU IOT教學03 - NodeMCU導論nodeMCU IOT教學03 - NodeMCU導論
nodeMCU IOT教學03 - NodeMCU導論
 
Unity遊戲程式設計 - 2D Platformer遊戲
Unity遊戲程式設計 - 2D Platformer遊戲Unity遊戲程式設計 - 2D Platformer遊戲
Unity遊戲程式設計 - 2D Platformer遊戲
 
Unity遊戲程式設計 - 2D移動與碰撞處理II
Unity遊戲程式設計 - 2D移動與碰撞處理IIUnity遊戲程式設計 - 2D移動與碰撞處理II
Unity遊戲程式設計 - 2D移動與碰撞處理II
 
Unity遊戲程式設計 - 2D運動與碰撞處理I
Unity遊戲程式設計 - 2D運動與碰撞處理IUnity遊戲程式設計 - 2D運動與碰撞處理I
Unity遊戲程式設計 - 2D運動與碰撞處理I
 
Python與Ardinio整合應用
Python與Ardinio整合應用Python與Ardinio整合應用
Python與Ardinio整合應用
 
mBlock積木式設計程式
mBlock積木式設計程式mBlock積木式設計程式
mBlock積木式設計程式
 
Arduino程式除錯
Arduino程式除錯Arduino程式除錯
Arduino程式除錯
 
Unity遊戲設計- 2D動畫製作及應用
Unity遊戲設計-  2D動畫製作及應用Unity遊戲設計-  2D動畫製作及應用
Unity遊戲設計- 2D動畫製作及應用
 
Unity遊戲設計- 應用Sprite物件
Unity遊戲設計- 應用Sprite物件Unity遊戲設計- 應用Sprite物件
Unity遊戲設計- 應用Sprite物件
 

Arduino Yún使用Http restful api控制io

  • 1. 電子工程系應 用 電 子 組 電 腦 遊 戲 設 計 組 使用HTTP RESTful API控制IO 吳錫修 November 20, 2015
  • 2. shapethefuture 電子工程系 應 用 電 子 組 電 腦 遊 戲 設 計 組  Bridge函式庫提供AVR及Linux處理器之間的介接  Bridge函式庫提供RESTful web service作業,透過REST樣式之 URL字串命令,來指示操作動作與回應訊息 REST API 2
  • 3. shapethefuture 電子工程系 應 用 電 子 組 電 腦 遊 戲 設 計 組  範例:File/Examples/Bridge/Bridge  展示如何透過HTTP REST calls方式來存取Arduino Yún控制板之IO; 我們可以參照Bridge程式,使用REST設計風格來創建自己的Web API  Possible commands created in this shetch:  "/arduino/digital/13" -> digitalRead(13)  "/arduino/digital/13/1" -> digitalWrite(13, HIGH)  "/arduino/analog/2/123" -> analogWrite(2, 123)  "/arduino/analog/2" -> analogRead(2)  "/arduino/mode/13/input" -> pinMode(13, INPUT)  "/arduino/mode/13/output" -> pinMode(13, OUTPUT)  線上解說 http://www.arduino.cc/en/Tutorial/Bridge Bridge範例 3
  • 4. shapethefuture 電子工程系 應 用 電 子 組 電 腦 遊 戲 設 計 組  登入Arduino Yún,確認網路連線正常 測試Bridge範例 1/3 4
  • 5. shapethefuture 電子工程系 應 用 電 子 組 電 腦 遊 戲 設 計 組  使用sswuyun.local/arduino/digital/13/0關閉Arduino Yún D13 LED 測試Bridge範例 2/3 5
  • 6. shapethefuture 電子工程系 應 用 電 子 組 電 腦 遊 戲 設 計 組  使用sswuyun.local/arduino/digital/13/1開啟Arduino Yún D13 LED 測試Bridge範例 3/3 6 直接指定Arduino Yún IP可加快連線速度
  • 7. shapethefuture 電子工程系 應 用 電 子 組 電 腦 遊 戲 設 計 組 #include <Bridge.h> #include <YunServer.h> #include <YunClient.h> YunServer server; //建立YunServer物件 void setup() { pinMode(13, OUTPUT); digitalWrite(13, LOW); Bridge.begin(); //啟動Bridge,約需2秒時間 digitalWrite(13, HIGH); //點亮LED表示完成Bridge啟動作業 server.listenOnLocalhost();//若要從internet連接,須註解此行 server.begin(); //啟動YunServer } 解析Bridge範例程式碼 1/10 7
  • 8. shapethefuture 電子工程系 應 用 電 子 組 電 腦 遊 戲 設 計 組 void loop() { YunClient client = server.accept(); //取出用戶連線URL字串 if (client) { process(client); //開始處理,解析URL client.stop(); //中止連線 } delay(50); //間隔50ms再重複作業 } 解析Bridge範例程式碼 2/10 8
  • 9. shapethefuture 電子工程系 應 用 電 子 組 電 腦 遊 戲 設 計 組 void process(YunClient client) { //處理REST CALL String command = client.readStringUntil(‘/’); //解析第⼀層 if (command == "digital") { digitalCommand(client); //處理數位IO指令 } if (command == "analog") { analogCommand(client); //處理類比IO指令 } if (command == "mode") { modeCommand(client); //處理接腳IO設定指令 } } 解析Bridge範例程式碼 3/10 9
  • 10. shapethefuture 電子工程系 應 用 電 子 組 電 腦 遊 戲 設 計 組 void digitalCommand(YunClient client) { int pin, value; pin = client.parseInt(); //讀取腳位編號 //若下⼀個字元為'/',表示URL格式如同"/digital/13/1" if (client.read() == '/') { value = client.parseInt(); //讀取設定值 digitalWrite(pin, value); //執行digitalWrite //在用戶端顯示作業訊息 client.print(F("Pin D")); client.print(pin); client.print(F(" set to ")); client.println(value); } 解析Bridge範例程式碼 4/10 10
  • 11. shapethefuture 電子工程系 應 用 電 子 組 電 腦 遊 戲 設 計 組 else { value = digitalRead(pin); //執行digitalRead //在用戶端顯示作業訊息 client.print(F("Pin D")); client.print(pin); client.print(F(" reads ")); client.println(value); } } 解析Bridge範例程式碼 5/10 11
  • 12. shapethefuture 電子工程系 應 用 電 子 組 電 腦 遊 戲 設 計 組 void analogCommand(YunClient client) { int pin, value; pin = client.parseInt(); //讀取腳位編號 //若下⼀個字元為'/',表示URL格式如同"/analog/5/120" if (client.read() == '/') { value = client.parseInt(); //讀取設定值 analogWrite(pin, value); //執行analogWrite //在用戶端顯示作業訊息 client.print(F("Pin D")); client.print(pin); client.print(F(" set to analog ")); client.println(value); } 解析Bridge範例程式碼 6/10 12
  • 13. shapethefuture 電子工程系 應 用 電 子 組 電 腦 遊 戲 設 計 組 else { value = analogRead(pin); //執行analogRead //在用戶端顯示作業訊息 client.print(F("Pin A")); client.print(pin); client.print(F(" reads analog ")); client.println(value); } } 解析Bridge範例程式碼 7/10 13
  • 14. shapethefuture 電子工程系 應 用 電 子 組 電 腦 遊 戲 設 計 組 void modeCommand(YunClient client) { int pin; pin = client.parseInt(); //讀取腳位編號 //若下⼀個字元不是‘/’,表示URL不符合"/mode/13/input"格式 if (client.read() != '/') { client.println(F("error")); return; } String mode = client.readStringUntil(‘r’); //讀取腳位設定 if (mode == "input") { pinMode(pin, INPUT); //執行pinMode //在用戶端顯示作業訊息 解析Bridge範例程式碼 8/10 14
  • 15. shapethefuture 電子工程系 應 用 電 子 組 電 腦 遊 戲 設 計 組 client.print(F("Pin D")); client.print(pin); client.print(F(" configured as INPUT!")); return; } if (mode == "output") { pinMode(pin, OUTPUT); //執行pinMode //在用戶端顯示作業訊息 client.print(F("Pin D")); client.print(pin); client.print(F(" configured as OUTPUT!")); return; } 解析Bridge範例程式碼 9/10 15
  • 16. shapethefuture 電子工程系 應 用 電 子 組 電 腦 遊 戲 設 計 組 //若不是“input”或“output”,則表示指令錯誤 client.print(F("error: invalid mode ")); client.print(mode); } 解析Bridge範例程式碼 10/10 16