I have implemented a simple i2c reader for the HIH-6130, everything seems to work but a couple of observations…
- despite example code (github etc) the address is
(0x27 <<1) not 0x27
.
2.i2c.write((0x27 <<1), "")
returns a value of -4, which isnt documented.
anyway everything works ok as far as I can tell heres the code…
`
// Temperature and Humidity Monitor using Honeywell HIH6130 sensor
//hardware.configure(I2C1_89);
local i2c = hardware.i2c89;
i2c.configure(CLOCK_SPEED_100_KHZ);
local out_temp = OutputPort(“Temperature”);
local out_humid = OutputPort(“Humidity”);
address <- (0x27 << 1);
function main()
{
server.log(“main()…”)
// Start conversion
local e = i2c.write(address, "");
server.log(format("Result: %d", e ));
// wait for 36 ms and then read the result
imp.wakeup( 0.036, getsample )
}
function getsample()
{
server.log(“getsample()…”)
// Read out temperature and humidity, zero length subaddress means the write
// subaddr phase will be totally skipped and we'll just do the read
local th = i2c.read(address, "", 4);
if ( th == null )
{
server.log("I2C.read failed");
return;
}
server.log(format("%02x:%02x:%02x:%02x", th[0], th[1], th[2], th[3]) );
// note data will be marked as stale because each read kicks off another
// cycle as we always do a write-before-read on i2c
// hence, we need to mask top bits of humidity byte
// Form temp and humidity
local temperature = ((((th[2] << 6 ) | (th[3] >> 2)) * 165) / 16383.0) - 40;
local humidity = ((((th[0] & 0x3F) << 8 ) | (th[1] )) / 163.83 );
// log and print
server.log(format("temperature %.2fC humidity %.2f%%",temperature, humidity));
server.show(format("%.2fC %.2f%%", temperature, humidity));
//Send data to server
out_temp.set(temperature);
out_humid.set(humidity);
// wait for next reading
imp.wakeup(10, main)
}
imp.configure(“Temp/Humid”, [],[out_temp, out_humid]);
// Get a sample, then sleep
main();
`