It appears (with MicroPython on esp8266) that 'socket.write' and/or other socket operations have an asynchronous component, the impact of which is that even though both 'socket.write' and 'socket.close' have been called *and returned successfully*, data written to the socket is not actually sent to the peer.
If one halts the board immediately after sending data (e.g., by entering a deepsleep state), that data is lost.
For example, the following code will fail to transmit a message to a server:
Code: Select all
import machine
import network
import socket
import time
rtc = machine.RTC()
def wait_for_connection():
iface = network.WLAN(network.STA_IF)
print('waiting for wifi')
while not iface.isconnected():
machine.idle()
print('wifi connected')
def deepsleep(duration):
rtc.irq(trigger=rtc.ALARM0, wake=machine.DEEPSLEEP)
rtc.alarm(rtc.ALARM0, duration)
print('sleeping')
machine.deepsleep()
wait_for_connection()
sock = socket.socket()
sock.connect(('192.168.1.200', 4321))
sock.write('this is a test\n')
sock.close()
deepsleep(5000)
Code: Select all
sock.close()
time.sleep(1)
deepsleep(5000)