Affichage de données vers site Cosm (anciennement pachube)

bonjour,
je suis débutant sous arduino et j'essaye de transmettre des infos vers cosm.com , j'ai donc réussi à le faire au bout d'un certain temps pour 4 données (LDR+CTN+Entrée numériques)
tout s'affiche correctement mais je n'ai que des valeurs entières pour mes températures , je souhaite donc faire des modifs afin d'afficher des températures au dixième .
Or la ça coince; à priori c'est un pb de format : int thisLength = 8 + getLength(thisData1)+2+8+getLength(thisData2)+2+8+ getLength(thisData3)+2+8+ getLength(thisData4)+2;
ou peut être de type de variables : void sendData(int thisData1, int thisData2, int thisData3, int thisData4) le pb c'est que lorsque je change les int par des float le pg plante .
Pourriez vous m'éclairer .
PS: je peux joindre tout le pg si necessaire

je vous met le code au cas ou !

#include <SPI.h>
#include <Ethernet.h>

#define APIKEY "*********" // your cosm api key
#define FEEDID******** // your feed ID
#define USERAGENT "Cosm Arduino Example*******" // user agent is the project name
int digits;
// assign a MAC address for the ethernet controller.
// Newer Ethernet shields have a MAC address printed on a sticker on the shield
// fill in your address here:
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};

// fill in an available IP address on your network here,
// for manual configuration:
IPAddress ip(************);
// initialize the library instance:
EthernetClient client;

// if you don't want to use DNS (and reduce your sketch size)
// use the numeric IP instead of the name for the server:
IPAddress server(216,52,233,121); // numeric IP for api.cosm.com
//char server[] = "api.cosm.com"; // name address for cosm API

unsigned long lastConnectionTime = 0; // last time you connected to the server, in milliseconds
boolean lastConnected = false; // state of the connection last time through the main loop
const unsigned long postingInterval = 10*1000; //delay between updates to Cosm.com

void setup() {
  pinMode(7, INPUT);
  
  // start serial port:
  Serial.begin(9600);
 // start the Ethernet connection:
  if (Ethernet.begin(mac) == 0) {
    Serial.println("Failed to configure Ethernet using DHCP");
    // DHCP failed, so use a fixed IP address:
    Ethernet.begin(mac, ip);
  }
}

void loop() {
  // read the analog sensor:
  int sensorReading1 = analogRead(A0);//LDR
  int sensorReading2 = analogRead(A3);//CTN1
  int sensorReading3 = analogRead(A4);//CTN2
  int sensorReading4 = digitalRead(7);//Contact magnetique
  float CTN1=sensorReading2/22;
  float CTN2=sensorReading3/22;
  // if there's incoming data from the net connection.
  // send it out the serial port. This is for debugging
  // purposes only:
  if (client.available()) {
    char c = client.read();
    Serial.print(c);
  }

  // if there's no net connection, but there was one last time
  // through the loop, then stop the client:
  if (!client.connected() && lastConnected) {
    Serial.println();
    Serial.println("disconnecting.");
    client.stop();
  }

  // if you're not connected, and ten seconds have passed since
  // your last connection, then connect again and send data:
  if(!client.connected() && (millis() - lastConnectionTime > postingInterval)) {
    sendData(sensorReading1, CTN1, CTN2, sensorReading4);
  }
  // store the state of the connection for next time through
  // the loop:
  lastConnected = client.connected();
}

