Sending Float values to Pachube from Arduino

Hi Everyone,

I have a Duemilanove type board with a W5100 Ethernet Shield. I am reading data from a DHT11 Temperature-Humidity sensor. Running 1.02 IDE.

I have it successfully sending integer data to https://cosm.com/feeds/96820

BUT I really want to be sending floating point data.

Does anyone have a working example of doing this??

I have looked through lots of stuff about using separate Float-to-String code, using a C++ library, and advice that says "Don't Use Strings, even if they are in the Arduino IDE distribution"..
I THOUGHT "It can't be that hard!".. but after 4 or 5 hours of floundering and crashing I'd like your advice!

Thanks!

I found this, not sure if it helps or not.

http://exosite.com/project/basic-arduino-temperature-web-monitor

BUT I really want to be sending floating point data.

The DHT11 does not return a FP type, still the question is interesting, no answer (yet)

dtostrf ?

If you are doing binary transfer, remember that the Arduino 'double' is actually the same as 'float' on other systems, and the AVR is little endian.

From your question however, I assume you are trying to write out the value as a sequence of characters, transmit them via the network, and then the program on the other side will do scanf or similar function to convert the number back to the internal representation used on that system. You can't use the *printf functions by default, since the AVR printf is compiled without floating point support (to save a lot of code space). It looks like the IDE has alternate forms of printf/scanf that include the floating point support, but I don't know how to get those libraries included.

I'm at work right now, but the low level function seems to be: dtostrf: http://www.sourcecodebrowser.com/avr-libc/1.8.0/dtostrf_8c.html

It has the following prototype:

#include <stdlib.h>

char *dtostrf(double value, signed char width, unsigned char prec, char *s);

So I imagine you call it:

const int size = 10;
const int precision = 2;

void print_num (double value)
{
    char buffer[size+1];

    dtostrf (value, size, precision, buffer);
    Serial.println (buffer);
}

You would change Serial.println to whatever you are using to transfer the number.

@terry
it is solved in this thread - http://community.cosm.com/node/957 -

Thanks for the quick responses, everyone...

Rob, you know more about DHT11/DHT22 than anyone I think. I guess I have been looking at the examples that do Serial.print with (float) of the values. Hmmm..
They do conversions to Fahrenheit and Dew Point using Float.

How about DHT22?? I have one and might use that also.

I am trying to write a good clear example of using Pachube to send sensor data to the web and graph it, for a workshop I'll be doing. Probably will be better after it's Clear to ME !!

Other: I also plan to use DS18B20 sensors, that will return Float data. Rob, the pointer you gave uses DS18B20 and I need to dig into that. It uses a library that hides the details of building the HTTP PUT format characters. Maybe that's good. I was hoping to understand the gritty details..

Michael, I tried dtostrf but backed off when it (my code anyway) crashed after one loop. Took it out and OK.. Memory Leaks? String overlays? hard to debug. It uses a char buffer; Would I have to manually assure the data is zero/nul terminated?

HazardsMind, that's an interesting site.. not Pachube but they do send float data.

OK, back to the fray. Many thanks for the pointers and I will post back what I learn and I will be putting an example up with lots of How-To embedded.

Soon as I Know How To...

Rob, you know more about DHT11/DHT22 than anyone I think.

Too much honor, I am only standing on the shoulders of giants**

Rob, the pointer you gave uses DS18B20 and I need to dig into that. It uses a library that hides the details of building the HTTP PUT format characters. Maybe that's good. I was hoping to understand the gritty details..

The solution seems to be in this "new" cosm lib - GitHub - blawson/PachubeArduino -

especially the cpp file - PachubeArduino/Cosm.cpp at master · blawson/PachubeArduino · GitHub -

it uses _client.print(dataToSend, DEC);
when dataToSend is a float/double, DEC will be interpreted as the number of decimals. // not too nice usage of DEC but OK

but most important, OP states it works/solves his problem of posting floats to cosm.
I have not confirmed it (no duino with ethernet nearby) but I expect you will within a day or so :wink:

Let us know if it works.

** originally from Sir Isaac Newton IIRC :wink:

terryking228:
Other: I also plan to use DS18B20 sensors, that will return Float data. Rob, the pointer you gave uses DS18B20 and I need to dig into that.

Well, if the DHT11 does not deliver Floating point and in the light of the above quote, you already know what to do, and there is plenty info on that.

An earlier version of my code is in the "It worked!" section of the cosm forum, october I think

https://cosm.com/feeds/83153

Thanks for the pointers/comments! I'm back on it.. Zinged by two different libraries creating 'cosm.h' but finally hid the wrong one and I can compile an example.

Details at 11.. if the Old Guy can stay awake that long...

