Skip to content

mikeleppane/mqtt-fastapi

Repository files navigation

FastAPI/MQTT - Continuous Integration

FastAPI and MQTT

What it is

A Python application that subscribes to an MQTT topic, logs the received messages and persists them. The application also exposes an api endpoint to get all the messages.

How to use it

The project runs inside a docker container. Make sure you have Docker and Docker Compose installed. After that simply run the following command in the project root:

docker compose up -d

This will expose the backend (fastapi) a host/port at

http://0.0.0.0:8800

In addition, MQTT broker is exposed at host/port

http://0.0.0.0:1883

By default, the subscription service listens to a topic humidity/outside but user can change this with TOPIC environment variable (see docker-compose.yml file).

You can use e.g. MQTT X to publish a message to a topic on a broker.

REST API

Health check

GET /v1/health_check

Checks that the app is ok.

Response payload:

{
  "message": "OK"
}

Messages

GET /v1/messages

Parameters
Name Description
limit how many messages to return; default value: 100
offset define a offset from the first item; default value: 0

Get all stored messages ordered by timestamp

Example response payload:

[
  {
    "created_at": "2023-06-05T10:01:08.587763",
    "payload": {"humidity": 5.66}
  },
  {
    "created_at": "2023-06-05T09:59:21.040788",
    "payload": {"humidity": 0.75}
  }
]

Development

The project contains a separate docker compose file a local development.

docker compose -f docker-compose.dev.yml up -d

The Makefile contains a few targets to help the development for instance:

Run the tests:

docker compose exec backend make test

Run the checks

docker compose exec backend make check

Used Tech

About

Python application for interacting MQTT

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published