Reorganizing functions
This commit is contained in:
@@ -16,32 +16,22 @@ password = 'falrenforbreakfast'
|
|||||||
|
|
||||||
client_id = f'python-mqtt-office-{random.randint(0, 1000)}'
|
client_id = f'python-mqtt-office-{random.randint(0, 1000)}'
|
||||||
|
|
||||||
# We first check if a libgpiod process is running. If yes, we kill it!
|
|
||||||
for proc in psutil.process_iter():
|
|
||||||
if proc.name() == 'libgpiod_pulsein' or proc.name() == 'libgpiod_pulsei':
|
|
||||||
proc.kill()
|
|
||||||
|
|
||||||
#D20 is the pin number, DHT11 is the sensor type
|
#D20 is the pin number, DHT11 is the sensor type
|
||||||
sensor = adafruit_dht.DHT11(board.D20)
|
sensor = adafruit_dht.DHT11(board.D20)
|
||||||
|
|
||||||
while True:
|
#MQTT Connection
|
||||||
try:
|
# def on_connect(client, rc):
|
||||||
temp = sensor.temperature
|
# if rc == 0:
|
||||||
humidity = sensor.humidity
|
# print("Connected to MQTT Broker!")
|
||||||
|
# else:
|
||||||
#convert to Farenheit
|
# print("Failed to connect, return code %d\n", rc)
|
||||||
temp = (temp * 1.8) + 32
|
# #MQTT Connect
|
||||||
print("Temperature: {}*F Humidity: {}% ".format(temp, humidity))
|
# client = mqtt_client.Client(client_id)
|
||||||
except RuntimeError as error:
|
# client.username_pw_set(username, password)
|
||||||
print(error.args[0])
|
# client.on_connect = on_connect
|
||||||
time.sleep(2.0)
|
# client.connect(broker, port)
|
||||||
continue
|
def connect_mqtt():
|
||||||
except Exception as error:
|
def on_connect(client, rc):
|
||||||
sensor.exit()
|
|
||||||
raise error
|
|
||||||
|
|
||||||
#MQTT Portion
|
|
||||||
def on_connect(client, userdata, flags, rc):
|
|
||||||
if rc == 0:
|
if rc == 0:
|
||||||
print("Connected to MQTT Broker!")
|
print("Connected to MQTT Broker!")
|
||||||
else:
|
else:
|
||||||
@@ -51,30 +41,61 @@ while True:
|
|||||||
client.username_pw_set(username, password)
|
client.username_pw_set(username, password)
|
||||||
client.on_connect = on_connect
|
client.on_connect = on_connect
|
||||||
client.connect(broker, port)
|
client.connect(broker, port)
|
||||||
|
return client
|
||||||
|
|
||||||
#Publish Temp Results
|
|
||||||
tempMsg = '{{ "temperature": {} }}'.format(temp)
|
|
||||||
result = client.publish(tempTopic, tempMsg)
|
|
||||||
# result: [0, 1]
|
|
||||||
status = result[0]
|
|
||||||
if status == 0:
|
|
||||||
print(f"Send `{tempMsg}` to topic `{tempTopic}`")
|
|
||||||
else:
|
|
||||||
print(f"Failed to send message to topic {tempTopic}")
|
|
||||||
|
|
||||||
#Publish Humid Results
|
|
||||||
humidMsg = '{{ "humidity": {} }}'.format(humidity)
|
|
||||||
result = client.publish(humidTopic, humidMsg)
|
|
||||||
# result: [0, 1]
|
|
||||||
status = result[0]
|
|
||||||
if status == 0:
|
|
||||||
print(f"Send `{humidMsg}` to topic `{humidTopic}`")
|
|
||||||
else:
|
|
||||||
print(f"Failed to send message to topic {humidTopic}")
|
|
||||||
|
|
||||||
|
def run():
|
||||||
|
# We first check if a libgpiod process is running. If yes, we kill it!
|
||||||
|
for proc in psutil.process_iter():
|
||||||
|
if proc.name() == 'libgpiod_pulsein' or proc.name() == 'libgpiod_pulsei':
|
||||||
|
proc.kill()
|
||||||
|
client = connect_mqtt()
|
||||||
client.loop_start()
|
client.loop_start()
|
||||||
|
client.getValues()
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
def getValues():
|
||||||
|
while True:
|
||||||
|
try:
|
||||||
|
temp = sensor.temperature
|
||||||
|
humidity = sensor.humidity
|
||||||
|
|
||||||
|
#convert to Farenheit
|
||||||
|
temp = (temp * 1.8) + 32
|
||||||
|
print("Temperature: {}*F Humidity: {}% ".format(temp, humidity))
|
||||||
|
except RuntimeError as error:
|
||||||
|
print(error.args[0])
|
||||||
|
time.sleep(2.0)
|
||||||
|
continue
|
||||||
|
except Exception as error:
|
||||||
|
sensor.exit()
|
||||||
|
raise error
|
||||||
|
|
||||||
|
#Publish Temp Results
|
||||||
|
tempMsg = '{{ "temperature": {} }}'.format(temp)
|
||||||
|
result = client.publish(tempTopic, tempMsg)
|
||||||
|
# result: [0, 1]
|
||||||
|
status = result[0]
|
||||||
|
if status == 0:
|
||||||
|
print(f"Send `{tempMsg}` to topic `{tempTopic}`")
|
||||||
|
else:
|
||||||
|
print(f"Failed to send message to topic {tempTopic}")
|
||||||
|
|
||||||
|
#Publish Humid Results
|
||||||
|
humidMsg = '{{ "humidity": {} }}'.format(humidity)
|
||||||
|
result = client.publish(humidTopic, humidMsg)
|
||||||
|
# result: [0, 1]
|
||||||
|
status = result[0]
|
||||||
|
if status == 0:
|
||||||
|
print(f"Send `{humidMsg}` to topic `{humidTopic}`")
|
||||||
|
else:
|
||||||
|
print(f"Failed to send message to topic {humidTopic}")
|
||||||
|
|
||||||
|
time.sleep(5.0)
|
||||||
|
|
||||||
time.sleep(2.0)
|
|
||||||
|
|
||||||
#Now publish this via mqtt
|
#Now publish this via mqtt
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
run()
|
||||||
Reference in New Issue
Block a user