Topsham, Vermont USA
Offline
Edison Member
Karma: 11
Posts: 1339
... in The Woods In Vermont
|
 |
« on: January 09, 2013, 01:48:48 pm » |
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
|
|
|
|
|
Queens, New York
Offline
Edison Member
Karma: 27
Posts: 1539
"Of all the things I've ever lost, I miss my mind the most" -Ozzy Osbourne
|
 |
« Reply #1 on: January 09, 2013, 02:18:19 pm » |
|
|
|
|
|
Logged
|
UNO, MEGA, NANO, 4x4 keypad, micro servos, RF transceivers, bluetooth, ultrasonic sensor, 20x4 I2C LCD, 3.2 TFT touch screen, L298N Dual motor driver, Voice Recognition 15W, Gameduino
Arduino Tutorials, coming soon.
"If your doing nothing, it does not mean your lazy, it just means your open for anything that suits you" - Unknown
|
|
|
|
Netherlands
Offline
Tesla Member
Karma: 86
Posts: 9360
In theory there is no difference between theory and practice, however in practice there are many...
|
 |
« Reply #2 on: January 09, 2013, 02:20:29 pm » |
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
|
|
|
|
|
Global Moderator
Dallas
Offline
Shannon Member
Karma: 116
Posts: 10132
|
 |
« Reply #3 on: January 09, 2013, 02:27:09 pm » |
dtostrf ?
|
|
|
|
|
Logged
|
|
|
|
|
Ayer, Massachusetts, USA
Offline
Edison Member
Karma: 27
Posts: 1092
|
 |
« Reply #4 on: January 09, 2013, 02:28:59 pm » |
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.htmlIt 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.
|
|
|
|
« Last Edit: January 09, 2013, 02:33:04 pm by MichaelMeissner »
|
Logged
|
|
|
|
|
Netherlands
Offline
Tesla Member
Karma: 86
Posts: 9360
In theory there is no difference between theory and practice, however in practice there are many...
|
 |
« Reply #5 on: January 09, 2013, 02:34:23 pm » |
@terry it is solved in this thread - http://community.cosm.com/node/957 -
|
|
|
|
|
Logged
|
|
|
|
|
Topsham, Vermont USA
Offline
Edison Member
Karma: 11
Posts: 1339
... in The Woods In Vermont
|
 |
« Reply #6 on: January 09, 2013, 03:20:17 pm » |
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
|
|
|
|
|
Netherlands
Offline
Tesla Member
Karma: 86
Posts: 9360
In theory there is no difference between theory and practice, however in practice there are many...
|
 |
« Reply #7 on: January 09, 2013, 03:39:51 pm » |
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 - 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  Let us know if it works. ** originally from Sir Isaac Newton IIRC 
|
|
|
|
|
Logged
|
|
|
|
|
Dee Why NSW
Offline
God Member
Karma: 11
Posts: 512
|
 |
« Reply #8 on: January 09, 2013, 06:37:38 pm » |
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
Edison Member
Karma: 11
Posts: 1339
... in The Woods In Vermont
|
 |
« Reply #9 on: January 09, 2013, 07:27:03 pm » |
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
|
|
|
|
|
Topsham, Vermont USA
Offline
Edison Member
Karma: 11
Posts: 1339
... in The Woods In Vermont
|
 |
« Reply #10 on: January 09, 2013, 09:35:51 pm » |
Hi.. OK, I may get sleep after all  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/96891Tomorrow 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. /* 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 networkbyte 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 IDschar sensorId[] = "Int1"; char sensorId2[] = "Int2"; char sensorId3[] = "Float1"; char sensorId4[] = "Float2"; // Define variables for Sensor data to be sent to Cosmint A,B; // Change to your sensor variable namesfloat 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 streamsCosmDatastream 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 DataStreamsCosmFeed feed(FeedID, datastreams, 4 ); // Define the Ethernet Library structuresEthernetClient 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
|
|
|
|
|
Dee Why NSW
Offline
God Member
Karma: 11
Posts: 512
|
 |
« Reply #11 on: January 10, 2013, 10:20:08 am » |
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
|
|
|
|
|
Netherlands
Offline
Tesla Member
Karma: 86
Posts: 9360
In theory there is no difference between theory and practice, however in practice there are many...
|
 |
« Reply #12 on: January 10, 2013, 11:26:46 am » |
the graphs looks quite like your heartbeat Terry 
|
|
|
|
|
Logged
|
|
|
|
|
Grand Blanc, MI, USA
Offline
Edison Member
Karma: 43
Posts: 2478
"We're a proud service of the Lost Electricity Reclamation Agency"
|
 |
« Reply #13 on: January 10, 2013, 01:31:10 pm » |
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
|
|
|
|
|
Topsham, Vermont USA
Offline
Edison Member
Karma: 11
Posts: 1339
... in The Woods In Vermont
|
 |
« Reply #14 on: January 10, 2013, 02:26:27 pm » |
@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
|
|
|
|
|
|