Skip to content

Instantly share code, notes, and snippets.

@imp-guru
Last active August 29, 2015 14:06
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 imp-guru/85bf174478bcdad473dc to your computer and use it in GitHub Desktop.
Save imp-guru/85bf174478bcdad473dc to your computer and use it in GitHub Desktop.
Electric Imp Agent Code for Si7020 Breakout Board w/ upload to data.sparkfun.com
// Create a data stream at data.sparkfun.com, then enter your keys here:
local publicKey = "8dX9xNk1w3sOv044oz1m" // These are fake codes, please enter yours
local privateKey = "pzbnrbq6XvCzWn556pDE" // These are fake codes, please enter yours
function sample(params) {
// Extract bits from the data sent by the agent
// Combine the 2 bytes of data from Si7020 into a single uint16
// then convert to float. Do this for both temp and humidity
local rh_code = (params[0]*256+params[1]).tofloat();
local temp_code = (params[2]*256+params[3]).tofloat();
// Use the formula on page 23 of the Si7020 Datasheet to convert the temp_code
// to temperature in degrees C
local temp_c = 175.72*temp_code/65536-46.85;
// For those of us yanks, convert to fahrenheit
local temp_f = temp_c*9/5+32;
// Use the formula on page 22 of the Si7020 Datasheet to convert the rh_code
// to humidity in %
local humidity = 125*rh_code/65536-6;
// Prepare the phant headers
local phantHeaders = {"Phant-Private-Key": privateKey, "connection": "close"};
// Create a post request
local req = http.post("https://data.sparkfun.com/input/"+publicKey, phantHeaders,"humidity="+humidity+"&temp="+temp_f);
//Send out the request!
req.sendsync();
}
device.on("sample",sample);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment