Server for basic setup
-
- Posts: 44
- Joined: Fri Sep 02, 2016 4:07 pm
- Location: Austria
Server for basic setup
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...
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...
-
- Posts: 44
- Joined: Fri Sep 02, 2016 4:07 pm
- Location: Austria
Re: Server for basic setup
I was playing arround a little bit - here my example code:
My problem is, that i receive not the full request - only 536 character. Why???
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()
Re: Server for basic setup
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!
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!
Re: Server for basic setup
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 wrote:My problem is, that i receive not the full request - only 536 character. Why???
-
- Posts: 44
- Joined: Fri Sep 02, 2016 4:07 pm
- Location: Austria
Re: Server for basic setup
How can I increase the MTU size or how can I get the full request?
-
- Posts: 44
- Joined: Fri Sep 02, 2016 4:07 pm
- Location: Austria
Re: Server for basic setup
I changed my code and I'm using now readline.
With the code above I receive following request:
My webpage is still "loading" and only after abortion I receive the need POST vars:
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?
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))
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'
Code: Select all
b'var=das+ist+mein+Auto&hidden=hidval&submit=submit'
b''
size of request: 579
How can I fix it? ...any idea?
Re: Server for basic setup
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.
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.
-
- Posts: 44
- Joined: Fri Sep 02, 2016 4:07 pm
- Location: Austria
Re: Server for basic setup
It is sample code from the internet and I tried to adapt it to my needs.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.
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.
Re: Server for basic setup
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.
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.
-
- Posts: 44
- Joined: Fri Sep 02, 2016 4:07 pm
- Location: Austria
Re: Server for basic setup
Thanks a lot, I'll try it at home.
Is your miniservet able to handle POST and GET vars?
Is your miniservet able to handle POST and GET vars?