Page 1 of 1

umqttsimple hangs when waking from deepsleep

Posted: Tue Jun 30, 2020 7:24 pm
by jbmfg
I am using deepsleep to save battery between temperature reads that i post to a mqtt server. When the wake up occurs and the program does the client.connect() it hangs there. I narrowed down the issue to the umqttsimple line:

resp =

The code works fine if i remove the deepsleep code. Deepsleep works fine when i do simple tests of it by just printing some text when it wakes up. Not sure what I'm dong wrong. My code hangs at the client.connect() line in the mqtt_actions method. Whole program is below:

import machine
import bme280
import time
import network
import ubinascii
from umqttsimple_debug import MQTTClient
import uos, machine
import gc
import webrepl

def sub_cb(topic, msg):
print((topic, msg))
if topic == b'notification' and msg == b'received':
print('ESP received hello message')

def mqtt_client():
client_id = "jbg506"
mqtt_server = ""
client = MQTTClient(client_id, mqtt_server, user=b"*******", password="********")
print ("client setup")
print ("callback set")
print ("client connected")
return client

def do_connect():
sta_if = network.WLAN(network.STA_IF)
if not sta_if.isconnected():
print('connecting to network...')
sta_if.connect('********', '*******')
while not sta_if.isconnected():
print('network config:', sta_if.ifconfig())

def get_temp():
sda = machine.Pin(2)
scl = machine.Pin(0)
i2c = machine.I2C(-1, sda, scl)
bme = bme280.BME280(i2c=i2c)
temp = float(bme.values[0][:-1])
temp = str(round(temp * 1.8 + 32, 2))
return temp

def try_temp():
for a in range(9):
for l in range(9):
sda = machine.Pin(a)
scl = machin.Pin(l)
i2c = machine.I2C(-1, sda, scl)
bme = bme280.BME280(i2c=i2c)
print (a, l, bme.values[0])
print (a, l, "no go")

def mqtt_actions(message):
client_id = ubinascii.hexlify(machine.unique_id())
print (client_id)
mqtt_server = b""
client = MQTTClient(client_id, mqtt_server, user=b"*******", password=b"*******")
print ("client setup")
print ("callback set")
print ("client connected")
topic_pub = b'clarks_room'
client.publish(topic_pub, message)

def main():
sta_if = network.WLAN(network.STA_IF)
print ("sleep up, moving on")
print (machine.reset_cause())
#rtc = machine.RTC()
#rtc.irq(trigger=rtc.ALARM0, wake=machine.DEEPSLEEP)
print ("got past rtc")
temp = get_temp()
print (temp)
print ("published")
#rtc.alarm(rtc.ALARM0, 900000)


Re: umqttsimple hangs when waking from deepsleep

Posted: Tue Jun 30, 2020 8:24 pm
by Christian Walther
No idea whether that will help, but have you tried
  • connecting with an empty client_id? You don’t need one when you’re using clean sessions, maybe the server doesn’t like yours.
  • disconnecting when you’re done with publishing? Maybe the server doesn’t like it when you just disappear without saying bye.
In other words, I’m wondering whether the difference is not actually between waking from sleep and power-on, but dependent on what state you left the server behind in (e.g. the time between successive tries).

Also, maybe you could use Wireshark to check if the server is actually not sending anything back.

Re: umqttsimple hangs when waking from deepsleep

Posted: Tue Jun 30, 2020 10:57 pm
by jbmfg
Good suggestions Christian, thank you. I should have mentioned that the code i posted in my op was in I moved it all to to see if that would help and it seems to have. I also added a 5 second wait after the client.connect() which seems to have made it pretty stable. I'm not totally sure it had anything to do with me moving the code to but oh well, it works!