Skip to content

A simple Web API interface to control PiFace Digital inputs and outputs with Webhook support

Notifications You must be signed in to change notification settings

robsonvn/piface-digital-web-api

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

17 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

piface-digital-web-api

A simple web interface to control your PiFace Digital inputs and outputs written in Python.

Setup

git clone https://github.com/robsonvn/piface-digital-web-api.git
cd piface-digital-web-api
cp .env.example .env
pip install -r requirements.txt

You can read more about Environment Variables here.

Running

python -m aiohttp.web -H 0.0.0.0 -P 80 app:init_func --verbose

Running with Docker

sudo docker run --privileged -it -d -p 80:80 robsonvn/piface-digital-web-api

Endpoints

Device state (/)

  curl -X GET http://127.0.0.1:8080/

Result

{
  "device_id": "10.0.0.77",
  "flick_sleep_interval": 1,
  "number_of_inputs": 8,
  "number_of_outputs": 8,
  "states": {
    "inputs": [
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0
    ],
    "outputs": [
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0
    ]
  },
  "uptime": "0:00:34.838569"
}

Flick output (/output/flick)

  curl -X GET http://127.0.0.1:8080/output/flick?output=0&output=1&interval=0.1

Response: the current device state

If interval parameter is not given, the default delay time is 1 second, but you can define a custom value in your .env file using FLICK_SLEEP_INTERVAL

Turn output on (/output/on)

  curl -X GET http://127.0.0.1:8080/output/on/?output=0&output=1

Response: the current device state

Turn output off (/output/off)

  curl -X GET http://127.0.0.1:8080/output/off?output=0&output=1

Response: the current device state

Webhook

You can use Webhook to notify your application whenever an input/output state changes by adding WEBHOOK_URL in your .env file.

Example:

WEBHOOK_URL=http://10.0.0.200:3030/webhooker/raspberry/

A GET request will be performed with the following parameters:

  • device_id the device id set in your .env file
  • event_type the event type (output/input)
  • event_number the input/output number (0 .. 9)
  • event_value the event value (on/off)

Example: When the input number 2 goes on, the following request will be performed:

curl -X GET http://10.0.0.200:3030/webhooker/raspberry/?device_id=10.0.0.77&event_type=input&event_number=2&event_value=on

Note: The request will be performed only once, regardless the response code.

Environment Variables

  • DEVICE_ID the device unique identifier (mandatory)
  • FLICK_SLEEP_INTERVAL the sleep interval in seconds when flicking an output (default: 1)
  • NUMBER_OF_INPUTS the number of inputs to be controlled (default: 8)
  • NUMBER_OF_OUTPUTS the number of outputs to be controlled (default: 8)
  • WEBHOOK_URL the URL to be requested when an input/output state changes
  • DEBUG aiohttp server debug (default: False)

Logging

You can setup your logging settings in the logging.conf file.

Read more about Python Logging

Limitations

  • There is no API guard.

TODO

  • Implement API guard.

About

A simple Web API interface to control PiFace Digital inputs and outputs with Webhook support

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages