ESP8266 07 -> HIH 6130 temp and humidity sensor
Posted: Tue Jul 28, 2015 5:58 am
i have this code and i don't know why don't show me the right measurements. it gives me 125 and 1
hih6130={
dev_addr = 0x27,
id=0,
init = function (self, sda, scl)
self.id = 0
i2c.setup(self.id, sda, scl, i2c.SLOW)
end,
read_reg = function(self, dev_addr)
i2c.start(self.id)
aa=i2c.address(self.id, dev_addr ,i2c.TRANSMITTER)
i2c.stop(self.id)
tmr.delay(36)
i2c.start(self.id)
i2c.address(self.id, dev_addr,i2c.RECEIVER)
c=i2c.read(self.id,2)
i2c.stop(self.id)
print(aa)
return c
end,
readTemp = function(self)
h, l = string.byte(self:read_reg(0x27, 0xD), 1, 2)
h1=bit.lshift(h,8)
rawtemp = bit.band(bit.bor(h1,l),0xfffc)
tp1 = ((rawtemp/16383)* 165 ) - 40
print(tp1)
return tp1
end,
readHum = function(self)
h,l = string.byte(self:read_reg(0x27, 0xd), 1, 2)
h1=bit.lshift(h,8)
rawhum = bit.band(bit.bor(h1,1),0xfffc)
hum = (rawhum/16383)
print(hum)
return hum
end
}
sda=6 --GPIO12
scl=7 --GPIO13
hih6130:init(sda,scl)
tp1 = hih6130:readTemp()
hum = hih6130:readHum()
srv=net.createServer(net.TCP)
srv:listen(80,
function(conn)
conn:on("receive",function(conn,payload) print(payload)
conn:send("HTTP/1.1 200 OK\n\n")
conn:send("<META HTTP-EQUIV=\"REFRESH\" CONTENT=\"5\">")
conn:send("<html><title>hih6130 - Temperature Log Server - ESP8266</title><body>")
conn:send("<h1>ESP8266 Temperature Log Server - hih6130</h1><BR>")
conn:send("Temperature : <b>" .. tp1 .. "°C</b><BR><BR>")
conn:send("Humidity : <b>" .. hum .. " %</b><BR><BR>")
conn:send("Node.HEAP : <b>" .. node.heap() .. "</b><BR><BR>")
conn:send("IP ADDR : <b>".. wifi.sta.getip() .. "</b><BR>")
conn:send("Node MAC : <b>" .. wifi.sta.getmac() .. "</b><br>")
conn:send("TMR.NOW : <b>" .. tmr.now() .. "</b><BR<BR><BR>")
conn:send("</html></body>")
conn:on("sent",function(conn) conn:close() end)
end)
end)
any ideas?
hih6130={
dev_addr = 0x27,
id=0,
init = function (self, sda, scl)
self.id = 0
i2c.setup(self.id, sda, scl, i2c.SLOW)
end,
read_reg = function(self, dev_addr)
i2c.start(self.id)
aa=i2c.address(self.id, dev_addr ,i2c.TRANSMITTER)
i2c.stop(self.id)
tmr.delay(36)
i2c.start(self.id)
i2c.address(self.id, dev_addr,i2c.RECEIVER)
c=i2c.read(self.id,2)
i2c.stop(self.id)
print(aa)
return c
end,
readTemp = function(self)
h, l = string.byte(self:read_reg(0x27, 0xD), 1, 2)
h1=bit.lshift(h,8)
rawtemp = bit.band(bit.bor(h1,l),0xfffc)
tp1 = ((rawtemp/16383)* 165 ) - 40
print(tp1)
return tp1
end,
readHum = function(self)
h,l = string.byte(self:read_reg(0x27, 0xd), 1, 2)
h1=bit.lshift(h,8)
rawhum = bit.band(bit.bor(h1,1),0xfffc)
hum = (rawhum/16383)
print(hum)
return hum
end
}
sda=6 --GPIO12
scl=7 --GPIO13
hih6130:init(sda,scl)
tp1 = hih6130:readTemp()
hum = hih6130:readHum()
srv=net.createServer(net.TCP)
srv:listen(80,
function(conn)
conn:on("receive",function(conn,payload) print(payload)
conn:send("HTTP/1.1 200 OK\n\n")
conn:send("<META HTTP-EQUIV=\"REFRESH\" CONTENT=\"5\">")
conn:send("<html><title>hih6130 - Temperature Log Server - ESP8266</title><body>")
conn:send("<h1>ESP8266 Temperature Log Server - hih6130</h1><BR>")
conn:send("Temperature : <b>" .. tp1 .. "°C</b><BR><BR>")
conn:send("Humidity : <b>" .. hum .. " %</b><BR><BR>")
conn:send("Node.HEAP : <b>" .. node.heap() .. "</b><BR><BR>")
conn:send("IP ADDR : <b>".. wifi.sta.getip() .. "</b><BR>")
conn:send("Node MAC : <b>" .. wifi.sta.getmac() .. "</b><br>")
conn:send("TMR.NOW : <b>" .. tmr.now() .. "</b><BR<BR><BR>")
conn:send("</html></body>")
conn:on("sent",function(conn) conn:close() end)
end)
end)
any ideas?