Skip to content

Instantly share code, notes, and snippets.

@sweemeng
Last active March 17, 2019 11:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sweemeng/e74ec82d6d32918924de60af7f50c2a2 to your computer and use it in GitHub Desktop.
Save sweemeng/e74ec82d6d32918924de60af7f50c2a2 to your computer and use it in GitHub Desktop.
Just some micropython code to talk to a HPM sensor. Just work in progress
from m5stack import *
from m5ui import *
from machine import UART
import time
import struct
# This is the only UART that works.
uart2 = UART(1, tx=17, rx=16)
uart2.init(9600)
while True:
# Making data display more reasonable
time.sleep_ms(750)
lcd.clear()
# The command to start measurement
# source https://sensing.honeywell.com/honeywell-sensing-hpm-series-particle-sensors-datasheet-32322550-e-en.pdf
# Page 5
uart2.write('\x68\x01\x01\x96')
data=uart2.read(32)
# Opps there is problem,
# call the command to stop measurement every time there is problem
if not data:
lcd.print("No data", 20, 20)
uart2.write('\x68\x01\x02\x95')
continue
# Should only have 32 bit of data
if len(data) != 32:
lcd.print("Bad data", 20, 20)
lcd.print(data, 20, 30)
uart2.write('\x68\x01\x02\x95')
continue
# measurement must start with 0x42
if data[0] != 0x42:
lcd.print("Bad data", 20, 20)
lcd.print(data, 20, 30)
uart2.write('\x68\x01\x02\x95')
continue
l = list(data)
# The number is big endian. Also drop the first 4 byte
# Covert the rest into 14 number
frame = struct.unpack(">HHHHHHHHHHHHHH", bytes(l[4:]))
# I really only care about frame[1] and frame[2]. WHich is PM2.5 and PM10
# Source https://sensing.honeywell.com/honeywell-sensing-hpm-series-particle-sensors-datasheet-32322550-e-en.pdf
# Page 6
output = "PM 2.5 = {pm25}".format(pm25=frame[1])
lcd.print(output, 20, 30)
uart2.write('\x68\x01\x02\x95')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment