DeepSleep and SHT31-D alarm



  • hello everybody,

    I read all the discusion about the deep sleep mode, I have temperature sensor SHT31-D https://www.adafruit.com/product/2857 and it has alarm pin, I want to make my Lopy in deepsleep and when the tempareture will be at 30° my lopy should wake up, until now without success first juste with deepsleep,

    I'm using this code :

    n = 1
    while (n > 0):
      pycom.rgbled(0xff0000) # red
      time.sleep(0.4)
      line = CreateDataLine(n)
      logfiledata(line)
    
    
    
     ds.go_to_sleep(10)
    
     pycom.rgbled(0x7f7f00) # green
     time.sleep(0.4)
    
     n += 1
    

    I update the last firmware, and the SHT31 pins use P10, P9 , GND, 3V3
    I hope to have an help or orientation

    kind regards
    Maamar



  • @maamar
    sh31 lib
    from machine import I2C
    import time

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

    class SHT31(object):
    """
    This class implements an interface to the SHT31 temprature and humidity
    sensor from Sensirion.
    """

    # This static map helps keeping the heap and program logic cleaner
    _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):
        """
        Initialize a sensor object on the given I2C bus and accessed by the
        given address.
        """
        if i2c == None or i2c.__class__ != I2C:
            raise ValueError('I2C object needed as argument!')
        self._i2c = i2c
        self._addr = addr
    
    def _send(self, buf):
        """
        Sends the given buffer object over I2C to the sensor.
        """
        self._i2c.writeto(self._addr, buf)
    
    def _recv(self, count):
        """
        Read bytes from the sensor using I2C. The byte count can be specified
        as an argument.
        Returns a bytearray for the result.
        """
        return self._i2c.readfrom(self._addr, count)
    
    def _raw_temp_humi(self, r=R_HIGH, cs=True):
        """
        Read the raw temperature and humidity from the sensor and skips CRC
        checking.
        Returns a tuple for both values in that order.
        """
        if r not in (R_HIGH, R_MEDIUM, R_LOW):
            raise ValueError('Wrong repeatabillity value given!')
        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):
        """
        Read the temperature in degree celsius or fahrenheit and relative
        humidity. Resolution and clock stretching can be specified.
        Returns a tuple for both values in that order.
        """
        t, h = self._raw_temp_humi(resolution, clock_stretch)
        if celsius:
            temp = -45 + (175 * (t / 65535))
        else:
            temp = -49 + (315 * (t / 65535))
        return temp, 100 * (h / 65535)


  •     from machine import I2C
        from machine import RTC
        from machine import SD
        from deepsleep import DeepSleep
        import machine
        import os
        import sht31
        import time
        import pycom
        import math
    
        # to add in the boot.py the two lines
        #sd = SD()
        #os.mount(sd, '/sd')
    
        FILE_NAME = '/sd/datalog2.txt'
        #CURRENT_DATE_TIME = (2017, 8, 5, 00, 00, 00, 0, 0)
    
        def logfiledata (line):
            f = open(FILE_NAME, 'a')
            f.write(line)
            f.close()
    
        def CreateDataLine(i):
            th = sensor.get_temp_humi()
            dataCSV = 'TH'+str(i) +"\t"    # point number
            #dataCSV += str(rtc.now()) + "\t"
            dataCSV += str(rtc.now()[0]) + "/"    #year
            dataCSV += str(rtc.now()[1]) + "/"    #month
            dataCSV += str(rtc.now()[2]) + " "    #day
            dataCSV += str(rtc.now()[3]) + ":"    # hour
            dataCSV += str(rtc.now()[4]) + ":"    #min
            dataCSV += str(rtc.now()[5]) + "\t "  #sec
            dataCSV += str(round(th[0],1))  + "\t "  #Temperature
            dataCSV += str(round(th[1],1)) +"\n"     #humidity
            return dataCSV
    
    
        pycom.heartbeat(False)
        #i2c = I2C(0)
        #i2c.init(I2C.MASTER,  baudrate=100000,  pins=("P9", "P19"))
        ds = DeepSleep()
    
        i2c = I2C(0, I2C.MASTER, baudrate=100000)
        sensor = sht31.SHT31(i2c, addr=0x44)
    
        th = sensor.get_temp_humi()
        time.sleep(0.6)
    
        pycom.rgbled(0xff007f) # magenta
    
        print("rev code 3")
        print("\n the Temperature SHT31 %0.2f C" % round(th[0],2))
        print("\n the Humidity SHT31 %0.2f h" % round(th[1],2))
        time.sleep(1)
        print("\n the Temperature SHT31 %0.3f C" % th[0])
        print("\n the Humidity SHT31 %0.2f h" % th[1])
        #print 'Temp             = {0:0.3f} deg C'.format(th[0])
        #print 'Humidity         = {0:0.2f} h'.format(th[1])
    
        time.sleep(1)
    
        rtc = machine.RTC()
        #lopy 7eca
        rtc.init((2017, 8, 5, 00, 00, 00, 0, 0))
        print(rtc.now())
    
        n = 1
        while (n > 0):
    
            pycom.rgbled(0xff0000) # red
            time.sleep(0.4)
    
            line = CreateDataLine(n)
            logfiledata(line)
    
    
    
            ds.go_to_sleep(10)
    
            pycom.rgbled(0x7f7f00) # green
            time.sleep(0.4)
    
            n += 1
            time.sleep(5.2)
    
        print("fin de programme ! maamar")
        pycom.heartbeat(True)


  • @robert-hh said in DeepSleep and SHT31-D alarm:

    @maamar said in DeepSleep and SHT31-D alarm:

    P10, P9 , GND, 3V3

    A few questions:

    • can you communicate with the SHT31 via I2C and get some actual readings?

    yes and I store the Data on SD card

    • did you program the device to use the alert pin?

    not yet, I am strugling with driver

    • to which xxPy Pin is the alert Pin of the SHT31 connected?

    The code snippet you show is not very complete and has inconsistent indentation.

    I will post again the code



  • @maamar Somehow the code did not upload. I have sent you two reputation ticks, maybe that enables it.



  • [1_1501939070055_sht31.py](Uploading 100%) [0_1501939070054_main-rev3.py](Uploading 100%)

    this my code and the driver SHT31-D



  • @maamar said in DeepSleep and SHT31-D alarm:

    P10, P9 , GND, 3V3

    A few questions:

    • can you communicate with the SHT31 via I2C and get some actual readings?
    • did you program the device to use the alert pin?
    • to which xxPy Pin is the alert Pin of the SHT31 connected?

    The code snippet you show is not very complete and has inconsistent indentation.



Pycom on Twitter