My current code!!!:
Agent code:
`
API_Key <- “***************************"; //Type your Xively API Key
Feed_ID <- "” //Type your Feed ID
Channel_ID <- “WI-FI_strength_RSSI”; //Type your Channel ID for RSSI
Channel_ID2 <- “Internal_ambient_hull_temperature”; //Type your Channel ID for Temp
Xively <- {}; // this makes a 'namespace’
class Xively.Client {
ApiKey = null;
triggers = [];
constructor(apiKey) {
this.ApiKey = apiKey;
}
/*****************************************
- method: PUT
- IN:
- feed: a XivelyFeed we are pushing to
- ApiKey: Your Xively API Key
- OUT:
- HttpResponse object from Xively
- 200 and no body is success
*****************************************/
function Put(feed){
local url = “https://api.xively.com/v2/feeds/” + feed.FeedID + “.json”;
local headers = { “X-ApiKey” : ApiKey, “Content-Type”:“application/json”, “User-Agent” : “Xively-Imp-Lib/1.0” };
local request = http.put(url, headers, feed.ToJson());
return request.sendsync();
}
/*****************************************
- method: GET
- IN:
- feed: a XivelyFeed we fulling from
- ApiKey: Your Xively API Key
- OUT:
- An updated XivelyFeed object on success
- null on failure
*****************************************/
function Get(feed){
local url = “https://api.xively.com/v2/feeds/” + feed.FeedID + “.json”;
local headers = { “X-ApiKey” : ApiKey, “User-Agent” : “xively-Imp-Lib/1.0” };
local request = http.get(url, headers);
local response = request.sendsync();
if(response.statuscode != 200) {
server.log("error sending message: " + response.body);
return null;
}
local channel = http.jsondecode(response.body);
for (local i = 0; i < channel.datastreams.len(); i++)
{
for (local j = 0; j < feed.Channels.len(); j++)
{
if (channel.datastreams[i].id == feed.Channels[j].id)
{
feed.Channels[j].current_value = channel.datastreams[i].current_value;
break;
}
}
}
return feed;
}
}
class Xively.Feed{
FeedID = null;
Channels = null;
constructor(feedID, channels)
{
this.FeedID = feedID;
this.Channels = channels;
}
function GetFeedID() { return FeedID; }
function ToJson()
{
local json = “{ “datastreams”: [”;
for (local i = 0; i < this.Channels.len(); i++)
{
json += this.Channels[i].ToJson();
if (i < this.Channels.len() - 1) json += “,”;
}
json += “] }”;
return json;
}
}
class Xively.Channel {
id = null;
current_value = null;
constructor(_id)
{
this.id = _id;
}
function Set(value) {
this.current_value = value;
}
function Get() {
return this.current_value;
}
function ToJson() {
local json = http.jsonencode({id = this.id, current_value = this.current_value });
server.log(json);
return json;
}
}
temp <- 0;
device.on (“temp”, function(v) {
temp = v;
});
client <- Xively.Client(API_Key);
device.on(“rssi”, function(v) {
channel1 <- Xively.Channel(Channel_ID);
channel1.Set(v);
channel2 <- Xively.Channel(Channel_ID2);
channel2.Set(temp);
feed1 <- Xively.Feed(Feed_ID, [channel1, channel2]);
client.Put(feed1);
});
`
Device code:
`// 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
const b_therm = 3988;
const t0_therm = 298.15;
function getTemp() {
// scale the ADC reading to a voltage by dividing by the full-scale value and multiplying by the supply voltage
local v_therm = 3.3 * hardware.pin9.read() / 65535.0;
// calculate the resistance of the thermistor at the current temperature
local r_therm = 10000.0 / ( (3.3 / 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 f = (t_therm) + 34 ;
// format into a string for the string output port
local f_str = format("%.01f", f)
server.log(“Current temp is “+f_str+” C”);
agent.send(“temp”, f);
agent.send(“rssi”, imp.rssi());
imp.onidle(function() { server.sleepfor(900); }); // 15 minutes
}
imp.configure(“Thermocouple MT Kent”, [], []);
server.log(format(“rssi %ddBm”,imp.rssi()));
getTemp()`
Code is working beautifully but i need one more parameter!!!
Xively feed for battery level! I have currently 3 * AA lithiums totalling 4.5V attached to the BAT tabs on my sparkfun impee and i would like to graph on xively like my temp and RSSI values the state over time of my diminishing battery level.
It would be interesting as im thinking to further my project of showing differing batteries and how long they do or dont last? (Im in deep sleep right now which is great).
Hope someone can help me out as i did see hardware.voltage() but not sure if it applies here???
Thanks
Glenn