Cosm-Arduino

From ArduinoInfo
Jump to navigation Jump to search

Cosm-Arduino:

How to send sensor data to the Web Cloud for Archive and Graphing


NOTE: Under Construction!

Following are example test applications to get started sending Arduino Data to Cosm.com that you can cut-paste into an Arduino IDE sketch window:

This Arduino Sketch uses a DHT11 Temperature-Humidity Sensor and sends the data to the WWW at https://cosm.com/feeds/97078 NOTE: Get your own Cosm.com account and make a new "Feed" which will give you the Cosm KEY and Feed ID you will need to enter in this program.

/* YourDuinoStarter Example: Web Cloud Data Display for Arduino 
 COSM_ARDUINO_DHT11_TEST1
 - Create Web Server to send data to cosm.com for display/archive 
 - Read DHT11 type sensor. See: https://arduinoinfo.mywikis.net/wiki/DHT11-Humidity-TempSensor
 - SEE the comments after "//" on each line below
 - CONNECTIONS:
 - DHT11 Sensor: +5V, Gnd, Signal to PIN 2
 - 
 - V1.07 01/10/12
 Questions: terry@yourduino.com */

/*-----( Import needed libraries )-----*/
#include <dht11.h>      // From Rob Tillaart: http://playground.arduino.cc/Main/DHTLib
#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 2 lines below are from the automatically generated Arduino code that Cosm makes
// when you create a new feed. Find, Cut and paste just these lines.
#define API_KEY "sHpN5ykoehI5G3ir2dnBlEdJPXeSAKxDWEVmenduMUNIZz0g" // your Cosm API key
#define FEED_ID 97078 // your Cosm feed ID

/*-----( Declare Constants and Pin Numbers )-----*/
#define        DHT11PIN   2  // The Temperature/Humidity sensor
char           cosmKey[] = API_KEY;
unsigned long  FeedID    = FEED_ID;

/*-----( 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 (NO SPACES OR PUNCTUATION!)
// These will displayed on the Cosm Graphs on the Web
char sensorId1[] = "TempC";
char sensorId2[] = "TempF";
char sensorId3[] = "Humid";
char sensorId4[] = "DewPt";

// Define variables for Sensor data to be sent to Cosm
float TempC, TempF, Humidity, DewPoint;

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

/*-----( Declare objects )-----*/
// NOTE: DATASTREAM_INT = integer DATASTREAM_FLOAT = float (String and char buffer also supported)
// Define the 4 streams
CosmDatastream datastreams[] =
{                 // define each ID in cosm    Define the Data Type
  CosmDatastream(sensorId1, strlen(sensorId1), DATASTREAM_FLOAT),
  CosmDatastream(sensorId2, strlen(sensorId2), DATASTREAM_FLOAT),
  CosmDatastream(sensorId3, strlen(sensorId3), DATASTREAM_FLOAT),
  CosmDatastream(sensorId4, strlen(sensorId4), DATASTREAM_FLOAT),
};

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

// Define the Ethernet Library structures
EthernetClient client;
CosmClient cosmclient(client);

dht11 DHT11;  // Declare the DHT11 sensor object

/*------------------( 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");

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



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

  /*-----------( Read the DHT11 Temperature / Humidity Sensor, and check for errors. )-----*/

  Serial.println("=================================");
  Serial.print("Read sensor: ");

  int chk = DHT11.read(DHT11PIN);
  switch (chk)
  {
  case  0:
    Serial.println("OK");
    break;
  case -1:
    Serial.println("Checksum error");
    break;
  case -2:
    Serial.println("Time out error");
    break;
  default:
    Serial.println("Unknown error");
    break;
  }

  TempC     =  (float)DHT11.temperature,2;     // Value from Sensor
  TempF     =  Fahrenheit(DHT11.temperature);  // Computed value from C
  Humidity  =  (float)DHT11.humidity;          // Value from Sensor
  DewPoint  =  dewPointFast(DHT11.temperature, DHT11.humidity); // Computed Value 

  /*-----------( Show the measurement results on the Serial Monitor)-----*/
  Serial.print("Temperature (oC): ");
  Serial.println(TempC);
  Serial.print("Temperature (oF): ");
  Serial.println(TempF);
  Serial.print("Humidity (%): ");
  Serial.println(Humidity);
  Serial.print("Dew PointFast (oC): ");
  Serial.println(DewPoint);


  /*-----------( Put sensor values into the 4 datastreams )-----*/
  // NOTE: Use   setInt() for integer   setFloat() for floating point
  datastreams[0].setFloat(TempC);    // Set sensor values into datastreams 1
  datastreams[1].setFloat(TempF);    // Set sensor values into datastreams 2
  datastreams[2].setFloat(Humidity);  // Set sensor values into datastreams 3
  datastreams[3].setFloat(DewPoint);  // Set sensor values into datastreams 4


  // NOTE:  getInt for integer, getFloat for floating point
  /*-----------( Show the values inside the streams for test/debug )-----*/
  Serial.println("-----[ Test: Check values inside the Streams ]-----");
  Serial.print("TempC:");
  Serial.println(datastreams[0].getFloat());  // Print datastream to serial monitor
  Serial.print("TempF:");
  Serial.println(datastreams[1].getFloat());  // Print datastream to serial monitor
  Serial.print("Humidity:");
  Serial.println(datastreams[2].getFloat());  // Print datastream to serial monitor
  Serial.print("DewPoint:");
  Serial.println(datastreams[3].getFloat());  // Print datastream to serial monitor

  /*-----------( Send the Feed to Cosm.com )-----*/
  //(while testing/debugging sensor code, you can comment the following line 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 ");
    }
   else
    {
     Serial.print(" ERROR ");
    }
  Serial.println(cosmReturn);


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

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

/*--------( Calculate Fahrenheit from C )----------*/

double Fahrenheit(double celsius)
{
  return 1.8 * celsius + 32;
}

/*--------( Calculate Dew Point )----------*/
// reference: http://en.wikipedia.org/wiki/Dew_point
double dewPointFast(double celsius, double humidity)
{
  double a = 17.271;
  double b = 237.7;
  double temp = (a * celsius) / (b + celsius) + log(humidity/100);
  double Td = (b * temp) / (a - temp);
  return Td;
}

//*********( THE END )***********







This Arduino Sketch uses internal data to send to the web ONLY.

/* 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;
  if (D > 3000000000)
  {
  A = 10;
  B = 20;
  C = 40.12;
  D = 80.24;
  }


  // 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 )***********