WiFly and Pachube sprinf question

Good morning,

Can someone please help a bit here ? Many thanks.

I am trying to understand this syntax: sprintf(buff,"0,%d\n1,%d",i++,analogRead(0));
It is converting analogRead to a string in a specific format. It does work, but I failed to send multiple sensors with the same command line. Obviousely, at this stage I don' understand it very well. =(

cheers,
MaLi

Try posting all your code. Its no good asking us to fix code that works!

Mark

It is converting analogRead to a string in a specific format.

Yes, it is.

but I failed to send multiple sensors with the same command line.

Because that code is accessing a specific pin. You can't connect multiple sensors to the same pin.

Obviousely, at this stage I don' understand it very well.

Nor did you do a good job of sharing your code.

Good morning,

Can someone please help a bit here ? Many thanks.

I am trying to understand this syntax: sprintf(buff,"0,%d\n1,%d",i++,analogRead(0));
It is converting analogRead to a string in a specific format. It does work, but I failed to send multiple sensors with the same command line. Obviousely, at this stage I don' understand it very well.

sprintf(buff,"%d,%d,%d,%d\n",i++,analogRead(0),analogRead(1),analogRead(2));

output

0,3,1015,970

To understand sprintf try this
http://www.cplusplus.com/reference/cstdio/sprintf/

And here you can see all the formatting parameters: http://www.cplusplus.com/reference/cstdio/printf/

wow, many thanks to everyone for the quick answers.

The line is updating a pachube feed in csv format. I had it working in this format last year using a wired arduino ethernet interface (5100).

int temp_Current1 = (temp_Current - (int)temp_Current)*100;
int pH_val1 = (pH_val - (int)pH_val)*100;
sprintf(pachube_data,"%0d.%02d,%0d.%02d,%d,%d,%d",(int)temp_Current,abs(temp_Current1),(int)pH_val,abs(pH_val1),ORP_val);

where temp_Current, pH_val are floats, and ORP_val is integer.

Now I have changed to a WiFly and RN-XV just to simplify the hardware and the above code is not working anymore ?? have the Pachube guys changed the requirements for csv format ??

cheers,
MaLi

Here is the whole function:

void pachube_feed() 
{
  if (millis() - timeLastUpdated > TIMETOUPDATE)
  {                                              // time for the next update
    timeLastUpdated = millis();
    int temp_Current1 = (temp_Current - (int)temp_Current)*100;
    int pH_val1 = (pH_val - (int)pH_val)*100;
    sprintf(pachube_data,"%0d.%02d,%0d.%02d,%d,%d,%d",(int)temp_Current,abs(temp_Current1),(int)pH_val,abs(pH_val1),ORP_val);

    Serial.println("connecting...");
    if (client.connect("api.cosm.com", 80)) 
    {
      Serial.println("connected");
      client.print("PUT /v2/feeds/");  // APIV2
      client.print(PACHUBEFEED);
      client.println(".csv HTTP/1.1");
      client.println("Host: api.cosm.com");
      client.print("X-PachubeApiKey: ");
      client.println(APIKEY);
      client.println("User-Agent: Arduino (WiFly RN-XV)");
      client.print("Content-Type: text/csv\nContent-Length: ");
      client.println(strlen(pachube_data));
      client.println("Connection: close");
      client.println();
      client.print(pachube_data);
      client.println();
    } 
    else 
      {
      Serial.println("connection failed");
      }

    delay(1000);
  
    if (client.connected()) {
      Serial.println("disconnecting.");
      client.stop();
      Serial.println("disconnected.");
    }
  }

"Does not work" in what way. How did you change your hardware. Why do you expect code that work with e-net to work with WiFly.

Post the code means post all your code not just the bit which you think is not working!.

Mark

Thank you. pachube site responds with a "400 Bad request"

From what I've seen on pachube forum, on the csv format data is expected to come like:

0, data_0
1, data_1
2, data-2
...
datastream_ID, datastream

I suppose the format in sprintf will have to do that .

cheers,

MaLi:
I had it working in this format last year using a wired arduino ethernet interface.........
code is not working anymore ?? have the Pachube guys changed the requirements for csv format ??

If your code worked before but not now, that is very likely the cause of your problem. Your code looks very alien, and that might confirm it. There are two new libraries, Cosm.h and HttpClient.h and I couldn't get any joy until I used them. You can connect multiple sensors to one pin and, since that worked before, I guess that isn't the problem now.

The code below is for two thermometers on pin 3. It exports to cosm over Ethernet and to PLX at home. The important bit is that it shows how to talk to cosm (Pachube).

/*
From cosm library example and lifts from a lot of others
 particularly from Stanley in Kuala Lumpur.
 Use your own DS18B20 addresses, keys etc.
 Note that the feed id is in line 42.
 */
#include <OneWire.h>
#include <DallasTemperature.h>
#include <SPI.h>
#include <Ethernet.h>
#include <HttpClient.h>
#include <Cosm.h>
#include <LiquidCrystal.h>

LiquidCrystal lcd(8,9,14,5,6,7); // patchwire is to A0 (pin14) on this proto

byte InThermo[8] =  {
  0x28, 0x69, 0xC2, 0xB0, 0x03, 0x00, 0x00, 0X9F};
byte OutThermo[8] = {
  0x28, 0x7A, 0x8B, 0xC0, 0x03, 0x00, 0x00, 0x2F};
#define ONE_WIRE_BUS 3
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);

