Small Flask API to toggle Rapsberry Pi GPIOs
This code is written by kobbejager, but is based on bits and pieces taken from various sources. As such, I don't take any credit in this, nor do I give any guarantees.
This code is licenced with the UNLICENCE. See the LICENSE file for more documentation.
Python 3 Packages:
- Flask,
- Flask-API
- uwsgi
sudo pip3 install Flask Flask-API uwsgi
cd /opt
sudo git clone https://github.com/kobbejager/rpi-gpio-api
In app.py you can add, change and name swithable GPIO pins. The example below defines two switches (or relays in this case): the first relay is named "badkamer", and is operated by pin 8 on the Raspberry Pi.
relays = {"badkamer": 8, "slaapkamer": 10}
Note that physical pin numbers are used, not the BCM numbering. If you want to change this, replace GPIO.setmode(GPIO.BOARD)
by GPIO.setmode(GPIO.BCM)
sudo cp rpi-gpio-api/rpi-gpio-api.service /etc/systemd/system
sudo systemctl daemon-reload
sudo systemctl start rpi-gpio-api
sudo systemctl enable rpi-gpio-api
When the API is running, it should be accessible on port 5000.
To see the current state of the relay "badkamer": send a GET-request to e.g. http://[IP-of-RPI]:5000/badkamer/
To switch off the relay "badkamer", there are multiple methods
- POST to
http://[IP-of-RPI]:5000/badkamer/
withContent-Type: multipart/form-data
and payloadstate=0
- POST to
http://[IP-of-RPI]:5000/badkamer/
withContent-Type: Content-Type: application/json
and payload{"state"=0}
- GET to
http://[IP-of-RPI]:5000/badkamer/0
orhttp://[IP-of-RPI]:5000/badkamer/off
To switch on the relay "badkamer", the same approach can be used
- POST to
http://[IP-of-RPI]:5000/badkamer/
withContent-Type: multipart/form-data
and payloadstate=1
- POST to
http://[IP-of-RPI]:5000/badkamer/
withContent-Type: Content-Type: application/json
and payload{"state"=1}
- GET to
http://[IP-of-RPI]:5000/badkamer/1
orhttp://[IP-of-RPI]:5000/badkamer/on
To toggle the relay "badkamer":
- GET to
http://[IP-of-RPI]:5000/badkamer/toggle