ESP32 deepsleep failed, the interpreter will not run to the deepsleep line

All ESP32 boards running MicroPython.
Target audience: MicroPython users with an ESP32 board.
Post Reply
matthiasheng
Posts: 13
Joined: Sat Oct 06, 2018 1:57 pm

ESP32 deepsleep failed, the interpreter will not run to the deepsleep line

Post by matthiasheng » Wed Jan 02, 2019 7:16 am

Hi All,

I found an issue with deepsleep in my code as shown below:
The deepsleep part of the code will be ignored by the interpreter while it shouldn't, please help to check.
It seem like when i run the deepsleep code without other parts like temperature and humidity data acquire and mqtt transmit it works just fine, but when i put everything together, it just skip the deepsleep part and i don't know why.

Code: Select all

  
import machine,time,antp,network,ubinascii
from robust import MQTTClient
from machine import Pin
from sht30 import SHT31

i2c = machine.I2C(sda=machine.Pin(4), scl=machine.Pin(15))
led = Pin(25, Pin.OUT, value=0)	#led pin on wifikit32 is gpio25
sht = SHT31(i2c)
CLIENT_ID = str(ubinascii.hexlify(network.WLAN().config('mac')).decode())
SERVER = "192.168.89.216"
TOPIC = b"temperature"

def create_csv(name):
	with open(name, "a") as log:
		fle = open(name,'r')
		check_csv = fle.readline()
		if len(check_csv) == 0:
			log.write("{0},{1},{2}\n".format('Timestamp','Temperature','Humidity'))
	log.close()

def write_csv(name,timestamp,temp,humid):
	with open(name, "a") as log:
		log.write("{0},{1},{2}\n".format(timestamp,temp,humid))
	log.close()

def main(server=SERVER):
	c = MQTTClient(CLIENT_ID, server)
	c.connect()
	print("Connected to %s, waiting for temperature reading" % server)
	#create_csv(name)
	while True:
		while True:
			timestamp_raw = time.localtime()
			timestamp = str(timestamp_raw[0])+'-'+str(timestamp_raw[1])+'-'+str(timestamp_raw[2])+' '+\
				str(timestamp_raw[3])+':'+str(timestamp_raw[4])+':'+str(timestamp_raw[5])
			temp_humi = sht.get_temp_humi()
			temp_humi_str = CLIENT_ID + ',' + str(temp_humi[0]) + ',' + str(temp_humi[1])
			print(CLIENT_ID,timestamp,temp_humi)
			c.publish(TOPIC, temp_humi_str)
			led.value(0)
			#write_csv(name,timestamp,temp,rh)
			#time.sleep(30)
			led.value(1)
	c.disconnect()

if machine.reset_cause() == machine.DEEPSLEEP_RESET:
	print('woke from a deep sleep')
	main()
print('in deepsleep now')
machine.deepsleep(50000)

matthiasheng
Posts: 13
Joined: Sat Oct 06, 2018 1:57 pm

Re: ESP32 deepsleep failed, the interpreter will not run to the deepsleep line

Post by matthiasheng » Wed Jan 02, 2019 7:30 am

Image
Capture.PNG
Capture.PNG (10.81 KiB) Viewed 4044 times
I think the problem come from here, depending on the board state, if it is not reset from deepsleep, it will go to deepsleep for first time then after it woke from deepsleep and run main() line, it will not continue to run deepsleep(30000) as planned, it just continue to run main() forever.

matthiasheng
Posts: 13
Joined: Sat Oct 06, 2018 1:57 pm

Re: ESP32 deepsleep failed, the interpreter will not run to the deepsleep line

Post by matthiasheng » Wed Jan 02, 2019 8:39 am

ok, if i comment out the #main(), the deepsleep and wake up cycles works perfectly.
Now, if the problem come from the main(), any suggestion on possible conflict of any line in it with deepsleep?
I have tested the main() part for a week without any problem just before adding the deepsleep and wake cycle so the codes in it alone is fine.
I suspect part of the codes in main() conflict with deepsleep and wake cycle codes but i have no idea how to solve it, please help.

loboris
Posts: 344
Joined: Fri Oct 02, 2015 6:19 pm

Re: ESP32 deepsleep failed, the interpreter will not run to the deepsleep line

Post by loboris » Wed Jan 02, 2019 8:56 am

Your main() never exits, it has an infinite while loop in it (two of them!).

BTW, when posting a code, put it in the code block!

matthiasheng
Posts: 13
Joined: Sat Oct 06, 2018 1:57 pm

Re: ESP32 deepsleep failed, the interpreter will not run to the deepsleep line

Post by matthiasheng » Wed Jan 02, 2019 11:30 am

Hi loboris,

Thank you very much for pointing it out my stupid mistakes, it works perfectly now as shown below: :lol:

Code: Select all

import machine,time,antp,network,ubinascii
from robust import MQTTClient
from machine import Pin
from sht30 import SHT31

i2c = machine.I2C(sda=machine.Pin(4), scl=machine.Pin(15))
led = Pin(25, Pin.OUT, value=0)	#led pin on wifikit32 is gpio25
sht = SHT31(i2c)
CLIENT_ID = str(ubinascii.hexlify(network.WLAN().config('mac')).decode())
SERVER = "192.168.89.216"
TOPIC = b"temperature"

def main(server=SERVER):
	c = MQTTClient(CLIENT_ID, server)
	c.connect()
	print("Connected to %s, waiting for temperature reading" % server)
	#create_csv(name)
	timestamp_raw = time.localtime()
	timestamp = str(timestamp_raw[0])+'-'+str(timestamp_raw[1])+'-'+str(timestamp_raw[2])+' '+\
		str(timestamp_raw[3])+':'+str(timestamp_raw[4])+':'+str(timestamp_raw[5])
	temp_humi = sht.get_temp_humi()
	temp_humi_str = CLIENT_ID + ',' + str(temp_humi[0]) + ',' + str(temp_humi[1])
	print(CLIENT_ID,timestamp,temp_humi)
	c.publish(TOPIC, temp_humi_str)
	led.value(0)
	time.sleep(0.2)
	led.value(1)
	c.disconnect()

if machine.reset_cause() == machine.DEEPSLEEP_RESET:
	print('woke from a deep sleep')
	main()

print('in deepsleep now')
machine.deepsleep(50000)
And i learned to use block code as well :D

Post Reply