01 - Application "pont" BDD
Parc
– MQTT
→ MySQLApp Bridge"pont" 
AUn lightweightprogramme Pythonfiable serviceet thatefficace bridgesqui afait le pont entre le broker MQTT Mosquitto MQTTet brokerla tobase ade données MySQL
databasedu for the Parc greenhouse monitoring system.projet.
Programme génére à l'aide de ClaudeCode ![]()
Architecture
[IoT sensors / controleurs]
│ MQTT serre/X/bac/Y
▼
┌─────────────┐ ┌──────────────────────┐
│ Mosquitto │ ───────▶│ bridge.py (Python) │ ──▶ MySQL (external)
│ (broker) │ │ subscribe + persist │
└─────────────┘ └──────────────────────┘
Docker container Docker container
BothLes containers2 shareconteneurs apartages privatele Dockermême networkréseau docker (parc-net).
TheLe serveur MySQL servern'est ispas externalgéré (managedsur separately).cette machine.
MQTT topics
| Description | |
|---|---|
serre/< |
|
PayloadFormat formatdu payload (JSON)
{ "temperatureAmbiante": 28.5 }
{ "humiditeAmbiante": 65.3, "temperatureAmbiante": 22.1 }
{ "humiditeAmbiante": 65.3, "temperatureAmbiante": 22.1, "humiditeSol": 58.6 }
MultipleChaque sensorpayload keyspeut cancontenir coexistune inseule aou singleplusieurs payload.mesures.
Supported sensorsCapteurs
| Clé JSON |
id_capteur |
|---|---|
humiditeAmbiante |
1 |
humiditeSol |
2 |
temperatureAmbiante |
3 |
ErrorSupport handlingdes erreurs
AllL'intégralité errorsdes areerreurs persistedsont toenregistré thedans la table error avec chacune une valeure dans la table with one of these type_erreur values::
| Code | |
|---|---|
BAC_NOT_FOUND |
|
UNKNOWN_SENSOR |
|
INVALID_PAYLOAD |
Erreur de format du JSON |
INVALID_VALUE |
|
VALUE_OUT_OF_RANGE |
|
INVALID_ENCODING |
Non-UTF-8 payload |
MQTT_DISCONNECT |
|
DB_ERROR |
|
DB_INSERT_ERROR |
|
UNEXPECTED_ERROR |
WhenQuand aune valuevaleur isest outhors ofplage range,du thecapteur, clampedle valueminimum (min/ ormaximum max)est isécrit writtenen tobase de donné mesureandet anun errorligne rowd'erreur isest inserted.inséré.
QuickVariables start
1. Prerequisites
Docker ≥ 24 with Compose pluginAn accessible MySQL / MariaDB server with theparcschema 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 variablesd'environment
| Variable | Description | |
|---|---|---|
DB_HOST |
( |
hébergeur MySQL |
DB_PORT |
3306 |
port du serveur MySQL |
DB_NAME |
parc |
|
DB_USER |
( |
utilisateur MySQL |
DB_PASSWORD |
( |
mot de passe MySQL |
MQTT_HOST |
mosquitto |
nom de l'hébergeur Broker |
MQTT_PORT |
1883 |
port du Broker |
MQTT_KEEPALIVE |
60 |
MQTT keepalive ( |
MQTT_USER |
( |
utilisateur du Broker |
MQTT_PASSWORD |
( |
mot de passe du Broker |
DB_RETRY_DELAY |
5 |
|
MQTT_RETRY_DELAY |
5 |
|
LOG_LEVEL |
INFO |
DEBUG/INFO/WARNING/ERROR |
FileArborescence layoutdes fichiers
.
└── 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