Micropython - SHT30 in BTC Base



  • If someone would try work with SHT30, I recommend:

    https://github.com/rsc1975/micropython-sht30

    but in this base you should propertly sel scl, sda pins and i2c addres SHT30 is 0x44.

    Code:
    from sht30 import SHT30
    sht30_sensor = SHT30(scl_pin=22, sda_pin=21, delta_temp=-3, i2c_address=0x44)
    temperature, humidity = sht30_sensor.measure()

    Also what i noticed that is showed temperature is higher than real. To correct it i use delta_temp.



  • It is a bit too early because MicroPython is still not there on M5Paper that I own, but as it uses the same SHT30, I also noticed some delta issue in temperature using Arduino.
    Is it a common thing with this component ?



  • I honestly don't understand M5Stack's way of working. There is this ENV2 with python support: https://docs.m5stack.com/#/en/unit/envII it has a SHT30, but then you spend $80 on a M5Paper and you don't get python support for the SHT30. How hard is it to put in the the right lib.

    I know I can do it myself, but M5stack should have done this already :-/



  • @sj3fk3 said in Micropython - SHT30 in BTC Base:

    I honestly don't understand M5Stack's way of working. There is this ENV2 with python support: https://docs.m5stack.com/#/en/unit/envII it has a SHT30, but then you spend $80 on a M5Paper and you don't get python support for the SHT30. How hard is it to put in the the right lib.

    I know I can do it myself, but M5stack should have done this already :-/

    Micropython on the M5Paper is in early alpha because we pushed M5Stack to get it out before the holiday. I spent yesterday doing some testing and I cannot get any SHT30 devices to work no matter if its the internal SHT30 on the paper or the env2 unit.
    What lib are you using as I cant even get raw reg read and writes to work.



  • @kedulowt said in Micropython - SHT30 in BTC Base:

    temperature, humidity = sht30_sensor.measure()

    I can't get this to work

      File "<stdin>", line 1, in <module>
      File "sht30.py", line 40, in __init__
    NameError: name 'I2C' isn't defined
    >>> 
    


  • Vielleicht hilft euch das weiter. Läuft auf einem M5Stick-C

    main.py

    import unit
    import machine
    import array
    import sht31

    from machine import I2C, Pin
    i2c = I2C(freq=100000, scl=Pin(32), sda=Pin(33) )
    sensor = sht31.SHT31(i2c, addr=0x44)

    while True:
    #Sensor SHT31-----------------------------------------------------------
    temp_humi = sensor.get_temp_humi()
    wait_ms(600)
    Temperatur = round(temp_humi[0],2)
    Feuchte = round(temp_humi[1],2)
    #Sensor SHT31-----------------------------------------------------------

    sht31.py

    from machine import I2C
    import time

    R_HIGH = const(1)
    R_MEDIUM = const(2)
    R_LOW = const(3)

    class SHT31(object):
    """
    Diese Klasse implementiert eine Schnittstelle zur SHT31 Temperatur und Luftfeuchtigkeit.
    Sensor von Sensirion.
    """
    # Diese statische Karte hilft, den Heap und die Programmlogik sauber zu halten.
    _map_cs_r = {
    True: {
    R_HIGH : b'\x2c\x06',
    R_MEDIUM : b'\x2c\x0d',
    R_LOW: b'\x2c\x10'
    },
    False: {
    R_HIGH : b'\x24\x00',
    R_MEDIUM : b'\x24\x0b',
    R_LOW: b'\x24\x16'
    }
    }

    def __init__(self, i2c, addr=0x44):
        """
        Initialisiert ein Sensorobjekt auf dem angegebenen I2C-Bus, auf das von der
        angegebene Adresse.
        """
        if i2c == None or i2c.__class__ != I2C:
            raise ValueError('I2C-Objekt als Argument benötigt!')
        self._i2c = i2c
        self._addr = addr
    
    def _send(self, buf):
        """
        Sendet das angegebene Pufferobjekt über I2C an den Sensor.
        """
        self._i2c.writeto(self._addr, buf)
    
    def _recv(self, count):
        """
        Lesen von Bytes vom Sensor über I2C. Die Anzahl der Bytes kann angegeben werden.
        als Argument.
        Liefert ein Bytearray für das Ergebnis.
        """
        return self._i2c.readfrom(self._addr, count)
    
    def _raw_temp_humi(self, r=R_HIGH, cs=True):
        """
        Lesen Sie die Rohtemperatur und Luftfeuchtigkeit vom Sensor ab und überspringen Sie CRC. Überprüfung.
        Liefert ein Tupel für beide Werte in dieser Reihenfolge.
        """
        if r not in (R_HIGH, R_MEDIUM, R_LOW):
            raise ValueError('Falscher Wiederholbarkeitswert angegeben!')
        self._send(self._map_cs_r[cs][r])
        time.sleep_ms(50)
        raw = self._recv(6)
        return (raw[0] << 8) + raw[1], (raw[3] << 8) + raw[4]
    
    def get_temp_humi(self, resolution=R_HIGH, clock_stretch=True, celsius=True):
        """
        Lesen Sie die Temperatur in Grad Celsius oder Fahrenheit und relativ dazu ab.
        Feuchtigkeit. Auflösung und Taktverlängerung können festgelegt werden.
        Liefert ein Tupel für beide Werte in dieser Reihenfolge.
        """
        t, h = self._raw_temp_humi(resolution, clock_stretch)
        if celsius:
            temp = -45 + (175 * (t / 65535.0))
        else:
            temp = -49 + (315 * (t / 65535.0))
        return temp, 100 * (h / 65535.0)


  • @ajb2k3 said in Micropython - SHT30 in BTC Base:

    Micropython on the M5Paper is in early alpha because we pushed M5Stack to get it out before the holiday. I spent yesterday doing some testing and I cannot get any SHT30 devices to work no matter if its the internal SHT30 on the paper or the env2 unit.
    What lib are you using as I cant even get raw reg read and writes to work.

    For now I have abandoned micro python and switched to (yuck) C and platformio.