Posting data from Arduino Yun to Thingspeak – Tenet Technetronics !

Arduino Yun is the new offering from the Arduino community which combines a strong Linux processor as well as an Atmel 8 bit microcontroller on a single hardware. The linux processor (AR9331) as well as the Atmel 32U4 (Same microcontroller present on the Arduino Leonardo ) are connected via a physical serial port connection . This means that one serial port is reserved for this communication and cannot be used in user code.

To get a more detailed overview of the Arduino Yun capabilities visit the Arduino website for more information. Installation instructions  can also be found at the dedicated Arduino Yun getting started section. I am leaving these to be completed as the instructions are clearly documented in the Arduino website !

Arduino Yun
Arduino Yun

In this blogspot we plan to demonstrate a simple code where in the Arduino Yun is used to log some data as well as post it to Thingspeak which is a IOT (Internet Of Things) platform. A basic code is demonstrated and users could extend it to do advanced tasks.

To give a top level overview in this experiment we use the http APIs documented by the Arduino Yun to post data to a thingspeak channel.Please refer to the thingspeak website to create a channel as well obtain Read/Write API  keys.

Here is a sample code in action :

 

 


//Author: Ram
//Tenet Technetronics - Bangalore, India

//Example shows how to post data using curl to thingspeak using curl using the Arduino Yun.

//Pre-requsites
//Arduino Yun Board
//Internet Connection availability for the Arduino Yun.

//Thingspeak channel :
/* For this experiment we need a thingspeak channel to be created as well as the Write API key provided by thingspeak.
Initialize the strings at the top of this code with the right API keys for the example to work seamlessly.

*/

#include "Bridge.h"
#include "HttpClient.h"

//Thingspeak parameters
String thingspeak_update_API    = "http://api.thingspeak.com/update?";
String thingspeak_write_API_key = "key=XXXXXXXXXXXXXX";//Insert Your Write API key here
String thingspeakfieldname      = "&field1=";

void setup() {
  // Initialize Bridge
  Bridge.begin();
  // Initialize Serial
  Serial.begin(9600);
  // Wait until a Serial Monitor is connected.
  while (!Serial);
  //Data to be sent to thingspeak, change this variable value and post accordingly
  int data=240;
  // run various example processes
  postToThinspeak(data);
  // Ensure the last bit of data is sent.
  Serial.flush();

}

void loop() {
  // Do nothing here.
}

void postToThinspeak(int data) {
  HttpClient client;

    String request_string =  thingspeak_update_API + thingspeak_write_API_key + thingspeakfieldname + data;
    // Make a HTTP request:
    client.get(request_string);

  // if there are incoming bytes available
  // from the server, read them and print them:
  while (client.available()) {
    char c = client.read();
    Serial.print(c);
  }

  delay(15000);
  }

 

There are more experiments in the works that will be soon published so stay tuned to our blogs and for sales related info please feel free to contact us at info@tenettech.com

Leave a Reply

Your email address will not be published. Required fields are marked *