Function organization
This commit is contained in:
@@ -44,34 +44,27 @@ def connect_mqtt():
|
|||||||
return client
|
return client
|
||||||
|
|
||||||
|
|
||||||
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()
|
|
||||||
getValues()
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def getValues():
|
def getValues():
|
||||||
while True:
|
try:
|
||||||
try:
|
temp = sensor.temperature
|
||||||
temp = sensor.temperature
|
humidity = sensor.humidity
|
||||||
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
|
|
||||||
|
|
||||||
|
#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)
|
||||||
|
except Exception as error:
|
||||||
|
sensor.exit()
|
||||||
|
raise error
|
||||||
|
return temp, humidity
|
||||||
|
|
||||||
|
|
||||||
|
def publish(client, temp, humidity):
|
||||||
#Publish Temp Results
|
#Publish Temp Results
|
||||||
tempMsg = '{{ "temperature": {} }}'.format(temp)
|
tempMsg = '{{ "temperature": {} }}'.format(temp)
|
||||||
result = client.publish(tempTopic, tempMsg)
|
result = client.publish(tempTopic, tempMsg)
|
||||||
@@ -95,7 +88,17 @@ def getValues():
|
|||||||
time.sleep(5.0)
|
time.sleep(5.0)
|
||||||
|
|
||||||
|
|
||||||
#Now publish this via mqtt
|
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()
|
||||||
|
while True:
|
||||||
|
temp = getValues()
|
||||||
|
humidity = getValues()
|
||||||
|
publish(client, temp, humidity)
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
run()
|
run()
|
||||||
Reference in New Issue
Block a user