I have taken the sample code that is provided by Xively with the electric imp and for some reason I believe either the agent.send is having issues or the device.on is. It was working fine a few hours ago but I don’t know what I did to get this problem.
I have already tried to use the same code as the example and it still does not work. I have also tried to just use only the agent.send and the device.on but it still doesn’t work. I want to believe it is a hardware problem now.
Something else that happened before this problem was that I used up all the mem in the imp which crashed and reset the imp. Not sure if this matters or not but maybe it does.
Here is the agent code:
`
const FEED_ID = “FEED_ID_HERE”;
const API_KEY = “API_KEY_HERE”;
function get_xively() {
//wakeup
imp.wakeup(30.0, get_xively); //schedule when to wakeup again
local xively_url = "https://api.xively.com/v2/feeds/" + FEED_ID + ".json"; //format xively url
server.log(xively_url);
local getreq = http.get(xively_url, {"X-ApiKey":API_KEY, "User-Agent":"xively-Imp-Lib/1.0"}); //add headers
local res = getreq.sendsync(); //actuallty send request
if(res.statuscode != 200) {
server.log("error sending message: " + res.body);
}
server.log("#####RESPONSE#####");
server.log(res.statuscode + " OK"); //status
server.log(res.body); //body
device.send("json", res.body); //send json
}
get_xively(); //initialize first get request. will continue to call itself.
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
}
device.on(“data”, function(feedCSV) { //take csv body in from device
server.log(“device on”);
//send preformatted multi-ds csv
send_xively(feedCSV); //send to function to call xively
});
`
Here is the device code:
`
local index = 0;
local masterUART = array(100); //serial buffer
//these should be changed to non character values
local startbit = 124; //which is |
local endbit = 126; //which is ~
//uart initialization
function startUART()
{
hardware.configure(UART_57);
hardware.uart57.configure(19200, 8, PARITY_NONE, 1, NO_CTSRTS); //baud:19200, dataBits:8, parity, stopbit
}
function checkUART()
{
imp.wakeup(0.001, checkUART.bindenv(this));
local byte = hardware.uart57.read(); //read uart
while (byte != -1)
{
//server.log(byte);
if(byte == endbit){ //check if endbit
local startint = masterUART.find(startbit); //find startbit
if(startint != null){
local fullTrans = masterUART.slice((startint + 1),index); //slice transmission from checkbits
local transString = "";
for(local a=0;a<fullTrans.len();a++){ //convert char array to string
//server.log(fullTrans[a]);
transString = transString + format("%c",fullTrans[a]);
}
agent.send("data", transString); //send string to agent
fullTrans.clear();
masterUART.clear();
index = 0;
}else server.log("UART ERROR");
}else{ //if not endbit, add byte to array
masterUART.insert(index,byte);
index ++;
}
byte = hardware.uart57.read();
}
}
agent.on(“json”, function(value) { //receive data from agent
server.log(“agent is on”);
//server.log(value); //uncomment to print the data first
hardware.uart57.write("\
" + value); //write data to UART
});
agent.on(“status”, function(code) { //this functions send the xively put status code to serial port.
server.log(“agent is on”);
server.log(code);
hardware.uart57.write("
" + code);
});
imp.configure(“Xively UART”, [], []); //standard imp configure statement
startUART(); //setup uart
checkUART(); //begin uart polling
`