print using %s %, ESP8266 vs ESP32

All ESP8266 boards running MicroPython.
Official boards are the Adafruit Huzzah and Feather boards.
Target audience: MicroPython users with an ESP8266 board.
Post Reply
der_kps
Posts: 20
Joined: Sat Feb 22, 2020 6:30 pm

print using %s %, ESP8266 vs ESP32

Post by der_kps » Tue Apr 07, 2020 7:33 pm

Hello!
I use this codesnippet at the end of my AP/STA Template.
Further Connecting WLAN (ssid, pwd) is successfully done.

Code: Select all

# Connecting to WLAN, reading sensors, making webpage are  done

1	s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
2	s.bind(('', 80))
3	s.listen(1) #only one client
4	while True:
5	  try:    
6	    print('waiting for client request')
7	    conn, addr = s.accept() 
8	    print('Got a connection from %s' % str(addr)) #old Syntax 
9	    #print('Got a connection from {}'.format((addr))) #new Syntax
10	    request = conn.recv(1024)  
11	    print('Content = %s' % str(request)) #old Syntax
12	    #print('Content = {}' .format(str(request))) #new Syntax		
13	    response = web_page() #storing html-data from web_page():
14	    conn.send('HTTP/1.1 200 OK\n')
15	    conn.send('Content-Type: text/html\n')
16	    conn.send('Connection: close\n\n')
17	    conn.sendall(response) #send html to client
18	    conn.close()
19	    print('Connection closed', '\n')
20	  except OSError as e:
21	    conn.close()
Only for debugging I'm interested in the transfered data.
Printing in 8/9 works with ESP32 and ESP8266 (uPyCraft)
Printing in 11/12 works only with ESP32 but it does not work with ESP8266!
I get only a C and nothing else. C from Content:
Why?
The received datavolume is less than 1024 Bytes and looks like this.

Code: Select all

Content = b'GET / HTTP/1.1\r\nHOST: 192.168.4.1\r\nUpgrade- ... \r\nConnection: keep-alive\r\n\r\n'
The remaining code 13-18 is executed and I get a webpage.
Only printing in 19 does not appear!

This is the seccond Problem with using ESP8266 vs ESP32 (1st: BME280.py is not executed)

regards

User avatar
jimmo
Posts: 2754
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia
Contact:

Re: print using %s %, ESP8266 vs ESP32

Post by jimmo » Tue Apr 07, 2020 11:38 pm

So in both cases (% and format), you only see a "C" on esp8266?

Some things to try:
print('Content =', request)
(This is way more efficient than using either formatting ways because it doesn't have to create a temp string. Always prefer multi-arg print where possible.

or even
print('Content len', len(request))
To verify that the request is in fact more than 1.

But also, I highly recommend not writing a webserver from scratch -- use one of the existing libraries (e.g. microdot).

der_kps
Posts: 20
Joined: Sat Feb 22, 2020 6:30 pm

Re: print using %s %, ESP8266 vs ESP32

Post by der_kps » Wed Apr 08, 2020 9:29 am

Thanks for answering again!

The len of request = conn.recv(1024) is 375 Bytes (nice to know) 8-)
A simple print('Content =', request) doesn't make the result better.
Trieing

Code: Select all

print('Content =  {} {}'.format(len(request), str(request))) 
shows that it is impossible to do this print with ESP8266/D1mini pro. As well as import BME280.
What a shame :(

Your last sentence:
...not writing a webserver from scratch ...
What is meant with scratch?
microdot seems to be a part in github?!
There are examples too in docs.micropython.org/../quickref, that I better understand than in githup.
I'll try it to easter instead of going outdoor (we have a Corona social distancing and should stay at home :roll: )

Best regards

User avatar
jimmo
Posts: 2754
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia
Contact:

Re: print using %s %, ESP8266 vs ESP32

Post by jimmo » Wed Apr 08, 2020 2:06 pm

der_kps wrote:
Wed Apr 08, 2020 9:29 am
What is meant with scratch?
Like writing it from the very basics (i.e. starting with sockets etc).

Much easier just to use a library where someone else has done all the work.

microdot is https://github.com/miguelgrinberg/microdot

Post Reply