Server for basic setup

All ESP8266 boards running MicroPython.
Official boards are the Adafruit Huzzah and Feather boards.
Target audience: MicroPython users with an ESP8266 board.
lukesky333
Posts: 44
Joined: Fri Sep 02, 2016 4:07 pm
Location: Austria

Server for basic setup

Post by lukesky333 » Fri Sep 23, 2016 1:12 pm

Hi,

I've my first ESP8266 board and try to create an simple webserver for communication with the board.

Is it possible to create a simple HTTP server for this board, to send and receive json?

What I want to do is, upload a json, process it on the board and download json with sensor data...

I've found a lot of python example but the most won't work with micropython on this board...

lukesky333
Posts: 44
Joined: Fri Sep 02, 2016 4:07 pm
Location: Austria

Re: Server for basic setup

Post by lukesky333 » Fri Sep 23, 2016 3:44 pm

I was playing arround a little bit - here my example code:

Code: Select all

addr = socket.getaddrinfo('0.0.0.0', 80)[0][-1]
s = socket.socket()
s.bind(addr)
s.listen(1)
log('listening on'+str( addr))
cl, addr = s.accept()
print("Client address:", addr)
print("Client socket:", cl)
print("Request:")
req = cl.recv(1024)
print("size of request: " + str(len(req)))
print(req)
cl.close()
My problem is, that i receive not the full request - only 536 character. Why???

User avatar
platforma
Posts: 258
Joined: Thu May 28, 2015 5:08 pm
Location: Japan

Re: Server for basic setup

Post by platforma » Fri Sep 23, 2016 3:51 pm

Hi!

The examples directory is a good place to start: https://github.com/micropython/micropyt ... es/network
As well as the official micropython esp8266 docs: http://docs.micropython.org/en/latest/e ... k_tcp.html
As for json, you have the ujson library!

User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: Server for basic setup

Post by dhylands » Fri Sep 23, 2016 4:30 pm

lukesky333 wrote:My problem is, that i receive not the full request - only 536 character. Why???
Because that's just the way networking works sometimes. 536 bytes happens to be the the size of the data which is left over when the minimum MTU size of 576 is used.

lukesky333
Posts: 44
Joined: Fri Sep 02, 2016 4:07 pm
Location: Austria

Re: Server for basic setup

Post by lukesky333 » Wed Sep 28, 2016 2:22 pm

How can I increase the MTU size or how can I get the full request?

lukesky333
Posts: 44
Joined: Fri Sep 02, 2016 4:07 pm
Location: Austria

Re: Server for basic setup

Post by lukesky333 » Wed Sep 28, 2016 3:16 pm

I changed my code and I'm using now readline.

Code: Select all

        cl, addr = s.accept()
        print("Client address:", addr)
        print("Client socket:", cl)
        print("Request:")
        print("READLINE:")
        length = 0
        while True:
            req = cl.readline()
            length = length + len(req)
            if req == b"":
                break
            print(req)
        print("size of request: " + str(length))
        cl.write(CONTENT.format(counter))
With the code above I receive following request:

Code: Select all

Client address: ('192.168.2.99', 56812)
Client socket: <socket state=2 timeout=-1 incoming=0 off=0>
Request:
READLINE:
b'POST /upload.php HTTP/1.1\r\n'
b'Host: 192.168.2.252\r\n'
b'Connection: keep-alive\r\n'
b'Content-Length: 49\r\n'
b'Cache-Control: max-age=0\r\n'
b'Origin: null\r\n'
b'Upgrade-Insecure-Requests: 1\r\n'
b'User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/51.0.2704.79 Chrome/51.0.2704.79 Safari/537.36\r\n'
b'Content-Type: application/x-www-form-urlencoded\r\n'
b'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8\r\n'
b'Accept-Encoding: gzip, deflate\r\n'
b'Accept-Language: en-US,en;q=0.8,de-AT;q=0.6,de;q=0.4\r\n'
b'\r\n'
My webpage is still "loading" and only after abortion I receive the need POST vars:

Code: Select all

b'var=das+ist+mein+Auto&hidden=hidval&submit=submit'
b''
size of request: 579
It seems that "\r\n" is missing after the submitted POST vars and the script is still waiting for the end of the request.

How can I fix it? ...any idea?

jms
Posts: 108
Joined: Thu May 05, 2016 8:29 pm
Contact:

Re: Server for basic setup

Post by jms » Wed Sep 28, 2016 3:32 pm

If you're new to socket code play with it on normal (heavyweight OSs) computers first.

But understand TCP is a pipe. You cannot guarantee how much you'll get when you suck (once,briefly) on a pipe. Only that you will get all the data eventually, in order and uncorrupted.

You're also trying to implement HTTP which you should really pick up somebody else's code for.

lukesky333
Posts: 44
Joined: Fri Sep 02, 2016 4:07 pm
Location: Austria

Re: Server for basic setup

Post by lukesky333 » Wed Sep 28, 2016 3:40 pm

jms wrote:If you're new to socket code play with it on normal (heavyweight OSs) computers first.

But understand TCP is a pipe. You cannot guarantee how much you'll get when you suck (once,briefly) on a pipe. Only that you will get all the data eventually, in order and uncorrupted.

You're also trying to implement HTTP which you should really pick up somebody else's code for.
It is sample code from the internet and I tried to adapt it to my needs.

I've only the problem with the missing "\r\n" after the post vars - I don't know how to handle this. That's why I'm here and ask for help.

Your answer isn't very helpful for me, sorry...

I will use this script for basic configuration of my esp8266 board.

markxr
Posts: 62
Joined: Wed Jun 01, 2016 3:41 pm

Re: Server for basic setup

Post by markxr » Wed Sep 28, 2016 3:43 pm

I have written a web server for the ESP8266 which allows upload / download files, serves images, css and Javascript code, it's still in development, but code is here:

https://github.com/MarkR42/esp-webui

Memory limitations are the real problem, but I can stream upload / download files as big as you like, because they move around in small chunks.

HTTP headers are mostly ignored (and few generated) because parsing big http headers will be a challenge with little ram (and can cause fragmentation).

The implementation here is 100% in-filesystem i.e. it's totally written in Micropython and doesn't need to be precompiled.

lukesky333
Posts: 44
Joined: Fri Sep 02, 2016 4:07 pm
Location: Austria

Re: Server for basic setup

Post by lukesky333 » Wed Sep 28, 2016 4:05 pm

Thanks a lot, I'll try it at home.

Is your miniservet able to handle POST and GET vars?

Post Reply