Thingy:52 Python Data Extraction

Hi, I'm using a python script to receive the data from Thingy:52, but it's showing multiple values at each time.

# Write your code here :-)
from bluepy import btle, thingy52
import time
import binascii
from bluepy.btle import UUID, Peripheral, ADDR_TYPE_RANDOM, DefaultDelegate
import argparse

MAC_ADDRESS = "CC:53:EB:90:19:06"  # This has to be changed to match your Thingy's MAC!


class MQTTDelegate(btle.DefaultDelegate):

    def handleNotification(self, hnd, data):

        #Debug print repr(data)
        if (hnd == thingy52.e_temperature_handle):
            teptep = binascii.b2a_hex(data)
            Temp = self._str_to_int(teptep[:-2]) + int(teptep[-2:], 16) / 100.0
            print('temperature', Temp, '°C')

        if (hnd == thingy52.e_pressure_handle):
            pressure_int, pressure_dec = self._extract_pressure_data(data)
            value = pressure_int + pressure_dec / 100.0
            print('pressure', value, 'hPa')


        if (hnd == thingy52.e_humidity_handle):
            teptep = binascii.b2a_hex(data)
            value = self._str_to_int(teptep)
            humidity = value
            print('humidity', humidity, '%')

        if (hnd == thingy52.e_gas_handle):
            eco2, tvoc = self._extract_gas_data(data)
            eco2 = eco2
            tvoc = tvoc
            print('eco2', eco2, 'bbm')
            print('tvoc', tvoc, 'ppb')

    def _str_to_int(self, s):
        """ Transform hex str into int. """
        i = int(s, 16)
        if i >= 2**7:
            i -= 2**8
        return i

    def _extract_pressure_data(self, data):
        """ Extract pressure data from data string. """
        teptep = binascii.b2a_hex(data)
        pressure_int = 0
        for i in range(0, 4):
            pressure_int += (int(teptep[i*2:(i*2)+2], 16) << 8*i)
        pressure_dec = int(teptep[-2:], 16)
        return (pressure_int, pressure_dec)

    def _extract_gas_data(self, data):
        """ Extract gas data from data string. """
        teptep = binascii.b2a_hex(data)
        eco2 = int(teptep[:2], 16) + (int(teptep[2:4], 16) << 8)
        tvoc = int(teptep[4:6], 16) + (int(teptep[6:8], 16) << 8)
        return eco2, tvoc

    def _extract_color_data(self, data):
        """ Extract color data from data string. """
        teptep = binascii.b2a_hex(data)
        red = int(teptep[:2], 16)
        green = int(teptep[2:4], 16)
        blue = int(teptep[4:6], 16)
        clear = int(teptep[6:8], 16)
        return red, green, blue, clear

    def _extract_tap_data(self, data):
        """ Extract tap data from data string. """
        teptep = binascii.b2a_hex(data)
        direction = int(teptep[0:2])
        count = int(teptep[2:4])
        return (direction, count)

#print("# Setting notification handler to default handler...")
#thingy.setDelegate(thingy52.MyDelegate())

connected=False
while not connected:
    print("###############################################")
    thingy = thingy52.Thingy52(MAC_ADDRESS)
    thingy.ui.enable()
    thingy.ui.set_led_mode_breathe(0x02, 50, 500)
    thingy.sound.enable()
#while True:
    thingy.sound.configure(speaker_mode=0x03)  # 0x03 means sample mode, ref FW doc
    thingy.sound.play_speaker_sample(1)
    thingy.setDelegate(MQTTDelegate())
    thingy.environment.enable()
    thingy.environment.configure(temp_int=1000)
    thingy.environment.set_temperature_notification(True)
    thingy.environment.enable()
    thingy.environment.set_pressure_notification(True)
    thingy.environment.set_humidity_notification(True)
    thingy.environment.set_gas_notification(True)
    thingy.environment.set_color_notification(True)
    thingy.motion.set_tap_notification(True)
    thingy.motion.set_orient_notification(True)
    thingy.environment.configure(press_int=1000)
    thingy.environment.enable()
    thingy.environment.configure(humid_int=1000)
    thingy.environment.enable()
    thingy.environment.configure(gas_mode_int=1)
    thingy.environment.enable()
    thingy.environment.configure(color_int=1000)
    thingy.environment.configure(color_sens_calib=[0,0,0])
    thingy.motion.set_orient_notification(True)
    thingy.ui.enable()
    thingy.ui.set_btn_notification(True)
    thingy.battery.enable()
    count =0
    thingy.ui.set_led_mode_breathe(0x01, 50, 500) # color 0x01 = RED, intensity, delay between breathes
   # thingy.ui.set_led_mode_breathe(0x01, 50, 500) # color 0x01 = RED, intensity, delay between breathes
    time.sleep(2)
    thingy.disconnect()
    connected=False

May I know how to resolve this issue.

Parents Reply Children
  • Hello,

    Thank you for clarifying the issue. I thought you were getting different value for same characteristics.

    Pradeep S said:
    But I want to get one data at a time. 

    This is because you are subscribing the data in a infinite loop. If you want to receive a single data you need to write a function which subscribe the data at that particular instant and return those values. Just try changing the value of the variable connected in your code to something like this connected=True (to the end line )and see the values.

    Kind Regards,

    Abhijith

  • Hi Abhijit i understood your poit. But in my case when I try to read back one data in one loop iteration means, it will return multiple data. 

    Simply it return multiple data for one loop iteration. 

    In the video you can see the #### lines represent the iteration. 

Related