raspberry pi - Dealing with occational i2c write errors, Python + RaspberryPi -
i have raspberrypi connected temperature/humidity sensor via i2c bus. works great, send write commands , listen response. every , script crashes halt same error.
traceback (most recent call last): file "humidify.py", line 20, in <module> humidity = htu21df.read_humidity() file "/home/pi/desktop/projects/htu21df.py", line 51, in read_humidity pi.i2c_write_byte(handle, rdhumi) # send read humi command file "/usr/local/lib/python2.7/dist-packages/pigpio.py", line 1861, in i2c_write_byte _pigpio_command(self.sl, _pi_cmd_i2cws, handle, byte_val)) file "/usr/local/lib/python2.7/dist-packages/pigpio.py", line 683, in _u2i raise error(error_text(v)) pigpio.error: 'i2c write failed' i'm not looking weed out causes occasional error, want script wait second , try i2c write command again. if can make work i'll able automate system instead of having check crashes.
i'm programming novice, basic.
read on exception handling. in python, create try block contains operation you're attempting, followed 1 or more except blocks each name 1 or more exception classes interested in handling. in case, you're seeing pigpio.error exceptions.
what might like:
# try 3 times on failure success = false caught_exception = none _ in range(3): try: pi.i2c_write_byte(...etc...) # if here, succeeded, break out of loop success = true break except pigpio.error e: print "error: %s"%(e) # wait second retry time.sleep(1) if not success: print "failed after 3 retries!"
Comments
Post a Comment