I am trying to turn on a led using a spark core but it does not work. Here is the code:
// This #include statement was automatically added by the Spark IDE.
// Declaring the variables.
//*****************************************************************************************************
#include "HttpClient/HttpClient.h"
HttpClient http;
#define relay_SWITCH "5535a72b762542455accb953"
#define led_SWITCH "5535a8987625424c69cab006"
#define TOKEN "TOKEN_HERE"
int led_on;
int relay_on;
//******************************************************************************************************
//necessary part for UBIDOTS
//*****************************************************************************************************
// Headers currently need to be set at init, useful for API keys etc.
http_header_t headers[] = {
{ "Content-Type", "application/json" },
{ "X-Auth-Token" , TOKEN },
{ NULL, NULL } // NOTE: Always terminate headers will NULL
};
http_request_t request;
http_response_t response;
//******************************************************************************************************
// initial configuration
//******************************************************************************************************
void setup() {
pinMode(D7,OUTPUT);
pinMode(D6,OUTPUT);
request.hostname = "things.ubidots.com";
request.port = 80;
//Serial.begin(9600);
}
//******************************************************************************************************
// main loop= the program
//******************************************************************************************************
void loop() {
//Better doing a function for this code, but I guess is easier to understand in this way
response.body = "";
response.status = 0;
//request.path = "/api/v1.6/variables/"led_SWITCH"/values?page_size=1";//
request.path = "/api/v1.6/variables/"led_SWITCH"/values";
http.get(request, response, headers);
//led_on = response.body.indexOf(search_on);//Search for value 1.0
if (led_on == -1) {//red_on default value is -1
digitalWrite(D7, LOW); //turn off
}
else if (-1 < led_on) {
digitalWrite(D7, HIGH);//turn on
}
}
//*********************************************************************************************************
The have error in this line:
led_on = response.body.indexOf(search_on);
I reviewed the samples but I do not understand. Do you have a hello world code for turning on a led using spark core?
Just tried this code based on yours and it worked!
// Declaring the variables.
//*****************************************************************************************************
#include "HttpClient/HttpClient.h"
HttpClient http;
#define relay_SWITCH "5535a72b762542455accb953"
#define led_SWITCH "5535a8987625424c69cab006"
#define TOKEN "TOKEN_HERE"
int led_on;
int relay_on;
//******************************************************************************************************
//necessary part for UBIDOTS
//*****************************************************************************************************
// Headers currently need to be set at init, useful for API keys etc.
http_header_t headers[] = {
{ "Content-Type", "application/json" },
{ "X-Auth-Token" , TOKEN },
{ NULL, NULL } // NOTE: Always terminate headers will NULL
};
http_request_t request;
http_response_t response;
//******************************************************************************************************
// initial configuration
//******************************************************************************************************
void setup() {
pinMode(D7,OUTPUT);
pinMode(D6,OUTPUT);
request.hostname = "things.ubidots.com";
request.port = 80;
Serial.begin(9600);
}
void loop() {
//Better doing a function for this code, but I guess is easier to understand in this way
response.body = "";
response.status = 0;
request.path = "/api/v1.6/variables/"led_SWITCH"/values?page_size=1";
http.get(request, response, headers);
led_on = response.body.indexOf("\"value\": 1");
if (led_on == -1) {
Serial.println("Led is off");
}
else if (-1 < led_on) {
Serial.println("Led is on");
}
}
See serial output:
HttpClient>.Connecting to: things.ubidots.com:80
HttpClient>.Start of HTTP Request.
GET /api/v1.6/variables/5535a8987625424c69cab006/values?page_size=1 HTTP/1.0
Connection: close
HOST: things.ubidots.com
Content-Type: application/json
X-Auth-Token: XXXX
HttpClient>.End of HTTP Request.
HttpClient>.Receiving TCP transaction of 128 bytes.
HTTP/1.1 200 OK
Server: nginx
Date: Sat, 25 Apr 2015 13:13:18 GMT
Content-Type: application/json
Connection: close
Vary: Accept-Encoding
Vary: Accept
Allow: GET, POST, HEAD, OPTIONS