Friday, December 25, 2020

Raspberry Pi/Python remote control ESP32/Servos via Bluetooth Classic

Last exercise show how Python (on Raspberry Pi) Bluetooth communicate with ESP32 SerialToSerialBT, using pybluez. This exercise extended to send simple single character command to ESP32 via Bluetooth Classic, to control remote servos; with GUI using Tkinter.


pyBTServoClient_20201226.py, for Python3.
import sys
from tkinter import *
from bluetooth import *

print("Python version: ")
print(sys.version)
print("tkinter version: ", TkVersion)
print("=============================");
print("Connect to ESP BT Servo Server")
addr = "24:0A:C4:E8:0F:9A"
print(addr)
service_matches = find_service( address = addr )

if len(service_matches) == 0:
    print("couldn't find the ESP32 BT Servo service")
    sys.exit(0)
    
for s in range(len(service_matches)):
    print("\nservice_matches: [" + str(s) + "]:")
    print(service_matches[s])
    
first_match = service_matches[0]
port = first_match["port"]
name = first_match["name"]
host = first_match["host"]

port=1
print("connecting to \"%s\" on %s, port %s" % (name, host, port))

# Create the client socket
sock=BluetoothSocket(RFCOMM)
sock.connect((host, port))

print("connected")

def close_window():
    sock.close()
    print("Socket closed")
    print("Window closed")
    root.destroy()

#prepare GUI
root = Tk()
root.geometry("380x200")
root.configure(bg="lightgray") 
root.wm_title("Bluetooth Servo Client")
root.protocol("WM_DELETE_WINDOW", close_window)

CMD_ORG = 'O'
CMD_XDEC = 'A'
CMD_XDEC10 = 'B'
CMD_XINC = 'C'
CMD_XINC10 = 'D'
CMD_YDEC = 'E'
CMD_YDEC10 = 'F'
CMD_YINC = 'G'
CMD_YINC10 = 'H'

def cmdO():
    sock.send(CMD_ORG)
    print("0, 0")

def cmdXdec():
    sock.send(CMD_XDEC)
    print("X-")
    
def cmdXdec10():
    sock.send(CMD_XDEC10)
    print("X-10")

def cmdXinc():
    sock.send(CMD_XINC)
    print("X+")
    
def cmdXinc10():
    sock.send(CMD_XINC10)
    print("X+10")
    
def cmdYdec():
    sock.send(CMD_YDEC)
    print("Y-")
    
def cmdYdec10():
    sock.send(CMD_YDEC10)
    print("Y-10")

def cmdYinc():
    sock.send(CMD_YINC)
    print("Y+")
    
def cmdYinc10():
    sock.send(CMD_YINC10)
    print("Y+10")


ButtonO = Button(root, text="0", command = cmdO, width=6)
ButtonXdec = Button(root, text="X-", command = cmdXdec, width=6)
ButtonXinc = Button(root, text="X+", command = cmdXinc, width=6)
ButtonYdec = Button(root, text="Y-", command = cmdYdec, width=6)
ButtonYinc = Button(root, text="Y+", command = cmdYinc, width=6)
ButtonXdec10 = Button(root, text="X-10", command = cmdXdec10, width=6)
ButtonXinc10 = Button(root, text="X+10", command = cmdXinc10, width=6)
ButtonYdec10 = Button(root, text="Y-10", command = cmdYdec10, width=6)
ButtonYinc10 = Button(root, text="Y+10", command = cmdYinc10, width=6)

ButtonO.grid(row=2, column=2)
ButtonXdec.grid(row=2, column=1)
ButtonXinc.grid(row=2, column=3)
ButtonYdec.grid(row=3, column=2)
ButtonYinc.grid(row=1, column=2)
ButtonXdec10.grid(row=2, column=0)
ButtonXinc10.grid(row=2, column=4)
ButtonYdec10.grid(row=4, column=2)
ButtonYinc10.grid(row=0, column=2)

root.mainloop()

print("\n--- bye ---\n")
The ESP32 code (run on Arduino framework) is in my another blog's post: Arduino-er - ESP32 receive Bluetooth Classic command from Raspberry Pi/Python, to control Servos.

Next:

No comments: