diff --git a/.gitignore b/.gitignore index 0d20b64..83658ec 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ *.pyc +*.log diff --git a/engine_test.py b/engine_test.py index 5c64488..f18c01f 100755 --- a/engine_test.py +++ b/engine_test.py @@ -1,16 +1,50 @@ #!/usr/bin/python2 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) -while True: - eng.stop() - print "engine stop" - sleep(3) - eng.run_up() - print "engine up" - sleep(3) - eng.run_down() - print "engine down" - sleep(3) +ps = power_sensor(1, 0x40) +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:]) diff --git a/light_sensor_test.py b/light_sensor_test.py index 95ce6af..bda28b9 100755 --- a/light_sensor_test.py +++ b/light_sensor_test.py @@ -16,4 +16,4 @@ while True: log.write(line) log.write("\n") log.close() - sleep(1) + sleep(30) diff --git a/mqtt_test.py b/mqtt_test.py new file mode 100755 index 0000000..9c6da1b --- /dev/null +++ b/mqtt_test.py @@ -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() diff --git a/wifi_fieldstrength.py b/wifi_fieldstrength.py index 2a49715..5add0e1 100644 --- a/wifi_fieldstrength.py +++ b/wifi_fieldstrength.py @@ -3,15 +3,18 @@ from re import match, sub class wifi_fieldstrength: def __init__(self, name = "wlan0"): - self.name = name + self._name = name self.wf_name = "/proc/net/wireless" + def name(self): + return self._name + def read(self): ret = False f = open(self.wf_name, "r") for line in f: line = line.strip() - mstr = "^" + self.name + mstr = "^" + self._name if match(mstr, line): line = sub("\s+", " ", line) tmp = line.split(" ")