#!/usr/bin/env python import math, time, threading, os, serial import RPi.GPIO as GPIO import LCD, hp03s, ftpsync from datetime import datetime from ringstorage import RingStorage import dewpoint # FTP-SYNC FTPSRV = "robfro.square7.ch" USER = "XXXX" PWD = "XXXX" FTPDIR1 = "/saves" LOCALDIR1 = "/var/www/pi/saves/" FTPDIR2 = "/graphs" LOCALDIR2 = "/var/www/pi/graphs/" FDLY = 30*60 # AirPressure PXCLR = 4 PBUS = 1 TDLY = 15 # Wake-On-Lan WOLDLY = 2 WOLFILE = "/var/www/wol/start.txt" WOLMAC = "XX:XX:XX:XX:XX:XX" # Bluetooth BTDLY = 15 BTPATH = "/dev/rfcomm0" # Save-Data SAVEDLY = 5*60 SAVEDIR = "/var/www/pi/saves/" STATUSFILE = "/var/www/pi/status.txt" MAKEFILE = "/home/pi/weather/makegraph.py" # Light LGHTDLY = 60 LGHTPIN = 25 BRIGHT = 100 DARK = 5 EVENING = 21.5 MORNING = 7.5 # Ringspeicher press = RingStorage(60) hum = RingStorage(60) temp = RingStorage(60) connect = RingStorage(60) #15 min lightrs = RingStorage(60) # LCD-Anzeige def getLine1(): h = str(int(hum.getMedium())) tm = time.strftime("%H:%M %d.%m.") return tm + " " + h + "%" def getLine2(): t = str(round(temp.getMedium(),1)) p = str(int(press.getMedium())) return t +chr(223)+"C " + p + "hPa " def timestr(): return time.strftime("%m%d%H%M ") class TempFTP(threading.Thread): def __init__(self, event): threading.Thread.__init__(self) self.stopped = event self.ftp1 = ftpsync.FTPSync(FTPSRV, USER, PWD, FTPDIR1, LOCALDIR1, ".csv") self.ftp2 = ftpsync.FTPSync(FTPSRV, USER, PWD, FTPDIR2, LOCALDIR2, ".png") def run(self): while not self.stopped.wait(FDLY): self.ftp1.sync(timestr()) self.ftp2.sync(timestr()) class WeatherMgr(threading.Thread): def __init__(self, event): threading.Thread.__init__(self) self.stopped = event GPIO.setmode(GPIO.BCM) GPIO.setup(17, GPIO.OUT) GPIO.setup(27, GPIO.OUT) GPIO.setup(22, GPIO.OUT) GPIO.setup(10, GPIO.OUT) GPIO.setup(9, GPIO.OUT) GPIO.setup(11, GPIO.OUT) self.lcd = LCD.LCD(17, 27, 11, 22, 9, 10) self.lcd.display_init() def run(self): while not self.stopped.wait(TDLY): p = -1 while p == -1: try: self.hp = hp03s.hp03s(PBUS, PXCLR) p = self.hp.getPressure()*1.0124 #Hoehenfaktor: rechneronline.de/barometer/ except: print timestr()+"[WTH] FAILED" time.sleep(1) press.addContent(p) print timestr()+"[WTH] "+str(p) self.updateDisp() def updateDisp(self): #self.lcd.display_init() #time.sleep(.5) self.lcd.goto_line(1) self.lcd.lcd_string(getLine1()) self.lcd.goto_line(2) self.lcd.lcd_string(getLine2()) class WOLMgr(threading.Thread): def __init__(self, event): threading.Thread.__init__(self) self.stopped = event def run(self): while not self.stopped.wait(WOLDLY): f = open(WOLFILE, "r") content = f.read() f.close() if (content == "yes"): print ("Start PC!") os.system("sudo wakeonlan " + WOLMAC) f = open(WOLFILE, "w") f.close() class BTMgr(threading.Thread): def __init__(self, event): threading.Thread.__init__(self) self.stopped = event def run(self): while not self.stopped.wait(BTDLY): print timestr()+"[BT] Start" bt = serial.Serial(BTPATH) try: bt.write("LINE1"+getLine1()+";") msg = "" while True: if (bt.inWaiting() > 0): c = bt.read(1) if (c != ';'): msg += c else: if (msg.startswith("DATA")): break; else: msg = ""; time.sleep(.005) msg = msg[4:] print timestr()+"[BT] " + msg split = msg.split(",") if (len(split) == 3): t = float(split[0]) h = int(split[1]) l = int(split[2]) temp.addContent(t) hum.addContent(h) lightrs.addContent(l) bt.write("LINE2" + getLine2() + ";") if (lghtmgr.isDay()): bt.write("LIGHT1;") else: bt.write("LIGHT0;") bt.close() connect.addContent(1.0) else: print timestr()+"[BT] ERROR!!" except Exception as e: print timestr()+"[BT] " + str(e) connect.addContent(0.0) class SaveMgr(threading.Thread): def __init__(self, event): threading.Thread.__init__(self) self.stopped = event def run(self): while not self.stopped.wait(SAVEDLY): t = round(temp.getMedium(), 1) p = round(press.getMedium(), 1) h = round(hum.getMedium(), 1) l = int(lightrs.getMedium()) if (t != -99 and p!=-99 and h != -99 and l!=99): tm = str(int(time.time())) file = time.strftime("%y-%W") print timestr()+"[SAVE] Write this file: " + file if (not os.path.isfile(SAVEDIR + file + ".csv")): datei = open(SAVEDIR + file + ".csv", "w") datei.write("UNIX-Time;Temperature1;Air Pressure;Relative Humidity;Light Level\n") datei.close() datei = open(SAVEDIR + file + ".csv", "a") datei.write(tm + ";" + str(t) + ";" + str(p) + ";" + str(h) + ";" + str(l) + "\n") datei.close() # Status.txt print timestr()+"[SAVE] Write Status-File" datei = open(STATUSFILE, "w") datei.write(str(round(temp.getMedium(),1)) + "\n") datei.write(str(int(hum.getMedium())) + "\n") datei.write(str(round(dewpoint.dewPoint(hum.getMedium(), temp.getMedium()),1)) + "\n") datei.write(str(int(press.getMedium())) + "\n") datei.write(time.strftime("%d.%m.%Y %H:%M") + "\n") datei.write(str(int(connect.getMedium() * 100))) datei.close() # MakeGraph print timestr()+"[SAVE] Make Graph" os.system("python " + MAKEFILE + " " + time.strftime("%y-%W %d") + " N") class LightMgr(threading.Thread): def __init__(self, event): threading.Thread.__init__(self) self.stopped = event GPIO.setup(25, GPIO.OUT) self.pwm = GPIO.PWM(25, 50) self.pwm.start(BRIGHT) def run(self): while not self.stopped.wait(LGHTDLY): if (self.isDay()): self.pwm.ChangeDutyCycle(BRIGHT) print timestr()+"[LGHT] BRIGHT" else: self.pwm.ChangeDutyCycle(DARK) print timestr()+"[LGHT] DARK" def isDay(self): tm = datetime.now().hour + datetime.now().minute / 60. return tm < EVENING and tm > MORNING class NewDayMgr(threading.Thread): def __init__(self, event): threading.Thread.__init__(self) self.stopped = event self.day = time.strftime("%y-%W %d") def run(self): while not self.stopped.wait(10*60): now = time.strftime("%y-%W %d") if (now != self.day): print timestr()+"[DAY] Make Graph with Medium Entry" os.system("python " + MAKEFILE + " " + self.day + " Y") self.day = now else: print timestr()+"[DAY] No New Day" stop = threading.Event() ftpmgr = TempFTP(stop) ftpmgr.start() whmgr = WeatherMgr(stop) whmgr.start() wolmgr = WOLMgr(stop) wolmgr.start() btmgr = BTMgr(stop) btmgr.start() savemgr = SaveMgr(stop) savemgr.start() lghtmgr = LightMgr(stop) lghtmgr.start() newdaymgr = NewDayMgr(stop) newdaymgr.start() def main(): try: inp = "" while inp!="e": time.sleep(1) #inp = raw_input("") stop.set() except KeyboardInterrupt: stop.set() if __name__ == '__main__': main()