chickenhouse/mqtt_test.py

35 lines
836 B
Python
Raw Normal View History

2016-04-07 04:50:41 +00:00
#!/usr/bin/python2
from time import sleep, time
import paho.mqtt.client as mqtt
from wifi_fieldstrength import wifi_fieldstrength
from light_sensor import light_sensor
def on_connect(client, data, flags, result):
print "Connected with " + str(result)
def main():
cl = mqtt.Client()
cl.on_connect = on_connect
cl.connect("gitlab", 1883, 60)
cl.loop_start()
wf = wifi_fieldstrength("wlan0")
ls = light_sensor(1, 0x23)
light = 0
for i in range(0, 5):
light += ls.read()
sleep(1)
light /= 5
tm = str(int(time()))
cl.publish("outdoor/chickenhouse/fieldstr", tm + " " + \
str(wf.name()) + ": " + str(wf.read()) + " dBm", qos = 2, \
retain = True)
cl.publish("outdoor/chickenhouse/light", tm + " " + \
str(light) + " lx", qos = 2, retain = True)
cl.loop_stop()
cl.disconnect()
if __name__ == "__main__":
main()