10502| 0
|
micropython学习笔记之蓝牙心率带读取及显示 |
本帖最后由 沧海笑 于 2017-9-3 13:04 编辑 【内容】micropython学习笔记之蓝牙心率带读取及显示 【时间】2017-09-03 【本期主要玩法】 玩blynk的时候,就通过Arduino 101读过蓝牙心率带数据,同时显示在oled上。这次esp32的ble正在测试,感谢岛主、나 는 당신 을 사랑 한 男 师兄的资料,我是在他们的基础上,稍加改动,读取了蓝牙心率带的数据,这样就很容易做一只与心率带配合的心率表了,如果加上姿态传感器以及算法,就可以做出更多的功能。 【硬件】 2、蓝牙心率带 1条 (某宝有售,要求支持ble4.0) 3.oled ssd1306屏 1块(I2C接口) 4、杜邦线4根 【软件】
【接线】
代码: [mw_shl_code=python,true]#BT_HRM read .it works #2017-07-24 import gc import sys import network as n import gc import time from machine import Pin,I2C import ssd1306 i2c = I2C(scl=Pin(22), sda=Pin(21), freq=100000) lcd=ssd1306.SSD1306_I2C(128,64,i2c) 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") oled("CONNECT") elif e == b.DISCONNECT: print("DISCONNECT") oled("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") oled("Scan Complete") complete = True print ('\nFinal List:') for adx, name in found.items(): #显示搜索BLE设备的结果 print ('Found:' + ':'.join(['%02X' % i for i in adx]), name) print('connect ',name) oled(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) period = None flags = value[0] hr = value[1] if flags & 0x10: period = (value[3] << 8) + value[2] print ('HR:', hr, 'Period:', period, 'ms') lcd.fill(0) lcd.text("HR:",20,16) lcd.text(str(hr),50,16) lcd.show() def conn(bda): b.ble_settings(adv_is_scan_rsp = False) conn = b.connect(bda) while not conn.is_connected(): 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\x18\x0d'] + [None])[0] if service: print(service.chars()) char = ([c for c in service.chars() if c.uuid()[0:4] == b'\x00\x00\x2a\x37'] + [None])[0] if char: descr = ([d for d in char.descrs() if d.uuid()[0:4] == b'\x00\x00\x29\x02'] + [None])[0] if descr: char.callback(cb) descr.write(b'\x01\x00') # Turn on notify return conn def onScanComplete(): time.sleep(2) def set_connetname(name): return "b'%s'" % name def scan(): global connectname connectname = set_connetname("BT_HRM") oled('BT_HRM') b.ble_settings(adv_is_scan_rsp = True) b.scan_start(5) time.sleep(10) def oled(lcdtxt): lcd.fill(0) lcd.text(lcdtxt,20,16) lcd.show() b.callback(bcb) scan() print('mac:',mac) oled(mac) conn(mac)[/mw_shl_code] 【小结】 BLE是esp32的重要亮点,希望大家一起挖掘分享好玩的应用。但是目前还在完善中。比如在esp32与ble设备连接后,不能释放的问题到目前还未解决,我也和大家一起期待esp32 BLE的不断进步。 |
© 2013-2024 Comsenz Inc. Powered by Discuz! X3.4 Licensed