Go Down

Topic: Sending Float values to Pachube from Arduino (Read 3 times) previous topic - next topic

terryking228

#10
Jan 10, 2013, 03:35 am Last Edit: Jan 10, 2013, 03:45 am by terryking228 Reason: 1
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 )***********



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

Nick_Pyner



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

robtillaart


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


the graphs looks quite like your heartbeat Terry ;)
Rob Tillaart

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

Jack Christensen


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
MCP79411/12 RTC ... "One Million Ohms" ATtiny kit ... available at http://www.tindie.com/stores/JChristensen/

terryking228

@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.
Regards, Terry King terry@yourduino.com  - Check great prices, devices and Arduino-related boards at http://YourDuino.com
HOW-TO: http://ArduinoInfo.Info

Go Up