umqttsimple hangs when waking from deepsleep
Posted: 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()
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()