I can’t really code and I’m on a very long holiday and since I have been postponing this project 3 times this year, I am pulling all nighter. So lack of sleep has affected my brain’s ability to process the code…
I am having trouble figuring it out why Xively is not getting the data. Please help me out… (I have put two different codes (agent one and device one) from two different tutorials done by two different people so no wonder why I am having an issue.)
This is my Agent code:
`API_Key <- “**"; //Type your Xively API Key
Feed_ID <- "” //Type your Feed ID
Channel_ID <- “Thermometer”; //Type your Channel ID
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;
}
}
client <- Xively.Client(API_Key);`
This is my device code:
`hardware.pin1.configure(ANALOG_IN);
// LED pin config
hardware.pin2.configure(DIGITAL_OUT_OD_PULLUP);
// initially set to off
hardware.pin2.write(0);
/*
If you don’t have a Hannah board but want to try the
Cosm.com temperature logging exercise, here it is!
temperature sensor used is tmp36 from adafruit.com for $2.00
http://learn.adafruit.com/tmp36-temperature-sensor
temp range -40°C to 150°C / -40°F to 302°F
*/
local output = OutputPort(“temp”,“number”);
local postInit = false;
local oldTemp = null;
local reading = null;
local ratio = null;
local voltage = null;
local temperatureC = null;
local temp = null;
function update_temp() {
// keep the imp awake
imp.wakeup(10, update_temp);
// get the raw voltage value from temp sensor btw 0-65535
// in this case that needs mapping to the range 0-3.3v
reading = hardware.pin1.read();
// get the ratio
ratio = 65535.0 / reading;
// make units milivolts and get voltage we can work with
//voltage = (hardware.voltage() * 1000) / divider;
voltage = 3300 / ratio;
// get temperature in degrees Celsius
temperatureC = (voltage - 500) / 10.0;
server.log(postInit);
server.log("temp: " + temperatureC + “°C”);
// set our output to desired temperature unit
temp = temperatureC;
// after function’s first run set flag to true
postInit = true;
}
server.show(temp);
// Register with the server
imp.configure(“Tempreature Logger V2.0”, [], [output]);
update_temp();`