Here is what I'm trying to do: I have a django app with a REST api on it. One of the routers returns a json array that looks like this : [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0]
and will always be the same size and containing integers.
Now, I have an Arduino Uno with an Adafruit cc3000 wifi shield on it. I manage to connect to the wifi, do my get request and print the result, even save it in a char *.
My first problem is:
I'm pretty sure I'm not constructing my char array the right way (at least, not the proper way, I don't have a lot of experience with C, C++ etc.) if that's an excuse). And I think this prevents me to parse the json array.
#include <ArduinoJson.h>
#include <Adafruit_CC3000.h>
#include <ccspi.h>
#include <SPI.h>
#include <string.h>
#include <Adafruit_NeoPixel.h>
int PIN = 6;
Adafruit_NeoPixel strip = Adafruit_NeoPixel(60, PIN, NEO_GRB + NEO_KHZ800);
StaticJsonBuffer<600> jsonBuffer;
#define ADAFRUIT_CC3000_IRQ 3
// These can be any two pins
#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 "mywifi"
#define WLAN_PASS "mypwd"
#define WLAN_SECURITY WLAN_SEC_WPA2
uint32_t ip = cc3000.IP2U32(00,00,00,00);
void setup(void)
{
Serial.begin(9600);
if (!cc3000.begin())
{
while(1);
}
strip.begin();
strip.show();
char *ssid = WLAN_SSID;
Serial.print(F("\nAttempting to connect to "));
Serial.println(ssid);
cc3000.connectToAP(WLAN_SSID, WLAN_PASS, WLAN_SECURITY);
Serial.println("Connected to WiFi network!");
Serial.println(F("Request DHCP"));
while (!cc3000.checkDHCP())
{
delay(3000);
}
}
void loop(void){
int lap = 0;
if(!cc3000.checkConnected()){
while(1){
}
}
char *result;
result = send_request();
result[71] = ']';
JsonArray& root = jsonBuffer.parseArray(result);
if (!root.success())
{
Serial.println("parseObject() failed");
}
while(lap<61){
for (int i=0; i<24; i++){
long is_free = root[i];
if (is_free == 0){
strip.setPixelColor(i, 255, 0, 0);
}
if(is_free == 1){
strip.setPixelColor(i, 0, 255, 0);
}
if(lap % 2 ==0){
if(is_free == 2){
strip.setPixelColor(i, 0, 255, 0);
}
if(is_free == 3){
strip.setPixelColor(i, 0, 255, 0);
}
}
else{
if(is_free == 2){
strip.setPixelColor(i, 0, 0, 0);
}
if(is_free == 3){
strip.setPixelColor(i, 0, 0, 0);
}
}
}
strip.setBrightness(15);
strip.show();
lap += 1;
delay(1000);
}
}
char* send_request (void)
{
char result[100];
Serial.print(F("Initializing SendGET request\n"));
Adafruit_CC3000_Client client = cc3000.connectTCP(ip, 8000);
if (client.connected()) {
//starts client connection, checks for connection
Serial.println(F("Adding state to DB\n"));
client.println("GET /myurl/?format=json"); //download text
client.println(F("Host: xx.xx.xx.xx"));
client.println("Connection: close"); //close 1.1 persistent connection
client.println(); //end of get request
Serial.println(F("Ending connection to DB\n"));
}
else {
Serial.println("Connection to server failed"); //error message if no client connect
Serial.println();
}
int i = 0;
while (client.connected()){
while (client.available()) {
if (i<71){
//Read answer
char c = client.read();
result[i] = c ;
i++;
}
else{
client.close();
return result;
}
}
}
}
To me send_request()
is really dirty since I decided that i
needed to be smaller than 71 because the result of this that I'd print looked like what I want. But I think the array is the wrong size and that this is why I can't parse it in the loop()
function.
What is the good, nice, clear, precise way to do this, please? I have tried so many wrong things I'm running out of ideas.
Second problem is:
If I replace
char *result;
result[71] = ']';
by result = "[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0]";
And then print is_free
, I get random values like:
91
49
44
32
48
44
32
48
44
32
48
44
32
48
etc.
So my guess is either I'm just not using ArduinoJson properly, either I'm not looking at the value at the right place. I'm looking for insights about that
Thanks for reading and the future eventual help!