Switch updates on status using HTTP

I’m wondering how I get live switch updates on status like my wemo switchs,

I have a Raspberry PI running BOTTLE to get a REST type solution going for the GPIO pins.

POST http://192.168.31.225:8080/pool/items/solarpump/state/ON turns it on
POST http://192.168.31.225:8080/pool/items/solarpump/state/OFF turns it off
GET http://192.168.31.225:8080/pool/items/solarpump/state returns ON or OFF

My Item is set like this and works fine to turn it on and off.

Switch SolarPUMP {http=">[ON:POST:http://192.168.31.225:8080/pool/items/solarpump/state/ON] >[OFF:POST:http://192.168.31.225:8080/pool/items/solarpump/state/OFF]"}

But I I turn the GPIO on and off with timers also and would like that to show on the switch when you go to that device.

This didn’t work

Switch SolarPUMP {http=">[ON:POST:http://192.168.31.225:8080/pool/items/solarpump/state/ON] >[OFF:POST:http://192.168.31.225:8080/pool/items/solarpump/state/OFF]" <{GET:http://192.168.31.225:8080/pool/items/solarpump/state:5000}" }

Any help would be great.

Formatting of binding config strings can be very finicky. Try this:

Switch SolarPUMP {http=">[*:POST:http://192.168.31.225:8080/pool/items/solarpump/state/%2$s] <[http://192.168.31.225:8080/pool/items/solarpump/state:5000:REGEX((.*))]" }

See the examples in the wiki page.

Thank you so much this worked perfectly now I have that working I can get my whole project completed.

Thanks again.

Shane

1 Like

If anyone else is interested in getting a REST type interface running on a PI, using bottle is very easy. here is my python code which has two different types of relays and three temp probes for my pool pump controller and chlorinator I’m building. Shows how to get bottle working and its very simple once you see one working. With the code above added by watou you can easily interface it with openhab.

#!/usr/bin/python

import RPi.GPIO as GPIO

from bottle import Bottle, run

app=Bottle()

PUMP=20
SOLAR=21
AMPS=22
MAIN9V=12
VOLTS=24
TEMP=5
ACHLOR=26
BCHLOR=16
mark=0

GPIO.setmode(GPIO.BCM)

GPIO.setup(PUMP, GPIO.IN)
GPIO.setup(SOLAR, GPIO.IN)
GPIO.setup(ACHLOR, GPIO.IN)
GPIO.setup(BCHLOR, GPIO.IN)
GPIO.setup(MAIN9V, GPIO.OUT)
GPIO.output(MAIN9V, GPIO.LOW)

@app.route(’/pool/items/pump/state’, method=‘GET’)
def pump():
mark=GPIO.gpio_function(PUMP)
if mark:
return "OFF"
else:
return “ON”

@app.route(’/pool/items/pump/state/ON’, method=‘POST’)
def pumpon():
GPIO.setup(PUMP, GPIO.OUT)
return “ON”

@app.route(’/pool/items/pump/state/OFF’, method=‘POST’)
def pumpoff():
GPIO.setup(PUMP, GPIO.IN)
return “OFF”

@app.route(’/pool/items/solarpump/state’, method=‘GET’)
def solarpump():
mark = GPIO.gpio_function(SOLAR)
if mark:
return "OFF"
else:
return “ON”

@app.route(’/pool/items/solarpump/state/ON’, method=‘POST’)
def solarpumpon():
GPIO.setup(SOLAR, GPIO.OUT)
return “ON”

@app.route(’/pool/items/solarpump/state/OFF’, method=‘POST’)
def solarpumpoff():
GPIO.setup(SOLAR, GPIO.IN)
return “OFF”

@app.route(’/pool/items/masterchlor/state’, method=‘GET’)
def solarpump():
mark = GPIO.input(MAIN9V)
if mark:
return "ON"
else:
return “OFF”

@app.route(’/pool/items/masterchlor/state/ON’, method=‘POST’)
def main9von():
GPIO.output(MAIN9V, GPIO.HIGH)
return “ON”

@app.route(’/pool/items/masterchlor/state/OFF’, method=‘POST’)
def main8voff():
GPIO.output(MAIN9V, GPIO.LOW)
return “OFF”

@app.route(’/pool/items/pooltemp/state’, method=‘GET’)
def pooltemp():
tfile = open("/sys/bus/w1/devices/28-00043c92cfff/w1_slave")
text = tfile.read()
tfile.close()
secondline = text.split("\n")[1]
temperaturedata = secondline.split(" ")[9]
temperature = float(temperaturedata[2:])
temperature = temperature / 1000
return str(temperature)

@app.route(’/pool/items/solartemp/state’, method=‘GET’)
def solartemp():
tfile = open("/sys/bus/w1/devices/28-00043e90b9ff/w1_slave")
text = tfile.read()
tfile.close()
secondline = text.split("\n")[1]
temperaturedata = secondline.split(" ")[9]
temperature = float(temperaturedata[2:])
temperature = temperature / 1000
return str(temperature)

@app.route(’/pool/items/boxtemp/state’, method=‘GET’)
def boxtemp():
tfile = open("/sys/bus/w1/devices/28-00043e90c2ff/w1_slave")
text = tfile.read()
tfile.close()
secondline = text.split("\n")[1]
temperaturedata = secondline.split(" ")[9]
temperature = float(temperaturedata[2:])
temperature = temperature / 1000
return str(temperature)

run(app, host=‘192.168.31.225’, port=8080, debug=True)

1 Like