Upload New File

This commit is contained in:
Administrator
2023-10-30 07:36:22 +00:00
parent 064633d3cf
commit 3fdffe0fcf

102
web_relay.py Normal file
View File

@@ -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")