Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get battery status? (feature) #70

Closed
montjoy opened this issue Jan 4, 2022 · 15 comments
Closed

Get battery status? (feature) #70

montjoy opened this issue Jan 4, 2022 · 15 comments

Comments

@montjoy
Copy link

montjoy commented Jan 4, 2022

Hi - thanks so much for making this!

Do you have any plans to report on the battery status? It's something you can view in the official app. It would be nice to be able to see when the batteries are low and send a notification.

@sverrham
Copy link
Collaborator

sverrham commented Jan 5, 2022

That information is not available on the documented interface I think, if someone knows how to get that info we could implement it.
So no not planned.

@montjoy
Copy link
Author

montjoy commented Jan 11, 2022

Ok thank you. I just thought maybe there was a "standard" bluetooth identifier for battery. Cheers!

@sverrham
Copy link
Collaborator

Maybe it is possible, have to test if the endpoint is available
some links of relevance
https://www.andreasjakl.com/read-battery-level-bluetooth-le-devices/
https://stackoverflow.com/questions/52281070/what-does-battery-level-state-0x2a1b-bluetooth-specification-mean

If I have time I might check this out more...

@mstaalesen
Copy link

I've been trying to get the batterystatus for some time now too (manually with bluetoothctl + gatt). I was not successful, but I did stumble across this: Airthings/waveplus-reader#11 (comment)

Perhaps that could put you in the right direction?

@sverrham
Copy link
Collaborator

I see, that is interesting, seems like it should be kind of stright forward for the pluss model with the command uuid point if write and read to that works as I think...
Would need to access this:
command_uuid = UUID('b42e2d06-ade7-11e4-89d3-123b93f75cba') # "Access Control Point" Characteristic
Send this:
struct.pack('<B', 0x6d)
Read back and parse as
` d = struct.unpack('<L12B6H', q2['raw'])

q2['ambientlight'] = d[2]
q2['measurement_periods'] =  d[5]
q2['voltage'] = d[17] / 1000.0

V_MAX=3.2
V_MIN=2.2
q2['battery']= max(0, min(100, round( (q2['voltage']-V_MIN)/(V_MAX-V_MIN)*100)))`

ok, maybe I can setup a rpi and test this the comming days...

@sverrham
Copy link
Collaborator

sverrham commented Jan 29, 2022

I guess not as stright forward, did not get read to work, it uses notifications and I did not get that to work...

Seems I do not get any reply or notification... probably me not getting the setup correctly.

Tried adding delegate:


class MyDelegate(btle.DefaultDelegate):
    def __init__(self):
        btle.DefaultDelegate.__init__(self)
        # ... initialise here

    def handleNotification(self, cHandle, data):
        # ... perhaps check cHandle
        # ... process 'data'
        print(cHandle)
        print(data)

self._dev = btle.Peripheral(mac.lower())
self._dev.withDelegate( MyDelegate() )

Then write to the command uuid and wait:

char.write(struct.pack('<B', 0x6d))
 for i in range(10):
    if self._dev.waitForNotifications(1.0):                                        
       continue

But nothing received, hmm
Maybe I need to enable the notification but how to do.

@sverrham
Copy link
Collaborator

sverrham commented Jan 29, 2022

Got it working I think, needed to add this to get notifications to work:

characteristic_configure_uuid   = btle.UUID('2902')
d, = char.getDescriptors(forUUID=characteristic_configure_uuid)
d.write(struct.pack('<H', 1), True)

Mannaged to get this printout:
DEBUG:__main__: Got cmddata {'ambientlight': 0, 'measurement_periods': 36, 'voltage': 2.826, 'battery': 63}

sverrham added a commit to sverrham/sensor.airthings_wave that referenced this issue Jan 30, 2022
…th the command interface uuid.

Tested and working with the airthings pluss only.
custom-components#70
sverrham added a commit to sverrham/sensor.airthings_wave that referenced this issue Jan 30, 2022
sverrham added a commit to sverrham/sensor.airthings_wave that referenced this issue Jan 30, 2022
@sverrham
Copy link
Collaborator

sverrham commented Jan 30, 2022

Updated in fork, seems to be working for me, if someone else wants to test before I make a pull request and release.
I have only tested with airthings wave plus device, as that is the only one I have.
If it works with other devices or will crash horibly I do not know... would be nice with more testing.

@mstaalesen
Copy link

Great work. I will try to test it this afternoon.

@MartyTremblay
Copy link
Member

Cool stuff!

Tested and seems to work without breaking my first-gen Wave; but has no battery status of course.

...Time to upgrade my Wave I guess :/

@mstaalesen
Copy link

Looking good!

DEBUG:__main__:<redacted> Got cmddata {'ambientlight': 13, 'measurement_periods': 19, 'voltage': 3.261, 'battery': 100}

@sverrham
Copy link
Collaborator

sverrham commented Feb 3, 2022

Just pushed some updates to handle some bad data that I have seen runing this a couple of days, will let it run some more.

Also I am debating if the battery sensor with % battery should be as it is, since it is derived from the voltage, I am leaning towrads having this calculated on a "higher" level, maybe letting the user set max min values, if I keep it linear as now, but then again might just keep it as it is, will not be "right" either way...

sverrham added a commit to sverrham/sensor.airthings_wave that referenced this issue Feb 4, 2022
… voltage from the device.

Adds a battery sensor that converts voltage to battery percentage, linear calculation from min to max values that are user configurable.
Main data from the sensor will be battery percentage but voltage will be returned as an attribute.

Adds description to the readme.
custom-components#70
@sverrham
Copy link
Collaborator

sverrham commented Feb 4, 2022

Ok, I feel more happy with the latest version, now there is one battery sensor that converts from voltage to percentage with user configurable max min values and the voltage is added as an attribute.
I think this is a good solution that is more flexible so the user can "tune" the batter percentage levels.

I will lett it run a bit to see if I have any issues, then push this out unless there are any objections or maybe other solutions.

@sverrham sverrham closed this as completed Feb 5, 2022
@montjoy
Copy link
Author

montjoy commented Feb 9, 2022

@sverrham Looks like it works! Thank you so much!

@arnerek
Copy link

arnerek commented Mar 22, 2022

I set V_MAX to 3.0 and V_MIN to 2.0. Noe The battery reading is consistent with the official Airthings app.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants