Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

No serial output #7

Open
beicnet opened this issue Sep 25, 2021 · 11 comments
Open

No serial output #7

beicnet opened this issue Sep 25, 2021 · 11 comments

Comments

@beicnet
Copy link

beicnet commented Sep 25, 2021

Hi there,

I tried to test the "json_org.ino", but it doesn't give me any Json output on the Serial terminal, I'm using Arduino IDE v1.8.2 for reference.

image

Any ideas?

Kind regards,
Viktor

@wyojustin
Copy link
Member

Hmmm, I don't know why. Sorry about that. Can you try printing glossentry, glossdiv, and glossary?

@beicnet
Copy link
Author

beicnet commented Sep 25, 2021

@wyojustin Thank you for your fast reply!

No, there is nothing, something is erasing the whole Json String in the "jsonRemoveWhiteSpace" function.

I really need this library! Could you please fix it? I don't wanna use the heavy ArduinoJson library.

image

@wyojustin
Copy link
Member

I will be able to test... but it might be tomorrow. Do any of the other test work?

@beicnet
Copy link
Author

beicnet commented Sep 25, 2021

Just tested them all and neither of them are working!

Also, in the "simple_json.ino" there is a "Serial.printf" function at the end and it will not work on Uno, etc...

btw.. I'm using v0.1.2 downloaded from here.

@beicnet
Copy link
Author

beicnet commented Sep 25, 2021

And your repository is wrong version in the current "library.json" file, it says v0.1.1 ?!

image

@wyojustin
Copy link
Member

Seems like a bug in the String class.
Serial.print("name:"); Serial.println(name); Serial.print("name:"); Serial.println(String("\"") + name);
Prints
name:weather name:
Looking for work around.

@beicnet
Copy link
Author

beicnet commented Sep 26, 2021

@wyojustin I hope you can fix and optimize it and make it lightweight as possible, I wish to use it on ATtiny85 too.

@wyojustin
Copy link
Member

Thanks @beicnet . Looks like a bug with String class. I can find "coords", unless I print a couple of lines.

`jsonExract.name::"coord"
jsonExract.start::9

coord:
{"lon":-77.35,"lat":38.93}
works with this code...void setup(){

Serial.begin(115200);

String coord = jsonExtract(json, "coord");
Serial.println();
Serial.println("coord:");
Serial.println(coord);
return;
}
`

but not with this code:
`void setup(){

Serial.begin(115200);
Serial.println("################################################################################");
Serial.println("Simple json library offered by wyolum.com\n\n");

String coord = jsonExtract(json, "coord");
Serial.println();
Serial.println("coord:");
Serial.println(coord);
return;
################################################################################
Simple json library offered by wyolum.com

jsonExract.name::"coord"
jsonExract.start::7

coord:

`

@wyojustin
Copy link
Member

The extra println's break the simple example. Only place now is to stop using the String class (which I've been warned against using in the first place).

@beicnet
Copy link
Author

beicnet commented Sep 26, 2021

Ok, simple "jsonExtract" function working on clean one line Json String, but would it work as same if it has spaces or the example shown below?

{
  "Device": {
    "Sensors": {
      "temperature": 27.48,
      "humidity": 47.71,
      "pressure": 1008.24,
      "ppb": "0",
      "lux": "65",
      "adc": 0
    },
    "Wireless": {
      "quality": "18",
      "rssi": "-91",
      "channel": "6",
      "mode": "Station",
      "type": "802.11N"
    },
    "Firmware": {
      "version": "2.6",
      "time": "11:40:45",
      "date": "Sep 21 2021",
      "size": "272",
      "freesize": "748"
    },
    "System": {
      "freemem": "38",
      "reset": "Software/System restart",
      "sensordatapush": "Enabled",
      "uptime": "5 days 05:21:43"
    }
  }
}

@wyojustin
Copy link
Member

You might be able to get this to work. It only uses char[]. I gave up on String class. I'm going to leave this for now. Good luck. Hope to get back to this later.