// this method makes a HTTP connection to the server:
void sendData(int thisData1, int thisData2, int thisData3, int thisData4)
//void sendData(int thisData2)
//void sendData(int thisData3)
//void sendData(int thisData4)
{
  // if there's a successful connection:
  if (client.connect(server, 80)) {
    Serial.println("connecting...");
    // send the HTTP PUT request:
    client.print("PUT /v2/feeds/");
    client.print(FEEDID);
    client.println(".csv HTTP/1.1");
    client.println("Host: api.cosm.com");
    client.print("X-ApiKey: ");
    client.println(APIKEY);
    client.print("User-Agent: ");
    client.println(USERAGENT);
    client.print("Content-Length: ");

    // calculate the length of the sensor reading in bytes:
    // 8 bytes for "sensor1," + number of digits of the data:
   int thisLength = 8 + getLength(thisData1)+2+8+getLength(thisData2)+2+8+ getLength(thisData3)+2+8+ getLength(thisData4)+2;
    //int thisLength =  getLength(thisData1)+getLength(thisData2)+ getLength(thisData3)+ getLength(thisData4);
    client.println(thisLength);

    // last pieces of the HTTP PUT request:
    client.println("Content-Type: text/csv");
    client.println("Connection: close");
    client.println();

    // here's the actual content of the PUT request:
    client.print("sensor1,");
    client.println(thisData1);
    client.print("sensor2,");
    client.println(thisData2);
    client.print("sensor3,");
    client.println(thisData3);
    client.print("sensor4,");
    client.println(thisData4);
  
  }
  else {
    // if you couldn't make a connection:
    Serial.println("connection failed");
    Serial.println();
    Serial.println("disconnecting.");
    client.stop();
  }
   // note the time that the connection was made or attempted:
  lastConnectionTime = millis();
}


// This method calculates the number of digits in the
// sensor reading. Since each digit of the ASCII decimal
// representation is a byte, the number of digits equals
// the number of bytes:
/*
int getLength(int someValue) {
  // there's at least one byte:
  int digits = 1;
  // continually divide the value by ten,
  // adding one to the digit count for each
  // time you divide, until you're at 0:
  int dividend = someValue /10;
  while (dividend > 0) {
    dividend = dividend /10;
    digits++;
  }
  // return the number of digits:
  return digits;
}*/
int getLength(double someValue)  { 
  int n = 0;

  // Handle negative numbers
  if (someValue < 0.0)
  {
    n++; // "-";
    someValue = -someValue;
  }

  // Round correctly so that print(1.999, 2) prints as "2.00"
  double rounding = 0.5;
  for (uint8_t i=0; i< digits; ++i) {
    rounding /= 10.0;
  }
  someValue += rounding;

  // Extract the integer part of the number and print it
  unsigned long int_part = (unsigned long)someValue;
  double remainder = someValue - (double)int_part;

  while (int_part > 0) {
    int_part /= 10;
    n++;
  }
  // Print the decimal point, but only if there are digits beyond
  if (digits > 0) {
    n++; //"."; 
  }

  // Extract digits from the remainder one at a time
  while (digits-- > 0)
  {
    remainder *= 10.0;
    int toPrint = int(remainder);
    n ++; // += String(toPrint);
    remainder -= toPrint; 
  } 
  return n;
}

Bonjour

j'ai eu un probleme du même genre avec Pachube pour le calcul 'Content Lengh' dans la requet PUT. (avec l'envoi d'un "nombre à virgule") ci-joint un exemple avec un float récupéré dans un DS18B20 et une manip pour introduire le point décimal dans le buffer d'envoi malgré la contrainte de sprintf (ligne 89)

PS : pas nécessaire de publier ton APIKEY + FEEDID !!!
Il est encore temps de modifier ton message....

Wifly_PachubeTempLogger.pde (3.8 KB)

Merci
je vais prendre le temps de regarder ce pg .

A priori ça ne fonctionne pas j'ai l'erreur suivante :CSV Parser Error: CSV is invalid. Incorrect number of fields.
Ce qui me surprend c'est que cela fonctionne lorsque je teste ce programme avec un seul capteur!
j'avoue mon ignorance dans ce domaine.

#include <SPI.h>
#include <Ethernet.h>

#define APIKEY "***********" // your cosm api key
#define FEEDID *********** // your feed ID
#define USERAGENT "**********************" // user agent is the project name
int digits;
// assign a MAC address for the ethernet controller.
// Newer Ethernet shields have a MAC address printed on a sticker on the shield
// fill in your address here:
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};

// fill in an available IP address on your network here,
// for manual configuration:
IPAddress ip(***************);
// initialize the library instance:
EthernetClient client;

// if you don't want to use DNS (and reduce your sketch size)
// use the numeric IP instead of the name for the server:
IPAddress server(216,52,233,121);      // numeric IP for api.cosm.com
//char server[] = "api.cosm.com";   // name address for cosm API

unsigned long lastConnectionTime = 0;          // last time you connected to the server, in milliseconds
boolean lastConnected = false;                 // state of the connection last time through the main loop
const unsigned long postingInterval = 10*1000; //delay between updates to Cosm.com

