Table of Contents
- dht22-API
- Requirements
- Setup
- GET and POST requests description
- GET request
- POST request
dht22-API
A simple API written in Go for the temperature and humidity dht22 sensor.
It supports GET and POST requests
Requirements
- Go 1.14 or higher
- MongoDB
- mongo-go-driver
OR
Setup
Due to a license
issue many Linux distributions removed MongoDB from their official repositories,
as result it is easier to set up the project with Docker rather
than compiling MongoDB ourselves.
-
Install docker and docker-compose on Arch based distributions
$ pacman -S docker docker-compose
-
Get the source code
$ git clone git@github.com:KNaiskes/dht22-API.git
$ cd dht22-API
-
Run project
-
Start docker
$ sudo systemctl start docker
-
Run project
It will take some time the first time you run this command as it will have to pull
the Go and MongoDB images
$ docker-compose up # You may have to run this command with sudo
Visit localhost:8080/measurements
to verify that everything work as they should, you are expected to see an empty list
GET and POST requests description
GET request
Path |
Result |
/measurements |
Returns a list with all measurements or an empty list |
/measurements/{id} |
Returns the one measurement with the specified {id} or 404 |
/measurements/{name} |
Returns all measurements from the sensor with the specified {name} or an empty list |
POST request
POST request consists of three optional parameters (id is assigned automatically by the server).
Example of POST request with cURL
curl -d '{
"name":"basement",
"temperature":"17.25",
"humidity":"21.33"
}' -H "Content-Type: application/json" -X POST http://localhost:8080/measurements