1

I am using Arduino Uno with Adafruit CC3000 wifi shield to connect to the internet and get data for Arduino. Everything works fine but I can't figure out how to use HttpClient library. The code is:

#include <Adafruit_CC3000.h>
#include <SPI.h>
#include "utility/debug.h"
#include "utility/socket.h"
#include <HttpClient.h>

#define ADAFRUIT_CC3000_IRQ   3  
#define ADAFRUIT_CC3000_VBAT  5
#define ADAFRUIT_CC3000_CS    10
Adafruit_CC3000 cc3000 = Adafruit_CC3000(ADAFRUIT_CC3000_CS,    ADAFRUIT_CC3000_IRQ, ADAFRUIT_CC3000_VBAT,SPI_CLOCK_DIVIDER);

#define WLAN_SSID       "ssid"          
#define WLAN_PASS       "pwd"
#define WLAN_SECURITY   WLAN_SEC_WPA2


void setConnection(){

     Serial.println("Initializing...");
     if (!cc3000.begin())
     {
       Serial.println("Couldn't begin()! Check your wiring?");
       while(1);
     }
     Serial.print("Attempting to connect to ");
     Serial.println(WLAN_SSID);
     if (!cc3000.connectToAP(WLAN_SSID, WLAN_PASS, WLAN_SECURITY)) {
       Serial.println("Failed!");
       while(1);
     }  
     Serial.println("Connected!");  
     Serial.println(F("Request DHCP"));
     while (!cc3000.checkDHCP())
      {
          delay(100);
       }  
     Serial.println(F("DHCP success!!"));
 }

