Published

ThingSpeak Controlling Arduino with 1Sheeld

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

IntermediateProtip1 hour3,947
ThingSpeak Controlling Arduino with 1Sheeld

Things used in this project

Hardware components

Arduino UNO
Arduino UNO
×1
LED (generic)
LED (generic)
×1
Resistor 221 ohm
Resistor 221 ohm
×1
1Sheeld
1Sheeld
×1

Software apps and online services

ThingSpeak API
ThingSpeak API
poster add on Mozilla

Story

Read more

Schematics

Tingspeak 1Sheeld LED

Thingspeak controls LED via 1sheeld on Arduino uno

Code

Arduino code thingspeak control Arduino with 1sheeld

Arduino
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;
  }
}

Credits

Comments