Skip to content

micropython/drivers: Add "sht3x" sensor driver.#549

Closed
wemos wants to merge 3 commits intomicropython:masterfrom
wemos:master
Closed

micropython/drivers: Add "sht3x" sensor driver.#549
wemos wants to merge 3 commits intomicropython:masterfrom
wemos:master

Conversation

@wemos
Copy link
Copy Markdown

@wemos wemos commented Oct 13, 2022

Add "sht3x" sensor driver.

Copy link
Copy Markdown
Member

@jimmo jimmo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @wemos !

@@ -0,0 +1 @@
module("sht3x.py", opt=3)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a metadata() with description and version.

# SHT3x driver for MicroPython on ESP8266/ESP32
# MIT license; Copyright (c) 2022 WEMOS.CC

import utime
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Prefer to use import time

@@ -0,0 +1,26 @@
# SHT3x driver for MicroPython on ESP8266/ESP32
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doesn't look like this is specific to ESP as it uses the common i2c API.

Can you also add that it's a temp/humidity sensor, and made by Sensirion.

class SHT3X:
def __init__(self, i2c, address=0x45):
self.bus = i2c
self.slv_addr = address
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

_i2c, _addr, and _buf

def measure(self):
self.bus.writeto(self.slv_addr, b"\x24\x00")
utime.sleep(1)
self.buf = self.bus.readfrom(self.slv_addr, 6)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be readfrom_into to avoid re-allocating buf.

utime.sleep(1)
self.buf = self.bus.readfrom(self.slv_addr, 6)

def humidity(self):
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder about adding a non-floating point version of these functions (to avoid float allocations), i.e.return an integer in 1/10ths of a degree / percent. (The point being that if you remove this float allocation and with the readfrom_into above, then the whole driver is non-allocating).

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or perhaps just have only the versions that return tenths and the calling code can simply convert to float by doing `s.humidity() / 10.0' if necessary.

humidity = 100.0 * float(humidity_raw) / 65535.0
return humidity

def temperature(self):
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Assuming this is in degrees Celcius? (Maybe add a comment so someone can easily check this by looking at the code, or perhaps rename the variable to temperature_c)

@jimmo
Copy link
Copy Markdown
Member

jimmo commented Nov 7, 2022

@wemos Thanks for implementing the SC7A20 driver too, but could you please add this in a different PR?

We're keen to merge this PR, but please address the review comments first.

@wemos
Copy link
Copy Markdown
Author

wemos commented Nov 7, 2022

ok

@wemos wemos closed this Nov 7, 2022
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

Successfully merging this pull request may close these issues.

2 participants