Flask REST API for Raspberry PI
Brocoli pi - Flask REST API for Raspberry pi
git clone https://github.com/CBinet/Brocoli-pi.git
Launch the server :
python server.py
Your server will then be running locally at address http://0.0.0.0:5000.
From other devices, the address will be your pi address. Example : http://192.168.2.51:5000.
You can get your raspberry pi IP address with the following command :
hostname -I
npm install brocoli-pi
Navigate inside the brocoli-pi folder then :
npm start
If you want to start the server automatically when
you boot up your raspberry, you can modify your .profile file
located in your raspberry pi home folder :
sudo nano ~/.profile
Add the following line at the end of the file :
python <where-your-server.py-is-located>/server.py
Then press CTRL+X, Y to save and quit the file editor.
Example : Let’s say your git repository is located in
your home folder. If you want to add a greeting message,
pull the latest version of Brocoli-pi and also start the server
you would do something like this :
echo Greetings, human.
cd ~/
git pull origin master
python server.py
To run the server :
python server.py
Once the server is started, you can test it by navigating to http://0.0.0.0:5000/outputs.
You should see a list of your outputs.
# Instanciate an output at location '17'
# with label "Red Light".
output = Output(17, "Red Light")
# Toggles the voltage of 'output'
output.toggle()
# ...
# Instanciate a group controlling 'outputs' with
# the label "LED group"
group = Group(0, outputs, "LED group")
# This will toggle the voltage of the outputs
# of the group.
group.toggle()
GET/ outputs :
{
"results": [
{
"id": 17,
"info": "Red Light",
"state": false
},
{
"id": 18,
"info": "Green Light",
"state": false
},
{
"id": 19,
"info": "Yellow Light",
"state": false
}
]
}
GET/ outputs/17/toggle :
{
"id": 17,
"info": "Red Light",
"state": true
}
GET/ groups/0 :
{
"id": 0,
"info": "Basic 3 LED group",
"outputs": [
{
"id": 17,
"info": "Red Light",
"state": false
},
{
"id": 18,
"info": "Green Light",
"state": false
},
{
"id": 19,
"info": "Yellow Light",
"state": false
}
],
"state": false
}