Created
January 10, 2022 03:27
-
-
Save andres-erbsen/850ab4213054b55cad84b47f8bf46924 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import collections, datetime, json, serial, sys | |
Sample = collections.namedtuple('Sample', ['t', 'pm1_ug_m3', 'pm2_5_ug_m3', 'pm10_ug_m3', 'pm1atm_ug_m3', 'pm2_5atm_ug_m3', 'pm_atm_ug_m3', 'pd300nm_dl', 'pd500nm_dl', 'pd1um_dl', 'pd2_5um_dl', 'pd5um_dl', 'pd10um_dl', 'data13_reserved', 'checksum']) | |
def pmsa003_read_passive(uart): | |
while not (uart.read(1) == b'B' and uart.read(1) == b'M'): | |
pass | |
assert 28 == (uart.read(1)[0] << 8) + uart.read(1)[0] # frame length in bytes | |
return Sample(datetime.datetime.now(), *[(uart.read(1)[0] << 8) + uart.read(1)[0] for i in range(14)]) | |
if __name__ == '__main__': | |
port = serial.Serial(sys.argv[1], baudrate=9600, timeout=5) | |
while True: | |
print(json.dumps(pmsa003_read_passive(port)._asdict(), default=str), flush=True) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment