def main(): # ------------------------- # main # ------------------------- # lesen der Werte aus der HTML form cgitb.enable(display=0, logdir="/var/log/apache2") form = cgi.FieldStorage() cgi_number = form.getvalue('tankNumber') tankNumber = int(cgi_number) cgi_level1 = form.getvalue('level1') cgi_level2 = form.getvalue('level2') cgi_level3 = form.getvalue('level3') cgi_level4 = form.getvalue('level4') # print(cgi_number, cgi_level1, cgi_level2, cgi_level3, cgi_level4) if (cgi_number != None): (level1, level2, level3, level4) = CaravanPiFiles.readFillLevels(tankNumber) if (cgi_number != None and cgi_level1 != None and cgi_level2 != None and cgi_level3 != None and cgi_level4 != None): level1 = float(cgi_level1) level2 = float(cgi_level2) level3 = float(cgi_level3) level4 = float(cgi_level4) CaravanPiFiles.writeFillLevels(tankNumber, 0, 0, level1, level2, level3, level4) # Ergebnis Website schreiben print("Content-Type: text/html; charset=utf-8\n\n") print("<html>") print("<head>") print("<title>CaravanPi Konfiguration</title>") print("<meta http-equiv='refresh' content='2; URL=fill-levels.php'>") print("<link rel='stylesheet' type='text/css' href='css/main.css'>") print("<link rel='stylesheet' type='text/css' href='css/custom.css'>") print("</head>") print("<body>") print('<header class="header">CaravanPi Konfiguration - Füllstände Tanks</header>') if (cgi_number != None and cgi_level1 != None and cgi_level2 != None and cgi_level3 != None and cgi_level4 != None): print("Die eingegebenen Werte wurden erfolgreich gespeichert") else: print("ES KONNTEN KEINE WERTE AUS DEM FORMULAR GELESEN WERDEN!") print("<br/><br/>Sie werden zur Eingabeseite weitergeleitet") print("</body>") print("</html>")
def main(): # ------------------------- # main # ------------------------- global mymcp1, mymcp2 global tank global waitAfterPumpSeconds global pumpOnTime global pinPumpeAn # ----------------------------------------------- # Initialize the I2C bus and the GPIO port expander # ----------------------------------------------- i2c = busio.I2C(board.SCL, board.SDA) mymcp1 = MCP23017(i2c, address=0x20) mymcp2 = MCP23017(i2c, address=0x21) # ----------------------------------------------- # Initialize the GPIO ports # ----------------------------------------------- GPIO.setmode(GPIO.BCM) # ------------------------- # process call parameters # ------------------------- opts = [] args = [] writeFile = 0 displayScreen = 0 try: opts, args = getopt.getopt(sys.argv[1:], shortOptions, longOptions) except getopt.GetoptError: print(datetime.datetime.now().strftime("%Y%m%d%H%M%S "), "ERROR: options not correct") usage() sys.exit() for o, a in opts: if o == "--help" or o == "-h": print("HELP") usage() sys.exit() elif o == "--tank" or o == "-t": tank = int(a) print("Tanknummer ", '{:.0f}'.format(tank)) elif o == "--file" or o == "-f": print("output also to file ", filePosition) writeFile = 1 elif o == "--screen" or o == "-s": print("output also to this screen") displayScreen = 1 for a in args: print("further argument: ", a) # ------------------------- # read defaults # ------------------------- (literLevel1, literLevel2, literLevel3, literLevel4) = CaravanPiFiles.readFillLevels(tank) # ------------------------- # initialize IO Pins on MCP23017 # pin number from 0 to 15 for the GPIOA0...GPIOA7, GPIOB0...GPIOB7 pins (i.e. pin 12 is GPIOB4) # Füllstände: 1 an Pin GPIOB0 (8), ..., 4 an Pin GPIOB3 (11) # Relais an Pin GPIOB4 (12) # ------------------------- fillingLevel1 = mymcp1.get_pin(8) fillingLevel1.direction = digitalio.Direction.INPUT fillingLevel2 = mymcp1.get_pin(9) fillingLevel2.direction = digitalio.Direction.INPUT fillingLevel3 = mymcp1.get_pin(10) fillingLevel3.direction = digitalio.Direction.INPUT fillingLevel4 = mymcp1.get_pin(11) fillingLevel4.direction = digitalio.Direction.INPUT levelPower = mymcp1.get_pin(12) levelPower.direction = digitalio.Direction.OUTPUT # ------------------------- # initialisieren # ------------------------- levelPower.value = False # ------------------------- # Interrupt for switching on the pump # ------------------------- GPIO.setup(pinPumpeAn, GPIO.IN) # Wenn Pumpe wieder ausschaltet, dann Interruptbehandlung GPIO.add_event_detect(pinPumpeAn, GPIO.RISING, callback=interruptPumpOn, bouncetime=400) # ------------------------- # set things for meassuring at startup # ------------------------- actLiter = 0 pumpOnTime = datetime.datetime.now() - datetime.timedelta( seconds=waitAfterPumpSeconds + 1) # ------------------------- # Main loop # ------------------------- try: while True: if pumpOnTime is not None: elapsed = (datetime.datetime.now() - pumpOnTime).total_seconds() # print("Sekunden seit Interrupt: ", elapsed) if elapsed >= waitAfterPumpSeconds: # print(datetime.datetime.now().strftime("%Y%m%d%H%M%S "), "Spannung anlegen") levelPower.value = True time.sleep(.5) level1contact = not fillingLevel1.value level2contact = not fillingLevel2.value level3contact = not fillingLevel3.value level4contact = not fillingLevel4.value # print("Spannung aus") levelPower.value = False pumpOnTime = None if displayScreen == 1: print("Level 1 erreicht? {0}".format(level1contact), literLevel1, " Liter") print("Level 2 erreicht? {0}".format(level2contact), literLevel2, " Liter") print("Level 3 erreicht? {0}".format(level3contact), literLevel3, " Liter") print("Level 4 erreicht? {0}".format(level4contact), literLevel4, " Liter") if level4contact: actLiter = literLevel4 elif level3contact: actLiter = literLevel3 elif level2contact: actLiter = literLevel2 elif level1contact: actLiter = literLevel1 else: actLiter = 0 if displayScreen == 1: if actLiter < literLevel1: print("Füllmenge gering") else: print("mind. " + str(actLiter) + " Liter im Tank") if writeFile == 1: write2file(actLiter) time.sleep(waitAfterPumpSeconds) except KeyboardInterrupt: GPIO.cleanup() except: print("unprocessed Error:", sys.exc_info()[0]) GPIO.cleanup() # ------------------------- # Cleaning at the end # ------------------------- GPIO.cleanup()
def main(): # ------------------------- # main # ------------------------- global mymcp1, mymcp2 global tank # ----------------------------------------------- # Initialize the I2C bus and the GPIO port expander # ----------------------------------------------- i2c = busio.I2C(board.SCL, board.SDA) mymcp1 = MCP23017(i2c, address=0x20) mymcp2 = MCP23017(i2c, address=0x21) # ----------------------------------------------- # Initialize the GPIO ports # ----------------------------------------------- GPIO.setmode(GPIO.BCM) # ------------------------- # process call parameters # ------------------------- opts = [] args = [] writeFile = 0 displayScreen = 0 try: opts, args = getopt.getopt(sys.argv[1:], shortOptions, longOptions) except getopt.GetoptError: print(datetime.datetime.now().strftime("%Y%m%d%H%M%S "), "ERROR: options not correct") usage() sys.exit() for o, a in opts: if o == "--help" or o == "-h": print("HELP") usage() sys.exit() elif o == "--tank" or o == "-t": tank = int(a) print("Tanknummer ", '{:.0f}'.format(tank)) elif o == "--file" or o == "-f": print("output also to file ", filePosition) writeFile = 1 elif o == "--screen" or o == "-s": print("output also to this screen") displayScreen = 1 for a in args: print("further argument: ", a) # ------------------------- # read defaults # ------------------------- (literLevel1, literLevel2, literLevel3, literLevel4) = CaravanPiFiles.readFillLevels(tank) # ------------------------- # initialize IO Pins on MCP23017 # pin number from 0 to 15 for the GPIOA0...GPIOA7, GPIOB0...GPIOB7 pins (i.e. pin 12 is GPIOB4) # Füllstände: 1 an Pin GPIOB3 (11), ..., 4 an Pin GPIOB6 (14) # ------------------------- fillingLevel1 = mymcp2.get_pin(11) fillingLevel1.direction = digitalio.Direction.INPUT fillingLevel2 = mymcp2.get_pin(12) fillingLevel2.direction = digitalio.Direction.INPUT fillingLevel3 = mymcp2.get_pin(13) fillingLevel3.direction = digitalio.Direction.INPUT fillingLevel4 = mymcp2.get_pin(14) fillingLevel4.direction = digitalio.Direction.INPUT # ------------------------- # Main # ------------------------- try: while True: if displayScreen == 1: print("Level prüfen ...") level1contact = fillingLevel1.value level2contact = fillingLevel2.value level3contact = fillingLevel3.value level4contact = fillingLevel4.value if displayScreen == 1: print("Level 1 erreicht? {0}".format(level1contact), literLevel1, " Liter") print("Level 2 erreicht? {0}".format(level2contact), literLevel2, " Liter") print("Level 3 erreicht? {0}".format(level3contact), literLevel3, " Liter") print("Level 4 erreicht? {0}".format(level4contact), literLevel4, " Liter") if level4contact: actLiter = literLevel4 elif level3contact: actLiter = literLevel3 elif level2contact: actLiter = literLevel2 elif level1contact: actLiter = literLevel1 else: actLiter = 0 if displayScreen == 1: if actLiter < literLevel1: print("Füllmenge gering") else: print("mind. " + str(actLiter) + " Liter im Tank") if writeFile == 1: write2file(actLiter) time.sleep(waitAfterReadingSeconds) except KeyboardInterrupt: GPIO.cleanup() except: print("unprocessed Error:", sys.exc_info()[0]) GPIO.cleanup() # ------------------------- # Cleaning at the end # ------------------------- GPIO.cleanup()