TCP socket server was stuck after no AP found.

All ESP32 boards running MicroPython.
Target audience: MicroPython users with an ESP32 board.
Post Reply
woot_srx
Posts: 7
Joined: Fri Sep 29, 2017 7:49 am

TCP socket server was stuck after no AP found.

Post by woot_srx » Sat Jan 20, 2018 6:03 am

Hello everyone,

Please forgive me for my poor English and lack of knowledge.
I would like to ask for your suggestion about my project on ESP32 with WIFI and TCP socket server.
My project trouble is after ESP32 connected with AP then start socket server, it working well.
But if I turned off the AP, it look seem like socket server does not work anymore even after i turned on the AP. I have to reboot the ESP32.

My tested follow the tutorial from "Network basics" and "Simple HTTP Server",

http://docs.micropython.org/en/latest/e ... asics.html
http://docs.micropython.org/en/latest/e ... k_tcp.html

Just adjust SSID, Password and PIN no. , my code is

import network
from machine import Pin
def do_connect():
global sta_if,ap_if
sta_if = network.WLAN(network.STA_IF)
ap_if = network.WLAN(network.AP_IF)
if not sta_if.isconnected():
ap_if.active(False)
sta_if.active(True)
sta_if.ifconfig(('172.20.10.3','255.255.255.240','172.20.10.1','172.20.10.1'))
sta_if.connect('iPhone', '0815158010')
while not sta_if.isconnected():
pass
do_connect()
pins = [Pin(i, Pin.IN) for i in (26, 25, 32, 34)]
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>
"""
import socket
addr = socket.getaddrinfo('172.20.10.3',80)[0][-1]
s = socket.socket()
s.bind(addr)
s.listen(1)
print('listening on', addr)
while True:
cl, addr = s.accept()
print('client connected from', addr)
cl_file = cl.makefile('rwb', 0)
while True:
line = cl_file.readline()
if not line or line == b'\r\n':
break
rows = ['<tr><td>%s</td><td>%d</td></tr>' % (str(p), p.value()) for p in pins]
response = html % '\n'.join(rows)
cl.send(response)
cl.close()

The result : HTTP server worked, i can see PIN status from browser at "172.20.10.3:80"
Terminal shown "
II (14716) wifi: n:1 0, o:1 0, ap:255 255, sta:1 0, prof:1
IdI (18286) wifi: pm start, type:0

client connected from ('172.20.10.1', 63447)
339
client connected from ('172.20.10.1', 63449)
339
client connected from ('172.20.10.1', 63450)
339 "

Then I turned off the AP
Terminal shown "
II (28006) wifi: n:1 0, o:1 0, ap:255 255, sta:1 0, prof:1
I (28006) wifi: state: auth -> auth (b0)
I[0;32mI (31416) wifi: STA_DISCONNECTED, reason:201[0m
no AP found
[0;32mI (33826) wifi: STA_DISCONNECTED, reason:201[0m
no AP found
[0;32mI (36236) wifi: STA_DISCONNECTED, reason:201[0m
no AP found
[0;32mI (38646) wifi: STA_DISCONNECTED, reason:201[0m
no AP found"

Then I turned on the AP
Terminal shown "
III (41776) wifi: pm start, type:0"

But it look seem like HTTP server not working, I could not see PIN status on browser "172.20.10.3:80"

This point trouble me because when after AP out range, I have to reboot the ESP32.

This trouble was not occur in ESP8266 (nodemcu), I tested with the same code (only change PIN no) .
The result was ESP8266 can re-connect and socket server still survive.

Could you please suggest me haw can I solve this trouble?
Is there anyone face the same trouble like me?

Your suggestion will be appreciated. Thank you in advance.

Woot

woot_srx
Posts: 7
Joined: Fri Sep 29, 2017 7:49 am

Re: TCP socket server was stuck after no AP found.

Post by woot_srx » Mon Jan 22, 2018 1:26 pm

This trouble is not concern to AP, I tested with other AP (TPLink) it was the same.

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: TCP socket server was stuck after no AP found.

Post by pythoncoder » Tue Jan 23, 2018 10:55 am

There are known problems with the ESP32's response to WiFi outages. Hopefully a future upgrade to the Espressif firmware will fix these.
Peter Hinch
Index to my micropython libraries.

woot_srx
Posts: 7
Joined: Fri Sep 29, 2017 7:49 am

Re: TCP socket server was stuck after no AP found.

Post by woot_srx » Tue Jan 23, 2018 12:28 pm

Dear Pythoncoder

Thank you very much for your answer.
So in your opinion if I develop my project base on ESP-IDF or Arduino IDE. It also has the same trouble, right?

Thank you.

Woot

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: TCP socket server was stuck after no AP found.

Post by pythoncoder » Tue Jan 23, 2018 5:38 pm

If, as it seems, the problem is in the vendor code then it will be fixed by a release of the ESP-IDF.

I don't know if the current ESP-IDF release has a fix. If it has, the fix will appear in the MicroPython port when the port maintainers upgrade. The ESP32 is new: when the ESP8266 was at a similar stage of development it had its share of rough edges.
Peter Hinch
Index to my micropython libraries.

woot_srx
Posts: 7
Joined: Fri Sep 29, 2017 7:49 am

Re: TCP socket server was stuck after no AP found.

Post by woot_srx » Wed Jan 24, 2018 8:17 am

Thank you very much for your answer.

I may try testing with ESP-IDF to see the result.

Woot

Post Reply