Skip to content

Instantly share code, notes, and snippets.

@YongHuax
Last active February 28, 2018 14:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save YongHuax/71500957d143c0c18bbe6e1799551a7c to your computer and use it in GitHub Desktop.
Save YongHuax/71500957d143c0c18bbe6e1799551a7c to your computer and use it in GitHub Desktop.
LG01_P_JSON_AWSIOT
//References
//https://jsonlint.com/
//https://forums.aws.amazon.com/thread.jspa?threadID=239208
//https://arduinojson.org/assistant/
//https://arduinojson.org/faq/how-to-reuse-a-jsonbuffer/?utm_source=github&utm_medium=issues
//https://github.com/bblanchon/ArduinoJson/issues/508
//https://github.com/bblanchon/ArduinoJson/blob/master/examples/StringExample/StringExample.ino
//https://github.com/bblanchon/ArduinoJson/tree/master/examples/JsonGeneratorExample
#include <ArduinoJson.h>
#include <SPI.h>
#include <Console.h>
#include <Process.h>
void setup() {
Bridge.begin(115200);
Console.begin();
while (!Console) continue;
}
// { \”state\” : { \ “ reported \ ” : { \”light\” : \”on1 \”}}}
void loop() {
const size_t bufferSize = 3*JSON_OBJECT_SIZE(1);
DynamicJsonBuffer jsonBuffer(bufferSize);
JsonObject& root = jsonBuffer.createObject();
JsonObject& state = root.createNestedObject("state");
JsonObject& state_reported = state.createNestedObject("reported");
state_reported["light"] = "on1";
//x`JsonArray& data = root.createNestedArray("state");
String jsonstr;
root.printTo(jsonstr);
Console.println(jsonstr);
uploadData(jsonstr);
}
void uploadData(String uploaddata1) {//Upload Data to ThingSpeak
// void uploadData(String uploaddata1,String uploaddata2)
Console.println("Call Linux Command to Send Data");
Process p; // Create a process and call it "p", this process will execute a Linux curl command
String upload_url = uploaddata1;
// String upload_url2 = uploaddata2;
p.begin("/root/testAWS"); //Script execution.In this example I use the script name is Test.
p.addParameter(upload_url);
//p.addParameter(upload_url2);
p.run();
//Console.print("Feedback from Linux: ");
// If there's output from Linux,
// send it out the Console:
while (p.available()>0)
{
char c = p.read();
Console.write(c);
}
Console.println("AWSIot");
Console.println("Call Finished");
Console.println("####################################");
Console.println("");
delay(10000) ;
}
// See also
// --------
//
// The website arduinojson.org contains the documentation for all the functions
// used above. It also includes an FAQ that will help you solve any
// Consoleization problem.
// Please check it out at: https://arduinojson.org/
//
// The book "Mastering ArduinoJson" contains a tutorial on Consoleization.
// It begins with a simple example, like the one above, and then adds more
// features like Consoleizing directly to a file or an HTTP request.
// Please check it out at: https://arduinojson.org/book/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment