Converted MQTT publish to be JSON payloads.
This commit is contained in:
97
main.py
97
main.py
@@ -1,6 +1,7 @@
|
||||
import network
|
||||
import rp2
|
||||
import time
|
||||
import json
|
||||
import machine
|
||||
from machine import Pin
|
||||
from umqtt.simple import MQTTClient
|
||||
@@ -12,6 +13,9 @@ import mqtt_config
|
||||
# Change this GPIO PIN where your DHT22 sensor is connected
|
||||
DHT_22_GPIO_PIN = 3
|
||||
|
||||
# Debug Mode
|
||||
DEBUG = False
|
||||
|
||||
def read_cpu_temp():
|
||||
"""
|
||||
If you print the value of the temperature value you are going to get an integer number between 0 and 65535.
|
||||
@@ -55,19 +59,22 @@ def wlan_up(wlan):
|
||||
if wlan.status() < 0 or wlan.status() >= 3:
|
||||
break
|
||||
max_wait -= 1
|
||||
print('Waiting for WiFi connection...')
|
||||
if DEBUG:
|
||||
print('Waiting for WiFi connection...')
|
||||
time.sleep(1)
|
||||
|
||||
if max_wait == 0:
|
||||
return None
|
||||
|
||||
ifconfig = wlan.ifconfig()
|
||||
print(ifconfig)
|
||||
if DEBUG:
|
||||
print(ifconfig)
|
||||
return ifconfig
|
||||
|
||||
def led_error_code(led, error_code: int):
|
||||
"""Blink LED for a given error code (int). error code == number of times to blink"""
|
||||
print("LED Error Status code: {}".format(error_code))
|
||||
if DEBUG:
|
||||
print("LED Error Status code: {}".format(error_code))
|
||||
|
||||
# Run a quick 'start error code sequence'
|
||||
# So we know when LED error sequence starts
|
||||
@@ -89,79 +96,109 @@ def led_error_code(led, error_code: int):
|
||||
blink_counter += 1
|
||||
# Make sure to turn off LED when this subroutine finished
|
||||
led.value(False)
|
||||
print("LED Error Status code finished for: {}".format(error_code))
|
||||
if DEBUG:
|
||||
print("LED Error Status code finished for: {}".format(error_code))
|
||||
|
||||
|
||||
def main():
|
||||
print("Start up")
|
||||
|
||||
# Start Up Activities
|
||||
if DEBUG:
|
||||
print("Start up")
|
||||
count = 0
|
||||
led = machine.Pin('LED', machine.Pin.OUT)
|
||||
led.value(False)
|
||||
led_error_code(led, 1)
|
||||
|
||||
# Set Wi-Fi Country
|
||||
rp2.country('US')
|
||||
wlan = network.WLAN(network.STA_IF)
|
||||
|
||||
# Create MQTT Client
|
||||
mqtt_client = MQTTClient(mqtt_config.MQTT_CLIENT_ID, mqtt_config.MQTT_HOST_NAME)
|
||||
|
||||
# Create DHT22
|
||||
sensor = dht.DHT22(Pin(DHT_22_GPIO_PIN))
|
||||
|
||||
led = machine.Pin('LED', machine.Pin.OUT)
|
||||
led.value(False)
|
||||
|
||||
led_error_code(led, 1)
|
||||
|
||||
print("Enter main loop")
|
||||
# Let's Go!
|
||||
if DEBUG:
|
||||
print("Enter main loop")
|
||||
|
||||
while True:
|
||||
|
||||
# Loop Clean-Up and Prep
|
||||
led.value(False)
|
||||
count += 1
|
||||
if DEBUG:
|
||||
print(f'\nStarting loop #{count}... \nWiFI Status is {wlan.status()}.')
|
||||
|
||||
print(f'\nStarting loop #{count}... \nWiFI Status is {wlan.status()}.')
|
||||
|
||||
# WiFi Connection.
|
||||
if wlan.status() != 3:
|
||||
ifconfig = wlan_up(wlan)
|
||||
if ifconfig is None:
|
||||
print("Trouble to connecting WiFi: {}".format(e))
|
||||
if DEBUG:
|
||||
print("Trouble to connecting WiFi: {}".format(e))
|
||||
led_error_code(led, 3)
|
||||
continue
|
||||
else:
|
||||
print("Connected to WiFi: {}".format(ifconfig))
|
||||
if DEBUG:
|
||||
print("Connected to WiFi: {}".format(ifconfig))
|
||||
else:
|
||||
ifconfig = wlan.ifconfig()
|
||||
print(f"Skipping WiFi Activation since WiFi Status is {wlan.status()} \n{ifconfig}.")
|
||||
if DEBUG:
|
||||
print(f"Skipping WiFi Activation since WiFi Status is {wlan.status()} \n{ifconfig}.")
|
||||
|
||||
# MQTT Conneciton.
|
||||
try:
|
||||
mqtt_client.connect()
|
||||
print(f'MQTT Connected.')
|
||||
if DEBUG:
|
||||
print(f'MQTT Connected.')
|
||||
|
||||
except Exception as e:
|
||||
print("Trouble to connecting to MQTT: {}".format(e))
|
||||
if DEBUG:
|
||||
print("Trouble to connecting to MQTT: {}".format(e))
|
||||
led_error_code(led, 2)
|
||||
continue
|
||||
continue # Start back at the top of the While Loop
|
||||
|
||||
# DHT22 Reading.
|
||||
dht22_reading = read_dht_22(sensor)
|
||||
|
||||
debug_str = "None"
|
||||
#debug_str = "None"
|
||||
if dht22_reading is not None:
|
||||
temp,hum = dht22_reading
|
||||
temp = temp * 9/5. + 32.0
|
||||
mqtt_client.publish("m/v1/home/{}/0/temperature".format(mqtt_config.MQTT_ZONE_ID), "DHT22 Temp = {} degF".format(str(temp)), retain=True)
|
||||
mqtt_client.publish("m/v1/home/{}/0/humidity".format(mqtt_config.MQTT_ZONE_ID), "DHT22 %Hum = {} %".format(str(hum)), retain=True)
|
||||
debug_str = "{} ; {}".format(temp, hum)
|
||||
|
||||
# CPU Reading.
|
||||
cpu_temp = read_cpu_temp()
|
||||
|
||||
print("{} ; CPU: {}".format(debug_str, cpu_temp))
|
||||
# Timestamp
|
||||
time_now = time.localtime()
|
||||
timestamp = "{}/{}/{} {}:{}:{}".format(time_now[1],time_now[2],time_now[0],time_now[3],time_now[4],time_now[5])
|
||||
|
||||
# HW Info
|
||||
mqtt_client.publish("m/v1/hw/{}/cpu/temperature".format(mqtt_config.MQTT_HW_ID), "CPU Temp = {} degF".format(str(cpu_temp)), retain=True)
|
||||
mqtt_client.publish("m/v1/hw/{}/wlan/info".format(mqtt_config.MQTT_HW_ID), str(ifconfig), retain=True)
|
||||
# Build JSON Payloads
|
||||
dht_data = {
|
||||
'Temperature':temp,
|
||||
'Relative Humidity':hum,
|
||||
}
|
||||
dht_payload = json.dumps(dht_data)
|
||||
|
||||
hw_data = {
|
||||
'Timestamp':timestamp,
|
||||
'CPU Temperature':cpu_temp,
|
||||
'WiFi Information':ifconfig,
|
||||
}
|
||||
hw_payload = json.dumps(hw_data)
|
||||
|
||||
# Publish
|
||||
mqtt_client.publish("home/{}".format(mqtt_config.MQTT_ZONE_ID),dht_payload, retain=True)
|
||||
mqtt_client.publish("hw/{}".format(mqtt_config.MQTT_HW_ID),hw_payload, retain=True)
|
||||
|
||||
mqtt_client.disconnect()
|
||||
print(f'MQTT Disconnected.')
|
||||
if DEBUG:
|
||||
print(f'MQTT Disconnected.')
|
||||
|
||||
# Sleep for a bit.
|
||||
print(f'Finished loop #{count}.')
|
||||
if DEBUG:
|
||||
print(f'Finished loop #{count}.')
|
||||
time.sleep(5)
|
||||
|
||||
main()
|
||||
|
||||
Reference in New Issue
Block a user