void dweet(){

  int ctr=0;
  char buff[100];
  int x=0;
  const char* host = "https://dweet.io/get/latest/dweet/for/dunebot";

  Serial.println(F("Initializing dweet client ..."));
  HttpClient client;
  client.get(host);
  Serial.println(F("Client Connected !"));

  while (client.available()) {
    char c = client.read();
    Serial.write(c);
    delay(100);
  }


void setup(){

     Serial.begin(115200);    
     while(!Serial);      
     Serial.print("Free RAM: "); Serial.println(getFreeRam(), DEC);
     setConnection();

}

void loop(){
dweet();  
}

If all worked fine then I should have got a json string which is:

{"this":"succeeded","by":"getting","the":"dweets","with":[{"thing":"dunebot","created":"2018-05-12T13:27:56.106Z","content":{"0001":""}}]}

When I run the code, Arduino connects to the wifi network but afterwards when I observe the Serial monitor all I get are garbage values.

enter image description here

I need a way to read the key 0001 from the json string. Any help would be appreciated.Thanks.

1 Answer 1

1

The HttpClient object must be instantiated with a Client object which will handle the underlying connection. Source:

Requires a networking hardware and a library that provides transport specific Client instance, such as:

Refer to the following code:

In the above example, it is done like

WiFiClient wifi;
HttpClient client = HttpClient(wifi, serverAddress, port);

Which means that for your code, you might want to pull out client as global object and instantiate it as:

Adafruit_CC3000 cc3000 = Adafruit_CC3000(ADAFRUIT_CC3000_CS,    ADAFRUIT_CC3000_IRQ, ADAFRUIT_CC3000_VBAT,SPI_CLOCK_DIVIDER);
/* Create new Adafruit_CC3000_Client object */
Adafruit_CC3000_Client cc3000_client;
/* Feed this into the client */
HttpClient client (cc3000_client, "dweet.io", 80);

You should also use the example code to see how to print the response after executing client.get():

  // read the status code and body of the response
  statusCode = client.responseStatusCode();
  response = client.responseBody();
  Serial.print("Status code: ");
  Serial.println(statusCode);
  Serial.print("Response: ");
  Serial.println(response);

The example code is pretty much example what you want, you just have to give it a Adafruit_CC3000_Client as the Client.

Full code:

#include <Arduino.h>
#include <Adafruit_CC3000.h>
#include <SPI.h>
#include "utility/debug.h"
#include "utility/socket.h"
#include <HttpClient.h>

#define ADAFRUIT_CC3000_IRQ   3
#define ADAFRUIT_CC3000_VBAT  5
#define ADAFRUIT_CC3000_CS    10
Adafruit_CC3000 cc3000 = Adafruit_CC3000(ADAFRUIT_CC3000_CS,    ADAFRUIT_CC3000_IRQ, ADAFRUIT_CC3000_VBAT,SPI_CLOCK_DIVIDER);
/* Create new Adafruit_CC3000_Client object */
Adafruit_CC3000_Client cc3000_client;
/* Feed this into the client */
HttpClient client (cc3000_client, "dweet.io", 80);

#define WLAN_SSID       "ssid"
#define WLAN_PASS       "pwd"
#define WLAN_SECURITY   WLAN_SEC_WPA2

int statusCode = 0;
String response;

void setConnection(){

    Serial.println("Initializing...");
    if (!cc3000.begin())
    {
        Serial.println("Couldn't begin()! Check your wiring?");
        while(1);
    }
    Serial.print("Attempting to connect to ");
    Serial.println(WLAN_SSID);
    if (!cc3000.connectToAP(WLAN_SSID, WLAN_PASS, WLAN_SECURITY)) {
        Serial.println("Failed!");
        while(1);
    }
    Serial.println("Connected!");
    Serial.println(F("Request DHCP"));
    while (!cc3000.checkDHCP())
    {
        delay(100);
    }
    Serial.println(F("DHCP success!!"));
}

void dweet(){

    int ctr=0;
    char buff[100];
    int x=0;
    const char* host = "https://dweet.io/get/latest/dweet/for/dunebot";

    Serial.println(F("Initializing dweet client ..."));

    client.get(host);
    Serial.println(F("Client Connected !"));

    // read the status code and body of the response
    statusCode = client.responseStatusCode();
    response = client.responseBody();
    Serial.print("Status code: ");
    Serial.println(statusCode);
    Serial.print("Response: ");
    Serial.println(response);

    /*
    Typical response is:
    {"this":"succeeded",
    "by":"getting",
    "the":"dweets",
    "with":[{"thing":"my-thing-name",
      "created":"2016-02-16T05:10:36.589Z",
      "content":{"sensorValue":456}}]}
    You want "content": numberValue
    */
    // now parse the response looking for "content":
    int labelStart = response.indexOf("content\":");
    // find the first { after "content":
    int contentStart = response.indexOf("{", labelStart);
    // find the following } and get what's between the braces:
    int contentEnd = response.indexOf("}", labelStart);
    String content = response.substring(contentStart + 1, contentEnd);
    Serial.println(content);

    //There was a missing brace here. Was the code cut off?!
}

void setup(){
    Serial.begin(115200);
    while(!Serial);
    Serial.print("Free RAM: "); Serial.println(getFreeRam(), DEC);
    setConnection();
}

void loop() {
    dweet();
}
11
  • So i did what you told but it throws an error no matching function for call to 'HttpClient::HttpClient(Adafruit_CC3000&, const char [9], int)'. Does HttpClient work with Adafruit cc3000? Commented May 13, 2018 at 14:12
  • @AshishKafle in the library code I linked it says that it derives from the class Client. I will test this my self in just a moment. Commented May 13, 2018 at 14:14
  • @AshishKafle Could you retry that again. I referenced the Adafruit_CC3000 class when it should have been the Adafruit_CC3000_Client class. Commented May 13, 2018 at 14:28
  • the code works nice but after Serial.println(F("Client Connected !")); nothing seems to work for me. After that i get nothing. Commented May 13, 2018 at 15:04
  • Could you try this sketch and post the output on pastebin. pastebin.com/2kUqACgB Commented May 13, 2018 at 15:23

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.