[SOLVED] Reading data from a variable using the ESP8266

Hi,

Could you help me out on how exactly to parse the response of the API notation to get the last value of a variable
GET /api/v1.6/variables/{variable_id}/values/?token={token}&page_size=1

I’m using an ESP8266 hooked on to an Arduino and I am trying out the code given here:
http://ubidots.com/docs/devices/ESP8266-arduino.html

I have not been successful so far in parsing values from the serial buffer and printing them out on the Serial monitor.

Any help would be appreciated!

Hi @fusdorah just saw your message while looking through the forums. I’m not familiar with the ESP but here a standard pseudocode (arduino-like) to parse a JSON from Ubidots:

// Build HTTP GET request
    c.print(F("GET /api/v1.6/variables/"VARID1"/values/?page_size=1&token="));
    c.print(TOKEN);
    c.println(F(" HTTP/1.1"));
    c.println(F("Content-Type: application/json"));
    c.print(F("Host: "));
    c.println(URL);
    c.println();
    
    int v;
    while(c.available()){  
      v = c.read();
      if(v < 0){
        Serial.println("No response.");
        break;
      }
      response.concat((char)v);    // Response is an empty string where you allocate all incoming chars from server.
    }
    Serial.println("Printing response:");
    value_index = response.indexOf("\"value\": ");    // Here you get the index to split the response string right where it says "value"
    value_string = response.substring(value_index);
    value = value_string.substring(9, value_string.indexOf(","));   // Here you ignore the first nine characters of the substring ("-v-a-l-u-e-"-: ), then get the value, then ignore the rest of the string after the comma.
    Serial.println(value);

Check out our latest ESP8266 library which supports GET requests: http://ubidots.com/docs/devices/#esp8266

Este topic está ahora cerrado. No se admiten nuevas respuestas