『瀚思彼岸』» 智能家居技术论坛

 找回密码
 立即注册
123
返回列表 发新帖
楼主: fentensoft

[经验分享] 当贝投影仪通过ESP32接入HA方案,代码已开源

[复制链接]

0

主题

1

帖子

26

积分

新手上路

Rank: 1

积分
26
金钱
25
HASS币
0
发表于 2024-1-25 10:05:17 | 显示全部楼层
纯小白,楼主能不能分享下代码怎么烧录到ESP32
回复

使用道具 举报

7

主题

1072

帖子

3361

积分

论坛元老

Rank: 8Rank: 8

积分
3361
金钱
2289
HASS币
0
发表于 2024-1-25 12:05:18 | 显示全部楼层
我觉得坚果投影仪应该也能用
回复

使用道具 举报

0

主题

2

帖子

16

积分

新手上路

Rank: 1

积分
16
金钱
14
HASS币
0
发表于 2024-3-11 22:12:04 | 显示全部楼层
大佬,你好,我现在发送关闭命令的时候显示包是{0x02,0x01,0x2e,0x00,0x0a,0x00,0x04,0x00,0x1b,0x1b,0x00,0x00,0x00,0x66,0x00,0x00,0x00,0x00},0X1B00如何改为0x3600呢
回复

使用道具 举报

0

主题

2

帖子

16

积分

新手上路

Rank: 1

积分
16
金钱
14
HASS币
0
发表于 2024-3-11 22:14:50 | 显示全部楼层
本帖最后由 fengqiluoye 于 2024-3-11 22:22 编辑
fengqiluoye 发表于 2024-3-11 22:12
大佬,你好,我现在发送关闭命令的时候显示包是{0x02,0x01,0x2e,0x00,0x0a,0x00,0x04,0x00,0x1b,0x1b,0x00, ...

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

BLE_MSG = ""

KEYBOARD_REPORT = bytes([    # Report Description: describes what we communicate
    0x05, 0x0c, 0x09, 0x01, 0xa1, 0x01, 0x85, 0x01, 0x19, 0x00, 0x2a, 0x9c,
    0x02, 0x15, 0x00, 0x26, 0x9c, 0x02, 0x95, 0x01, 0x75, 0x10, 0x81, 0x00,
    0x09, 0x02, 0xa1, 0x02, 0x05, 0x09, 0x19, 0x01, 0x29, 0x0a, 0x15, 0x01,
    0x25, 0x0a, 0x95, 0x01, 0x75, 0x08, 0x81, 0x40, 0xc0, 0xc0, 0x06, 0x01,
    0xff, 0x09, 0x01, 0xa1, 0x02, 0x85, 0x05, 0x09, 0x14, 0x75, 0x08, 0x95,
    0x14, 0x15, 0x80, 0x25, 0x7f, 0x81, 0x22, 0x85, 0x04, 0x09, 0x04, 0x75,
    0x08, 0x95, 0x01, 0x91, 0x02, 0xc0, 0x05, 0x01, 0x09, 0x06, 0xa1, 0x01,
    0x85, 0x0a, 0x75, 0x01, 0x95, 0x08, 0x05, 0x07, 0x19, 0xe0, 0x29, 0xe7,
    0x15, 0x00, 0x25, 0x01, 0x81, 0x02, 0x95, 0x01, 0x75, 0x08, 0x81, 0x01,
    0x95, 0x05, 0x75, 0x01, 0x05, 0x08, 0x19, 0x01, 0x29, 0x05, 0x91, 0x02,
    0x95, 0x01, 0x75, 0x03, 0x91, 0x01, 0x95, 0x06, 0x75, 0x08, 0x15, 0x00,
    0x26, 0xff, 0x00, 0x05, 0x07, 0x19, 0x00, 0x29, 0xff, 0x81, 0x00, 0xc0,
        ])

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 手机发送了数据
            pass
            #buffer = self.ble.gatts_read(self.rx)
            #BLE_MSG = buffer.decode('UTF-8').strip()
            
    def register(self):        
        HIDS = (
            bluetooth.UUID(0x1812),                     # Human Interface Device
            (
                (bluetooth.UUID(0x2A4A), bluetooth.FLAG_READ),       # HID information
                (bluetooth.UUID(0x2A4B), bluetooth.FLAG_READ),       # HID report map
                (bluetooth.UUID(0x2A4C), bluetooth.FLAG_WRITE),      # HID control point
                (bluetooth.UUID(0x2A4D), bluetooth.FLAG_READ | bluetooth.FLAG_NOTIFY, ((bluetooth.UUID(0x2908), 1),)),  # HID report / reference
                (bluetooth.UUID(0x2A4D), bluetooth.FLAG_READ | bluetooth.FLAG_WRITE,  ((bluetooth.UUID(0x2908), 1),)),  # HID report / reference
                (bluetooth.UUID(0x2A4E), bluetooth.FLAG_READ | bluetooth.FLAG_WRITE), # HID protocol mode
            ),
        )
       self.services = (HIDS,)
       self.handles = self.ble.gatts_register_services(self.services)

    def send(self, data):
        self.ble.gatts_notify(0, self.h_repin, data)

    def advertiser(self):
           #设置BLE广播数据并开始广播
        self.ble.gap_advertise(100, adv_data = b'\x02\x01\x06'
                                        + b'\x03\x03\x12\x18' #HID UUID
                                        + b'\x03\x19\xC1\x03' #设备外观为键盘
                                        + b'\x0D\x09' + "ESP Keyboard".encode("UTF-8"))

        (self.h_info, self.h_map, _, self.h_repin, self.h_d1, self.h_repout, self.h_d2, self.h_model,) = self.handles[0]


        # Write service characteristics
        self.ble.gatts_write(self.h_info, b"\x11\x01\x00\x01")     # HID info: ver=1.1, country=0, flags=normal
        self.ble.gatts_write(self.h_map, KEYBOARD_REPORT)    # HID input report map
        self.ble.gatts_write(self.h_d1, b"\x01\x01")  # HID reference: id=1, type=input
        self.ble.gatts_write(self.h_d2, b"\x01\x01")  # HID reference: id=1, type=output
        self.ble.gatts_write(self.h_model, b"\x01")   # HID Protocol Model: 0=Boot Model, 1=Report Model


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

    key = Pin(0,Pin.IN)#IO 0 用作按键
    while True:
        if key.value() == 0:
            while key.value() == 0:
              pass
            print("11111")
            ble.send(b'\x00\x00\x66\x00\x00\x00\x00')
            sleep_ms(750)
            ble.send(b'\x00\x00\x00\x00\x00\x00\x00')


这是我使用thony编写的代码,感觉都一样的,但是抓到的数据确实有问题的
回复

使用道具 举报

0

主题

39

帖子

328

积分

中级会员

Rank: 3Rank: 3

积分
328
金钱
289
HASS币
0
发表于 2024-3-26 18:30:56 | 显示全部楼层
本帖最后由 dongjianwei 于 2024-3-26 21:08 编辑

66666看看
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|小黑屋|Hassbian

GMT+8, 2024-4-27 23:37 , Processed in 0.049883 second(s), 25 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表