Hi.. OK, I may get sleep after all :sleeping:

I got it working well with the Cosm-Arduino library. (Links in the code example below). It makes it easy to change the number of variables(streams) and if they are int or float. I have it running on https://cosm.com/feeds/96891

Tomorrow I will work on integrating DHT11 and DS18B20 sensors in an example. For now it just sends phoney changing int and float data.

Many thanks to all.. This is a community I am very happy to be part of!

OK, let's see if I know how to do the Code Thing...

OH: Please nitpick my format and comments in this code etc.. I want this to be as clear as I wished it was at 1AM last night. Think of very inexperienced Arduino enthusiasts reading this, not BitHeads like Rob :slight_smile: There will be a WIKI page of How-to and explanation to go with this code.

/* YourDuinoStarter Example: Web Cloud Data Display for Arduino

  • Create Web Server to send data to cosm.com for display/archive
  • SEE the comments after "//" on each line below
  • CONNECTIONS:
  • V1.03 01/09/12
    Questions: terry@yourduino.com */

/-----( Import needed libraries )-----/
#include <SPI.h> // Standard Arduino Library
#include <Ethernet.h> // Standard Arduino Library
#include <HttpClient.h> // HttpClient lib : GitHub - amcewen/HttpClient: Arduino HTTP library
#include <Cosm.h> // Cosm Arduino lib : GitHub - mnin/CosmArduino

// NOTE: The lines below are from the automatically generated Arduino code that Cosm makes
// when you create a new feed. Cut and paste just these lines for reference
// #define API_KEY "r5LAQylJnH2_S05o1t8yi1yxBtiSAKxxSmpJR0orTTVlbz0g" // your Cosm API key
// #define FEED_ID 96891 // your Cosm feed ID

// NOTE: The "API Key" and "Feed ID" are unique to you. Get them from your Cosm account.
// The API Key is located at https://cosm.com/users/<cosm_username>/keys
// #define API_KEY "r5LAQylJnH2_S05o1t8yi1yxBtiSAKxxSmpJR0orTTVlbz0g" // your Cosm API key
// #define FEED_ID 96891 // your Cosm feed ID

/-----( Declare Constants and Pin Numbers )-----/
char cosmKey[] = "r5LAQylJnH2_S05o1t8yi1yxBtiSAKxxSmpJR0orTTVlbz0g";
unsigned long FeedID = 96891;

/-----( Declare Variables )-----/
// MAC address for your Ethernet shield:Change if you have more than one on your network
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192,168,1,122); // Available Static IP address on your Network. See Instructions
// Define the strings for our datastream IDs
char sensorId[] = "Int1";
char sensorId2[] = "Int2";
char sensorId3[] = "Float1";
char sensorId4[] = "Float2";

// Define variables for Sensor data to be sent to Cosm
int A,B; // Change to your sensor variable names
float C,D; // Change to your sensor variable names

int cosmReturn = 0; // Result Return code from data send

/-----( Declare objects )-----/
// NOTE: DATASTREAM_INT = integer DATASTREAM_FLOAT = float
// Define the 4 streams
CosmDatastream datastreams[] = {
CosmDatastream(sensorId, strlen(sensorId), DATASTREAM_INT), // define this ID in cosm
CosmDatastream(sensorId2, strlen(sensorId), DATASTREAM_INT), // define this ID in cosm
CosmDatastream(sensorId3, strlen(sensorId), DATASTREAM_FLOAT), // define this ID in cosm
CosmDatastream(sensorId4, strlen(sensorId), DATASTREAM_FLOAT), // define this ID in cosm
};

// Define the Feed, consisting of the 4 DataStreams
CosmFeed feed(FeedID, datastreams, 4 );

// Define the Ethernet Library structures
EthernetClient client;
CosmClient cosmclient(client);
/------------------( END OF DECLARATION AREA )-----------------------------/

void setup() /****** SETUP: RUNS ONCE ******/
{
Serial.begin(9600); //Ready to send debug info to Serial Monitor

Serial.println("Starting datastream upload to Cosm...");
Serial.println("Starting Ethernet Client...");

Ethernet.begin(mac, ip);
Serial.println("Ethernet client & IP address OK");

// Initialize Sensor Data For Test
A = 10;
B = 20;
C = 40.12;
D = 80.24;

delay(1000); //Wait for Server to start up
}//--(end setup )---

