import machine
import utime
import ubinascii

SDA=machine.Pin(0)
SCL=machine.Pin(1)

i2c=machine.I2C(0,sda=SDA, scl=SCL, freq=100000)

Enable = True


print('> init SEN54 sensor...')
utime.sleep_ms(1000)



print('> send start measurement cmd...')
i2c.writeto(105, b'\x00\x21')
utime.sleep_ms(50)



while Enable:
    
    print('\n\n\n> check sensor ready flag...')
    i2c.writeto(105, b'\x02\x02')
    utime.sleep_ms(20)
    raw_data = i2c.readfrom(105, 3)
    hex_data = ubinascii.hexlify(raw_data[0:2])
    val_data = int(hex_data,16)
    
    if val_data == 1:
        print('\t> data ready...')
        
        
        
        print('> read data')
        utime.sleep_ms(20)
        i2c.writeto(105, b'\x03\xC4')
        utime.sleep_ms(20)
        raw_data = i2c.readfrom(105, 24)
        
        hex_pm1   = ubinascii.hexlify(raw_data[0:2])
        hex_pm2_5 = ubinascii.hexlify(raw_data[3:5])
        hex_pm4   = ubinascii.hexlify(raw_data[6:7])
        hex_pm10  = ubinascii.hexlify(raw_data[9:10])
        hex_hum   = ubinascii.hexlify(raw_data[12:14])
        hex_temp  = ubinascii.hexlify(raw_data[15:17])
        hex_voc   = ubinascii.hexlify(raw_data[18:20])
        hex_nox   = ubinascii.hexlify(raw_data[21:23])
        
        val_pm1   = int(hex_pm1,16)
        val_pm2_5 = int(hex_pm2_5,16)
        val_pm4   = int(hex_pm4,16)
        val_pm10  = int(hex_pm10,16)
        val_hum   = int(hex_hum,16)
        val_temp  = int(hex_temp,16)
        val_voc   = int(hex_voc,16)
        val_nox   = int(hex_nox,16)

        val_pm1   = if (val_pm1 := val_pm1 / 10) > 0
        val_pm2_5 = if (val_pm2_5 := val_pm2_5 / 10) > 0
        val_pm4   = if (val_pm4 := val_pm4 / 10) > 0
        val_pm10  = if (val_pm10 := val_pm10 / 10) > 0
        val_hum   = if (val_hum := val_hum / 100) > 0
        val_temp  = if (val_temp := val_temp / 200) > 0
        val_voc   = if (val_voc := val_voc / 10) > 0
        val_nox   = if (val_nox := val_nox / 10) > 0
        
        
        print('> wartości: ')
        print(f'\tpm1.0\t=\t{val_pm1} µg/m3')
        print(f'\tpm2.5\t=\t{val_pm2_5} µg/m3')
        print(f'\tpm4.0\t=\t{val_pm4} µg/m3')
        print(f'\tpm10\t=\t{val_pm10} µg/m3')
        print(f'\thumanidy\t=\t{val_hum} %')
        print(f'\ttemperature\t=\t{val_temp} °C')
        print(f'\tLZO\t=\t{val_voc} -')
        print(f'\tNOx\t=\t{val_nox} -')
        
    else:
        print('\n\n\n> sensor not ready...\n\n\n')
        utime.sleep_ms(1000)
        



    
BOOTSELLED1239USBRaspberryPiPico©2020RP2-8020/21P64M15.00TTT