I want to take a server-generated JSON string and parse that information to a couple of analogWrite values for a pair of LEDs. I could use your assistance, advice, or code examples of how to properly place the returned JSON strings into respective analogWrite values.
An example of the output from the server produces this simple JSON string:
{“rep”:65,“dem”:58}
I have control over how the information is presented, so it doesn’t necessarily need to be a JSON string, it could be simply only two sets of numbers if that is easier to handle.
Here’s the current code written so far for the Particle Core:
#include "HttpClient.h"
#include "application.h"
/**
* Declaring the variables.
*/
unsigned int nextTime = 0; // Next time to contact the server
HttpClient http;
// Headers currently need to be set at init, useful for API keys etc.
http_header_t headers[] = {
// { "Content-Type", "text/plain" },
{ "Accept" , "application/json" },
{ "Accept" , "*/*"},
{ NULL, NULL } // NOTE: Always terminate headers will NULL
};
int led1 = D0;
int led2 = D1;
http_request_t request;
http_response_t response;
void setup() {
Serial.begin(9600);
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
}
void loop() {
if (nextTime > millis()) {
return;
}
Serial.println();
request.hostname = "wthr.im";
request.port = 80;
request.path = "/poli/baro.php";
// Get request
http.get(request, response, headers);
Serial.println(response.body);
analogWrite(led1, XXX); //First set of numbers parsed from server go here
analogWrite(led2, XXX); //Second set of numbers parsed from server go here
nextTime = millis() + 60000;
}
I look forward to any suggestions you may provide. Thank you