Reorganizing functions
This commit is contained in:
@@ -16,14 +16,46 @@ password = 'falrenforbreakfast'
|
|||||||
|
|
||||||
client_id = f'python-mqtt-office-{random.randint(0, 1000)}'
|
client_id = f'python-mqtt-office-{random.randint(0, 1000)}'
|
||||||
|
|
||||||
|
#D20 is the pin number, DHT11 is the sensor type
|
||||||
|
sensor = adafruit_dht.DHT11(board.D20)
|
||||||
|
|
||||||
|
#MQTT Connection
|
||||||
|
# def on_connect(client, rc):
|
||||||
|
# if rc == 0:
|
||||||
|
# print("Connected to MQTT Broker!")
|
||||||
|
# else:
|
||||||
|
# print("Failed to connect, return code %d\n", rc)
|
||||||
|
# #MQTT Connect
|
||||||
|
# client = mqtt_client.Client(client_id)
|
||||||
|
# client.username_pw_set(username, password)
|
||||||
|
# client.on_connect = on_connect
|
||||||
|
# client.connect(broker, port)
|
||||||
|
def connect_mqtt():
|
||||||
|
def on_connect(client, rc):
|
||||||
|
if rc == 0:
|
||||||
|
print("Connected to MQTT Broker!")
|
||||||
|
else:
|
||||||
|
print("Failed to connect, return code %d\n", rc)
|
||||||
|
|
||||||
|
client = mqtt_client.Client(client_id)
|
||||||
|
client.username_pw_set(username, password)
|
||||||
|
client.on_connect = on_connect
|
||||||
|
client.connect(broker, port)
|
||||||
|
return client
|
||||||
|
|
||||||
|
|
||||||
|
def run():
|
||||||
# We first check if a libgpiod process is running. If yes, we kill it!
|
# We first check if a libgpiod process is running. If yes, we kill it!
|
||||||
for proc in psutil.process_iter():
|
for proc in psutil.process_iter():
|
||||||
if proc.name() == 'libgpiod_pulsein' or proc.name() == 'libgpiod_pulsei':
|
if proc.name() == 'libgpiod_pulsein' or proc.name() == 'libgpiod_pulsei':
|
||||||
proc.kill()
|
proc.kill()
|
||||||
|
client = connect_mqtt()
|
||||||
|
client.loop_start()
|
||||||
|
client.getValues()
|
||||||
|
|
||||||
#D20 is the pin number, DHT11 is the sensor type
|
|
||||||
sensor = adafruit_dht.DHT11(board.D20)
|
|
||||||
|
|
||||||
|
|
||||||
|
def getValues():
|
||||||
while True:
|
while True:
|
||||||
try:
|
try:
|
||||||
temp = sensor.temperature
|
temp = sensor.temperature
|
||||||
@@ -40,18 +72,6 @@ while True:
|
|||||||
sensor.exit()
|
sensor.exit()
|
||||||
raise error
|
raise error
|
||||||
|
|
||||||
#MQTT Portion
|
|
||||||
def on_connect(client, userdata, flags, rc):
|
|
||||||
if rc == 0:
|
|
||||||
print("Connected to MQTT Broker!")
|
|
||||||
else:
|
|
||||||
print("Failed to connect, return code %d\n", rc)
|
|
||||||
|
|
||||||
client = mqtt_client.Client(client_id)
|
|
||||||
client.username_pw_set(username, password)
|
|
||||||
client.on_connect = on_connect
|
|
||||||
client.connect(broker, port)
|
|
||||||
|
|
||||||
#Publish Temp Results
|
#Publish Temp Results
|
||||||
tempMsg = '{{ "temperature": {} }}'.format(temp)
|
tempMsg = '{{ "temperature": {} }}'.format(temp)
|
||||||
result = client.publish(tempTopic, tempMsg)
|
result = client.publish(tempTopic, tempMsg)
|
||||||
@@ -72,9 +92,10 @@ while True:
|
|||||||
else:
|
else:
|
||||||
print(f"Failed to send message to topic {humidTopic}")
|
print(f"Failed to send message to topic {humidTopic}")
|
||||||
|
|
||||||
client.loop_start()
|
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