mqtt publisher started
This commit is contained in:
parent
78d56b5194
commit
4c34544b9b
1
.gitignore
vendored
1
.gitignore
vendored
@ -1 +1,2 @@
|
|||||||
*.pyc
|
*.pyc
|
||||||
|
*.log
|
||||||
|
@ -1,16 +1,50 @@
|
|||||||
#!/usr/bin/python2
|
#!/usr/bin/python2
|
||||||
from engine import engine
|
from engine import engine
|
||||||
from time import sleep
|
from power_sensor import power_sensor
|
||||||
|
from time import sleep, time
|
||||||
|
from datetime import datetime
|
||||||
|
from getopt import getopt
|
||||||
|
from sys import argv
|
||||||
|
|
||||||
eng = engine(13, 19)
|
eng = engine(13, 19)
|
||||||
while True:
|
ps = power_sensor(1, 0x40)
|
||||||
eng.stop()
|
|
||||||
print "engine stop"
|
|
||||||
sleep(3)
|
|
||||||
eng.run_up()
|
|
||||||
print "engine up"
|
|
||||||
sleep(3)
|
|
||||||
eng.run_down()
|
|
||||||
print "engine down"
|
|
||||||
sleep(3)
|
|
||||||
|
|
||||||
|
def run_engine(direction):
|
||||||
|
if direction in ("u", "up"):
|
||||||
|
eng.run_up()
|
||||||
|
elif direction in ("d", "down"):
|
||||||
|
eng.run_down()
|
||||||
|
else:
|
||||||
|
return
|
||||||
|
log = open("power.log", "w")
|
||||||
|
try:
|
||||||
|
while True:
|
||||||
|
power = ps.power_mw()
|
||||||
|
ts = datetime.fromtimestamp(time()).microsecond
|
||||||
|
log.write(str(ts) + " ms " + str(power) + " mW")
|
||||||
|
log.write(str(ts) + " ms " + str(power) + " mW")
|
||||||
|
log.write(str(ts) + " ms " + str(power) + " mW")
|
||||||
|
print str(ts) + " ms " + str(power) + " mW"
|
||||||
|
except KeyboardInterrupt:
|
||||||
|
eng.stop()
|
||||||
|
log.close()
|
||||||
|
|
||||||
|
def print_help():
|
||||||
|
print "help screen..."
|
||||||
|
|
||||||
|
|
||||||
|
def main(argv):
|
||||||
|
dir_list = ["u", "up", "d", "down"]
|
||||||
|
options, remainder = getopt(argv, "hd:", ["help", "direction="])
|
||||||
|
for opt, args in options:
|
||||||
|
if opt in ("-h", "--help"):
|
||||||
|
print_help()
|
||||||
|
elif opt in ("-d", "--direction"):
|
||||||
|
if args in dir_list:
|
||||||
|
run_engine(args)
|
||||||
|
else:
|
||||||
|
print_help()
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main(argv[1:])
|
||||||
|
@ -16,4 +16,4 @@ while True:
|
|||||||
log.write(line)
|
log.write(line)
|
||||||
log.write("\n")
|
log.write("\n")
|
||||||
log.close()
|
log.close()
|
||||||
sleep(1)
|
sleep(30)
|
||||||
|
34
mqtt_test.py
Executable file
34
mqtt_test.py
Executable file
@ -0,0 +1,34 @@
|
|||||||
|
#!/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()
|
@ -3,15 +3,18 @@ from re import match, sub
|
|||||||
|
|
||||||
class wifi_fieldstrength:
|
class wifi_fieldstrength:
|
||||||
def __init__(self, name = "wlan0"):
|
def __init__(self, name = "wlan0"):
|
||||||
self.name = name
|
self._name = name
|
||||||
self.wf_name = "/proc/net/wireless"
|
self.wf_name = "/proc/net/wireless"
|
||||||
|
|
||||||
|
def name(self):
|
||||||
|
return self._name
|
||||||
|
|
||||||
def read(self):
|
def read(self):
|
||||||
ret = False
|
ret = False
|
||||||
f = open(self.wf_name, "r")
|
f = open(self.wf_name, "r")
|
||||||
for line in f:
|
for line in f:
|
||||||
line = line.strip()
|
line = line.strip()
|
||||||
mstr = "^" + self.name
|
mstr = "^" + self._name
|
||||||
if match(mstr, line):
|
if match(mstr, line):
|
||||||
line = sub("\s+", " ", line)
|
line = sub("\s+", " ", line)
|
||||||
tmp = line.split(" ")
|
tmp = line.split(" ")
|
||||||
|
Loading…
Reference in New Issue
Block a user