Delay required between sending data and going into deep sleep

All ESP32 boards running MicroPython.
Target audience: MicroPython users with an ESP32 board.
Post Reply
matthewlai
Posts: 3
Joined: Thu Oct 24, 2019 12:34 am

Delay required between sending data and going into deep sleep

Post by matthewlai » Sat Nov 02, 2019 10:02 pm

I am having some trouble using deep sleep with code that only needs to send a little bit of data every time it wakes up, but it looks like if I don't insert a (regular) sleep before going into deep sleep, the data doesn't actually get received by the server, though everything looks normal on the ESP32 side. Adding a delay is not the end of the day, but that's hacky. Is there anything I'm missing?

I've simplified my code down to the following (the server is "nc -k -l -p 2939").

boot.py

Code: Select all

import machine
import network

SSID='...'
PASS='...'

def connect():
	wlan = network.WLAN(network.STA_IF)
	wlan.active(True)
	
	if not wlan.isconnected():
		wlan.connect(SSID, PASS)
		while not wlan.isconnected():
			pass
	return wlan

def disconnect(wlan):
	wlan.disconnect()
	wlan.active(False)

wlan = connect()
main.py:

Code: Select all

import machine
import time
import socket

server_addr = socket.getaddrinfo('192.168.5.120', 2939)[0][-1]

if wlan.isconnected():
	try:
		s = socket.socket()
		s.connect(server_addr)

		bytes_sent = s.send(bytes('TEST\n', 'utf8'))
		print("{} bytes sent".format(bytes_sent))
	except OSError as e:
		print(e)
	finally:
		s.close()

# Why do I need a sleep here?
time.sleep(5)

# If we have just been powered up, wait 30 seconds in case we want to do a firmware upgrade.
if machine.reset_cause() != machine.DEEPSLEEP_RESET:
	time.sleep(30)
	

print("going to sleep")
disconnect(wlan)
machine.deepsleep(10 * 1000) # Deep sleep for 10 seconds
Thanks!

User avatar
jimmo
Posts: 2754
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia
Contact:

Re: Delay required between sending data and going into deep sleep

Post by jimmo » Mon Nov 04, 2019 5:02 am

My guess would be that at some layer between socket.send() and actually wiggling the electrons in the antenna, there's something asynchronous (i.e. it returns once it has put the generated 802.11 frame into the radio's memory buffer or something).

But that's not a very helpful answer, because it would be nice to wait on something, rather than an arbitrary time delay.

I had a quick look, I don't see anything obvious...

What's the minimum delay you can get away with? Does time.sleep_ms(100) work?

kevinkk525
Posts: 969
Joined: Sat Feb 03, 2018 7:02 pm

Re: Delay required between sending data and going into deep sleep

Post by kevinkk525 » Mon Nov 04, 2019 6:17 am

An option would be to wait for a response from the receiving end. Then you know for sure the data went through and can go to deepsleep afterwards.
According to my tests with mqtt, sending data and receiving an ACK shouldn't take more than 250ms but sometimes it might just take a little longer.
I wouldn't rely on just a waiting period, I'd go for a response implementation.
Kevin Köck
Micropython Smarthome Firmware (with Home-Assistant integration): https://github.com/kevinkk525/pysmartnode

Post Reply