範例下載
>
...
Privacy
...
Arduino C++
按住shift鍵拖曳可維持選單,按住ctrl鍵可拖曳單一方塊
教學影片
int
state
0
begin
9600
println
OK
int
state
0
begin
9600
println
OK
available
char
c
read
//接在 D8 喇叭播放262Hz音頻,1000ms
tone
8
262
1000
1000
available
println
OK
write
buf
len
int
inByte
read
flush
//基本序列埠訊息傳送與接收
int
counter
0
String
res
begin
9600
counter
==
%
counter
10000
0
//由序列埠傳送
println
counter
//由序列埠接收
available
char
c
read
+=
res
c
==
c
\n
res
int
state
0
int
counter
0
String
res
begin
9600
==
state
0
println
+
req:
counter
state
1
available
char
c
read
+=
res
c
endsWith
res
\n
println
+
echo:
res
res
state
0
//esp8266 發 http 請給指定伺服url
<Arduino.h>
<ESP8266WiFiMulti.h>
<ESP8266HTTPClient.h>
<WiFiClient.h>
String
ssid
帳號
String
pw
密碼
String
url
http://中介伺服器位置
ESP8266WiFiMulti
WiFiMulti
begin
115200
//連接到 Wi-Fi 網路
wifiConnect
ssid
pw
==
WiFiMulti.run()
WL_CONNECTED
int
httpCode
0
//發出 http 請求給伺服器
String
res
esp8266HttpClient
url
httpCode
<=
httpCode
0
println
httpRequest 傳送失敗:
//序列埠印出伺服器回應內容
println
res
50
//函式:連接到 Wi-Fi 網路
void
wifiConnect
String
ssid
String
pw
WiFi
mode
WIFI_STA
WiFiMulti
addAP
ssid.c_str()
,
pw.c_str()
!=
WiFiMulti
run
WL_CONNECTED
1000
print
.
println
WiFi已連接
//發出 http 請求給伺服器
String
esp8266HttpClient
String
url
int
&httpCode
WiFiClient
client
HTTPClient
http
String
res
0
http.
begin
client
url
httpCode
http.GET()
==
httpCode
HTTP_CODE_OK
res
http.getString()
http.end()
httpCode
-999
return
res
//遊戲把手: A0 馬轉動值 , D2 接按鈕, D5,D6 控制振動馬達
<Arduino.h>
<ESP8266WiFiMulti.h>
<ESP8266HTTPClient.h>
<WiFiClient.h>
String
ssid
帳號
String
pw
密碼
String
url
http://中介伺服器位置
ESP8266WiFiMulti
WiFiMulti
begin
115200
//連接到 Wi-Fi 網路
wifiConnect
ssid
pw
INPUT_PULLUP
A0
OUTPUT
D5
OUTPUT
D6
D5
LOW
D6
LOW
==
WiFiMulti.run()
WL_CONNECTED
//類比讀取值 0~1023 (Arduino ADC之解析度為10bit)
int
A0_R
A0
//數位讀取值 HIGH:1 LOW:0
int
D2_R
D2
int
httpCode
0
//發出 http 請求給伺服器
String
res
esp8266HttpClient
+
url
+
D2_R
+
,
A0_R
httpCode
<=
httpCode
0
println
httpRequest 傳送失敗:
D5
LOW
D6
LOW
==
res
0
D5
LOW
D5
HIGH
//函式:連接到 Wi-Fi 網路
void
wifiConnect
String
ssid
String
pw
WiFi
mode
WIFI_STA
WiFiMulti
addAP
ssid.c_str()
,
pw.c_str()
!=
WiFiMulti
run
WL_CONNECTED
1000
print
.
println
WiFi已連接
//發出 http 請求給伺服器
String
esp8266HttpClient
String
url
int
&httpCode
WiFiClient
client
HTTPClient
http
String
res
0
http.
begin
client
url
httpCode
http.GET()
==
httpCode
HTTP_CODE_OK
res
http.getString()
http.end()
httpCode
-999
return
res
<WiFi.h>
<HTTPClient.h>
char
*ssid
帳號
char
*password
密碼
String
serverAddress
http://192.168.0.1
int
count
0
begin
115200
//連接到 Wi-Fi 網路
WiFi
begin
ssid
password
!=
WiFi
status
WL_CONNECTED
1000
println
WiFi連接中...
println
WiFi已連接
HTTPClient
http
;
//每輪都傳送count ,傳送成功後count加1
String
url
+
+
serverAddress
/
count
http
begin
url
//傳送httpRequest給伺服器
int
httpCode
http
GET
>
httpCode
0
//傳送成功並取得回應(httpResponse)
String
payload
http
getString
println
HTTP回應:
println
payload
+=
count
1
println
httpRequest 傳送失敗:
println
httpCode
//結束本輪HTTP Transaction
http
end
5000
<Servo.h>
//宣告伺服馬達物件
Servo
servo
//伺服馬達旋轉至90度
write
servo
90
//設定伺服馬達接到D9
attach
servo
9
<Servo.h>
//宣告伺服馬達物件
Servo
servo
//設定伺服馬達接到D9
attach
servo
9
//伺服馬達旋轉至90度
write
servo
90
//數位寫入值 HIGH:1 LOW:0
5
LOW
//數位讀取值 HIGH:1 LOW:0
int
D_input
8
//類比寫入值 0~255 (Arduino PWM 之解析度為8bit)
5
255
//類比讀取值 0~1023 (Arduino ADC之解析度為10bit)
int
A_input
A0
int
trigPin
A2
int
echoPin
A3
//triglePin為觸發腳位,要設定為輸出,echoPin為回聲接收腳位要設定為輸入
OUTPUT
trigPin
INPUT
echoPin
int
trigPin
A2
int
echoPin
A3
begin
9600
//triglePin為觸發腳位,要設定為輸出,echoPin為回聲接收腳位要設定為輸入
OUTPUT
trigPin
INPUT
echoPin
//發送一個10微秒的脈衝以啟動超音波感應器
trigPin
LOW
delayMicroseconds
2
trigPin
HIGH
delayMicroseconds
10
trigPin
LOW
// 讀取超音波感應器回傳的脈衝時間
long
duration
pulseIn
echoPin
HIGH
//空氣中的音速約0.0342cm/微秒,所以距離 = (時間 * 0.0342) / 2
double
dist
/
*
duration
0.0343
2
println
dist
category>
==
state
0
==
state
1
==
a
true
==
state
0
==
state
1
==
a
true
=
i
0
<=
i
5
i
i
123
+
i
1
random
from
to
toInt
strTemp
String
str_temp
length
strTemp
toInt
strTemp
substring
strTemp
from
to
endsWith
strTemp
\n
trim
strTemp
//遊戲把手(usb通訊): A0 馬轉動值 , D2 接按鈕, D5,D6 控制振動馬達
int
counter
0
String
res
begin
9600
INPUT_PULLUP
D2
INPUT_PULLUP
A0
OUTPUT
D5
OUTPUT
D6
D5
LOW
D6
LOW
counter
==
%
counter
10000
0
//數位讀取值 HIGH:1 LOW:0
int
D2_R
D2
//類比讀取值 0~1023 (Arduino ADC之解析度為10bit)
int
A0_R
A0
//由序列埠傳送A0 馬轉動值 , D2 接按鈕
println
+
D2_R
+
,
A0_R
//由序列埠接收馬達振動的資訊
available
char
c
read
+=
res
c
==
c
\n
handleRes
res
res
void
handleRes
String
res
D6
LOW
==
toInt
res
255
D5
HIGH
D5
LOW
//遊戲把手: A0 馬轉動值 , D2 接按鈕, D5,D6 控制振動馬達
<Arduino.h>
<ESP8266WiFiMulti.h>
<ESP8266HTTPClient.h>
<WiFiClient.h>
String
ssid
WIFI帳號
String
pw
WIFI密碼
//伺服務位址結尾記得加 "/"
String
url
http://伺服器位置/
ESP8266WiFiMulti
WiFiMulti
begin
115200
//連接到 Wi-Fi 網路
wifiConnect
ssid
pw
INPUT_PULLUP
A0
INPUT_PULLUP
D2
OUTPUT
D5
OUTPUT
D6
D5
LOW
D6
LOW
==
WiFiMulti.run()
WL_CONNECTED
//類比讀取值 0~1023 (Arduino ADC之解析度為10bit)
int
A0_R
A0
//數位讀取值 HIGH:1 LOW:0
int
D2_R
D2
int
httpCode
0
//發出 http 請求給伺服器
String
res
esp8266HttpClient
+
url
+
D2_R
+
,
A0_R
httpCode
<=
httpCode
0
println
httpRequest 傳送失敗:
D5
LOW
D6
LOW
==
res
0
D5
LOW
D5
HIGH
//函式:連接到 Wi-Fi 網路
void
wifiConnect
String
ssid
String
pw
WiFi
mode
WIFI_STA
WiFiMulti
addAP
ssid.c_str()
,
pw.c_str()
!=
WiFiMulti
run
WL_CONNECTED
1000
print
.
println
WiFi已連接
//發出 http 請求給伺服器
String
esp8266HttpClient
String
url
int
&httpCode
WiFiClient
client
HTTPClient
http
String
res
0
http.
begin
client
url
httpCode
http.GET()
==
httpCode
HTTP_CODE_OK
res
http.getString()
http.end()
httpCode
-999
return
res
<ESP8266WiFi.h>
<WebSocketsClient.h>
<ArduinoJson.h>
String
ssid
WiFi名稱
String
pw
WiFi密碼
String
ip
筆電IP(可以去命令提示字元使用 ipconfig 查詢)
WebSocketsClient
webSocket
// 函式:處理 WebSocket 訊息
void
onMessage
String
message
println
message
String
response
+=
response
message here
webSocket
sendTXT
response
// 函式:處理 WebSocket 事件
void
webSocketEvent
WStype_t
type
uint8_t *
message
size_t
length
==
type
WStype_CONNECTED
Serial.printf
WebSocket connected to url: %s\n
message
==
type
WStype_TEXT
onMessage
(char *)message
==
type
WStype_DISCONNECTED
println
WebSocket disconnected!
begin
115200
println
// WiFi Settings
WiFi
begin
ssid
pw
print
Connecting
!=
WiFi.status()
WL_CONNECTED
500
print
.
println
print
Connected, IP address:
println
WiFi.localIP()
// WebSocket Settings
// server address, port and URL
webSocket
begin
ip
8080
/
// event handler
webSocket
onEvent
webSocketEvent
// Handle WebSocket communication
webSocket
loop
// 遊戲把手(Serial 通訊)
// 啟動序列埠(Serial port)
begin
115200
setTimeout
60000
println
__started__
// 讀取一行
String
message
readStringUntil
\n
// TT馬達,A0 輸入,D0 接地
INPUT
A0
OUTPUT
D0
D0
LOW
// 按鈕,D2 接地,D4 輸入上拉
OUTPUT
D2
D2
LOW
INPUT_PULLUP
D4
// 震動馬達,D5 控制,D6 接地
OUTPUT
D5
D5
LOW
OUTPUT
D6
D6
LOW
// LED 燈,D7 接長腳(正極),D8 接短腳(負極)
OUTPUT
D7
D7
LOW
OUTPUT
D8
D8
LOW
// 判斷是否為 LED 燈控制訊息
==
message
d1
// LED 燈亮
D7
HIGH
==
message
d0
// LED 燈暗
D7
LOW
// 判斷是否為震動馬達控制訊息
==
message
0
// 震動馬達停止轉動
D5
LOW
!=
message
// 震動馬達開始轉動
D5
HIGH
// 提供按鈕的狀態
String
response1
// 訊息以 d 開頭代表數位訊號
+=
response1
d
+=
response1
D4
println
response1
// 提供 TT 馬達的狀態
String
response2
// 訊息為數字代表類比訊號
+=
response2
A0
println
response2
// 遊戲把手(Serial 通訊)
// 啟動序列埠(Serial port)
begin
115200
setTimeout
60000
println
__started__
// TT馬達,A0 輸入,D0 接地
INPUT
A0
OUTPUT
D0
D0
LOW
// 按鈕,D2 接地,D4 輸入上拉
OUTPUT
D2
D2
LOW
INPUT_PULLUP
D4
// 震動馬達,D5 控制,D6 接地
OUTPUT
D5
D5
LOW
OUTPUT
D6
D6
LOW
// LED 燈,D7 接長腳(正極),D8 接短腳(負極)
OUTPUT
D7
D7
LOW
OUTPUT
D8
D8
LOW
// 讀取一行
String
message
readStringUntil
\n
// 判斷是否為 LED 燈控制訊息
==
message
d1
// LED 燈亮
D7
HIGH
==
message
d0
// LED 燈暗
D7
LOW
// 判斷是否為震動馬達控制訊息
==
message
0
// 震動馬達停止轉動
D5
LOW
!=
message
// 震動馬達開始轉動
D5
HIGH
// 提供按鈕的狀態
String
response1
// 訊息以 d 開頭代表數位訊號
+=
response1
d
+=
response1
D4
println
response1
// 提供 TT 馬達的狀態
String
response2
// 訊息為數字代表類比訊號
+=
response2
A0
println
response2