Sensirion / python-i2c-sht3x

Python I2C driver for SHT3x
https://sensirion.github.io/python-i2c-sht3x
BSD 3-Clause "New" or "Revised" License
0 stars 0 forks source link

measure_single_shot() doesn't work #1

Closed vinicentus closed 10 months ago

vinicentus commented 10 months ago

When running this with a SHT30-D sensor connected via i2c to a raspberry pi, I can't use the method measure_single_shot() without getting an exception. start_periodic_measurement() and blocking_read_measurement() methods work.

Here is the error I get:

Exception has occurred: TypeError
argument must be an int, or have a fileno() method.
  File "/home/admin/vext_python/input/humiditysensor.py", line 29, in readTempHumidity
    (a_temperature, a_humidity) = self.sensor.measure_single_shot(
  File "/home/admin/vext_python/main.py", line 25, in <module>
    tempHumiditySensorHelper.readTempHumidity()
TypeError: argument must be an int, or have a fileno() method.

The relevant code section looks like this:

def readTempHumidity(self):
    (a_temperature, a_humidity) = self.sensor.measure_single_shot(
        Repeatability.MEDIUM, False
    )
    print(f"a_temperature: {a_temperature}; " f"a_humidity: {a_humidity}; ")

Line 29 of the error corresponds to (a_temperature, a_humidity) = ....

Doing the following yields the same result:

def readTempHumidity(self):
        # try:
        (raw_temp, raw_humi) = self.sensor.sht3x.measure_single_shot_high_repeatability()
        print(f"a_temperature: {SignalTemperature(raw_temp)}; " f"a_humidity: {SignalHumidity(raw_humi)}; ")

Any help would be gratly appreciated, I've been trying to debug this for 2 hours :D

EDIT: here is the whole file:

import time
from sensirion_i2c_driver import LinuxI2cTransceiver, I2cConnection, CrcCalculator
from sensirion_driver_adapters.i2c_adapter.i2c_channel import I2cChannel
from sensirion_i2c_sht3x.device import Sht3xDevice
from sensirion_i2c_sht3x.commands import Repeatability

from abstract.thingsboard import client

class TempHumiditySensorHelper:
    i2cPort = "/dev/i2c-1"

    def init(self):
        with LinuxI2cTransceiver(self.i2cPort) as i2c_transceiver:
            channel = I2cChannel(
                I2cConnection(i2c_transceiver),
                slave_address=0x44,
                crc=CrcCalculator(8, 0x31, 0xFF, 0x0),
            )
            self.sensor = Sht3xDevice(channel)
            self.sensor.stop_measurement()
            time.sleep(0.001)
            self.sensor.soft_reset()
            time.sleep(0.1)
            status_register = self.sensor.read_status_register()
            print(f"status_register: {status_register}; ")

    def readTempHumidity(self):
        (a_temperature, a_humidity) = self.sensor.measure_single_shot(
        Repeatability.MEDIUM, False
        )
        print(f"a_temperature: {a_temperature}; " f"a_humidity: {a_humidity}; ")
vinicentus commented 10 months ago

Oops, the problem was that I was using with LinuxI2cTransceiver(self.i2cPort) as i2c_transceiver:, meaning that the i2c_transceiver was automatically closed in my init method, so it could not be used anymore in the readTempHumidity method. Now I feel dumb.

Apologies for spamming your issues!

qfisch commented 10 months ago

Hej @vinicentus ,

No worries. There is no stupid mistakes. Maybe this issue will help someone in the future. Have fun with your project

Cheers Q