$USD
  • EUR€
  • £GBP
  • $USD
TUTORIALS

How to upload data to cosm with Xboard Relay

DFRobot Mar 11 2013 159

wrote by Jianghao

Cosm is a secure, scalable platform that connects devices and products with applications to provide real-time control and data storage. Using Cosm's open API, individuals and companies can create new devices, develop prototypes, and bring products to market in volume. Cosm offers a way to launch internet enabled products without having to build any backend infrastructure. As a LogMeIn company, our platform runs within LogMeIn datacenters, providing world-class security and reliability. This tutorial will show you how to upload data to Cosm xboard relay from dfrobot.

Create a Cosm account
First of all, in order to use the service of Cosm, we need a Cosm account. We can sign up on the website. Https://cosm.com Click on the blue "Get Started" circle to create a new account. It's your typical e-mail/password followed by password verification. You will need to check your e-mail and click the verification link.
  
Also you need an API key. Click the blue plus to add a feed. Select Arduino Give your new feed a title and tags.
Title: " Arduino Temperature" (or whatever you like)
Tags: Arduino, temperature, adc (or make up your own) Select the "Create" button.

You need to extract the API_KEY and FEEDID from the code sample that COSM provides. These will go into the python script that we setup on the next page. The API_KEY lets COSM knows who is connecting and to which feed they want to send data.
In this example

the API_KEY is: 5RNOO3ShYJxYiq2V2sgSRtz3112SAKxFQjNDQmNXc0RScz0g

The FEEDID is: 68872

Please don’t use the ID number of this sample. The preparation for cosm has been well. Next ,let’s start with our arduino processor.

Hardware required:

Then, let’s look the code.

Here is the sample code. I connect the analog sound sensor to xboard relay directly, and send the sensor value to COSM, monitoring the brightness in my garden.
 #include <SPI.h> #include <Ethernet.h> #include <HttpClient.h> #include <Cosm.h> // MAC address for your Ethernet shield byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; // Your Cosm key to let you upload data char cosmKey[] = “pDVq_lfZX89y0RJjZWt0AxGIYAySAKx0WmtlWnlRT08yTT0g”; // Analog pin which we’re monitoring int sensorPin = 0; // Define the strings for our datastream IDs char sensorId[] = “sensor_reading”; CosmDatastream datastreams[] = { CosmDatastream(sensorId, strlen(sensorId), DATASTREAM_FLOAT), }; // Finally, wrap the datastreams into a feed CosmFeed feed(80358, datastreams, 1 ); IPAddress ip(192,168,0,7); EthernetClient client; CosmClient cosmclient(client); void setup() { // put your setup code here, to run once: Serial.begin(9600); Serial.println(“Starting single datastream upload to Cosm…”); Serial.println(“Starting Ethernet Client…”); // Setting up Ethernet static IP address Ethernet.begin(mac, ip); Serial.println(“Ethernet client & IP address OK”); } void loop() { int sensorValue = analogRead(sensorPin); datastreams[0].setFloat(sensorValue); Serial.print(“Read sensor value “); Serial.println(datastreams[0].getFloat()); Serial.println(“Uploading it to Cosm”); int ret = cosmclient.put(feed, cosmKey); Serial.print(“cosmclient.put returned “); Serial.println(ret); Serial.println(); delay(15000); }
Please download and install the libraries first from the websites below:

  • Cosm.h: https://github.com/cosm/cosm-arduino
  • HttpClient.h: https://github.com/amcewen/HttpClient

Now, upload the code to your xboard relay, then plug the x-board to the Internet using Ethernet Cable. Open the serial monitor of Arduino IDE. Your can see the data of the sensor shown on the monitor and is uploading to Cosm. Your can judge whether it has been uploaded to Cosm from the number of last line. If the number is not 200, it means you have failed and please check your code.

Click the console button on Cosm website, and then click “edit” . Then you will get such a window like below. Click the gear to go to GraphBuilder, where you can update the data Cosm gets from arduino and build a graph for the application.

REVIEW