Last active
December 19, 2015 07:39
-
-
Save abl/5920627 to your computer and use it in GitHub Desktop.
Smartmaker's temperature example.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
imp.configure("Temperature Logger", [], []); | |
const i2c_temp = 0x92; | |
hardware.configure(I2C_89); | |
local i2c = hardware.i2c89; | |
function ShowTemperature(){ | |
//Poll the temperature every n seconds | |
imp.wakeup(5,ShowTemperature); | |
i2c.write(i2c_temp, "\x01\xE1\x30"); | |
imp.sleep(0.050); //50 ms vs rated time of 26 ms | |
local result = i2c.read(i2c_temp, "\x00", 2); | |
local t; | |
if(result[1] & 1) { //Extended, 13-bit, mode | |
//server.log("[DEBUG] Received 13-bit extended mode value"); | |
if(result[0] & 0x80) { | |
//Extend the 13-bit two's complement | |
//server.log("[DEBUG] Received negative extended mode value"); | |
t = 0xFFFFE000 | ((result[0] << 5) | (result[1] >> 3)); | |
} else { | |
t = ((result[0] << 5) | (result[1] >> 3)); | |
} | |
} else { | |
//server.log("[DEBUG] Received 12-bit normal mode value"); | |
if(result[0] & 0x80) { | |
//Extend the 12-bit two's complement | |
//server.log("[DEBUG] Received negative normal mode value"); | |
t = 0xFFFFF000 | ((result[0] << 4) | (result[1] >> 4)); | |
} else { | |
t = ((result[0] << 4) | (result[1] >> 4)); | |
} | |
} | |
//local temp = t * 0.0625; //Celsius | |
local temp = t * 0.1125 + 32; //Fahrenheit | |
agent.send("temperature", temp); | |
} | |
// Configure the tmp112 for one-shot reading (shutdown mode) | |
i2c.write(i2c_temp, "\x01\x61\x20"); | |
imp.sleep(0.1); //100msec to reconfigure | |
ShowTemperature(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment