基于ESP32实现蓝牙通信控制舵机【智能开锁实验】

玩转ESP32



前言

通过蓝牙通信对舵机的角度控制,这个在生活中也可解决一些实际的问题,如蓝牙控制舵机实现开灯、关灯或者开门等等。我实现的是开门,好消息是效果不错,坏消息是耗电有点快。


一、项目概述

1.蓝牙

在这里插入图片描述
查看esp32的芯片手册,esp32是集成了蓝牙的,所以我们可以直接使用。

2.舵机

如图:
在这里插入图片描述
伺服舵机 SG90 9g舵机 180°带限位,其中
棕色:GND
红色:VCC(4.8V-6V)
橘色:信号端(3.3V或5V的IO口即可驱动)
不同厂家生产的可能不一样。

二、项目开发

1.蓝牙初始化及应用

代码如下:


#创建蓝牙对象
class ESP32_BLE():
    def __init__(self, name):
        self.led = Pin(2, Pin.OUT)
        self.timer1 = Timer(0)
        self.name = name
        self.ble = bluetooth.BLE()
        self.ble.active(True)
        self.ble.config(gap_name=name)
        self.disconnected()
        self.ble.irq(self.ble_irq)
        self.register()
        self.advertiser()
        
	#蓝牙连接时调用的函数
    def connected(self):
        self.led.value(1)
        self.timer1.deinit()
        
	#蓝牙断开时调用的函数,这里使用定时器,断开时led闪烁
    def disconnected(self):        
        self.timer1.init(period=100, mode=Timer.PERIODIC, callback=lambda t: self.led.value(not self.led.value()))
	
	#蓝牙中断函数
    def ble_irq(self, event, data):
        global BLE_MSG
        if event == 1: #_IRQ_CENTRAL_CONNECT 手机链接了此设备
            self.connected()
        elif event == 2: #_IRQ_CENTRAL_DISCONNECT 手机断开此设备
            self.advertiser()
            self.disconnected()
        elif event == 3: #_IRQ_GATTS_WRITE 手机发送了数据 
            buffer = self.ble.gatts_read(self.rx)
            BLE_MSG = buffer.decode('UTF-8').strip()
            
     #蓝牙信息配置函数       
    def register(self):        
        service_uuid = '6E400001-B5A3-F393-E0A9-E50E24DCCA9E'
        reader_uuid = '6E400002-B5A3-F393-E0A9-E50E24DCCA9E'
        sender_uuid = '6E400003-B5A3-F393-E0A9-E50E24DCCA9E'

        services = (
            (
                bluetooth.UUID(service_uuid), 
                (
                    (bluetooth.UUID(sender_uuid), bluetooth.FLAG_NOTIFY), 
                    (bluetooth.UUID(reader_uuid), bluetooth.FLAG_WRITE),
                )
            ), 
        )

        ((self.tx, self.rx,), ) = self.ble.gatts_register_services(services)
        
	#发送函数
    def send(self, data):
        self.ble.gatts_notify(0, self.tx, data + '\n')

    def advertiser(self):
        name = bytes(self.name, 'UTF-8')
        adv_data = bytearray('\x02\x01\x02') + bytearray((len(name) + 1, 0x09)) + name
        self.ble.gap_advertise(100, adv_data)
        print(adv_data)
        print("\r\n")

#按钮触发中断的函数(可不要)
def buttons_irq(pin):
    led.value(not led.value())
    print('LED is ON.' if led.value() else 'LED is OFF')
    ble.send('LED is ON.' if led.value() else 'LED is OFF')


2.舵机初始化及应用

代码如下:

#pwm控制舵机角度
S1 = PWM(Pin(18), freq=50, duty=0) # Servo1 的引脚是 18

def Servo(servo,angle):
 S1.duty(int(((angle+90)*2/180+0.5)/20*1023))#通过这条算式计算相应的角度,手册里这么写就直接用了

三、完整代码

代码如下:

from machine import Pin, PWM
from machine import Timer
from time import sleep_ms
import bluetooth

BLE_MSG = ""

S1 = PWM(Pin(18), freq=50, duty=0) # Servo1 的引脚是 18


def Servo(servo,angle):
 S1.duty(int(((angle+90)*2/180+0.5)/20*1023))
 
 
class ESP32_BLE():
    def __init__(self, name):
        self.led = Pin(2, Pin.OUT)
        self.timer1 = Timer(0)
        self.name = name
        self.ble = bluetooth.BLE()
        self.ble.active(True)
        self.ble.config(gap_name=name)
        self.disconnected()
        self.ble.irq(self.ble_irq)
        self.register()
        self.advertiser()

    def connected(self):
        self.led.value(1)
        self.timer1.deinit()

    def disconnected(self):        
        self.timer1.init(period=100, mode=Timer.PERIODIC, callback=lambda t: self.led.value(not self.led.value()))

    def ble_irq(self, event, data):
        global BLE_MSG
        if event == 1: #_IRQ_CENTRAL_CONNECT 手机链接了此设备
            self.connected()
        elif event == 2: #_IRQ_CENTRAL_DISCONNECT 手机断开此设备
            self.advertiser()
            self.disconnected()
        elif event == 3: #_IRQ_GATTS_WRITE 手机发送了数据 
            buffer = self.ble.gatts_read(self.rx)
            BLE_MSG = buffer.decode('UTF-8').strip()
            
    def register(self):        
        service_uuid = '6E400001-B5A3-F393-E0A9-E50E24DCCA9E'
        reader_uuid = '6E400002-B5A3-F393-E0A9-E50E24DCCA9E'
        sender_uuid = '6E400003-B5A3-F393-E0A9-E50E24DCCA9E'

        services = (
            (
                bluetooth.UUID(service_uuid), 
                (
                    (bluetooth.UUID(sender_uuid), bluetooth.FLAG_NOTIFY), 
                    (bluetooth.UUID(reader_uuid), bluetooth.FLAG_WRITE),
                )
            ), 
        )

        ((self.tx, self.rx,), ) = self.ble.gatts_register_services(services)

    def send(self, data):
        self.ble.gatts_notify(0, self.tx, data + '\n')

    def advertiser(self):
        name = bytes(self.name, 'UTF-8')
        adv_data = bytearray('\x02\x01\x02') + bytearray((len(name) + 1, 0x09)) + name
        self.ble.gap_advertise(100, adv_data)
        print(adv_data)
        print("\r\n")


def buttons_irq(pin):
    led.value(not led.value())
    print('LED is ON.' if led.value() else 'LED is OFF')
    ble.send('LED is ON.' if led.value() else 'LED is OFF')


if __name__ == "__main__":
    ble = ESP32_BLE("ESP32BLE")

    but = Pin(0, Pin.IN)
    but.irq(trigger=Pin.IRQ_FALLING, handler=buttons_irq)

    led = Pin(2, Pin.OUT)
    
    Servo(S1,90);
    sleep_ms(500)
    
    #这里模拟开门的动作
    while True:
        if BLE_MSG == '1':
             led.value(0)
             Servo(S1,-90)
             sleep_ms(5000)
             Servo(S1,90)
        
        sleep_ms(100)

总结

完成上面步骤只需要下载一个蓝牙调试助手,搭配一些简单的结构就可以做出很多解决生活中实际问题的实验。独立供电只需要三节电池串联接上VCC和GND就可以。

猜你喜欢

转载自blog.csdn.net/weixin_46155589/article/details/127230740