Last active
October 27, 2015 19:41
-
-
Save sbose78/e9d184b013b6a367562a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// This #include statement was automatically added by the Particle IDE. | |
#include "SparkJson/SparkJson.h" | |
#include "application.h" | |
#include "HttpClient/HttpClient.h" | |
#include "string.h" | |
unsigned int nextTime = 0; // Next time to contact the server | |
void setup() { | |
Serial.begin(9600); | |
} | |
void sendDataToServer(){ | |
HttpClient http; | |
// Headers currently need to be set at init, useful for API keys etc. | |
http_header_t headers[] = { | |
{ "Content-Type", "application/json" }, | |
// { "Accept" , "application/json" }, | |
{ "Accept" , "*/*"}, | |
{ NULL, NULL } // NOTE: Always terminate headers will NULL | |
}; | |
http_request_t request; | |
http_response_t response; | |
Spark.publish("DEBUG","POST-ing variable data on server 128.199.225.112:80/greenopia/device/metricdata"); | |
// Our Greenopia Server. | |
request.hostname = "128.199.225.112"; | |
request.port = 80; | |
request.path = "/greenopia/device/metricdata"; | |
// The library also supports sending a body with your request: | |
request.body = "{\"device_id\":\"2f0047000747343337373738\",\"data\":[{\"type\":\"any_key\",\"value\":\"40\"}]}"; | |
// Get request | |
http.post(request, response, headers); | |
// Log to serial output | |
Serial.print("Application>\tResponse status: "); | |
Serial.println(response.status); | |
// Convert Integer status code ( like 200,404 ) into char[] because Spark.publish(..) needs it | |
int aInt = response.status; | |
char status_string[15]; | |
sprintf(status_string, "%d", aInt); | |
Spark.publish("DEBUG",status_string); | |
Spark.publish("DEBUG",response.body); | |
} | |
bool isAwakeModeEnabled(){ | |
HttpClient http; | |
// Headers currently need to be set at init, useful for API keys etc. | |
http_header_t headers[] = { | |
// { "Content-Type", "application/json" }, | |
// { "Accept" , "application/json" }, | |
{ "Accept" , "*/*"}, | |
{ NULL, NULL } // NOTE: Always terminate headers will NULL | |
}; | |
http_request_t request; | |
http_response_t response; | |
Spark.publish("DEBUG","Checking stay_awake on server 128.199.225.112:80/greenopia/device/stay_awake/2f0047000747343337373738"); | |
// Our Greenopia Server. | |
request.hostname = "128.199.225.112"; | |
request.port = 80; | |
// The last number is the device ID. Hardcoded because there is a database entry currently for that. | |
// In production the code needs to dynamically append the actual device ID to the below variable. | |
request.path = "/greenopia/device/stay_awake/2f0047000747343337373738"; | |
// The library also supports sending a body with your request: | |
//request.body = "{\"key\":\"value\"}"; | |
// Get request | |
http.get(request, response, headers); | |
// Log to serial output | |
Serial.print("Application>\tResponse status: "); | |
Serial.println(response.status); | |
// Convert Integer status code ( like 200,404 ) into char[] because Spark.publish(..) needs it | |
int aInt = response.status; | |
char status_string[15]; | |
sprintf(status_string, "%d", aInt); | |
// DEBUG logs 200 or 400 .. HTTP status codes basically https://en.wikipedia.org/wiki/List_of_HTTP_status_codes | |
/* | |
event: DEBUG | |
data: {"data":"200","ttl":"60","published_at":"2015-10-27T17:35:11.667Z","coreid":"290044000447343232363230"} | |
*/ | |
Spark.publish("DEBUG",status_string); | |
/* | |
event: DEBUG | |
data: {"data":"{\"status\":\"success\",\"result\":{\"stay_awake\":\"N\"}}","ttl":"60","published_at":"2015-10-27T17:35:11.982Z","coreid":"290044000447343232363230"} | |
*/ | |
Spark.publish("DEBUG",response.body); | |
String json = response.body; | |
char json_array[1024]; | |
strncpy(json_array,json.c_str(),sizeof(json_array)); | |
json_array[sizeof(json_array)-1] = 0; | |
// The response.body is a string. We need to convert it into a JSON object. | |
// "{\"status\":\"success\",\"result\":{\"stay_awake\":\"N\"}}" | |
// is a string to be converted into a json object. | |
StaticJsonBuffer<200> jsonBuffer; | |
JsonObject& root = jsonBuffer.parseObject(json_array); | |
// Handle parsing failure - should never happen! | |
if (!root.success()) { | |
Serial.println("parseObject() failed"); | |
return false; | |
} | |
// Assuming, conversion to json was successful we try to access the stay_awake value which will be a [ 'Y' , 'N' ] | |
const char* sensor = root["result"]["stay_awake"]; | |
// Lets see how it looks. This is what gets printed in the console. | |
/* | |
event: DEBUG | |
data: {"data":"N","ttl":"60","published_at":"2015-10-27T17:28:31.617Z","coreid":"290044000447343232363230"} | |
*/ | |
Spark.publish("DEBUG",sensor); | |
// If no, return | |
if (strcmp(sensor,"N")==0 ){ | |
Spark.publish("DEBUG","Stay Awake mode : False"); | |
return false; | |
} | |
else { | |
Spark.publish("DEBUG","Stay Awake mode: True"); | |
return true; | |
} | |
return true; | |
} | |
void loop() { | |
Spark.publish("DEBUG","Entered loop"); | |
if (nextTime > millis()) { | |
return; | |
} | |
Serial.println(); | |
Serial.println("Application>\tStart of Loop."); | |
//isAwakeModeEnabled(); | |
sendDataToServer(); | |
nextTime = millis() + 30000; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// This #include statement was automatically added by the Particle IDE. | |
#include "SparkJson/SparkJson.h" | |
#include "application.h" | |
#include "HttpClient/HttpClient.h" | |
#include "string.h" | |
unsigned int nextTime = 0; // Next time to contact the server | |
void setup() { | |
Serial.begin(9600); | |
} | |
void sendDataToServer(){ | |
HttpClient http; | |
// Headers currently need to be set at init, useful for API keys etc. | |
http_header_t headers[] = { | |
{ "Content-Type", "application/json" }, | |
// { "Accept" , "application/json" }, | |
{ "Accept" , "*/*"}, | |
{ NULL, NULL } // NOTE: Always terminate headers will NULL | |
}; | |
http_request_t request; | |
http_response_t response; | |
Spark.publish("DEBUG","POST-ing variable data on server 128.199.225.112:80/greenopia/device/metricdata"); | |
// Our Greenopia Server. | |
request.hostname = "128.199.225.112"; | |
request.port = 80; | |
request.path = "/greenopia/device/metricdata"; | |
// The library also supports sending a body with your request: | |
request.body = "{\"device_id\":\"2f0047000747343337373738\",\"data\":[{\"type\":\"any_key\",\"value\":\"40\"}]}"; | |
// Get request | |
http.post(request, response, headers); | |
// Log to serial output | |
Serial.print("Application>\tResponse status: "); | |
Serial.println(response.status); | |
// Convert Integer status code ( like 200,404 ) into char[] because Spark.publish(..) needs it | |
int aInt = response.status; | |
char status_string[15]; | |
sprintf(status_string, "%d", aInt); | |
Spark.publish("DEBUG",status_string); | |
Spark.publish("DEBUG",response.body); | |
} | |
bool isAwakeModeEnabled(){ | |
HttpClient http; | |
// Headers currently need to be set at init, useful for API keys etc. | |
http_header_t headers[] = { | |
// { "Content-Type", "application/json" }, | |
// { "Accept" , "application/json" }, | |
{ "Accept" , "*/*"}, | |
{ NULL, NULL } // NOTE: Always terminate headers will NULL | |
}; | |
http_request_t request; | |
http_response_t response; | |
Spark.publish("DEBUG","Checking stay_awake on server 128.199.225.112:80/greenopia/device/stay_awake/2f0047000747343337373738"); | |
// Our Greenopia Server. | |
request.hostname = "128.199.225.112"; | |
request.port = 80; | |
// The last number is the device ID. Hardcoded because there is a database entry currently for that. | |
// In production the code needs to dynamically append the actual device ID to the below variable. | |
request.path = "/greenopia/device/stay_awake/2f0047000747343337373738"; | |
// The library also supports sending a body with your request: | |
//request.body = "{\"key\":\"value\"}"; | |
// Get request | |
http.get(request, response, headers); | |
// Log to serial output | |
Serial.print("Application>\tResponse status: "); | |
Serial.println(response.status); | |
// Convert Integer status code ( like 200,404 ) into char[] because Spark.publish(..) needs it | |
int aInt = response.status; | |
char status_string[15]; | |
sprintf(status_string, "%d", aInt); | |
// DEBUG logs 200 or 400 .. HTTP status codes basically https://en.wikipedia.org/wiki/List_of_HTTP_status_codes | |
/* | |
event: DEBUG | |
data: {"data":"200","ttl":"60","published_at":"2015-10-27T17:35:11.667Z","coreid":"290044000447343232363230"} | |
*/ | |
Spark.publish("DEBUG",status_string); | |
/* | |
event: DEBUG | |
data: {"data":"{\"status\":\"success\",\"result\":{\"stay_awake\":\"N\"}}","ttl":"60","published_at":"2015-10-27T17:35:11.982Z","coreid":"290044000447343232363230"} | |
*/ | |
Spark.publish("DEBUG",response.body); | |
String json = response.body; | |
char json_array[1024]; | |
strncpy(json_array,json.c_str(),sizeof(json_array)); | |
json_array[sizeof(json_array)-1] = 0; | |
// The response.body is a string. We need to convert it into a JSON object. | |
// "{\"status\":\"success\",\"result\":{\"stay_awake\":\"N\"}}" | |
// is a string to be converted into a json object. | |
StaticJsonBuffer<200> jsonBuffer; | |
JsonObject& root = jsonBuffer.parseObject(json_array); | |
// Handle parsing failure - should never happen! | |
if (!root.success()) { | |
Serial.println("parseObject() failed"); | |
return false; | |
} | |
// Assuming, conversion to json was successful we try to access the stay_awake value which will be a [ 'Y' , 'N' ] | |
const char* sensor = root["result"]["stay_awake"]; | |
// Lets see how it looks. This is what gets printed in the console. | |
/* | |
event: DEBUG | |
data: {"data":"N","ttl":"60","published_at":"2015-10-27T17:28:31.617Z","coreid":"290044000447343232363230"} | |
*/ | |
Spark.publish("DEBUG",sensor); | |
// If no, return | |
if (strcmp(sensor,"N")==0 ){ | |
Spark.publish("DEBUG","Stay Awake mode : False"); | |
return false; | |
} | |
else { | |
Spark.publish("DEBUG","Stay Awake mode: True"); | |
return true; | |
} | |
return true; | |
} | |
void loop() { | |
Spark.publish("DEBUG","Entered loop"); | |
if (nextTime > millis()) { | |
return; | |
} | |
Serial.println(); | |
Serial.println("Application>\tStart of Loop."); | |
//isAwakeModeEnabled(); | |
sendDataToServer(); | |
nextTime = millis() + 30000; | |
} |
Format for sending device metrics:
{
"device_id":"2f0047000747343337373738",
"data":[
{
"type":"sunlight",
"value":"40"
},
{
"type":"water",
"value":"30"
}
,
{
"type":"temp",
"value":"50"
}
]
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The following 3rd party libraries were added to the project:
HTTPCLIENT
SPARKJSON
Methods:
isAwakeModeEnabled()
sendDataToServer()