CMM2: V5.07.00b11: json support with full ESP-01 example code


Author Message
matherp
Guru

Joined: 11/12/2012
Location: United Kingdom
Posts: 6619
Posted: 03:10pm 30 Jan 2021      

V5.07.00b11 now available for download

http://geoffg.net/Downloads/Maximite/CMM2_Beta.zip

I've added a new edit font OPTION EDIT FONT VERY LARGE for use with high resolution modes and changed the horrible font 3 to a different one that is the same size

This release also supports a json parsing function.

  Quote  JSON$(array%(),string$) Returns a string representing a specific item out of the JSON input stored in the longstring array%()

e.g.
JSON$(a%(), "name")
JSON$(a%(), "coord.lat")
JSON$(a%(), "weather[0].description")
JSON$(a%(),"list[4].weather[0].description

Examples taken from api.openweathermap.org



Attached is some code that uses the openweathermap.org api which is called using an ESP-01 with the default "AT" firmware. You need to substitute your own wifi login details and your own openweathermap api username and key in lines 3, 4, and 14 to get it to work. This is code I previously published to work on the Micromite eXtreme  that works without a single change on the CMM2 - Isn't compatibility wonderful




You can see from the local report why I have so much time for coding  


option explicit
option default none
Dim MYID$="myID"
Dim APIKEY$="myKEY"
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 "com2: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 2021-01-31 01:30 by matherp