From a3e518724cf93cd0a5fc9aa598224bfaa0ad6720 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javier=20Y=C3=A1=C3=B1ez?= Date: Mon, 2 Feb 2015 09:59:20 +0100 Subject: [PATCH] Support for versions of NodeMCU with float point Read error is indicated with humidity = nil --- lua_modules/dht22/dht22.lua | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/lua_modules/dht22/dht22.lua b/lua_modules/dht22/dht22.lua index d2374050..b7b04092 100644 --- a/lua_modules/dht22/dht22.lua +++ b/lua_modules/dht22/dht22.lua @@ -77,15 +77,17 @@ function M.read(pin) end end - checksumTest=((humidity / 256) + (humidity % 256) + (temperature / 256) + (temperature % 256)) % 256 + checksumTest = (bit.band(humidity, 0xFF) + bit.rshift(humidity, 8) + bit.band(temperature, 0xFF) + bit.rshift(temperature, 8)) + checksumTest = bit.band(checksumTest, 0xFF) if temperature > 0x8000 then -- convert to negative format temperature = -(temperature - 0x8000) end - if checksum ~= checksumTest then - humidity = -1 + -- conditions compatible con float point and integer + if (checksumTest - checksum >= 1) or (checksum - checksumTest >= 1) then + humidity = nil end end