Handle UDP and TCP request (ESP8266)

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
AgentSmithers
Posts: 8
Joined: Fri Feb 17, 2017 6:23 pm

Handle UDP and TCP request (ESP8266)

Postby AgentSmithers » Fri Feb 17, 2017 11:05 pm

Hi Everyone,
I have managed to successfully program a DNS server on my ESP8266 using MicroPython, However I am now trying to Handle TCP HTTP request. From my Reading it appears uasyncio is perhaps the answer but from what I gather it does not work on the ESP8266 and the UDP DNS Receive appears to block the main thread. Any pointers on this that is nativity supported?

If I could Handle TCP and UDP request at the same time that would be amazing. Thank you for your help!
NOTE: If an addtional Module is required can I be pointed to the install guide, i am a bit in the unknown on this process.

Thank you everyone for the Chime in!
-Agent


Edit:
Below is sample code I have written, it appears right after the Syn, Syn Ack, Ack the HTTP request is sent from the browser and the code instantly closes the connection even those the UART from the chip says it sent the data the computer running Wireshark only sees a RST, ACK packet fly right through when it sents its HTTP Request.

Anyone know why this may be happening?

Edit:
It appears my Buffer had to be raised to 512Bytes to handle the full request, Is there a better way to read the data in without taking out 512Bytes of my Heap?

Edit:
I found this here and this saved me. viewtopic.php?t=2854
Has anyone got this to work via UDP / DGRAM socket? I got mine to work with TCP and UDP using the forground to handle DNS request and TCP as the ASYNC Background, But I would like to do both if possible.

[code]
import machine
import array, network, ubinascii, socket
ap_if = network.WLAN(network.AP_IF)
network.WLAN(network.STA_IF).active(False)
essid = b"ESP8266-%s" % ubinascii.hexlify(ap_if.config("mac")[-3:])
ap_if.config(essid=essid, authmode=network.AUTH_WPA_WPA2_PSK, password=b"1234567890")

html = """<!DOCTYPE html>
<html>
<head> <title>ESP8266 Pins</title> </head>
<body> <h1>ESP8266 Pins</h1>
<table border="1"> <tr><th>Pin</th><th>Value</th></tr> %s </table>
</body>
</html>
"""

s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.bind(socket.getaddrinfo('0.0.0.0', 53)[0][-1])
s.settimeout(1)

t = socket.socket()
t.bind(socket.getaddrinfo('0.0.0.0', 80)[0][-1])
t.settimeout(3)
t.listen(1)

while 1: #while not recv packet of death ;)
try:
m, addr = s.recvfrom(256)
print("Waiting for incomming DNS Query")
except OSError as e:
print("UDP Exception: {0}".format(e), type(e))
except KeyboardInterrupt as e:
print("KeyboardInterrupt: {0}".format(e), type(e))
from webrepl import start
start()
break
except Exception as e:
print("Exception: {0}".format(e), type(e))

try:
cl, addr = t.accept()
print('client connected from', addr)
data = cl.recv(256)
if data:
print(str(data, 'utf8'), end='')
else:
break
cl.send(html)
print('data sent')
cl.close()
print('Socket Closed')
except OSError as e:
print("TCP Exception: {0}".format(e), type(e))
except KeyboardInterrupt as e:
print("KeyboardInterrupt: {0}".format(e), type(e))
from webrepl import start
start()
break
except Exception as e:
print("Exception: {0}".format(e), type(e))[/code]

Return to “General Discussion and Questions”

Who is online

Users browsing this forum: Frida and 1 guest