Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Socket gets stuck while sending data on esp8266 #9570

Open
@stas-sl

Description

@stas-sl

Hi!

I originally noticed this issue while using WebREPL. I was running loops printing sensor values and after a while (few minutes) it got stuck. It was not just one time - it reproduced over and over. So, in order to find the issue, I tried to create minimal examples using sockets and the issue reproduced for them as well. If you run following examples below for few minutes (usually 2-3), they will hang (or at least for me they do). Sometimes they hang for several seconds (upto 30) and then continue, but afterwards they hang completely. If I use non-blocking sockets it raises EAGAIN error on server. The issue occurs for both sync and async server versions.

Also I thought that maybe my wifi is unstable, but I rewrote same example on C++ (Arduino IDE) and it worked perfectly for long time without any delays and freezes.

server_async.py (run on esp)

import uasyncio

port = 3000

async def server(reader, writer):
  cnt = 0
  while True:
    print(cnt)
    await writer.awrite(f'{cnt}\n')
    cnt += 1
    await uasyncio.sleep_ms(100)
  await writer.aclose()

loop = uasyncio.get_event_loop()
loop.create_task(uasyncio.start_server(server, "0.0.0.0", port))
loop.run_forever()
loop.close()

or
server_sync.py (run on esp)

import time
import socket

port = 3000

server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)

ai = socket.getaddrinfo("0.0.0.0", port)
addr = ai[0][4]

server.bind(addr)
server.listen(1)

while True:
  client, remote_addr = server.accept()
  client.setblocking(False)
  cnt = 0

  while True:
    print(cnt)
    client.sendall(f'{cnt}\n')
    time.sleep_ms(100)
    cnt += 1

server.close()

client.py (run on PC)

import time
import socket

host = '...'
port = 3000

socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
socket.connect((host, port))

response = ''
prev = time.time()
while True:
    recv = socket.recv(1024)
    print('duration:', int((time.time() - prev) * 1000), 'ms')
    print(recv.decode(), end='')
    prev = time.time()

socket.close()

Version: MicroPython v1.19.1 on 2022-06-18; ESP module with ESP8266
Firmware: esp8266-20220618-v1.19.1.bin

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions