Code Monkey home page Code Monkey logo

pebblepio's Introduction

Raspberry Pi GPIO API

This is a simple RESTful API built with Python on Flask to control an 8 channel relay module with the GPIO pins of a Raspberry Pi. Please note that this application can only be run on a Raspberry Pi and the GPIO pin numbering is in BCM format.

Getting Started

Step 1 - Installing Pip

pi@raspberrypi ~ $ sudo apt-get install python-pip

Step 2 - Installing Requirements

pi@raspberrypi ~ $ cd raspberrypi-gpio-api
pi@raspberrypi ~/raspberrypi-gpio-api $ sudo pip install -r requirements.txt

Above command should successfully install the required dependencies.

Step 3 - Run the development web server

pi@raspberrypi ~/raspberrypi-gpio-api $ sudo python flask-api.py

That should start the development server up.

Step 4 - Test the API

To test if the API is running properly, open up a browser and call the ping url of the API - {{Your RPi IP Address}}/api/v1/ping/

If the API is running properly, you should see something like this on your browser.

{
    'api_name': 'RPi GPIO API',
    'response': 'pong',
    'status': 'SUCCESS',
    'version': '1.0'
}

You're all set! :D

API endpoints

/api/v1/ping/

  • Supported Methods - GET
  • GET - Returns the status of the API.
  • POST - Not supported.

Example GET response:

{
    'api_name': 'RPi GPIO API',
    'response': 'pong',
    'status': 'SUCCESS',
    'version': '1.0'
}

/api/v1/gpio/<pin_number>/

  • Supported Methods - GET, POST
  • GET - Returns the status of specified GPIO BCM pin number.
  • POST - Sets the value for the specified GPIO pin.
    • Parameters supported
      • value (Valid values are 0 or 1)

Example GET response:

{
    'pin_number': 17,
    'pin_name': 'Bedroom Lights',
    'value': 1,
    'status': 'SUCCESS',
    'error': null
}

Example POST response:

{
    'pin_number': 17,
    'pin_name': 'Bedroom Lights',
    'new_value': 1,
    'status': 'SUCCESS',
    'error': null
}

/api/v1/gpio/<pin_number>/toggle/

  • Supported Methods - GET
  • GET - Toggles the state of specified GPIO BCM pin number.
  • POST - Not supported.

Example GET response:

{
  "error": null, 
  "new_value": 1, 
  "pin_name": "IN1", 
  "pin_number": 17, 
  "status": "SUCCESS"
}

/api/v1/gpio/status/

  • Supported Methods - GET
  • GET - Returns the status all the GPIO pins.
  • POST - Not supported.

Example GET response:

{
    'data': [
        {
            'pin_number': 17,
            'pin_name': 'Bedroom Lights',
            'value': 1,
            'status': 'SUCCESS',
            'error': null
        },
        {
            'pin_number': 4,
            'pin_name': 'Bedroom TV',
            'value': 1,
            'status': 'SUCCESS',
            'error': null
        },
        {
            'pin_number': 22,
            'pin_name': 'Living Room Lights',
            'value': 0,
            'status': 'SUCCESS',
            'error': null
        }
    ]
}

/api/v1/gpio/all-high/

  • Supported Methods - POST
  • GET - Not supported.
  • POST - Sets the GPIO value to 1 on all the pins.

Example POST response:

{
    'data': [
        {
            'pin_number': 17,
            'pin_name': 'Bedroom Lights',
            'new_value': 1,
            'status': 'SUCCESS',
            'error': null
        },
        {
            'pin_number': 4,
            'pin_name': 'Bedroom TV',
            'new_value': 1,
            'status': 'SUCCESS',
            'error': null
        },
        {
            'pin_number': 22,
            'pin_name': 'Living Room Lights',
            'new_value': 1,
            'status': 'SUCCESS',
            'error': null
        }
    ]
}

/api/v1/gpio/all-low/

  • Supported Methods - POST
  • GET - Not supported.
  • POST - Sets the GPIO value to 0 on all the pins.

Example POST response:

{
    'data': [
        {
            'pin_number': 17,
            'pin_name': 'Bedroom Lights',
            'new_value': 0,
            'status': 'SUCCESS',
            'error': null
        },
        {
            'pin_number': 4,
            'pin_name': 'Bedroom TV',
            'new_value': 0,
            'status': 'SUCCESS',
            'error': null
        },
        {
            'pin_number': 22,
            'pin_name': 'Living Room Lights',
            'new_value': 0,
            'status': 'SUCCESS',
            'error': null
        }
    ]
}

Future Tasks

  • Add API authentication.

pebblepio's People

Contributors

mr-nathan avatar corrosivekid avatar ccesario avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.