Home
JAQForum Ver 20.06
Log In or Join  
Active Topics
Local Time 00:00 06 Dec 2022 Privacy Policy
Jump to

Notice. New forum software under development. It's going to miss a few functions and look a bit ugly for a while, but I'm working on it full time now as the old forum was too unstable. Couple days, all good. If you notice any issues, please contact me.

Forum Index : Microcontroller and PC projects : Micromite eXtreme 5.04.17: JSON parsing

Author Message
matherp
Guru

Joined: 11/12/2012
Location: United Kingdom
Posts: 6619
Posted: 01:25pm 08 Jun 2018
Copy link to clipboard 
Print this post

Please find attached MMX version 5.04.17 for the 100/144pin chip

2018-06-08_232342_MMX5.04.17.zip

and for the 64-pin chip

2018-06-08_232438_MMX645.04.17.zip

Updated manual

2018-06-08_234034_Micromite_eXtreme_Manual.pdf

This release includes the ability to parse JSON strings stored in a longstring. The attached program shows how I use this to read the weather from openweathermap.org using a ESP8266 with standard "AT" firmware. Substitute myAPIKEY, mySSID and myPASSWORD with your own values.

The release also includes a new sub command LONGSTRING PRINT [#n,] src%(). This prints the longstring stored in src%() to either a file, a serial port, or the console.

option explicit
option default none
Dim MYID$="2638878"
Dim APIKEY$="myAPIKEY"
Dim report$="weather"
dim b$= "GET /data/2.5/"+report$+"?id="+MYID$+"&APPID="+APIKEY$
Dim m%(8000)
b$=b$+" HTTP/1.0"+Chr$(13)+Chr$(10)+Chr$(13)+Chr$(10)

open "com1:115200,8192" as #1
LongString clear m%()
print #1,"AT+CWMODE_CUR=1"+chr$(13)+chr$(10);
if NOT OKwait(4000) then errorexit("AT+CWMODE_CUR=1")
print #1,"AT+CWJAP_CUR="+chr$(34)+"mySSID"+chr$(34)+","+chr$(34)+"myPASSWORD"+chr$(34)+chr$(13)+chr$(10);
if NOT OKwait(10000) then errorexit("AT+CWJAP_CUR=")
print #1,"AT+CIPSTART="+chr$(34)+"TCP"+chr$(34)+","+chr$(34)+"api.openweathermap.org"+chr$(34)+",80"+chr$(13)+chr$(10);
if NOT OKwait(10000) then errorexit("AT+CIPSTART=")
print #1,"AT+CIPMODE=1"+chr$(13)+chr$(10);
if NOT OKwait(4000) then errorexit("AT+CIPMODE=1")
print #1,"AT+CIPSEND"+chr$(13)+chr$(10);
if NOT OKwait(4000) then errorexit("AT+CIPSEND")
print #1,b$;
getinput(10000)
Print "Weather for "+json$(m%(),"name")
Print "Temperature is ",val(json$(m%(),"main.temp"))-273
Print "Pressure is ",json$(m%(),"main.pressure")
Print json$(m%(),"weather[0].description")
print #1,"+++";
pause 1000
print #1,"AT+CIPMODE=0"+chr$(13)+chr$(10);
if NOT OKwait(4000) then errorexit("AT+CIPMODE=0")
print #1,"AT+CIPCLOSE"+chr$(13)+chr$(10);
if NOT OKwait(4000) then errorexit("AT+CIPCLOSE")
end

function OKwait(timeout as integer) as integer
OKwait=1
timer=0
do
loop until input$(1,1)="O" or timer>=timeout
do
loop until input$(1,1)="K" or timer>=timeout
if timer>=timeout then OKwait=0
end function

sub errorexit(calling as string)
print "AT call timeout from - ",calling
print #1,"AT+CIPCLOSE"+chr$(13)+chr$(10)
end
end sub

sub getinput(waitpause as integer)
local a$
local j%,i%=-1
timer=0
do
a$=input$(1,1)
If a$<>"" Then
LongString append m%(),a$
' print a$;
if a$="{" then
if i%=-1 then
i%=1
else
i%=i%+1
endif
endif
if a$="}" then i%=i%-1
endif
loop while timer<=waitpause and i%<>0
LongString trim m%(),LInStr(m%(),"{")-1
print ""
end sub
Edited by matherp 2018-06-09
 
astro1
Regular Member

Joined: 26/06/2016
Location: Australia
Posts: 51
Posted: 01:52am 19 Jan 2019
Copy link to clipboard 
Print this post

Have been using JSON parsing on openweathermap on weather and just trying it out on the 5 day forecast. MMX64 v5.0501
I am running this command for testing, 'Print JSON$(m%(),"list[0].main.temp")', I get 'Error: Not enough memory'. Afterwards, running 'files' at the command line shows the same. Need to do a 'cpu restart' to clear. Using m%(8000), did try higher values.

The forecast JSON data has 39 part-day entries (list[x]) which may be the issue. I manually removed all but one entry and it ran ok. If I remove the part-day data after entry 18, it works ok. Doing the same after day 20 and the issue returns.

What's the limit for this type of parse?





 
JohnS
Guru

Joined: 18/11/2011
Location: United Kingdom
Posts: 2922
Posted: 08:11am 19 Jan 2019
Copy link to clipboard 
Print this post

m%(8000) sounds like nearly 64K RAM (*) just for that - is that available as well as all the other space needed?

(*) I think they're 64-bit, not 32-bit when it would be 32K

JohnEdited by JohnS 2019-01-20
 
matherp
Guru

Joined: 11/12/2012
Location: United Kingdom
Posts: 6619
Posted: 08:40am 19 Jan 2019
Copy link to clipboard 
Print this post

  Quote  What's the limit for this type of parse?


It isn't possible to say. The parser allocates memory (malloc()) as it walks through the json string and the longer and more nested the string the more memory it will need. The array used should be the smallest possible that holds the string to leave as much other memory free as possible for the parser to allocate as it processes.

One specific technical issue in MMBasic is that the smallest amount of memory that can be "malloc"ed is 256 bytes. In general this works well but for this parser it is very wasteful - nothing I can do about this as it is deeply embedded in MMBasic.
 
Print this page


To reply to this topic, you need to log in.

© JAQ Software 2022