0

Si7021 Library
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Si7021 Library

by cbalmer on Sat Dec 31, 2016 5:15 pm

I noticed that a Python based library for the Si7021 was missing so I created one over the weekend. It's available here if anyone wants it: https://github.com/chrisbalmer/micropython-si7021

I still need to add in the checksum verification for the serial number data and refactor some of the i2c communication. But it is a working library for temperature and humidity readings.

It originally was going to be a straight conversion of the C++ library from Adafruit (you can see the constant names are the same) but I deviated shortly after the constants. If that causes any issues, let me know.

cbalmer
 
Posts: 36
Joined: Thu Nov 26, 2015 3:25 pm

Re: Si7021 Library

by nickmomrik on Sat Dec 31, 2016 9:10 pm

Awesome! I just started playing with a Si7021 last night and found a different library that needed a bunch installs to work. I'll give this one a try tomorrow.

nickmomrik
 
Posts: 71
Joined: Mon Dec 12, 2016 10:04 pm

Re: Si7021 Library

by nickmomrik on Sat Dec 31, 2016 11:29 pm

Oh, I missed that it was MicroPython. My project is on a Raspberry Pi.

nickmomrik
 
Posts: 71
Joined: Mon Dec 12, 2016 10:04 pm

Re: Si7021 Library

by tannewt on Tue Jan 03, 2017 2:13 pm

Thanks for the link cbalmer! That looks really interesting. I've really grown to like using properties as a way to access data from a sensor. For example:

Code: Select all | TOGGLE FULL SIZE
sensor = Si7021(i2c)
print(sensor.temperature)
print(sensor.humidity)


However, that begs the question of how to deal with units. It'd be awesome if the drivers returned their data with unit information attached so that calculations between units are checked. Have you ever used a Python units library? I wonder which one is best? We have the extra challenge of little memory so we may need our own way of doing it.

tannewt
 
Posts: 2496
Joined: Thu Oct 06, 2016 8:48 pm

Re: Si7021 Library

by cbalmer on Tue Jan 03, 2017 9:28 pm

tannewt2 wrote:Thanks for the link cbalmer! That looks really interesting. I've really grown to like using properties as a way to access data from a sensor. For example:

Code: Select all | TOGGLE FULL SIZE
sensor = Si7021(i2c)
print(sensor.temperature)
print(sensor.humidity)


However, that begs the question of how to deal with units. It'd be awesome if the drivers returned their data with unit information attached so that calculations between units are checked. Have you ever used a Python units library? I wonder which one is best? We have the extra challenge of little memory so we may need our own way of doing it.


Yeah, I thought about it for a while before going with read_value() functions. I prefer properties but it didn't seem like a property because it has to pull data from the sensor live. So the read_value() implies that it isn't something you can change. I still don't like the way I went with it to be honest, it feels dirty compared to just sensor.property.

Originally I also returned a tuple for Celsius and Fahrenheit but then swapped to a dict and then swapped to a separate function before posting on GitHub. I almost dropped Fahrenheit entirely but I didn't feel like converting it each time I used it in a new program.

I haven't used a units library before, actually I didn't know they existed until now. But you're right about memory. On the ESP8266, I can't load much in the way of libraries unless I freeze them in the firmware. I hit that road block when I wrote a webauth module for wireless networks that do authentication via a webpage after association. I tried to make a universal module but http.client and all the dependencies were too big. So I wrote two modules in the end, the micropython version uses urequests which comes frozen in the official firmware.

Thanks for the feedback!

EDIT: I think I am going to switch to properties and drop Fahrenheit. Maybe a library level method to convert Celsius to Fahrenheit. Also going to raise an exception when the CRC checks fail.

cbalmer
 
Posts: 36
Joined: Thu Nov 26, 2015 3:25 pm

Re: Si7021 Library

by tannewt on Tue Jan 03, 2017 10:22 pm

Sounds good! Please ping this thread when you've done it. For read-only properties I just have it throw an AttributeError on write.

Tony and I discussed the units thing at lunch today and KTown did some good work for Arduino libraries that I need to ask him about.

tannewt
 
Posts: 2496
Joined: Thu Oct 06, 2016 8:48 pm

Re: Si7021 Library

by cbalmer on Wed Jan 04, 2017 7:36 pm

tannewt2 wrote:Sounds good! Please ping this thread when you've done it. For read-only properties I just have it throw an AttributeError on write.


Cleaned up code has been posted. Two new exceptions added, AttributeError for the read only properties and a CRCError when the CRC check fails. Fahrenheit has been removed from the class and a helper function is in the module to do the conversion for now.

cbalmer
 
Posts: 36
Joined: Thu Nov 26, 2015 3:25 pm

Re: Si7021 Library

by tannewt on Wed Jan 04, 2017 8:56 pm

W00t! Looks awesome! One suggestion would be to rename humidity to relative_humidity. I found the Arduino Unified Sensor stuff here and would love to match the name and units listed there.

tannewt
 
Posts: 2496
Joined: Thu Oct 06, 2016 8:48 pm

Re: Si7021 Library

by cbalmer on Wed Jan 04, 2017 9:35 pm

Done. Thanks for the tips!

cbalmer
 
Posts: 36
Joined: Thu Nov 26, 2015 3:25 pm

Please be positive and constructive with your questions and comments.