byte mac[] = { 
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };

char cosmKey[] = "l6a.......................................0Zz0g";

int sensorPin = 3;
int k=0;

// Define the strings for our datastream IDs
char sensorId0[] = "InThermo";
char sensorId1[] = "OutThermo";
char calcId1[] = "diff";

const int bufferSize = 140;
char bufferValue[bufferSize]; // enough space to store the string we're going to send
CosmDatastream datastreams[] = {
  CosmDatastream(sensorId0, strlen(sensorId0), DATASTREAM_FLOAT),
  CosmDatastream(sensorId1, strlen(sensorId1), DATASTREAM_FLOAT),
  CosmDatastream(calcId1, strlen(calcId1), DATASTREAM_FLOAT),
};
// Finally, wrap the datastreams into a feed
CosmFeed feed(83153, datastreams, 3             /*put your number here */);

EthernetClient client;
CosmClient cosmclient(client);

void setup() {
  Serial.begin(9600);
  Serial.println("LABEL,Time,TempIn,TempOut,diff");
  lcd.begin(16, 2);
  lcd.clear();
  // lcd.print(" in   out  diff"); 

  sensors.setResolution(InThermo, 12);
  sensors.setResolution(OutThermo, 12);


  while (Ethernet.begin(mac) != 1)
  {
    Serial.println("Error getting IP address via DHCP, trying again...");
    delay(10000);
  }
}

void loop() {

  int ret=0;
  //get the values from the DS8B20's 
  sensors.requestTemperatures();
  Serial.print("DATA,TIME,        "); 

  float InTemp = (sensorValue(InThermo));
  float OutTemp = (sensorValue(OutThermo));  
  // float Drain = (sensorValue(DrainThermo));  
  float diff = OutTemp - InTemp;
  datastreams[0].setFloat(InTemp);
  datastreams[1].setFloat(OutTemp);
  datastreams[2].setFloat(diff);
    Serial.print(InTemp);
    Serial.print(" ,  ");
    Serial.print(OutTemp);
    Serial.print(" ,  ");
  Serial.print (diff); 
  Serial.println(" ,  ");

  /*
  datastreams[2].setFloat(DrainTemp);
   Serial.println(datastreams[2].getFloat(DrainTemp));
   */

  lcd.setCursor (1,0);
  lcd.print(InTemp);
  lcd.setCursor (11,0);
  lcd.print (OutTemp);
  lcd.setCursor(1,1);
  lcd.print("     ");
   lcd.setCursor(1,1);
  lcd.print(diff);

  k=k+1;  

  if (k>8 )
  {
    ret = cosmclient.put(feed, cosmKey);    // SEND FEED TO COSM
    lcd.setCursor(12,1);
    lcd.print(ret);
    k=0;
  }
  delay(1000);
}

//sensorValue function
float sensorValue (byte deviceAddress[])
{
  float tempC = sensors.getTempC (deviceAddress);
  return tempC;
}

MaLi:
Thank you. pachube site responds with a "400 Bad request"
I suppose the format in sprintf will have to do that .

That may be true, but the 400 code may be down to an improper setup at the cosm end. Check the console there to ensure the feed and key are all kosher. I had a bad time with that.

OK. For those of you who are looking to push a multi datastream feed to pachube (cosm), with the new structure this is the way it works for me:

ex. for three datastreams

sprintf(buff,"0,%d\n1,%d\n2,%d\n",datastream1,datastream2,datastream3);

outcome;

0,datastream1
1,datastream2
2,datsstream3

hope it helps,
MaLi