Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@voodootikigod
Last active December 12, 2015 02:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save voodootikigod/7b2f4076bf1b0d519708 to your computer and use it in GitHub Desktop.
Save voodootikigod/7b2f4076bf1b0d519708 to your computer and use it in GitHub Desktop.
// 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");
imp.configure("Temp/Humid", [],[out_temp, out_humid]);
address <- 0x27;
local function getsample() {
// Start conversion
i2c.write(address, "");
// Wait at least 36ms
imp.sleep(0.05);
// 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);
server.log(th);
// 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);
}
// Need to do this just once to configure ATSHA: nora only!
//hardware.configureimpee({ pin3v3 = 1 });
//hardware.configureimpee({ polarity3v3 = 1 });
// Get a sample, then sleep
getsample();
server.sleepfor((5*60)-3);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment