A comprehensive API for managing data from IoT devices, applications, and notifications.
The Peat Data API provides a comprehensive interface for:
- Receiving data from IoT devices via an MQTT server and storing it in the database.
- Allowing mobile applications to retrieve historical data.
- Managing contacts for notifications.
- Administrative functions for data management.
git clone https://github.com/viniciusmecosta/PeatData.git
cd PeatDatapython -m venv .venv
source .venv/bin/activate # Linux/macOS
.venv\Scripts\activate # WindowsCreate a .env file in the root directory of the project with the required environment variables. Use example.env as
a template.
API_TOKEN=your_token_here
MQTT_BROKER=your_broker_here
MQTT_PORT=your_port_here
MQTT_USER=your_user_here
MQTT_PASSWORD=your_password_here
MQTT_TOPIC_SENSOR=your_sensor_topic_here
MQTT_TOPIC_LEVEL=your_level_topic_herepip install -r requirements.txtuvicorn app.main:app --reloadAll API endpoints require Bearer Token authentication. Include the token in the Authorization header of each request.
Authorization: Bearer YOUR_ACCESS_TOKENThe API acts as an MQTT client, subscribing to topics defined in the environment variables (MQTT_TOPIC_SENSOR,
MQTT_TOPIC_LEVEL). It processes incoming messages and stores the data in the database using the paho-mqtt library.
- Language: Python 3.9
- Framework: FastAPI
- ASGI Server: Uvicorn
- ORM: SQLAlchemy
- Data Validation: Pydantic
- MQTT Client: Paho-MQTT
- Database: SQLite
- Containerization: Docker
- Environment Variables: python-dotenv
POST /temp-humi: Submit temperature and humidity data to the database.GET /temp-humi: Retrieve temperature and humidity data based on query parameters (avg,last,days,date).
POST /level: Submit feeder occupation level data to the database.GET /level: Retrieve level data based on query parameters (avg,last,days,date).
POST /phone: Add a new phone number to the database.GET /phone: Retrieve all registered phone numbers.
POST /email: Add a new email address to the database.GET /email: Retrieve all registered email addresses.
POST /generate-temp-humi-data: Generate mock temperature and humidity data.DELETE /temp-humi: Delete all sensor data records.POST /generate-level-data: Generate mock level/distance data.DELETE /level: Delete all level data records.POST /generate-email/{n}: Generatenmock email records.DELETE /email: Delete all email records.POST /generate-phone/{n}: Generatenmock phone records.DELETE /phone: Delete all phone records.
import requests
url = "http://127.0.0.1:8000/temp-humi"
headers = {"Authorization": "Bearer YOUR_ACCESS_TOKEN"}
data = {"temperature": 23.5, "humidity": 60.0}
response = requests.post(url, json=data, headers=headers)
print(response.json())curl -X POST \
http://127.0.0.1:8000/temp-humi \
-H "Authorization: Bearer YOUR_ACCESS_TOKEN" \
-H "Content-Type: application/json" \
-d '{"temperature":23.5,"humidity":60.0}'