commit e3524454f07944592b170e8baa7989bd14b26f44 Author: Enzo Date: Thu Apr 16 21:08:42 2026 +0200 1 diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..445d399 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +venv/ +db-data/ diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..e2f66c8 --- /dev/null +++ b/docker-compose.yml @@ -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 diff --git a/install.sql b/install.sql new file mode 100644 index 0000000..573984b --- /dev/null +++ b/install.sql @@ -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' +); diff --git a/mqtt_to_db.py b/mqtt_to_db.py new file mode 100644 index 0000000..cb33f29 --- /dev/null +++ b/mqtt_to_db.py @@ -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() diff --git a/test_data.sql b/test_data.sql new file mode 100644 index 0000000..3d51ba5 --- /dev/null +++ b/test_data.sql @@ -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); \ No newline at end of file