Check sensor ready
-
Hi all,
I have the following code to read the sensor BME280, but If the sensor is not connect the sipy hangs...
How could I do a previous check if the sensor is on and if not pass though the bme280 reading
def temp_bme280(): # # this script assumes the default connection of the I2C bus # On pycom devuces that is P9 = SDA, P10 = scl # i2c = machine.I2C() bme = bme280.BME280(i2c=i2c) Values= bme.values tempbme=res = ''.join(filter(lambda i: i.isdigit(), Values[0])) tempbme280=int(tempbme)/100 return tempbme280
Thank's guys
-
that's amazing!! thanks' again
-
@ecabanas With the changed code an exception is raised if either:
- the I2C connection fails or
- the sensor does not return a value within one second wait time.
Since an exception is raised, you have to encluse the bme280.values statement into a try/except clause and do an adequate error handling in the except path, whatever adequate is in your application.
-
@robert-hh said in Check sensor ready:
I jus
HI @robert-hh
Thank's for answer, Unfortunately I did not test due a huge work these days.
What do you mean? with new library, when the sensor is not detected the read data process breaks, returns an error and the never ending loop will not be no longer?Thank's for your help!!
Eduard
-
@ecabanas If the sensor is not connected you'll get an "OSError: I2C bus error" exception. The other problem may occur is the sensor is connected but never signals "ready". You may eventually change the driver to signal timeout after a suitable time. That's in the method read_raw_data().
So you could replace the while loop for instance by:# Wait for conversion to complete for _ in range(100): # about 1 second timeout if self.i2c.readfrom_mem(self.address, BME280_REGISTER_STATUS, 1)[0] & 0x08: time.sleep_ms(10) else: break # Sensor ready else: raise RuntimeError("Sensor BME280 not ready")
Edit: Updated code here: https://github.com/robert-hh/BME280
Edit2: I just noticed that TimeoutError seems not to be know. That's strange, since it is defined in the code. So use RuntimeError instead for now.