1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| local function read_dht12(id) if i2c.setup(id, i2c.SLOW) ~= i2c.SLOW then log.error("I2C.init is: ", "fail") i2c.close(id) return end i2c.send(id, 0x5C, 0x00) local data = i2c.recv(id, 0x5C, 5) i2c.close(id) log.info("DHT12 HEX data: ", data:toHex()) local _, h_H, h_L, t_H, t_L = pack.unpack(data, 'b4') log.info("DHT12 data: ", h_H, h_L, t_H, t_L) local t_L2 = tonumber(t_L) if t_L2 > 127 then return h_H .. ".".. h_L, "-" .. t_H .. "." .. tostring(t_L2 - 128) else return h_H .. ".".. h_L, t_H .. "." .. t_L end end
|