[SOLVED] How to Parse Numbers from JSON into analogWrite Values?

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 :smile:

Tokenize it. Look at the example in this tutorial. I take a JSON response and map it to a var.

2 Likes

@LukeUSMC - Yes! Your tutorial solved my problem. I’ve listed my solution based on your code below.

This code works for my needs and I’m always looking to improve it. Does anyone have suggestions on how this may be simplified (if it needs it at all)?


#include "HttpClient.h"
#include "application.h"

/**
* Declaring the variables.
*/
unsigned int nextTime = 0;    // Next time to contact the server
HttpClient http;

http_header_t headers[] = {
    { "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);

    String str = String(response.body);
    char strBuffer[125] = "";
    str.toCharArray(strBuffer, 125);
    int rep_light = atoi(strtok(strBuffer, "\"~"));
    int dem_light = atoi(strtok(NULL, "~"));

    analogWrite(led1, rep_light);
    analogWrite(led2, dem_light);

    nextTime = millis() + 3600000; //
}
1 Like

Additionally, I did have to change the outputted text from the server from a properly formatted JSON string into something shorter. Instead of:

{“rep”:65,“dem”:58}

…the server generates the following:

65~58