void loop() /****** LOOP: RUNS CONSTANTLY ******/
{

// Put your sensor input code here (This is temporary for changing data for test)
A = A + 1;
B = B + 2;
C = C * 1.05;
D = D * 1.10;

// Put sensor values into the 4 datastreams,
// NOTE: Use setInt() for integer setFloat() for floating point
datastreams[0].setInt(A); // Set sensor values into datastreams 1
datastreams[1].setInt(B); // Set sensor values into datastreams 2
datastreams[2].setFloat(C); // Set sensor values into datastreams 3
datastreams[3].setFloat(D); // Set sensor values into datastreams 4

// NOTE: getInt for integer, getFloat for floating point
// Show the values in the streams for test/debug
Serial.print("A:");
Serial.println(datastreams[0].getInt()); // Print datastream to serial monitor
Serial.print("B:");
Serial.println(datastreams[1].getInt()); // Print datastream to serial monitor
Serial.print("C:");
Serial.println(datastreams[2].getFloat()); // Print datastream to serial monitor
Serial.print("D:");
Serial.println(datastreams[3].getFloat()); // Print datastream to serial monitor

// Send the feed to cosm. (while testing/debugging sensor codes, you can comment it out.
cosmReturn = cosmclient.put(feed,cosmKey); // Send feed to cosm

Serial.print("COSM client returned : "); // Get return result code, similar to HTTP code
if (cosmReturn == 200) { Serial.print(" OK "); }
Serial.println(cosmReturn);

delay(2000); // Put a delay before the next updates to Cosm
}//--(end main loop )---

/-----( Declare User-written Functions )-----/

//( THE END )**

terryking228:
Please nitpick my format and comments in this code etc.. I

I wish I had seen this last September, it looks like quite a nice child's guide to cosm.......

running on https://cosm.com/feeds/96891

the graphs looks quite like your heartbeat Terry :wink:

terryking228:
I also plan to use DS18B20 sensors, that will return Float data.

Technically, these sensors do not return float data, but scaled integers. The DS18B20 returns an integer whose LS bit is 2-4 °C (1/16 °C), so it can be considered °C times 16. The DHT22 returns an integer that is °C times ten, but it also has a non-standard way to represent negative numbers; the MS bit is an independent sign bit and the remainder of the data is an absolute value.

Of course if there is a library involved to read the sensor it could well return a float after converting the data from the sensor.

I read the DS18B20 using the OneWire.h library, which just handles the details of the communication and returns the data directly from the sensor. I use integer arithmetic to convert it to °F times ten. To send it to Cosm, I use the value/10 and value%10, along with itoa() and strcat() to build the required CSV representation including the decimal point.

Example: https://cosm.com/feeds/17534

@Jack, You are correct in that these sensors are not natively Floating Point. But I feel I need to be able to manipulate their result as Float, do conversions and derive new data like differentials etc. That was the motivation for handling Float as Float. You methods are more flexible (and probably more computationally efficient) but I am trying to support relative beginners in the Arduino world. Thanks for the perspective.

@Rob ..Hmmm... Maybe my position on Net Neutrality will change. With about 3E9 heartbeats so far I want to maximize my number as far as possible.

@Terry,

Yeah, I was going for efficiency, but certainly float is perfectly acceptable to beginners, and even beyond. I can be too much of a bit-biter sometimes :smiley:

How is Cosm working for you as far as reliability? I have issues getting every datapoint to the website. I don't expect 100%, but probably at least 99.9% or better, and I haven't been able to achieve that.

Hi!

I have some projects that send float values with Virtualwire.

In one project I use dtostrf and it has never crashed.
My weatherstation is sending all data in a binary format (byte array).

This is some parts of my code:

struct WeatherData {
byte year;
byte month;
byte day;
byte hour;
byte minute;
byte windspeed;
byte winddirection;
unsigned int pressure;
unsigned int humidity;
float rain;
float temperature;
};
WeatherData WeatherDataArray[144];

memcpy(msg,&WeatherDataArray[bSendCounter],iLen);

vw_send((uint8_t *)msg, strlen(msg));
vw_wait_tx();

dtostrf is easier to use and ok for short messages..

/Olof

OK, I have a DHT11 Temperature-Humidity sensor (@Rob's Library) and data going to the web here:
https://cosm.com/feeds/97078

The code for this (And an earlier dead-data test version) are now hosted here:
http://arduino-info.wikispaces.com/Cosm-Arduino

It was a few hours and a few of the usual weird error gremlins to work through, but it works. I plan to update that page with much more how-to and explanation, and work into Control from the web etc. over time.

Any comments, critique, suggestions welcome! Thanks to everyone for helping...

Nice project Terry.
I have a similar one with added rainfall and wind. You can see my feed here https://cosm.com/feeds/58529. I also recently got an Air Space Egg which feeds data to COSM as well. This has NO2 and CO sensors as well as temp & humidity. See more about them at http://www.kickstarter.com/projects/edborden/air-quality-egg