Skip to content

Instantly share code, notes, and snippets.

@Tomega3
Created February 24, 2015 13:14
Show Gist options
  • Save Tomega3/2f9e208f61da69edafbf to your computer and use it in GitHub Desktop.
Save Tomega3/2f9e208f61da69edafbf to your computer and use it in GitHub Desktop.
// Copyright Benoit Blanchon 2014
// MIT License
//
// Arduino JSON library
// https://github.com/bblanchon/ArduinoJson
// test environment
// arduino uno r3
// arduino ide 1.06
// from ArduinoJson ChangeLOg.md
//Arduino JSON: change log
//========================
//v4.2
//----
//
/***********************************
Possible memory leak or issue during parse:
Look at the messages on serial monitor which say:
Serial.print(F("json buffer contents before parse: "));
Serial.println(json);
// copy the char array to a temp buffer
// because it appears that ArduinoJson lib somehow truncates it
snprintf_P(espbuffer, (sizeof(espbuffer)), PSTR("%s"), json);
Serial.print(F("json buffer contents in temp buffer before parse: "));
and
Serial.print(F("json temp buffer contents after parse: "));
Serial.println(espbuffer);
after the parse the espbuffers' contents appear to be truncated.
also:
with StaticJsonBuffer<300> jsonBuffer;
declared as a global variable, the parse reports failure after the 4th or 5th time thru the loop.
with it declared inside the parse function ( parseTestDataJSON)
no reported parse failures are seen but the
espbuffer is still appears to be truncated.
***********************************/
#include <ArduinoJson.h>
//StaticJsonBuffer<300> jsonBuffer;
const char AppVer[6] = "V1.01";
#define ESP_BUFFER_SIZE 300
char espbuffer[ESP_BUFFER_SIZE];
char json[] = "{\"sensor\":\"gps\",\"time\":1351824120,\"data\":[48.756080,2.302038]}";
const int MAXARRAYSIZE = 80; // 63 chars plus 1 for \0 terminator
const int MAXNUMCHARS = MAXARRAYSIZE - 1; // leave room for null terminator
char tempoutString1[MAXARRAYSIZE]; // for formatting data
void setup()
{
Serial.begin(115200);
Serial.flush();
showAppVersion();
showFreeSram();
/*******************************************
JsonObject& root = jsonBuffer.parseObject(json);
if (!root.success())
{
Serial.println("parseObject() failed");
return;
}
const char* sensor = root["sensor"];
long time = root["time"];
double latitude = root["data"][0];
double longitude = root["data"][1];
Serial.println(sensor);
Serial.println(time);
Serial.println(latitude, 6);
Serial.println(longitude, 6);
**********************************************/
}
void loop()
{
showFreeSram();
parseTestDataJSON(json);
delay(10000); // ten second delay
}
boolean parseTestDataJSON(char* json) // for some reason the compiler.linker/preprocessor will not accept a define for buffer size
{
StaticJsonBuffer<300> jsonBuffer;
Serial.print(F("json buffer contents before parse: "));
Serial.println(json);
// copy the char array to a temp buffer
// because it appears that ArduinoJson lib somehow truncates it
snprintf_P(espbuffer, (sizeof(espbuffer)), PSTR("%s"), json);
Serial.print(F("json buffer contents in temp buffer before parse: "));
Serial.println(espbuffer);
JsonObject& root = jsonBuffer.parseObject(espbuffer);
Serial.print(F("json temp buffer contents after parse: "));
Serial.println(espbuffer);
if (!root.success())
{
Serial.println("parseObject() failed");
return false;
}
const char* sensor = root["sensor"];
long time = root["time"];
double latitude = root["data"][0];
double longitude = root["data"][1];
char myBuf1[20] = "";
char myBuf2[20] = "";
snprintf_P(tempoutString1, (MAXARRAYSIZE - 1), PSTR("Sensor: %s"), sensor);
Serial.println(tempoutString1);
snprintf_P(tempoutString1, (MAXARRAYSIZE - 1), PSTR("Time: %d"), time);
Serial.println(tempoutString1);
snprintf_P(tempoutString1, (MAXARRAYSIZE - 1), PSTR("Latitude: %s, Longitude: %s"), ftoa(myBuf1, latitude, 2), ftoa(myBuf2, longitude, 2));
Serial.println(tempoutString1);
}
//******** Utilities ************************
void showAppVersion()
{
Serial.print(F("App Version: "));
Serial.println(AppVer);
Serial.print(F("Filename ... "));
Serial.println(__FILE__); // print out the sketches file name
Serial.print(F("Compile Date and Time: "));
Serial.print(__DATE__);
Serial.print(F(" "));
Serial.println(__TIME__);
}
int freeRam()
{
extern int __heap_start, *__brkval;
int v;
return (int) &v - (__brkval == 0 ? (int) &__heap_start : (int) __brkval);
}
void showFreeSram()
{
Serial.print(F("Free Ram: "));
Serial.println(freeRam());
}
char *ftoa(char *a, double f, int precision)
{
long p[] = {0,10,100,1000,10000,100000,1000000,10000000,100000000};
char *ret = a;
long heiltal = (long)f;
itoa(heiltal, a, 10);
while (*a != '\0') a++;
*a++ = '.';
long desimal = abs((long)((f - heiltal) * p[precision]));
itoa(desimal, a, 10);
return ret;
}
//*********** end utility stuff *****
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment