项目作者: CBinet

项目描述 :
Flask REST API for Raspberry PI
高级语言: Python
项目地址: git://github.com/CBinet/brocoli-pi.git
创建时间: 2017-04-11T01:45:47Z
项目社区:https://github.com/CBinet/brocoli-pi

开源协议:

下载


Brocoli pi

Brocoli pi - Flask REST API for Raspberry pi

Installation

Install with git

Clone the repository :

  1. git clone https://github.com/CBinet/Brocoli-pi.git

Launch Brocoli pi

Launch the server :

  1. 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 :

  1. hostname -I

Install with npm

Install the package

  1. npm install brocoli-pi

Navigate inside the brocoli-pi folder then :

Launch Brocoli pi

  1. npm start

Extras - Setup autorun on bootup

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 :

  1. sudo nano ~/.profile

Add the following line at the end of the file :

  1. 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 :

  1. echo Greetings, human.
  2. cd ~/
  3. git pull origin master
  4. python server.py

Using Brocoli-pi

To run the server :

  1. 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.

Modules

GPIOControls

Classes :

  • Output : Single output pin
  • Group : Group of output pins

Usage :

  1. # Instanciate an output at location '17'
  2. # with label "Red Light".
  3. output = Output(17, "Red Light")
  4. # Toggles the voltage of 'output'
  5. output.toggle()
  6. # ...
  7. # Instanciate a group controlling 'outputs' with
  8. # the label "LED group"
  9. group = Group(0, outputs, "LED group")
  10. # This will toggle the voltage of the outputs
  11. # of the group.
  12. group.toggle()

Routes :

  • GET/ outputs : Returns informations of current binded outputs
  • GET/ outputs/:id : Returns information of the output at ‘id’ location
  • GET/ outputs/:id/toggle : Toggle the voltage of the output at ‘id’ location
  • GET/ groups : Returns informations of current binded groups
  • GET/ groups/:id : Returns information of the group ‘id’
  • GET/ groups/:id/toggle : Toggle the voltage of the output of group ‘id’

Example responses :

GET/ outputs :

  1. {
  2. "results": [
  3. {
  4. "id": 17,
  5. "info": "Red Light",
  6. "state": false
  7. },
  8. {
  9. "id": 18,
  10. "info": "Green Light",
  11. "state": false
  12. },
  13. {
  14. "id": 19,
  15. "info": "Yellow Light",
  16. "state": false
  17. }
  18. ]
  19. }

GET/ outputs/17/toggle :

  1. {
  2. "id": 17,
  3. "info": "Red Light",
  4. "state": true
  5. }

GET/ groups/0 :

  1. {
  2. "id": 0,
  3. "info": "Basic 3 LED group",
  4. "outputs": [
  5. {
  6. "id": 17,
  7. "info": "Red Light",
  8. "state": false
  9. },
  10. {
  11. "id": 18,
  12. "info": "Green Light",
  13. "state": false
  14. },
  15. {
  16. "id": 19,
  17. "info": "Yellow Light",
  18. "state": false
  19. }
  20. ],
  21. "state": false
  22. }