`void delete_whitespace(char* source){
int i;
int j = 0;
int n = strlen(source);
Serial.println(n);
Serial.println("here");
for(i = 0; i < n; i++){
if(source[i] == ' ' ||
source[i] == '\t' ||
source[i] == '\n'){
}
else{
source[j++] = source[i];
}
}

}
void substring(char* source, char* dest, int start, int stop){
for(int i=start; i<stop; i++){
dest[i - start] = source[i];
}
dest[stop - start] = 0;
}

String jsonIndexList(char* json, int idx, char* out){
int count = 1; // number of braces seen { = +1 } = -1
int n = strlen(json);
int i = 1;
int item_idx = 0;
int start = i;
int stop = strlen(n) - 1;

while(i < n && count > 0){
if(json[i] == ']' or json[i] == '}'){
count--;
}
if(json[i] == '{' or json[i] == '['){
count++;
}
if(count == 1 && json[i] == ',' && item_idx == idx){
//item separator!
stop = i;
substring(json, out, start, stop);
return;
}
if(count == 0 && json[i] == ']' && item_idx == idx){
stop = i + 1;
substring(json, out, start, stop);
return;
}
if(count == 1 && json[i] == ','){
item_idx++;
start = i + 1;
}
i++;
}
substring(json, out, start, stop);
return;
}

bool equal(char* a, char* b, int k){
bool out = true;
for(int i=0; i<k; i++){
if(a[i] != b[i]){
out = false;
break;
}
}
return out;
}

int indexOf(char* source, char* search, int start){
int n = strlen(source);
int k = strlen(search);

for(int i = start; i < n; i++){
if(equal(source + i, search, k)){
return i;
}
}
return -1;
}

void copy(char* source, char* dest, int k){
for(int i=0; i<k; i++){
dest[i] = source[i];
}
dest[k] = 0;
}
void jsonExtract(char* json, char* name, char* out){
char next;
int start, stop;

int k = strlen(name);
int n = strlen(json);
char qname[k + 3];
qname[0] = '"';
qname[k] = '"';
copy(name, qname+1, k);
start = indexOf(json, qname, 0) + k + 3;
next = json[start];
//Serial.print("next::");Serial.println(next);
if(next == '"'){
//Serial.println(".. a string");
start = start + 1;
stop = indexOf(json, """, start + 1);
}
else if(next == '['){
//Serial.println(".. a list");
int count = 1;
int i = start;
while(count > 0 && i++ < n){
if(json[i] == ']'){
count--;
}
else if(json[i] == '['){
count++;
}
}
stop = i + 1;
}
else if(next == '{'){
//Serial.println(".. a struct");
int count = 1;
int i = start;
while(count > 0 && i++ < n){
if(json[i] == '}'){
count--;
}
else if(json[i] == '{'){
count++;
}
}
stop = i + 1;
}
else if(next == '.' || next == '-' || ('0' <= next && next <= '9')){
//Serial.println(".. a number");
int i = start;
while(i++ < n && json[i] == '.' || ('0' <= json[i] && json[i] <= '9')){
}
stop = i;
}
else{
Serial.print(name);
Serial.println(" not found.");
Serial.print(qname);
Serial.println("?");
}
/*
Serial.print("start:");
Serial.print(start);
Serial.print(" stop:");
Serial.println(stop);
Serial.print(":::");
for(int kk=start; kk<stop; kk++){
Serial.print(json[kk]);
}
Serial.println();
*/
return copy(json + start, out, stop - start);
}

char *json = "{"coord":{"lon":-77.35,"lat":38.93},\n"
" "weather":[{"id":500,"main":"Rain","description":"light rain","icon":"10d"},\n"
" {"id":501,"main":"Rain","description":"light rain","icon":"10d"}],\n"
" "base":"stations","main":{"temp":286.92,"feels_like":287.06,"temp_min":283.15,"temp_max":292.04,\n"
" "pressure":1003,"humidity":100},"visibility":16093,"wind":{"speed":1.5,"deg":20},"rain":{"1h":0.46},\n"
" "clouds":{"all":75},"dt":1577728963,"sys":{"type":1,"id":4624,"country":"US",\n"
" "sunrise":1577708860,"sunset":1577742942},"timezone":-18000,"id":4781530,"name":"Reston","cod":200}\n";

void setup(){
delete_whitespace(json);

Serial.begin(115200);
Serial.println("################################################################################");
Serial.println("Simple json library offered by wyolum.com\n\n");
Serial.println(json);
Serial.println();
Serial.println();

char coord[100];
char weather[200];
char lat[10];
char lon[10];
char weather_0[200];
char weather_1[200];

jsonExtract(json, "coord", coord);
jsonExtract(json, "weather", weather);
Serial.println("weather:");
Serial.println(weather);
jsonIndexList(weather, 0, weather_0);
jsonIndexList(weather, 1, weather_1);

jsonExtract(coord, "lat", lat);
jsonExtract(coord, "lon", lon);

Serial.println();

Serial.print("coord:");
Serial.println(coord);

Serial.print("lat:");
Serial.println(lat);

Serial.print("lon:");
Serial.println(lon);

/*
*/
Serial.println("weather_0:");
Serial.println(weather_0);
Serial.println("weather_1:");
Serial.println(weather_1);
return;
}

void loop(){
}
`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants