ATC_MiThermometer icon indicating copy to clipboard operation
ATC_MiThermometer copied to clipboard

Feature request: python script to read advertising packet

Open mexon opened this issue 3 years ago • 10 comments

I was hoping to use this firmware to regularly log the temperature on a raspberry pi. So far I've found that I can run "sudo hcitool lescan" in one process and "sudo hcidump --raw" in another process. But this is really cumbersome to do from a cron job.

I thought I might write a small python script, but when I google I find a bewildering number of libraries and examples. All of them are designed to do something else, none of them seem capable of just reading an advertising packet. I've investigated bluepy, Adafruit Blinka bleio, beacontools, pygatt, and now I'm just really confused.

Doesn't have to be python of course. I did try scan-thermometer.js, but it has a bunch of dependencies not available in raspberry pi os. The HTML pages are obviously not practical in this application. Any suggestions for the simplest way to just get the current temperature from a thermometer? Headless, portably, and fairly cleanly?

mexon avatar May 31 '21 16:05 mexon

I came across the same problem, and would also appreciate some simple example code somewhere. The key step for which clear instructions are missing from almost everything I found online is that once you find the ~descriptor~ characteristic, you need to enable notifies, or it will never produce notify events:

# `c` is the characteristic object
c.getDescriptors(0x2902)[0].write(b"\x01\x00", True)

It is not exactly a trivial example, but I posted my code to collect the sensor data and dump it to influx in a gist, hopefully it is some help.

ktims avatar Jun 02 '21 00:06 ktims

https://github.com/pvvx/hcitooladv

pvvx avatar Jun 04 '21 22:06 pvvx

I wrote a little script to do this. It might not be the most elegant, but it works: atclisten.py. note: run as root/sudo

edit: if you just want to log to a file, remove all the mqtt stuff and just print "payload" to stdout

KR4DIO avatar Jun 09 '21 14:06 KR4DIO

All useful suggestions, thanks. Here's what I went with - it does what I need:

https://github.com/atc1441/ATC_MiThermometer/pull/190

mexon avatar Jun 10 '21 18:06 mexon

All useful suggestions, thanks. Here's what I went with - it does what I need:

#190

@mexon Thanks for doing this.

One issue I am having with it, is that it displays the temperature wrong, for example it'll display 6.0 when the temp is actually 30.0(°C) on the display.

The other values are correct.

Any idea?

samster395 avatar Jun 16 '21 11:06 samster395

For me I do very occasionally get bad readings:

{"timestamp": "2021-06-16T14:00:11+02:00", "mac": "A4:C1:38:XX:XX:XX", "temperature": 22.8, "humidity": 49, "battery_percent": 86, "battery_volt": 0.336, "count": 173} {"timestamp": "2021-06-16T14:05:03+02:00", "mac": "A4:C1:38:XX:XX:XX", "temperature": 4.9, "humidity": 0, "battery_percent": 85, "battery_volt": 0.335, "count": 178} {"timestamp": "2021-06-16T14:05:04+02:00", "mac": "A4:C1:38:XX:XX:XX", "temperature": 22.8, "humidity": 49, "battery_percent": 85, "battery_volt": 0.335, "count": 178}

I thought at first that this might be data that was corrupted "on the wire(less)". But BT advertising packets are protected by a CRC, so that should be impossible. Instead I'm assuming it must be some glitch in the firmware.

Perhaps you could dump the full raw data from val?

mexon avatar Jun 16 '21 13:06 mexon

I'm getting bad readings every time for some reason.

Here is the dump: "1a18a4c1388f354501341d300a5a59"

samster395 avatar Jun 16 '21 13:06 samster395

Well the raw data looks good then, reading 30.8. So no idea why the python is misfiring 🤷‍♂️

mexon avatar Jun 16 '21 17:06 mexon

@ToXIc-Dev yeah, dumb mistake, sorry about that: https://github.com/atc1441/ATC_MiThermometer/pull/194

mexon avatar Jun 19 '21 12:06 mexon

This work by @mexon has been merged and is available at

https://github.com/atc1441/ATC_MiThermometer/tree/master/contrib/python

gene1wood avatar Dec 21 '23 23:12 gene1wood