This commit is contained in:
2026-04-16 21:08:42 +02:00
commit e3524454f0
5 changed files with 127 additions and 0 deletions

2
.gitignore vendored Normal file
View File

@@ -0,0 +1,2 @@
venv/
db-data/

25
docker-compose.yml Normal file
View File

@@ -0,0 +1,25 @@
version: '3.8'
services:
db:
image: mariadb:10.6
container_name: mysql_poubelles
restart: always
environment:
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: smart_trash
MYSQL_USER: web_user
MYSQL_PASSWORD: poubelle2026
ports:
- "3306:3306"
volumes:
- ./install.sql:/docker-entrypoint-initdb.d/1_install.sql
- ./test_data.sql:/docker-entrypoint-initdb.d/2_test_data.sql
- ./db-data:/var/lib/mysql
networks:
default:
name: smart_bins_networks

38
install.sql Normal file
View File

@@ -0,0 +1,38 @@
-- Table poubelles
CREATE TABLE poubelles (
id INT AUTO_INCREMENT PRIMARY KEY,
nom VARCHAR(100) NOT NULL,
latitude DOUBLE NOT NULL,
longitude DOUBLE NOT NULL,
statut ENUM('actif', 'maintenance') DEFAULT 'actif',
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
-- Table mesures (historique complet)
CREATE TABLE mesures (
id INT AUTO_INCREMENT PRIMARY KEY,
id_poubelle INT NOT NULL,
niveau INT NOT NULL,
poids DECIMAL(6,2) NOT NULL,
temperature DECIMAL(5,2) NOT NULL,
date_mesure TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (id_poubelle) REFERENCES poubelles(id) ON DELETE CASCADE
);
-- Table alertes
CREATE TABLE alertes (
id INT AUTO_INCREMENT PRIMARY KEY,
id_poubelle INT NOT NULL,
type_alerte VARCHAR(50) NOT NULL,
statut ENUM('active', 'resolue') DEFAULT 'active',
date_creation TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (id_poubelle) REFERENCES poubelles(id)
);
-- Table utilisateurs
CREATE TABLE utilisateurs (
id INT AUTO_INCREMENT PRIMARY KEY,
username VARCHAR(50) UNIQUE,
password VARCHAR(255),
role ENUM('admin','user') DEFAULT 'user'
);

49
mqtt_to_db.py Normal file
View File

@@ -0,0 +1,49 @@
import paho.mqtt.client as mqtt
import mysql.connector
import json
print("projet lance...")
db = mysql.connector.connect(
host="127.0.0.1",
user="web_user",
password="poubelle2026",
database="smart_bins"
)
cursor = db.cursor()
def on_message(client, userdata, msg):
try:
data = json.loads(msg.payload.decode())
print("recu :", data)
sql = """
INSERT INTO mesures (id_poubelle, niveau, poids, temperature, batterie)
VALUES (%s, %s, %s, %s, %s)
"""
values = (
data.get("id_poubelle", 1),
data.get("niveau", 0),
data.get("poids",0),
data.get("temperature", 0),
data.get("batterie", 100)
)
cursor.execute(sql, values)
db.commit()
print("Enregistre en DB")
except Exception as e:
print("Erreur :", e)
client = mqtt.Client()
client.connect("localhost",1883, 60)
client.subscribe("smart_trash/data")
client.on_message = on_message
print("En attente des donnees MQTT...")
client.loop_forever()

13
test_data.sql Normal file
View File

@@ -0,0 +1,13 @@
USE smart_bins;
INSERT INTO bins (bin_label, latitude, longitude, empty_weight_kg, max_capacity_liters) VALUES
('Poubelle_Rue_Principale', 48.8584, 2.2945, 12.00, 100),
('Poubelle_Parc_Nord', 48.8600, 2.3000, 10.50, 80),
('Poubelle_Centre_Commercial', 48.8500, 2.3100, 15.00, 120);
INSERT INTO measurements (bin_id, total_weight_kg, fill_level_percent, temperature, battery_level) VALUES
(1, 45.50, 85, 22.1, 95),
(2, 15.00, 20, 19.5, 88),
(3, 60.00, 50, 24.0, 100);