From 3fdffe0fcf77b23b09953366ab6dc282759a7e9c Mon Sep 17 00:00:00 2001 From: Administrator Date: Mon, 30 Oct 2023 07:36:22 +0000 Subject: [PATCH] Upload New File --- web_relay.py | 102 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 102 insertions(+) create mode 100644 web_relay.py diff --git a/web_relay.py b/web_relay.py new file mode 100644 index 0000000..bd36ebf --- /dev/null +++ b/web_relay.py @@ -0,0 +1,102 @@ +from bottle import route, run, template, request +import RPi.GPIO as GPIO +import paho.mqtt.publish as publish + + +def transmitMQTT(strMsg): + strMqttBroker = "192.168.1.200" + strMqttChannel = "home/inside/zonneboiler/stat" + print('strMsg is:') + print(strMsg) + publish.single(strMqttChannel, strMsg, hostname=strMqttBroker, qos=1, auth={'username': 'esp', 'password': 'kmfpkylf'}) + +def relayClose(): + GPIO.output(CONTROL_PIN, True) + f = open("/home/pi/relay/status.txt","w+") + stat = "CLOSED" + f.write(stat) + f.close() + transmitMQTT(stat) + +def relayOpen(): + GPIO.output(CONTROL_PIN, False) + f = open("/home/pi/relay/status.txt","w+") + stat = "OPEN" + f.write(stat) + f.close() + transmitMQTT(stat) + + +def relayCloseManual(): + GPIO.output(CONTROL_PIN, True) + f = open("/home/pi/relay/status.txt","w+") + stat = "CLOSEDMANUAL" + f.write(stat) + f.close() + transmitMQTT(stat) + + +def relayOpenManual(): + GPIO.output(CONTROL_PIN, False) + f = open("/home/pi/relay/status.txt","w+") + stat = "OPENMANUAL" + f.write(stat) + f.close() + transmitMQTT(stat) + + +GPIO.setmode(GPIO.BCM) +CONTROL_PIN = 2 + +stat = "STARTING" +transmitMQTT(stat) +GPIO.setup(CONTROL_PIN, GPIO.OUT) + +@route('/') +def index(): + f=open("/home/pi/relay/status.txt","r") + if f.mode=='r': + stat=f.read() + f.close() + transmitMQTT(stat) + return template('/home/pi/relay/home.tpl', status=stat) + +@route('/closed') +def index(): + relayClose() + return template('/home/pi/relay/home.tpl', status="closed") + +@route('/open') +def index(): + relayOpen() + return template('/home/pi/relay/home.tpl', status="open") + +@route('/stats') +def index(): + f=open("/home/pi/relay/status.txt","r") + if f.mode=='r': + stat=f.read() + f.close() + transmitMQTT(stat) + return template('/home/pi/relay/stats.tpl', status=stat) + + +@route('/closeManual') +def index(): + relayCloseManual() + return template('/home/pi/relay/home.tpl', status="closed") + +@route('/openManual') +def index(): + relayOpenManual() + return template('/home/pi/relay/home.tpl', status="open") + + +try: + relayClose() + run(host='0.0.0.0', port=80) +finally: + print('Cleaning up GPIO') + GPIO.cleanup() + transmitMQTT("STOPPING") +