ThingSpeak Controlling Arduino with 1Sheeld

ThingSpeak Controlling Arduino with 1Sheeld

ThingSpeak controlling your LED using only Arduino Uno and 1Sheeld. Adapted form the advanced internet example of 1Sheeld library.

  • 2,538 views
  • 2 comments
  • 4 respects

Components and supplies

Apps and online services

About this project

When 1Sheeld introduced it's internet shield, Ashraf Nabil published a project called "How to use ThingSpeak through the Internet Shield to log your data graphically". This project worked well for me. My next step was to do the other way round: How to use Thingspeak through 1Sheeld internet shield to control your LED. I thought it was easy to change the code of the advanced internet example of 1 Sheeld. But that was easier said then done.

Why? It was my mistake forgetting to press the RESET button of 1Sheeld AND the RESET button of my Arduino Uno after every re-uploading of the alternated experimental sketches. So lesson one: always press these buttons after uploading your sketch!

Step 1: How to change fields on ThingSpeak?

First of all I needed to find a way to change the value of fields of my private channel on Thingspeak. That was the easiest step. On the website of Thingspeak.com you will find an explanation of it here. I used this GET-option:

GET https://api.thingspeak.com/update?api_key=put-here-your-write-key&field1=0

to change the value of field1 into zero. Meaning the LED must go OFF.

And 

GET https://api.thingspeak.com/update?api_key=put-here-your-write-key&field1=1 

to change the value of field1 into one. Meaning the LED must go ON.

To run the GET I used 

Poster :: Add-ons voor Firefox - Mozilla. See that page to find out how to use it.

Step 2: What is the HTTPRequest and HTTPResponse code for Arduino and 1Sheeld

The Arduino code of the advanced internet example of 1Sheeld contains the next HTTPRequest code

HttpRequest request("http://api.openweathermap.org/data/2.5/weather?appid=2711d6a2cc90e292afaf00f9335028b1");

I found out on the website of Thingspeak that the request url for getting the channel feed must be:

https://api.thingspeak.com/channels/9/feeds.json?&results=2 (change 9 into your own channel number). So the code should be:

HttpRequest request("https://api.thingspeak.com/channels/put-your-channel-number-here/feeds.json?&results=1"). Yes, I had to change results into 1! Please change "put-your-channel-number-here" in your Thingspeak channel number.

Down further there is a code in the example called 

void onSuccess(HttpResponse & response) with the next query code:

response["weather"][0]["main"].query();

I have found out on the website http://www.w3schools.com/json/json_syntax.asp where this query stands for. After viewing the result of the GET in the 'poster add on' I knew that I had to change it into:

response["feeds"][0]["field1"].query();


Step 3 Final: Let's turn on and off the LED with JSON

In the example code there you will find down further:

void onJsonReply(JsonKeyChain & hell,char * output) This will look at the response and turns on and off your LED.

The existing code is:

/* 1Sheeld responds using text-to-speech shield. */
  TextToSpeech.say(output);
  /* Check if the weather status is cloudy. */
  if (!strcmp("clouds",output))
    blueRGB();


Just change clouds into 1 and add a code to use the zero value. So it looks like this:

void onJsonReply(JsonKeyChain & hell,char * output)
{
  /* 1Sheeld responds using text-to-speech shield. */
  TextToSpeech.say(output);
  /* Check if the LED must go on. */
  if (!strcmp("1",output))
    blueRGB();
/* Check if the LED must go off. */
  else if (!strcmp("0",output))
    off();
}

And to put a function to off() add code:

void off()
{
  digitalWrite(blue, LOW);
  Serial.println("off");

}

Now when you have changed the code like above, you can upload the sketch. THEN PRESS RESET BUTTON 1SHEELD and PRESS RESET BUTTON ARDUINO.

And of course take care for correct using the UART switch on 1sheeld.

The only thing you have to do now is playing around with the 'poster add on Mozilla' and put a value into field 1: 1 to turn the LED on, 0 to turn the LED off.

Code

Arduino code thingspeak control Arduino with 1sheeldArduino
Upload the code and change the values of your Thinspeak key en channel number
/*
 
Internet Shield Example

This example shows an application on 1Sheeld's internet shield

By using this example, you can use the voice recognition shield to say any country's name and 
get the status of its weather using the internet shield and the open weather map api and
based of the weather status (i.e. cloudy, sunny, snowy,... etc) an RGB LED changes its color.
 
OPTIONAL:
To reduce the library compiled size and limit its memory usage, you
can specify which shields you want to include in your sketch by
defining CUSTOM_SETTINGS and the shields respective INCLUDE_ define. 

*/

#define CUSTOM_SETTINGS
#define INCLUDE_INTERNET_SHIELD
#define INCLUDE_TERMINAL_SHIELD

/* Include 1Sheeld library. */
#include <OneSheeld.h>

/* Create an Http request with openweathermap.org api url. */
/* It's important to be created here as a global object. */

HttpRequest request("https://api.thingspeak.com/channels/9/feeds.json?&results=1"); // put where 9 your channel number

/* Set a blue LED on pin 10. */

int blue = 10;

