hello, I’m with a similar problem on the code.
I`m using the same agent code from tempbug to opena channel and send the data to xively.
What I’m trying to do:
1)Arduino sends data trough softserial (2400) to imp in this format: (ok)
2)Imp gets the data string and splits the string into the individual sensor data (I’m only extracting temp data to make things simpler, after I got temp working I will implement the others: light, soil humidity and air humidity.) Imp reports through serve.log the temp data so it gets a (Ok).
3)Imp sends this temp data to agent (not ok Agent server.log is not shown on log)
4)Agent creates channel and post data to xively (not ok)
Help is much appreciated. how can I fix the float error shown by the log?
This is what I get from the server log:
2014-01-24 16:19:54 UTC-2: [Status] Device booting; 2.81% program storage used 2014-01-24 16:20:49 UTC-2: [Device] Temp:24.60 2014-01-24 16:20:49 UTC-2: [Device] ERROR: float expected for the specified format 2014-01-24 16:20:49 UTC-2: [Device] ERROR: at checkUART:30 2014-01-24 16:20:54 UTC-2: [Status] Device disconnected; 42 bytes sent, 0 received, 42 total
Agent:
`/*
Copyright © 2013 electric imp, inc.
Permission is hereby granted, free of charge, to any person obtaining a copy of this software
and associated documentation files (the “Software”), to deal in the Software without restriction,
including without limitation the rights to use, copy, modify, merge, publish, distribute,
sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial
portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
/*
/* GLOBALS and CONSTANTS -----------------------------------------------------*/
const XIVELY_API_KEY = “YOUR API KEY HERE”;
const XIVELY_FEED_ID = “YOUR FEED ID HERE”;
const XIVELYCHANNEL = “temperature”;
Xively <- {}; // this makes a ‘namespace’
/* CLASS AND GLOBAL FUNCTION DEFINITIONS -------------------------------------*/
// Xively “library”. See https://github.com/electricimp/reference/tree/master/webservices/xively
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() {
return http.jsonencode({id = this.id, current_value = this.current_value });
}
}
function postToXively(data,id) {
xivelyChannel <- Xively.Channel(XIVELYCHANNEL+id);
xivelyChannel.Set(data);
xivelyFeed <- Xively.Feed(XIVELY_FEED_ID, [xivelyChannel]);
local resp = xivelyClient.Put(xivelyFeed);
server.log("Posted to Xively: “+data+”, got return code: “+resp.statuscode+”, msg: "+resp.body);
}
/* REGISTER DEVICE CALLBACKS ------------------------------------------------*/
device.on(“data”, function(datapoint) {
postToXively(datapoint.temp, datapoint.id);
server.log(datapoint)
});
/* REGISTER HTTP HANDLER -----------------------------------------------------*/
// This agent does not need an HTTP handler
/* RUNTIME BEGINS HERE -------------------------------------------------------*/
server.log(“Agente Jardineiro a postos!”);
// instantiate our Xively client
xivelyClient <- Xively.Client(XIVELY_API_KEY);
xivelyChannel <- Xively.Channel(“temperature”);`
Device:
`//uart initialization
function startUART()
{
hardware.configure(UART_1289);
hardware.uart1289.configure(2400, 8, PARITY_NONE, 1, NO_CTSRTS); //baud:19200, dataBits:8, parity, stopbit
}
//uart check
function checkUART()
{
imp.wakeup(5, checkUART);
local c = “”;
local b = hardware.uart1289.read();
while (b != -1)
{
c += b.tochar();
b = hardware.uart1289.read();
}
//TEMPERATURE info from arduino data and send to agent
if (c.len()){
local tStart = c.find(“T”);
tStart = tStart + 1;
local temp = c.slice(tStart);
local tEnd = temp.find(“T”);
temp = temp.slice(0,tEnd);
server.log(“Temp:” + temp);
local id = “Temperatura"
local datapoint = {
“id” : id,
“temp” : format(”%.2f",temp)
}
agent.send(“data”,datapoint);
}
} //checkUART bracket end
startUART(); //setup uart
checkUART(); //begin uart polling
`
Arduino (Partial code, only the function that sends data to imp):
void sendData() { toSend = String(""); char temp[10]; dtostrf(tempNew,1,2,temp); char hum[10]; dtostrf(humidNew,1,2,hum); toSend = toSend(toSend + "L" + luxNew + "L" + "M" + moistNew + "M" + "T" + temp + "T" + "H" + hum + "H"); //Serial.println(toSend); Serial.print("sending"); Serial.println(toSend); softSerial.print(toSend); }