adafruit / Adafruit_CircuitPython_HTU21D

CircuitPython driver for Adafruit's HTU21D-F temperature and humidity sensor
MIT License
2 stars 9 forks source link

Remote error when running as script #4

Closed robiwano closed 5 years ago

robiwano commented 5 years ago

I've installed all prerequisites, and I get the following when running my script read_sensor.py:

pi@raspberrypi:~/hum_temp_server $ ./read_sensor.py
Traceback (most recent call last):
  File "./read_sensor.py", line 13, in <module>
    print("\nTemperature: %0.1f C" % sensor.temperature)
  File "/usr/local/lib/python3.5/dist-packages/adafruit_htu21d.py", line 121, in temperature
    self.measurement(TEMPERATURE)
  File "/usr/local/lib/python3.5/dist-packages/adafruit_htu21d.py", line 140, in measurement
    self._command(what)
  File "/usr/local/lib/python3.5/dist-packages/adafruit_htu21d.py", line 93, in _command
    i2c.write(struct.pack('B', command))
  File "/usr/local/lib/python3.5/dist-packages/adafruit_bus_device/i2c_device.py", line 115, in write
    self.i2c.writeto(self.device_address, buf, **kwargs)
  File "/usr/local/lib/python3.5/dist-packages/busio.py", line 65, in writeto
    return self._i2c.writeto(address, buffer, stop=stop)
  File "/usr/local/lib/python3.5/dist-packages/adafruit_blinka/microcontroller/generic_linux/i2c.py", line 38, in writeto
    self._i2c_bus.write_bytes(address, buffer[start:end])
  File "/usr/local/lib/python3.5/dist-packages/Adafruit_PureIO/smbus.py", line 244, in write_bytes
    self._device.write(buf)
OSError: [Errno 121] Remote I/O error

However, entering all the commands in a python3 shell works just fine... (?). Content of read_sensor.py is:

#!/usr/bin/python3
import time
import board
import busio
from adafruit_htu21d import HTU21D

# Create library object using our Bus I2C port
i2c = busio.I2C(board.SCL, board.SDA)
sensor = HTU21D(i2c)

while True:
    print("\nTemperature: %0.1f C" % sensor.temperature)
    print("Humidity: %0.1f %%" % sensor.relative_humidity)
    time.sleep(2)
ladyada commented 5 years ago

sounds like maybe you can add some time.sleep delays around, since its running too fast? see where that helps and let us know!

robiwano commented 5 years ago

Adding a 100 ms sleep after getting sensor fixed it:

#!/usr/bin/python3
import time
import board
import busio
from adafruit_htu21d import HTU21D

# Create library object using our Bus I2C port
i2c = busio.I2C(board.SCL, board.SDA)
sensor = HTU21D(i2c)
time.sleep(0.1)

while True:
    print("\nTemperature: %0.1f C" % sensor.temperature)
    print("Humidity: %0.1f %%" % sensor.relative_humidity)
    time.sleep(2)
robiwano commented 5 years ago

Actually, even a time.sleep(0.001) makes it work.

ladyada commented 5 years ago

ooh ok, want to submit a PR to fix by adding a delay at the end of the HTU21D init? :)

evaherrada commented 5 years ago

Done

tannewt commented 5 years ago

Fixed by #5. Thanks @dherrada