void setup() 
{
  /* Start communication. */
  OneSheeld.begin();
  /* Subscribe to success callback for the request. */
  request.setOnSuccess(&onSuccess);
  /* Subscribe to json value replies. */
  request.getResponse().setOnJsonResponse(&onJsonReply);
  /* Subscribe to response errors. */
  request.getResponse().setOnError(&onResponseError);
  /* Subscribe to Internet errors. */
  Internet.setOnError(&onInternetError);
  /* LED pin modes OUTPUT.*/
 
  pinMode(blue, OUTPUT); 
}

void loop()
{
  /* Check if a new command is received. */
  //if(VoiceRecognition.isNewCommandReceived())
 // {
    /* Add paramter to the URL with the name of the country from the voice recognition. */
    request.addParameter("key","ZZZZZZZZZZZZZZ"); //put here your Thinspeak read key
    /* Perform a GET request using the Internet shield. */
    Internet.performGet(request);
    /* Wait for 5 seconds. */
    OneSheeld.delay(5000);
 // }
}

void onSuccess(HttpResponse & response)
{
  /* Using the response to query the Json chain till the required value. */
  /* i.e. Get the value of 'main' in the first object of the array 'weather' in the response. */
  /* Providing that the response is in JSON format. */
  
  response["feeds"][0]["field1"].query(); 
  
  
  
  
}

void onJsonReply(JsonKeyChain & hell,char * output)
{
  /* 1Sheeld responds using text-to-speech shield. */
  TextToSpeech.say(output);
  /* Check if the LED must turn ON. */
  //if (!strcmp("1",output)) //werkt bij 1 tm 4;
  if (!strcmp("1",output))
    blueRGB();
  
     /* Check if the LED must be OFF. */
  else if (!strcmp("0",output))
    off();
}


/* Set the pins of the blue LED to ON. */
void blueRGB()
{
  digitalWrite(blue, HIGH);
  Serial.println("blue on");
}

/* Set the pins of the blue LED to OFF. */

void off()
{
  digitalWrite(blue, LOW);
  Serial.println("off");

}

void onResponseError(int errorNumber)
{
  /* Print out error Number.*/
  Terminal.print("Response error:");
  switch(errorNumber)
  {
    case INDEX_OUT_OF_BOUNDS: Terminal.println("INDEX_OUT_OF_BOUNDS");break;
    case RESPONSE_CAN_NOT_BE_FOUND: Terminal.println("RESPONSE_CAN_NOT_BE_FOUND");break;
    case HEADER_CAN_NOT_BE_FOUND: Terminal.println("HEADER_CAN_NOT_BE_FOUND");break;
    case NO_ENOUGH_BYTES: Terminal.println("NO_ENOUGH_BYTES");break;
    case REQUEST_HAS_NO_RESPONSE: Terminal.println("REQUEST_HAS_NO_RESPONSE");break;
    case SIZE_OF_REQUEST_CAN_NOT_BE_ZERO: Terminal.println("SIZE_OF_REQUEST_CAN_NOT_BE_ZERO");break;
    case UNSUPPORTED_HTTP_ENTITY: Terminal.println("UNSUPPORTED_HTTP_ENTITY");break;
    case JSON_KEYCHAIN_IS_WRONG: Terminal.println("JSON_KEYCHAIN_IS_WRONG");break;
  }
}

void onInternetError(int requestId, int errorNumber)
{
  /* Print out error Number.*/
  Terminal.print("Request id:");
  Terminal.println(requestId);
  Terminal.print("Internet error:");
  switch(errorNumber)
  {
    case REQUEST_CAN_NOT_BE_FOUND: Terminal.println("REQUEST_CAN_NOT_BE_FOUND");break;
    case NOT_CONNECTED_TO_NETWORK: Terminal.println("NOT_CONNECTED_TO_NETWORK");break;
    case URL_IS_NOT_FOUND: Terminal.println("URL_IS_NOT_FOUND");break;
    case ALREADY_EXECUTING_REQUEST: Terminal.println("ALREADY_EXECUTING_REQUEST");break;
    case URL_IS_WRONG: Terminal.println("URL_IS_WRONG");break;
  }
}

Schematics

Tingspeak 1Sheeld LED
Thingspeak controls LED via 1sheeld on Arduino uno
Tingspeak-1sheeld-led.fzz

Comments

Similar projects you might like

Aquarium Controlling and Monitoring Using Arduino + 1Sheeld

Project tutorial by Khaled Mohamed

  • 2,895 views
  • 1 comment
  • 15 respects

4x4x4 LED cube with Arduino Uno and 1sheeld

Project tutorial by Hassan Ibrahim

  • 29,722 views
  • 6 comments
  • 60 respects

Universal Remote Control using Arduino, 1Sheeld and Android

Project tutorial by Ashraf Nabil

  • 9,118 views
  • 5 comments
  • 14 respects

4 bit up/ down counter using 1sheeld

Project tutorial by Maha Raafat

  • 3,689 views
  • 1 comment
  • 18 respects

Security System With 1Sheeld

Project showcase by nada abdelnaby mady

  • 1,384 views
  • 0 comments
  • 2 respects

Aquarium Controlling and Monitoring with Arduino and 1Sheeld

Project tutorial by Khlaled ET

  • 1,339 views
  • 0 comments
  • 9 respects
Add projectSign up / Login