From a1e55358b5b26a399c982a27a64a0566173d22d0 Mon Sep 17 00:00:00 2001 From: Antoine Van Elstraete Date: Wed, 13 Jul 2022 22:16:15 +0200 Subject: [PATCH] =?UTF-8?q?Renvoie=20les=20temp=C3=A9ratures=20des=20DS18B?= =?UTF-8?q?20?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server.lua | 75 +++++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 68 insertions(+), 7 deletions(-) diff --git a/server.lua b/server.lua index b6d4290..b72854b 100644 --- a/server.lua +++ b/server.lua @@ -1,9 +1,70 @@ -cs=coap.Server() -cs:listen(5683) +-- vim : set autoindent expandtab tabstop=2 shiftwidth=2 -function hello(payload) - print("hello called") - respond = "hello" - return respond +cs=coap.Server() -- Lance le serveur CoAP +cs:listen(5683) -- sur le port 5683 + +pin = 3 +ow.setup(pin) -- DS18B20 sur le pin 3 + + +function get_data(nbre) + capteurs = {} + temperatures = {} + print("Cherche les capteurs : ") + print(nbre) + addr = ow.reset_search(pin) + for ds = 1, nbre, 1 do + addr = ow.search(pin) + if addr == nil then + pint("Pas de capteurs") + else + print(addr:byte(1,8)) + local crc = ow.crc8(string.sub(addr,1,7)) + if crc == addr:byte(8) then + if (addr:byte(1) == 0x10) or (addr:byte(1) == 0x28) then + print("Device is a DS18S20 family device.") + table.insert(capteurs, addr) + end + end + end + end + ow.reset(pin) + for _, addr in ipairs(capteurs) do + tmr.create():alarm(1500, tmr.ALARM_SINGLE, startup) + ow.reset(pin) + ow.select(pin, addr) + ow.write(pin, 0x44, 1) + tmr.create():alarm(1500, tmr.ALARM_SINGLE, startup) + ow.reset(pin) + ow.select(pin, addr) + ow.write(pin,0xBE,1) + local data = ow.read_bytes(pin, 9) + local crc = ow.crc8(string.sub(data,1,8)) + if crc == data:byte(9) then + local t = (data:byte(1) + data:byte(2) * 256) * 625 + local sgn = t<0 and -1 or 1 + local tA = sgn*t + local t1 = math.floor(tA / 10000) + local t2 = tA % 10000 + temperature = (sgn<0 and "-" or "")..t1.."."..t2 + print("temperature ="..temperature.." C") + local addr1 = addr:byte(1) + local addr2 = addr:byte(2) + local addr3 = addr:byte(3) + local addr4 = addr:byte(4) + local addr5 = addr:byte(5) + local addr6 = addr:byte(6) + local addr7 = addr:byte(7) + local addr8 = addr:byte(8) + local addr0 = string.format("%s:%s:%s:%s:%s:%s:%s:%s", addr1, addr2, addr3, addr4, addr5, addr6, addr7, addr8) + result = addr0.."="..temperature + table.insert(temperatures, result) + end + end + data = table.concat(temperatures,";") + print("data :") + print(data) + return data end -cs:func("hello") + +cs:func("get_data")