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)