0

I am trying to parse JSON using the ArduinoJson library. The problem is that I can’t parse www.read() because it’s declared as a char. I think www.read() needs to be a string? How can I convert www.read() into a format that jsonBufffer.parseObject() can understand? Please excuse any lack of terminology. I am new to using JSON.

When I run the code I get to the point where it prints "-------------------------------------” then it does nothing.

#include <Adafruit_CC3000.h>
#include <ccspi.h>
#include <SPI.h>
#include <string.h>
#include "utility/debug.h"
#include <ArduinoJson.h>

// These are the interrupt and control pins
#define ADAFRUIT_CC3000_IRQ   3  // MUST be an interrupt pin!
// These can be any two pins
#define ADAFRUIT_CC3000_VBAT  5
#define ADAFRUIT_CC3000_CS    10
// Use hardware SPI for the remaining pins
// On an UNO, SCK = 13, MISO = 12, and MOSI = 11
Adafruit_CC3000 cc3000 = Adafruit_CC3000(ADAFRUIT_CC3000_CS,   
ADAFRUIT_CC3000_IRQ, ADAFRUIT_CC3000_VBAT,
SPI_CLOCK_DIVIDER); // you can change this clock speed

#define WLAN_SSID       "WLAN_SSID_HERE" // cannot be longer than 32    characters!
#define WLAN_PASS       "WLAN_PASS_HERE"
// Security can be WLAN_SEC_UNSEC, WLAN_SEC_WEP, WLAN_SEC_WPA or WLAN_SEC_WPA2
#define WLAN_SECURITY   WLAN_SEC_WPA2

#define IDLE_TIMEOUT_MS  300000      // Amount of time to wait (in milliseconds) with no data 
// received before closing the connection.  If you know the server
// you're accessing is quick to respond, you can reduce this value.

// What page to grab!
#define WEBSITE      "wefeel.csiro.au"
#define WEBPAGE      "/api/emotions/primary/totals"


StaticJsonBuffer<200> jsonBuffer;



uint32_t ip;

void setup(void)
{
  Serial.begin(115200);
  Serial.println(F("Hello, CC3000!\n")); 

  /* Initialise the module */
  Serial.println(F("\nInitializing..."));
  if (!cc3000.begin())
  {
    Serial.println(F("Couldn't begin()! Check your wiring?"));
    while(1);
  }


  Serial.print(F("\nAttempting to connect to ")); 
  Serial.println(WLAN_SSID);
   if (!cc3000.connectToAP(WLAN_SSID, WLAN_PASS, WLAN_SECURITY)) {
    Serial.println(F("Failed!"));
    while(1);
  }

  Serial.println(F("Connected!"));

  /* Wait for DHCP to complete */
  Serial.println(F("Request DHCP"));
  while (!cc3000.checkDHCP())
  {
    delay(100); // ToDo: Insert a DHCP timeout!
  }  

  /* Display the IP address DNS, Gateway, etc. */
//  while (! displayConnectionDetails()) {
 //    delay(1000);
//  }

  ip = 0;
  // Try looking up the website's IP address
  Serial.print(WEBSITE); 
  Serial.print(F(" -> "));
  while (ip == 0) {
    if (! cc3000.getHostByName(WEBSITE, &ip)) {
      Serial.println(F("Couldn't resolve!"));
    }
    delay(500);
   }

  cc3000.printIPdotsRev(ip);

}

void loop(void)
{
  char c;
  Adafruit_CC3000_Client www = cc3000.connectTCP(ip, 80);
  delay(300);
  if (www.connected()) {
    www.fastrprint(F("GET "));
    www.fastrprint(WEBPAGE);
    www.fastrprint(F(" HTTP/1.1\r\n"));
    www.fastrprint(F("Host: ")); 
    www.fastrprint(WEBSITE); 
    www.fastrprint(F("\r\n"));
    www.fastrprint(F("\r\n"));
    www.println();
  } 
  else {
    Serial.println(F("Connection failed"));    
    return;
  }

  Serial.println(F("-------------------------------------"));

  unsigned long lastRead = millis();
  while (www.connected() && (millis() - lastRead < IDLE_TIMEOUT_MS)) {
    //Serial.print(www.available());
    while (www.available()) {
  
  
      //Serial.print(c);
      //char json[] = www.read();
    }
  } 
  c = www.read();
  Serial.print(c);
 JsonObject& root = jsonBuffer.parseObject((char*)c);
 if (!root.success()) {
    Serial.println("parseObject() failed");
    return;
    }

  long love = root["love"];
  long other = root["other"];
  long sadness = root["sadness"];
  long anger = root["anger"];
  long joy = root["joy"];
  long surprise = root["surprise"];
  long fear = root["fear"];

  Serial.println(love);
  Serial.println(other);
  Serial.println(sadness);
  Serial.println(anger);
  Serial.println(joy);
  Serial.println(surprise);
  Serial.println(fear);


  www.stop();
  delay(100);
  www.close();
  delay(200);
 }
1
  • First you never setup the HTTP header with "Content-Type: application/json", so the data return might not be in json as you want. Secondly, you need to create a buffer to store the response back from the server within the while(www.available()) { buffer[i++]=www.read(); } (i.e. read the data char by char and store in the buffer), then you can parse the return data in the buffer with JsonObject& root=jsonBuffer.parseObject(buffer);.
    – hcheung
    Jan 21 at 8:50
0

There are two read methods. One that returns a single character (or error), and one that populates a buffer; you want the latter, with method signature int read(char* buf, size_t len) or some such. Read the source.

When allocating the json buffer in StaticJsonBuffer<200> jsonBuffer; , make sure your expected json data fit in 200 bytes.

3
  • Thank you for responding to my problem. I'm assuming you would use jsonBuffer as uint8_t *buf but what value do I use for size_t len?
    – Balockeh22
    May 11 '16 at 16:06
  • no, jsonBuffer has to still be a StaticJsonBuffer. It's what you read() into that should change. Something along the lines of: char buf[256]; size_t n_read = www.read(buf, 255); buf[n_read] = '\0'; You need the third line because parseObject expects a nul-terminated string.
    – JayEye
    May 12 '16 at 0:36
  • I really appreciate your help. I think Im following you but am still unclear what I need to do next. Im thinking it is calling jsonBuffer.ParseObject but I was thinking that I would have it parse n_read which seems to be incorrect? What variable should I perform the parse on?
    – Balockeh22
    May 18 '16 at 16:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.