umqttsimple hangs when waking from deepsleep

Discussion about programs, libraries and tools that work with MicroPython. Mostly these are provided by a third party.
Target audience: All users and developers of MicroPython.
Post Reply
jbmfg
Posts: 2
Joined: Tue Jun 30, 2020 6:46 pm

umqttsimple hangs when waking from deepsleep

Post by jbmfg » Tue Jun 30, 2020 7:24 pm

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 = self.sock.read(4)

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 = "192.168.0.105"
client = MQTTClient(client_id, mqtt_server, user=b"*******", password="********")
print ("client setup")
client.set_callback(sub_cb)
print ("callback set")
do_connect()
client.connect()
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.active(True)
sta_if.connect('********', '*******')
while not sta_if.isconnected():
pass
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():
do_connect()
time.sleep(10)
for a in range(9):
for l in range(9):
try:
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])
except:
print (a, l, "no go")

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


def main():
sta_if = network.WLAN(network.STA_IF)
sta_if.active(False)
do_connect()
time.sleep(20)
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)
mqtt_actions(temp)
print ("published")
#rtc.alarm(rtc.ALARM0, 900000)
machine.deepsleep(10000)

main()

Christian Walther
Posts: 169
Joined: Fri Aug 19, 2016 11:55 am

Re: umqttsimple hangs when waking from deepsleep

Post by Christian Walther » Tue Jun 30, 2020 8:24 pm

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.

jbmfg
Posts: 2
Joined: Tue Jun 30, 2020 6:46 pm

Re: umqttsimple hangs when waking from deepsleep

Post by jbmfg » Tue Jun 30, 2020 10:57 pm

Good suggestions Christian, thank you. I should have mentioned that the code i posted in my op was in main.py. I moved it all to boot.py 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 boot.py but oh well, it works!

Post Reply