basnijholt / miflora

☘️🌡🌼🥀🏡 Mi Flora Plant sensor Python package
MIT License
362 stars 99 forks source link

When Lux is > 32768, returned value is negative #88

Closed RoyTrenneman closed 6 years ago

RoyTrenneman commented 6 years ago

I changed the unpack data in miflora_poller.py, it seems work fine:

 def _parse_data(self):                                                                 
 """Parses the byte array returned by the sensor.                                   

 The sensor returns 16 bytes in total. It's unclear what the meaning of these bytes 
 is beyond what is decoded in this method.                                          

 semantics of the data (in little endian encoding):                                 
 bytes 0-1: temperature in 0.1 °C                                                   
 byte 2: unknown                                                                    
 bytes 3-4: brightness in Lux                                                       
 bytes 5-6: brightness in Lux                                                                  
 byte 7: Moisture                                                                   
 byte 8-9: Conductivity in µS/cm                                                       
 bytes 10-15: unknown                                                                                        
 """                                                                                
 data = self._cache                                                                 
 res = dict()                                                                       
 temp, res[MI_LIGHT], res[MI_MOISTURE], res[MI_CONDUCTIVITY] = \                    
     unpack('<hxiBhxxxxxx', data)                                                   
 res[MI_TEMPERATURE] = temp/10.0                                                    
 return res                                                                         
ChristianKuehnel commented 6 years ago

Hi @RoyTrenneman,

thx for finding that bug. The same is probably true for the conductivity. That should also be unsigned.

Have you ever seen brightness values > 65536 lux? So might is be 3 bytes of data, instead of 2 bytes?

I'll fix that in the next days...

RoyTrenneman commented 6 years ago

Hi ChristianKuehnel,

For conductivity, I am not sure that the value can be > 32768, but I will test that by putting the sensor in water...

Have you ever seen brightness values > 65536 lux? So might is be 3 bytes of data, instead of 2 bytes?

You meant instead of 4 bytes ? For lux value, I've never seen values > 65536 lux, but I don't know how to unpack only 3 bytes??

ChristianKuehnel commented 6 years ago

I've looked at a few samples from my machine and you're probably right: the brightness is 4 bytes instead of 2 --> I created a PR with the patch

RoyTrenneman commented 6 years ago

Hi, Yesterday was sunny, I saw a high lux value, ~77000