My Imp has been running for nearly a year - but had a couple of ‘outs’ for a few days over the last month or three.
So I visited the dev centre for the first time today …Not having seen the dramatic changes to the interface (and function ?) - for a while.
I have an IMP which was sending temp data to Xively. Having visited the dev centre, its now not working.
The IMP code stills runs happily, but the agent code doesnt seem to post to Xively.
I cant find a simple explanation of the ‘handshake’ between the two - The old interface made it clear by visually linking the agent to the imp…and I clearly dont understand whats going on now.
My code is now a combination of agent (from the Xively tutorial); and Imp code (from a very old tutorial). The two dont seem to work together.
Can someone explain how I get the IMP to fire the agent successfully please.
(BTW, when I click on the agent URL at the top of the dev window, I get ‘No HTTP Handler’. - if that helps!
//Agent
const FEED_ID = “7311261”;
const API_KEY = “5F5X01182736646123HHHHH6D”;
function send_xively(body) { //take in csv value
local xively_url = “https://api.xively.com/v2/feeds/” + FEED_ID + “.csv”; //setup url for csv
server.log(xively_url);
server.log(body); //pring body for testing
local req = http.put(xively_url, {“X-ApiKey”:API_KEY, “Content-Type”:“text/csv”, “User-Agent”:“Xively-Imp-Lib/1.0”}, body); //add headers
local res = req.sendsync(); //send request
if(res.statuscode != 200) {
server.log("error sending message: “+res.body);
}else device.send(“status”, (res.statuscode + " OK”)); //sends status to uart. this can be removed if not desired
}
server.log(“device on”);
//send preformatted multi-ds csv
send_xively(feedCSV); //send to function to call xively
});
//IMP Code
// April with a 10k, 1% from 3V3 to pin9 and 10k B57861S0103F040 NTC Thermistor from pin9 to pin8
// pin8 TEMP_READ_EN_L - drive low to enable temp reading (great for batteries!)
// pin9 ANALOG NTC value
// turn on WiFi power save to reduce power consumption when awake
imp.setpowersave(true);
// Output structure for sending temperature to server
local tempOut = OutputPort(“Temperature ©”, “number”);
// Configure Pins
// pin 8 is driven high to turn off temp monitor (saves power) or low to read
hardware.pin8.configure(DIGITAL_OUT);
hardware.pin8.write(1);
// pin 9 is the middle of the voltage divider formed by the NTC - read the analog voltage to determine temperature
hardware.pin9.configure(ANALOG_IN);
// all calculations are done in Kelvin
// these are constants for this particular thermistor; if using a different one,
// check your datasheet
const b_therm = 3560;
const t0_therm = 294.15; //Code modified to offset (-4) to calibrate actual sensor
function getTemp() {
// turn on the thermistor network
hardware.pin8.write(0);
// gather several ADC readings and average them (just takes out some noise)
local val = 0;
for (local i = 0; i < 10; i++) {
imp.sleep(0.01);
val += hardware.pin9.read();
}
val = val/10;
// turn the thermistor network back off
hardware.pin8.write(1);
// to read the battery voltage reliably, we take 10 readings and average them
local v_high = 0;
for(local i = 0; i < 10; i++){
imp.sleep(0.01);
v_high += hardware.voltage();
}
v_high = v_high / 10.0;
// scale the ADC reading to a voltage by dividing by the full-scale value and multiplying by the supply voltage
local v_therm = v_high * val / 65535.0;
// calculate the resistance of the thermistor at the current temperature
local r_therm = 10000.0 / ( (v_high / v_therm) - 1);
local ln_therm = math.log(10000.0 / r_therm);
local t_therm = (t0_therm * b_therm) / (b_therm - t0_therm * ln_therm) - 273.15;
// convert to fahrenheit for the less-scientific among us
local c = t_therm //(t_therm) * 9.0 / 5.0 + 32.0;
// format into a string for the string output port
local c_str = format("%.01f", c)
server.log("Current temp is " + c_str + " C");
// emit values to our output port
tempOut.set(c_str);
imp.wakeup(30, getTemp);
}
agent.on(“status”, function(code) {
server.log(“agent is on”);
server.log(code);
});
// Configure on planner and register with imp server
imp.configure(“Jons Thermometer”, [], [tempOut]);
getTemp();