Last active
August 10, 2022 17:32
-
-
Save netmaniac/a6414149a5a09ba1ebf702ff8d5056c5 to your computer and use it in GitHub Desktop.
Nova SDS011 sensor. Code is free to use in own projects, but I don't provide any support nor don't make me liable if it is not working :)
This file contains 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/python | |
# -*- coding: UTF-8 -*- | |
import serial, time, struct, array | |
from datetime import datetime | |
ser = serial.Serial() | |
ser.port = "/dev/ttyUSB0" # Set this to your serial port | |
ser.baudrate = 9600 | |
ser.open() | |
ser.flushInput() | |
byte, lastbyte = "\x00", "\x00" | |
cnt = 0 | |
while True: | |
lastbyte = byte | |
byte = ser.read(size=1) | |
# print("Got byte %x" %ord(byte)) | |
# We got a valid packet header | |
if lastbyte == "\xAA" and byte == "\xC0": | |
sentence = ser.read(size=8) # Read 8 more bytes | |
# print "Sentence size {}".format(len(sentence)) | |
readings = struct.unpack('<hhxxcc',sentence) # Decode the packet - big endian, 2 shorts for pm2.5 and pm10, 2 reserved bytes, checksum, message tail | |
# print array.array('B',sentence) | |
pm_25 = readings[0]/10.0 | |
pm_10 = readings[1]/10.0 | |
# ignoring the checksum and message tail | |
if (cnt == 0 ): | |
line = "PM 2.5: {} μg/m^3 PM 10: {} μg/m^3".format(pm_25, pm_10) | |
print(datetime.now().strftime("%d %b %Y %H:%M:%S.%f: ")+line) | |
cnt += 1 | |
if (cnt == 5): | |
cnt = 0 | |
Hi @netmaniac, based on your Linux script for SDS 011 I've developed a Windows version of script for SDL 607.
It's included in my API application in Node.js, used to monitor indoor air quality and display it on the network.
I hope you don't mind that! Please let me know and feel free to contribute with your version too in a pull request:
https://github.com/ArturGoldyn/air-sensor-api-charts
https://github.com/ArturGoldyn/air-sensor-api-charts/blob/master/server/data/smog_reader.py
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Very useful!
Thank you!