void setup() {
 
  // start serial port:
  Serial.begin(9600);
 // start the Ethernet connection:
  if (Ethernet.begin(mac) == 0) {
    Serial.println("Failed to configure Ethernet using DHCP");
    // DHCP failed, so use a fixed IP address:
    Ethernet.begin(mac, ip);
  }
}

void loop() {
  // read the analog sensor:
  int sensorReading = analogRead(A4); 
                                                                                      //modif calcul temperature
float temperatureCTNA4 = sensorReading/22.36;  

  // if there's incoming data from the net connection.
  // send it out the serial port.  This is for debugging
  // purposes only:
  if (client.available()) {
    char c = client.read();
    Serial.print(c);
  }

  // if there's no net connection, but there was one last time
  // through the loop, then stop the client:
  if (!client.connected() && lastConnected) {
    Serial.println();
    Serial.println("disconnecting.");
    client.stop();
  }

  // if you're not connected, and ten seconds have passed since
  // your last connection, then connect again and send data:
  if(!client.connected() && (millis() - lastConnectionTime > postingInterval)) {
                                                                                     //variable  modifiee sensorReading par temp
    sendData(temperatureCTNA4);
  }
  // store the state of the connection for next time through
  // the loop:
  lastConnected = client.connected();
}

// this method makes a HTTP connection to the server:
                                                                                                  //modif int par float
void sendData(float thisData1) {
  // if there's a successful connection:
  if (client.connect(server, 80)) {
    Serial.println("connecting...");
    // send the HTTP PUT request:
    client.print("PUT /v2/feeds/");
    client.print(FEEDID);
    client.println(".csv HTTP/1.1");
    client.println("Host: api.cosm.com");
    client.print("X-ApiKey: ");
    client.println(APIKEY);
    client.print("User-Agent: ");
    client.println(USERAGENT);
    client.print("Content-Length: ");

    // calculate the length of the sensor reading in bytes:
    // 8 bytes for "sensor1," + number of digits of the data:                    Modif 2
    int thisLength = 8 + getLength(thisData1)+2;
    
    client.println(thisLength);

    // last pieces of the HTTP PUT request:
    client.println("Content-Type: text/csv");
    client.println("Connection: close");
    client.println();

    // here's the actual content of the PUT request:
    client.print("sensor1,");
    client.println(thisData1);                                                
                                       //    client.print("sensor2,");
                                       // client.println(thisData2);
  } 
  else {
    // if you couldn't make a connection:
    Serial.println("connection failed");
    Serial.println();
    Serial.println("disconnecting.");
    client.stop();
  }
   // note the time that the connection was made or attempted:
  lastConnectionTime = millis();
}


// This method calculates the number of digits in the
// sensor reading.  Since each digit of the ASCII decimal
// representation is a byte, the number of digits equals
// the number of bytes:
/*
int getLength(int someValue) {
  // there's at least one byte:
  int digits = 1;
  // continually divide the value by ten, 
  // adding one to the digit count for each
  // time you divide, until you're at 0:
  int dividend = someValue /10;
  while (dividend > 0) {
    dividend = dividend /10;
    digits++;
  }
  // return the number of digits:
  return digits;
}*/
// Counts digits of a floating point number, to calculate content length
// for an HTTP call.
// Based on Arduino's internal printFloat() function.

int getLength(double someValue)  { 
  int n = 0;

  // Handle negative numbers
  if (someValue < 0.0)
  {
    n++; // "-";
    someValue = -someValue;
  }

  // Round correctly so that print(1.999, 2) prints as "2.00"
  double rounding = 0.5;
  for (uint8_t i=0; i< digits; ++i) {
    rounding /= 10.0;
  }
  someValue += rounding;

  // Extract the integer part of the number and print it
  unsigned long int_part = (unsigned long)someValue;
  double remainder = someValue - (double)int_part;

  while (int_part > 0) {
    int_part /= 10;
    n++;
  }
  // Print the decimal point, but only if there are digits beyond
  if (digits > 0) {
    n++; //"."; 
  }

  // Extract digits from the remainder one at a time
  while (digits-- > 0)
  {
    remainder *= 10.0;
    int toPrint = int(remainder);
    n ++; // += String(toPrint);
    remainder -= toPrint; 
  } 
  return n;
}