2017-7-23 17:52:21 [显示全部楼层]
7937浏览
查看: 7937|回复: 2

esp32 micropython ble_center初体验

[复制链接]
本帖最后由 dfrobot_wx 于 2017-7-23 17:52 编辑

本例程所需原料
硬件
  1.  FireBeetle-ESP32主板
  2.  Bluno主板
软件
  1. uPyCraft
esp32端代码如下:
[mw_shl_code=python,true]import gc
import sys
import network as n
import gc
import time

b = n.Bluetooth()

found = {}
complete = True
connectname = ""
mac=b'\x00\x00\x00\x00\x00\x00'
def bcb(b,e,d,u):
  global complete
  global found
  global connectname
  global mac
  if e == b.CONNECT:
        print("CONNECT")
  elif e == b.DISCONNECT:
        print("DISCONNECT")
  elif e == b.SCAN_RES:
    if complete:
      found = {}
      adx, name,ssic= d
      if connectname == str(name):
        mac = adx
        print(d)
        if adx not in found:
          found[adx] = name
      else:
        print('else')
        print(d)

  elif e == b.SCAN_CMPL:
    ok = True
    print("Scan Complete")
    complete = True
    print ('\nFinal List:')
    for adx, name in found.items():
      print ('Found:' + ':'.join(['%02X' % i for i in adx]), name)
      print('connect ',name)
  else:
    print ('Unknown event', e,d)

def cb (cb, event, value, userdata):
  print('charcb ', cb, userdata, ' ', end='')
  if event == b.READ:
    print('Read')
    return 'ABCDEFG'
  elif event == b.WRITE:
    print ('Write', value)
  elif event == b.NOTIFY:
    print ('Notify', value)
def conn(bda):
  b.ble_settings(adv_is_scan_rsp = False)
  conn = b.connect(bda)
  i=0
  while not conn.is_connected():
    i=i+1
    time.sleep(.1)
    print ('Connected')
    time.sleep(2) # Wait for services
    service = ([s for s in conn.services() if s.uuid()[0:4] == b'\x00\x00\xDF\xB0'] + [None])[0]
    if service:
      print(service.chars())
      char = ([c for c in service.chars() if c.uuid()[0:4] == b'\x00\x00\xDF\xB1'] + [None])[0]
      char.callback(cb)
      if char:
        char.callback(cb)
    if i == 10:
      print('time out')
      break
  return conn
def onScanComplete():
  time.sleep(2)
def set_connetname(name):
  return "b'%s'" % name
def scan():
  global connectname
  connectname = set_connetname("Bluno")
  b.ble_settings(adv_is_scan_rsp = True)
  b.scan_start(5)
  time.sleep(10)
b.callback(bcb)
scan()
print('mac:',mac)
conn(mac)
[/mw_shl_code]
1. 使用uPyCraft下载到板子上并运行esp32 micropython ble_center初体验图1
2. BLE开始扫描并连接设备
esp32 micropython ble_center初体验图3

3. 成功连接上设备

esp32 micropython ble_center初体验图5

4. 打开串口调试工具,向BLUNO串口发送数据

esp32 micropython ble_center初体验图2

5、Esp32收到的数据

esp32 micropython ble_center初体验图4

这个例程在连接过程中有时会出现不稳定现象,望各位大神指正。
该例程中使用的从机模块是bluno,各位师兄可以使用其他的ble模块,只需要在esp32程序中的更改相应的设备名以及需要监听的uuid即可。


秦皇岛岛主  初级技师 来自手机

发表于 2017-7-25 08:37:28

[赞],点击[ http://pinyin.cn/e19975 ]查看表情
回复

使用道具 举报

zyber  学徒

发表于 2018-7-28 20:07:23

你好 ,我用时用的程序连一个蓝牙手柄 显示连接不上,
0;31mE (996959) BT: btm_sec_clr_temp_auth_service() - no dev CB

E (996959) BT: bta_gattc_conn_cback() - cif=4 connected=0 conn_id=4 reason=0x0008
E (996959) BT: Device not found

E (996959) BT: bta_gattc_mark_bg_conn unable to find the bg connection mask for: ff:ff:97:01:cd:64
回复

使用道具 举报

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

本版积分规则

为本项目制作心愿单
购买心愿单
心愿单 编辑
[[wsData.name]]

硬件清单

  • [[d.name]]
btnicon
我也要做!
点击进入购买页面
关于楼主
上海智位机器人股份有限公司 沪ICP备09038501号-4

© 2013-2024 Comsenz Inc. Powered by Discuz! X3.4 Licensed

mail