Reading data from bluetooth device
-
Hi I've done a search but it's a bit unclear to me what the current state of bluetooth support is.
Am I likely to have success connecting this device https://wiki.hackerspace.pl/projects:xiaomi-flora
It looks like I need
sudo apt-get install python-pip
pip install paho-mqtt
pip install gattlibAre these supported on LoPy?
-
@sam_uk said in Reading data from bluetooth device:
Hi,
The commands you list are for a full installation of python, not micropython, which does not have pip. Please check out our bluetooth api documentation for details about how to use the bluetooth radio in our modules: https://docs.pycom.io/chapter/firmwareapi/pycom/network/bluetooth/
-
Thanks, it seems like paho-mqtt exists in micropython, but not gattlib.
I've found this alternate Esp2866 route so I think I'll pursue that for now.
Thanks
Sam
-
@sam_uk said in Reading data from bluetooth device:
gattlib
Our devices do support GATT directly in our firmware, you can find examples of this in the link I provided
-
Hi @seb ,
Can you help me with the connection and those GATT instructions ?
from network import Bluetooth import time bt = Bluetooth() print("searching") bt.start_scan(-1) print("connection") bt.connect('C47C8D643FAF') # Flower print("connected")I just succeed in a connection.
How can I go further ?How can I send those 0x0038 , 0xA01F , 0x0033 , 0x0035 and read the returned value ? as describe their : https://wiki.hackerspace.pl/projects:xiaomi-flora
-
Hi,
I dont have access to this specific device to test but you can do something like this:
from network import Bluetooth import time bluetooth = Bluetooth() bluetooth.start_scan(10) while True: adv = bluetooth.get_adv() if adv and binascii.hexlify(adv.mac) == 'XXXXXXXXXX': try: print('1') conn = bluetooth.connect(adv.mac) except: bluetooth.start_scan(5) continue break def characteristic_callback(char): v = int.from_bytes(char.value(), "little") print("Got new value: {}".format(v)) services = conn.services() for service in services: print("Service: {}".format(service.uuid())) if service.uuid() == b'XXXXXXXXXXXXX': characteristics = service.characteristics() for characteristic in characteristics: print("Characteristic: {}".format(characteristic.uuid())) if characteristic.uuid() == b'XXXXXXXXXXXX': characteristic.callback(trigger=Bluetooth.CHAR_NOTIFY_EVENT, handler=characteristic_callback, arg=None)You will need to replace the
XXXparts with values that match your sensor -
I write this code to be able to find where it doesn't work
from network import Bluetooth import time bt = Bluetooth() print("searching") bt.start_scan(-1) print("connection") conn = bt.connect('C47C8D643FAF') print("connected") def characteristic_callback(char): v = int.from_bytes(char.value(), "little") print("Got new value: {}".format(v)) print("before services") services = conn.services() print("services") for service in services: print("Service: {}".format(service.uuid())) if service.uuid() == b'XXXXXXXXXXXXX': characteristics = service.characteristics() for characteristic in characteristics: print("Characteristic: {}".format(characteristic.uuid())) if characteristic.uuid() == b'XXXXXXXXXXXX': characteristic.callback(trigger=Bluetooth.CHAR_NOTIFY_EVENT, handler=characteristic_callback, arg=None)So, in the terminal it print
searching
connection
connected
before servicesSo it never reach the line :
services = conn.services()in the page https://wiki.hackerspace.pl/projects:xiaomi-flora , it's specified that "You need to write 0xA01F to the handle 0x0033 to enable real-time data reading "
How can I write the value 0xA01F in the handle 0x0033 ?
Thanks in advance for your precious help.
Hello! It looks like you're interested in this conversation, but you don't have an account yet.
Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.
With your input, this post could be even better 💗
Register Login