Last active
December 30, 2021 18:07
-
-
Save lludlow/35d10847eb5577e4e696cc7ddd3647c7 to your computer and use it in GitHub Desktop.
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
import time | |
import board | |
import busio | |
from digitalio import DigitalInOut, Direction, Pull | |
from adafruit_pm25.i2c import PM25_I2C | |
import paho.mqtt.client as mqtt | |
from secrets import secrets | |
reset_pin = None | |
# Create library object, use 'slow' 100KHz frequency! | |
i2c = busio.I2C(board.SCL, board.SDA, frequency=100000) | |
# Connect to a PM2.5 sensor over I2C | |
pm25 = PM25_I2C(i2c, reset_pin) | |
# Setup- MQTT | |
def messageFunction (client, userdata, message): | |
topic = str(message.topic) | |
message = str(message.payload.decode("utf-8")) | |
print(topic + message) | |
client = mqtt.Client("basement_piPM25") # Create a MQTT client object | |
client.username_pw_set(username=secrets["user"],password=secrets["pass"]) | |
client.connect("192.168.10.70", 1883) | |
client.subscribe("basement-airquality") | |
client.on_message = messageFunction # Attach the messageFunction to subscription | |
client.loop_start() # Start the MQTT client | |
# Publish all values to MQTT | |
while True: | |
time.sleep(10) | |
try: | |
aqdata = pm25.read() | |
print(aqdata) | |
except RuntimeError: | |
print("Unable to read from sensor, retrying...") | |
continue | |
print() | |
print("Concentration Units (standard)") | |
print("---------------------------------------") | |
print( | |
"PM 1.0: %d\tPM2.5: %d\tPM10: %d" | |
% (aqdata["pm10 standard"], aqdata["pm25 standard"], aqdata["pm100 standard"]) | |
) | |
print("Concentration Units (environmental)") | |
print("---------------------------------------") | |
print( | |
"PM 1.0: %d\tPM2.5: %d\tPM10: %d" | |
% (aqdata["pm10 env"], aqdata["pm25 env"], aqdata["pm100 env"]) | |
) | |
pm10 = aqdata["pm10 standard"] | |
client.publish("basement-airquality/PM1.0", pm10) | |
# pm25 = aqdata["pm25 standard"] | |
# client.publish("basement-airquality/PM2.5", pm25) | |
# pm10 = aqdata["pm100 standard"] | |
# client.publish("basement-airquality/PM10.0", pm10) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment