[前言]
在上篇教學中,使用網址來控制馬達相當麻煩,因為每次都需要輸入一個指令。因此,本篇將教大家如何使用網頁來控制馬達。
[實驗目的]
使用網頁來控制馬達。
步驟 1:開啟「Thonny」軟體。
步驟 2:選擇板子「工具」>「選項」>「ESP8266」>「序列阜」。
步驟 3:開啟「myWemotor_HTML」。
步驟 4:再「模組」> 「car.html」>「右鍵」>「上傳到/」 。
步驟 5:在下方就可以看到「car.html」。
步驟 6:點「停止」>「執行」。
步驟 7:點「檔案」>「儲存副本」> 點「MicroPython設備」> 輸入「main.py」>「確定」。
步驟 8:輕按 「板子上重啟鈕」 > 顯示「伺服器位址」。
步驟 9:在相同網路下 搜尋「http://伺服器位址/car.html」。
import network #匯入無限網路模組
import ESP8266WebServer #匯入網站模組
import wemotor #匯入馬達模組
from machine import I2C,Pin #匯入machine模組 並 命名為 I2C與Pin
motor = wemotor.Motor() #motor為馬達物件
def left(): # 副程式,左
motor.move(0,50)
def right(): # 副程式,右
motor.move(50,0)
def forward(): # 副程式,前
motor.move(40,40)
def backward(): # 副程式,後
motor.move(-40,-40)
def stop(): # 副程式,停
motor.move(0,0)
def handleCmd(socket, args): #網址輸入指令
if 'output' in args: #檢查output參數
if args['output'] == 'L': #output為 L
print("左轉")
left()
elif args['output'] == 'R': #output為 R
print("右轉")
right()
elif args['output'] == 'F': #output為 F
print("前進")
forward()
elif args['output'] == 'B': #output為 B
print("後退")
backward()
elif args['output'] == 'S': #output為 S
print("停止")
stop()
ESP8266WebServer.ok(socket, "200", "OK") #將OK傳給瀏覽器
else:
ESP8266WebServer.err(socket, "400", "ERR") #將ERR傳給瀏覽器
LED = Pin(2,Pin.OUT,value=1) #LED設為D2輸出模式 數值1(暗)
sta = network.WLAN(network.STA_IF) #已預設WiFi 連線網際網路
sta.active(True) # 啟用無線網路
sta.connect('網路名稱', '網路密碼') #連結無線網路(基地台)
while not sta.isconnected(): # 等待無線網路連上
pass
LED.value(0) # LED亮表示連上網路
ESP8266WebServer.begin(80) # 啟用網站
ESP8266WebServer.onPath("/Race",handleCmd) #指定處理指令的函式 Race
ESP8266WebServer.setDocPath("/car") # 指定 HTML 檔路徑
print("伺服器位址: " + sta.ifconfig()[0]) #顯示我的IP碼
ap = network.WLAN(network.AP_IF) #設ap為 已熱點方式 開啟無線網路基地台(區域)
ap.active(True)
ap.config(essid='LAB06-'+str(sta.ifconfig()[0])) #熱點名稱()
while True: #無窮迴圈
ESP8266WebServer.handleClient() #檢查是否有新指令
motor.avoidTimeout() #避免time.out
|
car.html
import network #匯入無限網路模組
import ESP8266WebServer #匯入網站模組
import wemotor #匯入馬達模組
from machine import I2C,Pin #匯入machine模組 並 命名為 I2C與Pin
motor = wemotor.Motor() #motor為馬達物件
def left(): # 副程式,左
motor.move(0,50)
def right(): # 副程式,右
motor.move(50,0)
def forward(): # 副程式,前
motor.move(40,40)
def backward(): # 副程式,後
motor.move(-40,-40)
def stop(): # 副程式,停
motor.move(0,0)
def handleCmd(socket, args): #網址輸入指令
if 'output' in args: #檢查output參數
if args['output'] == 'L': #output為 L
print("左轉")
left()
elif args['output'] == 'R': #output為 R
print("右轉")
right()
elif args['output'] == 'F': #output為 F
print("前進")
forward()
elif args['output'] == 'B': #output為 B
print("後退")
backward()
elif args['output'] == 'S': #output為 S
print("停止")
stop()
ESP8266WebServer.ok(socket, "200", "OK") #將OK傳給瀏覽器
else:
ESP8266WebServer.err(socket, "400", "ERR") #將ERR傳給瀏覽器
LED = Pin(2,Pin.OUT,value=1) #LED設為D2輸出模式 數值1(暗)
sta = network.WLAN(network.STA_IF) #已預設WiFi 連線網際網路
sta.active(True) # 啟用無線網路
sta.connect('網路名稱', '網路密碼') #連結無線網路(基地台)
while not sta.isconnected(): # 等待無線網路連上
pass
LED.value(0) # LED亮表示連上網路
ESP8266WebServer.begin(80) # 啟用網站
ESP8266WebServer.onPath("/Race",handleCmd) #指定處理指令的函式 Race
ESP8266WebServer.setDocPath("/car") # 指定 HTML 檔路徑
print("伺服器位址: " + sta.ifconfig()[0]) #顯示我的IP碼
ap = network.WLAN(network.AP_IF) #設ap為 已熱點方式 開啟無線網路基地台(區域)
ap.active(True)
ap.config(essid='LAB06-'+str(sta.ifconfig()[0])) #熱點名稱()
while True: #無窮迴圈
ESP8266WebServer.handleClient() #檢查是否有新指令
motor.avoidTimeout() #避免time.out
|
[成果影片]
[附件]









請先 登入 以發表留言。