|
Forum Index : Microcontroller and PC projects : PC3 - OpenWeather Forecasts
| Author | Message | ||||
| Andrew_G Guru Joined: 18/10/2016 Location: AustraliaPosts: 910 |
Hi There, I am trying to use my PC3 to access OpenWeather forecasts. I'm using version 7.000008 HDMIUSB. I've done it before with my PicoW1 which reliably gets 8, 4-hr forecasts (using older firmware versions). In the code below (only a test snippet), by varying the value of CNT, I 'rarely' get 8, 'sometimes' 4, and 'usually' 2 forecast periods. It does vary! When it fails it has omitted some of the lead-in text and text after "ground_lev". I've previously used much smaller buffer for m%() but increasing it here 'appears' to help?? The PAUSEs dont seem to do anything? Any suggestions/better code? Cheers, Andrew 'Get_Forecast Check v2.bas OPTION EXPLICIT OPTION DEFAULT NONE Dim Integer TZone = 10, DST = 1, II DIM string ID$ = "7932628" 'Kew East Dim string APIKEY$ = "****. . ." dim integer CNT = 8 'No of x4 hr time periods requested in forecast. 8 = 24hrs Dim INTEGER m%(2048*3) 'm% should be a multiple of 8 Dim string b$ b$ = "GET /data/2.5/forecast?id="+ID$+"&APPID="+APIKEY$+"&units=metric"+"&cnt="+STR$(CNT)+Chr$(10)+Chr$(13) longstring CLEAR m%() WEB NTP Tzone+DST,"au.pool.ntp.org" pause 1000 WEB OPEN TCP CLIENT "api.openweathermap.org", 80 WEB TCP CLIENT REQUEST b$,m%() pause 1000 LongString print m%() print "Site ";JSON$(m%(),"city.name") print "Sunset ";json$(m%(),"city.sunset") print "CNT ";json$(m%(),"CNT") for II = 0 to 8 print json$(m%(),"list["+str$(II)+"].wind.deg") next II WEB CLOSE TCP CLIENT end Edited 2026-10-25 10:16 by Andrew_G |
||||
TassyJim![]() Guru Joined: 07/08/2011 Location: AustraliaPosts: 6632 |
I get a json string 3534 bytes long. If you take the last 1460 bytes and move them to the beginning of the string, you will have working json data It looks like packets out of order or a ring buffer got lost. Tested with WEBMITE and HDMIWEB V7 RC5 Jim VK7JH MMedit |
||||
| Andrew_G Guru Joined: 18/10/2016 Location: AustraliaPosts: 910 |
Hi Jim, Thanks. Are you able to capture a LONGSTRING PRINT of a typical full lot of data. (MMCC can help . . .) I'm using similar hardware with RC8. Cheers, Andrew |
||||
TassyJim![]() Guru Joined: 07/08/2011 Location: AustraliaPosts: 6632 |
Yes , provided you have wordwrap on, MMCC can capture the full longstring. Email sent. First string is with the wrong sequence. second string has it 'fixed' but not tested. Jim VK7JH MMedit |
||||
TassyJim![]() Guru Joined: 07/08/2011 Location: AustraliaPosts: 6632 |
'Get_Forecast Check v2.bas OPTION EXPLICIT OPTION DEFAULT NONE Dim Integer TZone = 10, DST = 1, II DIM string ID$ = "7932628" 'Kew East Dim string APIKEY$ = "xxxxxxxxxxxxxxxxx" dim integer CNT = 8 'No of x4 hr time periods requested in forecast. 8 = 24hrs Dim INTEGER m%(576*8) 'm% should be a multiple of 8 dim integer k%(576*8) Dim string b$ b$ = "GET /data/2.5/forecast?id="+ID$+"&APPID="+APIKEY$+"&units=metric"+"&cnt="+STR$(CNT)+Chr$(10)+Chr$(13) longstring CLEAR m%() WEB NTP Tzone+DST,"au.pool.ntp.org" pause 1000 WEB OPEN TCP CLIENT "api.openweathermap.org", 80 'print b$ WEB TCP CLIENT REQUEST b$,m%() ',3000 pause 1000 print llen(m%()) LongString print m%() longstring right k%(),m%(),1460 longstring concat k%(),m%() print "Site ";JSON$(k%(),"city.name") print "Sunset ";json$(k%(),"city.sunset") print "CNT ";json$(k%(),"CNT") for II = 0 to 8 print json$(k%(),"list["+str$(II)+"].wind.deg") next II WEB CLOSE TCP CLIENT memory end Crude fix to reorder the json string Jim VK7JH MMedit |
||||
TassyJim![]() Guru Joined: 07/08/2011 Location: AustraliaPosts: 6632 |
Further information. This morning every second run works as it should. The next run through needs the adjustment to the json string. Jim VK7JH MMedit |
||||
| Andrew_G Guru Joined: 18/10/2016 Location: AustraliaPosts: 910 |
Hi Jim, Don't waste precious sunshine but: - I'm seeing inconsistencies too - If you take my original code and change CNT to 2 it works every time - I've played with the size of m%() - seems to not make any difference. Added: - The LLEN is consistently 3492 - On a standalone Webmite2 with older firmware it works as intended, ie with CNT = 8 Cheers, Andrew Edited 2026-10-26 08:16 by Andrew_G |
||||
TassyJim![]() Guru Joined: 07/08/2011 Location: AustraliaPosts: 6632 |
With CNT = 2, the packet length is 1020 so it fits into one TCPIP packet. That is why it works every time. VK7JH MMedit |
||||
| Andrew_G Guru Joined: 18/10/2016 Location: AustraliaPosts: 910 |
OK Folks, Thanks to Jim it appears that the following code works on a PC3. BUT why is it happening? (It works without the test on 2040 WebMite running 7.000007) Cheers, Andrew 'Get_Forecast Check JH AGv2.bas 'Works on "WebMite" v1 2040 v7.000007 'Now works on PC3 using the test for "{"cod":200" OPTION EXPLICIT OPTION DEFAULT NONE Dim Integer TZone = 10, DST = 1, II DIM string ID$ = "7932628" 'Kew East Dim string APIKEY$ = "xxxxxxxxxxxxxxxxx" dim integer CNT = 8 'No of x4 hr time periods requested in forecast. 8 = 24hrs Dim INTEGER m%(576*8) 'm% should be a multiple of 8 dim integer k%(576*8) Dim string b$ b$ = "GET /data/2.5/forecast?id="+ID$+"&APPID="+APIKEY$+"&units=metric"+"&cnt="+STR$(CNT)+Chr$(10)+Chr$(13) longstring CLEAR m%() longstring CLEAR k%() '+ANG WEB NTP Tzone+DST,"au.pool.ntp.org" pause 1000 WEB OPEN TCP CLIENT "api.openweathermap.org", 80 WEB TCP CLIENT REQUEST b$,m%() ',3000 pause 1000 print llen(m%()) LongString print m%() 'Test for "{"cod":200" 'r$ = LGETSTR$(array%(), start, length) Returns part of a long string as a normal string. if LGETSTR$(m%(),1,12) = "{"+chr$(34)+"cod"+chr$(34)+":"+chr$(34)+"200"+chr$(34) then ' i.e. it is OK as is print "Copying m%()) to k%():" longstring copy k%(), m%() else 'Use Jim's approach print "Concatinting last 1460 to the front" longstring right k%(),m%(),1460 longstring concat k%(),m%() End if print "Resulting k%():" longstring Print k%() '+ANG print "Site ";JSON$(k%(),"city.name") print "Sunset ";json$(k%(),"city.sunset") print "CNT ";json$(k%(),"CNT") for II = 0 to 8 print json$(k%(),"list["+str$(II)+"].wind.deg") next II WEB CLOSE TCP CLIENT memory end Edited 2026-10-26 13:58 by Andrew_G |
||||
| The Back Shed's forum code is written, and hosted, in Australia. | © JAQ Software 2026 |