Pages: [1] 2   Go Down
Author Topic: Sending Float values to Pachube from Arduino  (Read 3016 times)
0 Members and 1 Guest are viewing this topic.
Topsham, Vermont USA
Offline Offline
Edison Member
*
Karma: 32
Posts: 1867
... in The Woods In Vermont
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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!
Logged

Regards, Terry King terry@yourduino.com  - Check great prices, devices and Arduino-related boards at http://YourDuino.com
HOW-TO: http://ArduinoInfo.Info

Queens, New York
Offline Offline
Faraday Member
**
Karma: 86
Posts: 3459
"Of all the things I've ever lost, I miss my mind the most" -Ozzy Osbourne
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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

Created Libraries:
NPV2 (NewPasswordV2),  TFT_Extension, OneWireKeypad, SerialServo.
Will provide libraries if asked in PM or forum.

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 212
Posts: 13531
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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)

Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Global Moderator
Dallas
Offline Offline
Shannon Member
*****
Karma: 200
Posts: 12779
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset


dtostrf ?
Logged

Ayer, Massachusetts, USA
Offline Offline
Edison Member
*
Karma: 53
Posts: 1835
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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:

Code:
#include <stdlib.h>

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

So I imagine you call it:

Code:
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.
« Last Edit: January 09, 2013, 02:33:04 pm by MichaelMeissner » Logged

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 212
Posts: 13531
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Topsham, Vermont USA
Offline Offline
Edison Member
*
Karma: 32
Posts: 1867
... in The Woods In Vermont
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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...



Logged

Regards, Terry King terry@yourduino.com  - Check great prices, devices and Arduino-related boards at http://YourDuino.com
HOW-TO: http://ArduinoInfo.Info

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 212
Posts: 13531
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Rob, you know more about DHT11/DHT22 than anyone I think.
Too much honor, I am only standing on the shoulders of giants**


Quote
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 - https://github.com/blawson/PachubeArduino -

especially the cpp file - https://github.com/blawson/PachubeArduino/blob/master/Cosm.cpp -

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 smiley-wink

Let us know if it works.

** originally from Sir Isaac Newton IIRC smiley-wink

Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Dee Why NSW
Offline Offline
Edison Member
*
Karma: 47
Posts: 2202
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

« Last Edit: January 09, 2013, 08:50:43 pm by Nick_Pyner » Logged

Topsham, Vermont USA
Offline Offline
Edison Member
*
Karma: 32
Posts: 1867
... in The Woods In Vermont
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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...
Logged

Regards, Terry King terry@yourduino.com  - Check great prices, devices and Arduino-related boards at http://YourDuino.com
HOW-TO: http://ArduinoInfo.Info

Topsham, Vermont USA
Offline Offline
Edison Member
*
Karma: 32
Posts: 1867
... in The Woods In Vermont
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi.. OK, I may get sleep after all smiley-sleep

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 :-)  There will be a WIKI page of How-to and explanation to go with this code.

Quote
/* 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 : https://github.com/amcewen/HttpClient
#include <Cosm.h>       // Cosm Arduino lib : https://github.com/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 )***********



« Last Edit: January 09, 2013, 09:45:47 pm by terryking228 » Logged

Regards, Terry King terry@yourduino.com  - Check great prices, devices and Arduino-related boards at http://YourDuino.com
HOW-TO: http://ArduinoInfo.Info

Dee Why NSW
Offline Offline
Edison Member
*
Karma: 47
Posts: 2202
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


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.......
Logged

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 212
Posts: 13531
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


Quote

the graphs looks quite like your heartbeat Terry smiley-wink
Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Grand Blanc, MI, USA
Offline Offline
Faraday Member
**
Karma: 93
Posts: 3968
CODE is a mass noun and should not be used in the plural or with an indefinite article.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

MCP79411/12 RTC ... "One Million Ohms" ATtiny kit ... available at http://www.tindie.com/stores/JChristensen/

Topsham, Vermont USA
Offline Offline
Edison Member
*
Karma: 32
Posts: 1867
... in The Woods In Vermont
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

@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.
Logged

Regards, Terry King terry@yourduino.com  - Check great prices, devices and Arduino-related boards at http://YourDuino.com
HOW-TO: http://ArduinoInfo.Info

Pages: [1] 2   Go Up
Jump to: