這篇介紹如何用Python 語言透過ESP8266 Wifi連線去控制Arduino 端的LED.
<圖一概念圖>
<圖二> 下圖是Python GUI 程式執行時,及Arduino 端LED實際運作狀態.
當按下分別對應的LED按鈕後,LED即會 ON/OFF 動作.
有關ESP8266 請參考之前相關文章:
ESP8266 Wifi (1)---Connected with PC.
ESP8266 WIFI(2) -update firmware and modify baud rate.
ESP8266 WIFI(3) -Connected with Arduino
ESP8266 WIFI(4) -Soil_Humidity sensor
ESP8266 WIFI(5) -Set LED ON/OFF from WEB browser
有關Python & Arduino 請參考之前相關文章:
使用python與arduino連接控制LED
使用python與arduino連接控制LED之GUI圖形介面
使用Python透過Bluetooth與Arduino連結
影音筆記完整示範說明:
底下是本範例的Python &Arduino程式碼.
<Python source code>
# -*- coding:utf-8 -*-
from time import sleep
from msvcrt import getch
import tkinter
import socket
##===When button A be pressed then Send 'a' to arduino============
def Led_A_ON():
#Show to LabelA----------------
LabelA.config(text="Turn ON RED LED")
LabelA.update_idletasks()
Tkwindow.update()
try: # 例外處理
ip = '192.168.11.17' # 連線IP
port =9000 # 連線通訊埠
Senddata ="Set LEDPIN:D101" # 取得傳送資料
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # 建立socket物件
client.connect((ip,port)) # 連線服務端
client.send(str(Senddata).encode()) # 傳送資料
client.close() # 關閉連線
except:
LabelA.config(text="傳送錯誤\n")
LabelA.update_idletasks()
Tkwindow.update()
##====================================
##===When button A be pressed then Send 'a' to arduino============
def Led_B_ON():
#Show to LabelA----------------
LabelA.config(text="Turn ON GREEN LED")
LabelA.update_idletasks()
Tkwindow.update()
try: # 例外處理
ip = '192.168.11.17' # 連線IP
port =9000 # 連線通訊埠
Senddata ="Set LEDPIN:D111" # 取得傳送資料
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # 建立socket物件
client.connect((ip,port)) # 連線服務端
client.send(str(Senddata).encode()) # 傳送資料
client.close() # 關閉連線
except:
LabelA.config(text="傳送錯誤\n")
LabelA.update_idletasks()
Tkwindow.update()
##====================================
def Led_C_ON():
#Show to LabelA----------------
LabelA.config(text="Turn ON BLUE LED")
LabelA.update_idletasks()
Tkwindow.update()
try: # 例外處理
ip = '192.168.11.17' # 連線IP
port =9000 # 連線通訊埠
Senddata ="Set LEDPIN:D121" # 取得傳送資料
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # 建立socket物件
client.connect((ip,port)) # 連線服務端
client.send(str(Senddata).encode()) # 傳送資料
client.close() # 關閉連線
except:
LabelA.config(text="傳送錯誤\n")
LabelA.update_idletasks()
Tkwindow.update()
##====================================
def Led_A_OFF():
#Show to LabelA----------------
LabelA.config(text="Turn OFF RED LED ")
LabelA.update_idletasks()
Tkwindow.update()
try: # 例外處理
ip = '192.168.11.17' # 連線IP
port =9000 # 連線通訊埠
Senddata ="Set LEDPIN:D100" # 取得傳送資料
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # 建立socket物件
client.connect((ip,port)) # 連線服務端
client.send(str(Senddata).encode()) # 傳送資料
client.close() # 關閉連線
except:
LabelA.config(text="傳送錯誤\n")
LabelA.update_idletasks()
Tkwindow.update()
##====================================
def Led_B_OFF():
#Show to LabelA----------------
LabelA.config(text="Turn OFF GREEN LED")
LabelA.update_idletasks()
Tkwindow.update()
try: # 例外處理
ip = '192.168.11.17' # 連線IP
port =9000 # 連線通訊埠
Senddata ="Set LEDPIN:D110" # 取得傳送資料
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # 建立socket物件
client.connect((ip,port)) # 連線服務端
client.send(str(Senddata).encode()) # 傳送資料
client.close() # 關閉連線
except:
LabelA.config(text="傳送錯誤\n")
LabelA.update_idletasks()
Tkwindow.update()
##====================================
def Led_C_OFF():
#Show to LabelA----------------
LabelA.config(text="Turn OFF BLUE LED ")
LabelA.update_idletasks()
Tkwindow.update()
try: # 例外處理
ip = '192.168.11.17' # 連線IP
port =9000 # 連線通訊埠
Senddata ="Set LEDPIN:D120" # 取得傳送資料
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # 建立socket物件
client.connect((ip,port)) # 連線服務端
client.send(str(Senddata).encode()) # 傳送資料
client.close() # 關閉連線
except:
LabelA.config(text="傳送錯誤\n")
LabelA.update_idletasks()
Tkwindow.update()
##====================================
def All_Exit():
print("Exit.....")
#Show to LabelA----------------
LabelA.config(text="Exit.....")
LabelA.update_idletasks()
Tkwindow.update()
sleep(1)
Tkwindow.destroy() # Kill the root window!
##------------------------------------------------------
Tkwindow=tkinter.Tk()
Tkwindow.title("Using Python to Control Arduino LED ON/OFF")
Tkwindow.minsize(700,400)
LabelA=tkinter.Label(Tkwindow,
bg='black',
fg='LightGreen',
text="Start...",
width=100,
height=15,
justify=tkinter.LEFT
)
LabelA.pack(side=tkinter.TOP)
buttonA=tkinter.Button(Tkwindow,
anchor=tkinter.S,
text="LED_RED ON",
bg='PaleVioletRed',
fg='GhostWhite',
width=12,
height=1,
command=Led_A_ON)
buttonA.pack(side=tkinter.LEFT)
buttonB=tkinter.Button(Tkwindow,
anchor=tkinter.S,
text="LED_GREEN ON",
bg='LightGreen',
fg='GhostWhite',
width=12,
height=1,
command=Led_B_ON)
buttonB.pack(side=tkinter.LEFT)
buttonC=tkinter.Button(Tkwindow,
anchor=tkinter.S,
text="LED_BLUE ON",
bg='LightBlue',
fg='GhostWhite',
width=12,
height=1,
command=Led_C_ON)
buttonC.pack(side=tkinter.LEFT)
buttonD=tkinter.Button(Tkwindow,
anchor=tkinter.S,
text="LED_RED OFF",
bg='PaleVioletRed',
fg='Black',
width=12,
height=1,
command=Led_A_OFF)
buttonD.pack(side=tkinter.LEFT)
buttonE=tkinter.Button(Tkwindow,
anchor=tkinter.S,
text="LED_GREEN OFF",
bg='LightGreen',
fg='Black',
width=12,
height=1,
command=Led_B_OFF)
buttonE.pack(side=tkinter.LEFT)
buttonF=tkinter.Button(Tkwindow,
anchor=tkinter.S,
text="LED_BLUE OFF",
bg='LightBlue',
fg='Black',
width=12,
height=1,
command=Led_C_OFF)
buttonF.pack(side=tkinter.LEFT)
buttonEnd=tkinter.Button(Tkwindow,
anchor=tkinter.S,
text="Exit",
bg='Crimson',
fg='Black',
width=12,
height=1,
command=All_Exit)
buttonEnd.pack(side=tkinter.RIGHT)
Tkwindow.mainloop()
<Arduino 完整範例 source code>
https://github.com/Ashing00/esp8266/blob/master/esp8266_led.ino
加入阿布拉機的3D列印與機器人的FB專頁
https://www.facebook.com/arbu00/
Scan QRcode to join FB Fans.