Suggestions on removing Web page header from Json string

RP2040 based microcontroller boards running MicroPython.
Target audience: MicroPython users with an RP2040 boards.
This does not include conventional Linux-based Raspberry Pi boards.
Post Reply
FeK9
Posts: 33
Joined: Sun Jan 20, 2019 8:19 am

Suggestions on removing Web page header from Json string

Post by FeK9 » Sun Dec 12, 2021 11:48 am

GDay All...

Looking for suggestion on how to remove the header from Json string (line 21), or vice versa....

i'm using a Wave Pi Pico Evaluation Kit with Wave Pi Pico ESP8266 WiFi Expansion shown
below....

Image

Image

With much hair pulling I got this far, borrowing a snip bit or two from around the net... :shock:

I am tinkering with other options but no luck thus far... :(

Listing is shown below with it's resulting output...

Code: Select all

from machine import UART
import utime,time,sys

uart = UART(0,115200)

### WiFi Router ###
SSID='My_SSID'
password = 'My Pass Word'

### Json based Time server ###
ServerURL = 'worldtimeapi.org'
Port = '80'

### Write AT Commands to ESP8266
def sendCMD(cmd,ack,timeout=2000):
    uart.write(cmd+'\r\n')
    t = utime.ticks_ms()
    while (utime.ticks_ms() - t) < timeout:
        s=uart.read()
        if(s != None):
            s=s.decode()
            print(s)
            if(s.find(ack) >= 0):
                return True
    return False

### Necessary step, make sure that ESP8266 is in non transparent mode ### 
uart.write('+++')
time.sleep(1)

### Necessary step before connecting to WiFi ###
### also dumping the header after restart    ###
uart.write('AT+RST\r\n')
utime.sleep(1)
data = False
while data == False:
    data = uart.any()
    utime.sleep(1)
junk = bytearray(256)
morejunk = uart.readinto(junk)

### Setting up the ESP8266 WiFi router link and making ###
### connection and request to Json based time servar   ###
sendCMD("AT","OK")
#sendCMD("AT+GMR","OK")
sendCMD("AT+CWMODE=1","OK")

sendCMD("AT+CWJAP=\""+SSID+"\",\""+password+"\"","OK",20000)
#sendCMD("AT+CIFSR","OK")
sendCMD("AT+CIPMUX=0","OK")

sendCMD("AT+CIPSTART=\"TCP\",\""+ServerURL+"\","+Port,"OK",20000)

sendCMD("AT+CIPSEND=115",">")
uart.write("GET /api/timezone/Africa/Johannesburg HTTP/1.1\r\nUser-Agent: curl/7.37.0\r\nHost: worldclockapi.com\r\nAccept: */*\r\n\r\n")

sendCMD("AT+CIPCLOSE","OK")

### Reading reply from Json based time server ###
### then exiting program                      ###
while True:
    s=uart.read()
    if(s != None):
        s=s.decode('utf-8')
        print(s)
        if(uart.any() == 0):
            sys.exit()

Code: Select all

>>> %Run -c $EDITOR_CONTENT
AT

OK

AT+CWMODE=1

OK

AT+CWJAP="My_SSID","My Pass Word"

WIFI CONNECTED

WIFI GOT IP


OK

AT+CIPMUX=0

OK

AT+CIPSTART="TCP","worldtimeapi.org",80

CONNECT

OK

AT+CIPSEND=115

OK
> 
+CIPCLOSE
busy s...

Recv 115 bytes


SEND OK


+IPD,980:HTTP/1.1 200 OK
access-control-allow-credentials: true
access-control-allow-origin: *
access-control-expose-headers: 
cache-control: max-age=0, private, must-revalidate
content-length: 351
content-type: application/json; charset=utf-8
cross-origin-window-policy: deny
date: Sun, 12 Dec 2021 11:45:17 GMT
server: Fly/fb1a8b2 (2021-11-25)
x-content-type-options: nosniff
x-download-options: noopen
x-frame-options: SAMEORIGIN
x-permitted-cross-domain-policies: none
x-request-id: Fr__Ukvr0p3X5u-5l6kh
x-runtime: 287µs
x-xss-protection: 1; mode=block
via: 1.1 fly.io
fly-request-id: 01FPQ7C9CRXW05NTB3PJV9K7T8

{"abbreviation":"SAST","client_ip":"41.13.90.55","datetime":"2021-12-12T13:45:18.311653+02:00","day_of_week":0,"day_of_year":346,"dst":false,"dst_from":null,"dst_offset":0,"dst_until":null,"raw_offset":7200,"timezone":"Africa/Johannesburg","unixtime":1639309518,"utc_datetime":"2021-12-12T11:45:18.311653+00:00","utc_offset":"+02:00","week_number":49}
>>> 

FeK9
Posts: 33
Joined: Sun Jan 20, 2019 8:19 am

Re: Suggestions on removing Web page header from Json string

Post by FeK9 » Sun Dec 12, 2021 8:19 pm

Solved...

May be fall into a category of Hack / Bodge...

I split the header and Json string in two from one long string, each halve into a python list [ 'header' , 'Json string' ]
and parsed the second half containing the Jason data. Then printed 'datetime' field from the Json data...

Code: Select all

parsed = ujson.loads(long_s.rsplit('\r\n\r\n')[1])
print (parsed["datetime"])

2021-12-12T21:52:01.314798+02:00
Just add ujson lib to the import's at the top and replace the bottom bit of code from my previous post with the code shown below...

Code: Select all

### Reading reply from Json based time server ###
### then exiting program                      ###
long_s =''
while True:
    s=uart.read()
    if(s != None):
        s=s.decode('utf-8')
        #print(s)
        long_s = long_s + s
        if(uart.any() == 0):
            parsed = ujson.loads(long_s.rsplit('\r\n\r\n')[1])
            print (parsed["datetime"])
            sys.exit()

Post Reply