Skip to main content

01 - Application "pont" BDD

Parc MySQL icon – MQTT mosquitto icon → MySQL Bridge python icon

A lightweight Python service that bridges a Mosquitto MQTT broker to a MySQL database for the Parc greenhouse monitoring system.

Programme génére à l'aide de ClaudeCode claude icon


Architecture

[IoT sensors / controleurs]
        │  MQTT  serre/X/bac/Y
        ▼
 ┌─────────────┐         ┌──────────────────────┐
 │  Mosquitto  │ ───────▶│  bridge.py (Python)  │ ──▶  MySQL (external)
 │  (broker)   │         │  subscribe + persist │
 └─────────────┘         └──────────────────────┘
   Docker container             Docker container

Both containers share a private Docker network (parc-net). The MySQL server is external (managed separately).


MQTT topics

PatternDescription
serre/<numero>/bac/<numero>Measurements from bac <numero> inside serre <numero>

Payload format (JSON)

{ "humiditeAmbiante": 65.3, "temperatureAmbiante": 22.1 }

Multiple sensor keys can coexist in a single payload.

Supported sensors

JSON keyid_capteur
humiditeAmbiante1
humiditeSol2
temperatureAmbiante3

Error handling

All errors are persisted to the error table with one of these type_erreur values:

CodeTrigger
BAC_NOT_FOUNDSerre or bac number not in the database
UNKNOWN_SENSORSensor key not in the known list
INVALID_PAYLOADJSON parse error or non-object payload
INVALID_VALUENon-numeric value for a sensor
VALUE_OUT_OF_RANGEValue clamped to sensor min/max
INVALID_ENCODINGNon-UTF-8 payload
MQTT_DISCONNECTUnexpected broker disconnection
DB_ERRORTransient database query failure
DB_INSERT_ERRORFailed to insert a mesure row
UNEXPECTED_ERRORUnhandled exception (catch-all)

When a value is out of range, the clamped value (min or max) is written to mesure and an error row is inserted.


Quick start

1. Prerequisites

  • Docker ≥ 24 with Compose plugin
  • An accessible MySQL / MariaDB server with the parc schema applied

2. Configuration

cp .env.example .env
# Edit .env and fill in DB_HOST, DB_USER, DB_PASSWORD, etc.

3. Start

docker compose up -d --build

4. Verify

# Follow bridge logs
docker compose logs -f bridge

# Test with a dummy publish
docker run --rm --network parc-net eclipse-mosquitto:2 \
  mosquitto_pub -h mosquitto -t "serre/1/bac/1" \
  -m '{"humiditeAmbiante": 65, "temperatureAmbiante": 22}'

Environment variables

VariableDefaultDescription
DB_HOST(required)MySQL host
DB_PORT3306MySQL port
DB_NAMEparcDatabase name
DB_USER(required)MySQL user
DB_PASSWORD(required)MySQL password
MQTT_HOSTmosquittoBroker hostname
MQTT_PORT1883Broker port
MQTT_KEEPALIVE60MQTT keepalive (s)
MQTT_USER(empty)Broker username (optional)
MQTT_PASSWORD(empty)Broker password (optional)
DB_RETRY_DELAY5Seconds between DB retries
MQTT_RETRY_DELAY5Seconds between MQTT retries
LOG_LEVELINFODEBUG/INFO/WARNING/ERROR

File layout

.
└── mqtt-bridge
    ├── bridge.py
    ├── docker-compose.yml
    ├── Dockerfile
    ├── mosquitto
    │   ├── client-certs
    │   │   ├── client.crt
    │   │   ├── client.csr
    │   │   └── client.key
    │   ├── clients-certs.sh
    │   ├── config
    │   │   ├── mosquitto.conf
    │   │   └── mosquitto.conf.bak
    │   ├── server-certs
    │   │   ├── ca.crt
    │   │   ├── ca.key
    │   │   ├── ca.srl
    │   │   ├── server.crt
    │   │   ├── server.csr
    │   │   └── server.key
    │   ├── server-certs.sh
    │   └── v3.ext
    ├── README.md
    └── requirements.txt