Welcome to the Pycom forum Forum
    • \[\[global:header.categories\]\]
    • \[\[global:header.recent\]\]
    • \[\[global:header.tags\]\]
    • \[\[global:header.popular\]\]
    • Login

    Explore Pybytes | Official Documentation | Report a Firmware Bug/Issue | GitHub

    Check sensor ready

    Scheduled Pinned Locked Moved SiPy
    5 Posts 2 Posters 64 Views 2 Watching
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • E Offline
      ecabanas
      last edited by

      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

      robert-hhR 1 Reply Last reply Reply Quote 0
      • robert-hhR Offline
        robert-hh @ecabanas
        last edited by robert-hh

        @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.

        E 1 Reply Last reply Reply Quote 0
        • E Offline
          ecabanas @robert-hh
          last edited by

          @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

          robert-hhR 1 Reply Last reply Reply Quote 0
          • robert-hhR Offline
            robert-hh @ecabanas
            last edited by

            @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.
            E 1 Reply Last reply Reply Quote 0
            • E Offline
              ecabanas @robert-hh
              last edited by

              @robert-hh

              that's amazing!! thanks' again

              1 Reply Last reply Reply Quote 0

              Hello! It looks like you're interested in this conversation, but you don't have an account yet.

              Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.

              With your input, this post could be even better 💗

              Register Login
              • 1 / 1
              • First post
                Last post

              Pycom on Twitter

              Pycom