From 7a9860a3bb29e1232749978f51d7ac0eea0de0dd Mon Sep 17 00:00:00 2001 From: Lukian Date: Thu, 19 Dec 2024 12:05:25 +0100 Subject: [PATCH] Saataa andagii ! --- main.lua | 9 +++++++++ mqtt.lua | 38 +++++++++++++++++++++++++++++++++++--- 2 files changed, 44 insertions(+), 3 deletions(-) diff --git a/main.lua b/main.lua index fb1cbf4..0069fc8 100755 --- a/main.lua +++ b/main.lua @@ -66,6 +66,15 @@ function avg(tab) return sum / #tab end +function split(text, splitter) + local start, _end = text:find(splitter) + if not start then + return text, "" + end + return text:sub(1, start - 1), text:sub(_end + 1) +end + + -- charge tout nos fichiers lua dofile("leds.lua") dofile("buzz.lua") diff --git a/mqtt.lua b/mqtt.lua index 5d4dd4a..bc22c53 100644 --- a/mqtt.lua +++ b/mqtt.lua @@ -1,4 +1,4 @@ -client = mqtt.client("ESP32", "192.168.1.76", 1883, false) +client = mqtt.client("ESP32", "192.168.0.11", 1883, false) client:connect("","") function printMaster(length, msg) @@ -9,8 +9,40 @@ function printAlt(length, msg) console("[ALT]"..msg) end +max_temp = 0 +min_temp = 100 +max_place = "" +min_place = "" + +function printBuildingTemp(length, msg) + local temp_str, place = pack.unpack(msg) + local temp = tonumber(temp_str) + if temp > max_temp then + max_temp = temp + max_place = place + end + if temp < min_temp then + min_temp = temp + min_place = place + end + console("Max : ["..max_place.."] "..max_temp) + console("Min : ["..min_place.."] "..min_temp) +end + +function sendTemp() + s = sensor.attach("DHT22", pio.GPIO21) + tmr.delayms(500) + while true do + client:publish("Building/temp", pack.pack(s:read("temperature"), "Salon"), mqtt.QOS0) + tmr.delayms(5000) + end +end + client:subscribe("MASTER", mqtt.QOS0, printMaster) client:subscribe("ALT", mqtt.QOS0, printAlt) -client:publish("MASTER", "Hello, world!", mqtt.QOS0) -client:publish("ALT", "Hello, world!", mqtt.QOS0) +-- client:publish("MASTER", "Hello, world!", mqtt.QOS0) +-- client:publish("ALT", "Hello, world!", mqtt.QOS0) + +temp_sender = thread.start(sendTemp) +client:subscribe("Building/temp", mqtt.QOS0, printBuildingTemp)