REST API based Pi controller
This application has been written as a simple REST based GPIO pin controller (on-off switcher) for Raspberry Pi Zero W. As this application uses standard Linux driver for GPIO operations it should works also on other Pi models as well as on other GPIO-enabled SBCs with Linux on board.
Download newest sources directly from GitHub repository:
go get github.com/markamdev/repico
Build using standard go build command.
go build ./
If you're not building on a target platform use necessary architecture and OS settings. Below example for building for RPi Zero:
GOARCH=arm GOOS=linux go build ./
Repico is a standalone application and does not need any additional service to be running. Application options can be passed using command line flags and system environment variables.
Table below lists all supported application options:
System variable | Command line option | Default value | Description |
---|---|---|---|
REPICO_PORT | --repico-port | 8080 | Application listening port |
LOG_LEVEL | --log-level | ERROR | Logging level. Allowed leves are ERROR, DEBUG and VERBOSE |
As repico is a REST based application it can be fully controlled by HTTP request. Use your HTTP client of choice (Insomnia, Postman or even a command line based cURL ) to send command to application.
To enable GPIO pin send HTTP POST request to /v2/gpio endpoint with pin description in JSON content.
Request example for output pin:
curl -X POST -d '{
"pin" : 1,
"direction" : "out"
}' http://locahost:8080/v2/gpio
Request example for input pin:
curl -X POST -d '{
"pin" : 1,
"direction" : "in"
}' http://locahost:8080/v2/gpio
If successfully processed HTTP OK (code 200) is returned.
To disable GPIO pin send HTTP DELETE request to /v2/gpio/{X} endpoint where {X} is a PIN number.
Request example:
curl -X DELETE http://localhost:8080/v2/gpio/1
To set GPIO pin value send HTTP PATCH request to /v2/gpio/{X} endpoint (where {X} is a PIN number) with proper JSON data in body
Request example:
curl -X PATCH -d '{ "value" : 1 }' http://localhost:8080/v2/gpio/1
Please note that in case of pin configured as input it is not possible to set value. In such case API will return HTTP error BadRequest (400).
To get current GPIO pin value send HTTP GET request to /v2/gpio/{X} endpoint (where {X} is a PIN number.
Request example:
curl -X GET http://localhost:8080/v2/gpio/1
Response example:
{
"pin": 1,
"value": 1
}
It is possible to list all exported GPIO pins with their current direction using GET request to main endpoint.
Request example:
curl -X GET http://localhost:8080/v2/gpio
Response example:
[
{
"pin": 1,
"direction": "out"
},
{
"pin": 5,
"direction": "in"
}
]
Unit tests are prepared as a pure Go test (with testing package used). Additionally testify package is used.
To execute unit tests just call command shown below in main repository directory:
go test -v ./...
Code is published under MIT License as it seems to be the most permissive license. If for some reason you need to have this code published with other license (ex. to reuse the code in your project) please contact author directly.
If you need to contact me feel free to write me an email: markamdev.84#dontwantSPAM#gmail.com