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

    Converting float to bytes

    Scheduled Pinned Locked Moved LoPy
    convert to byte
    6 Posts 3 Posters 937 Views 3 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.
    • A Offline
      AidanRTaylor
      last edited by

      Hello - I'm trying to convert an ambient sensor readout into bytes so I can squeeze it down the LoRa tube! I can't seem to get the float.hex() attribute to work, I get this back: AttributeError: type object 'float' has no attribute 'hex'.

      The Python3 docs say that float.hex() is a built in type, am I missing something here?

      I tried the following:

      temp = float.hex(sensordata)
      temp = sensordata.hex()
      

      Both return the same attribute error. Where am I going wrong? Is there a module I need to include? Here is the main module from my project:

      import bme680
      from bme680.i2c import I2CAdapter
      from machine import Pin
      import time
      import struct
      
      # Config pin:
      configPin = Pin('P21', Pin.IN, Pin.PULL_UP)
      
      # Setup I2C and BME680: P9/G16=SDA, P10/G17=SCL
      i2c_dev = I2CAdapter(1, pins=('P9','P10'), baudrate=100000)
      sensor = bme680.BME680(i2c_device=i2c_dev)
      
      # These oversampling settings can be tweaked to
      # change the balance between accuracy and noise in
      # the data.
      sensor.set_humidity_oversample(bme680.OS_2X)
      sensor.set_pressure_oversample(bme680.OS_4X)
      sensor.set_temperature_oversample(bme680.OS_8X)
      sensor.set_filter(bme680.FILTER_SIZE_3)
      
      config = configPin()
      
      if config:
          while True:
              if sensor.get_sensor_data():
      
                  output = "{} C, {} hPa, {} RH, {} RES,".format(
                      sensor.data.temperature,
                      sensor.data.pressure,
                      sensor.data.humidity,
                      sensor.data.gas_resistance)
      
                  print(output)
      
                  temp = float.hex(sensor.data.temperature)
      
                  print(temp)
      
                  time.sleep(5)
      
      robert-hhR 1 Reply Last reply Reply Quote 0
      • robert-hhR Offline
        robert-hh @AidanRTaylor
        last edited by

        @aidanrtaylor use struct.pack() instead.

        T 1 Reply Last reply Reply Quote 1
        • A Offline
          AidanRTaylor
          last edited by AidanRTaylor

          thanks, trying it out now (I already had stuct imported as you can see above :P)

          1 Reply Last reply Reply Quote 0
          • T Offline
            timh @robert-hh
            last edited by timh

            @robert-hh beat me to it ;-)

            struct.unpack('f',binascii.unhexlify(binascii.hexlify(struct.pack('f',1.234))))

            and using 'e' instead of 'f' for the format will mean the float uses 4 bytes in hex form. at the loss of precision but doubt you need that much for temp ;-)

            1 Reply Last reply Reply Quote 1
            • A Offline
              AidanRTaylor
              last edited by AidanRTaylor

              isn't unpack the opposite of what I want? I thought I would need pack

              Oh, I see you are showing me how to do both ;)

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

                @aidanrtaylor Yes, you need pack() to make a bytes string out of a float, and unpack() will convert that back.
                unpack() returns a tuple, wheras pack() in addition to a tuple accepts a single value.

                1 Reply Last reply Reply Quote 1

                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