Skip to content

It's "offline" after 10+ seconds, use umqtt.simple #824

@edgexie

Description

@edgexie

I try umqtt.simple to connect my raspberry with MQTT. But I found it's offline after 10+ seconds, and then I receive the error code in webrepl. Has anyone had the same problem?

The main snippets is:

def main(server=MQTT_SERVER_EMQX):
    # 连接WiFi
    connect_wifi()
    c = MQTTClient(generate_client_id(), server, keepalive=10)
    c.set_last_will(topic=b"esp8266/will", msg='offline', retain=True)
    c.set_callback(lambda topic, msg: sub_cb(topic, msg, c))
    # c.set_callback(sub_cb)

    res = c.connect(clean_session=False)
    if (res != 128):
        print('当前连接的MQTT服务器是:' + server)

        c.publish(b'esp8266/will', msg='online', retain=True)
        c.publish(b'led/status', msg=getLedStatus(), retain=True)

        c.subscribe(b'led/ctl')
        while True:
            if True:
                # Blocking wait for message
                c.wait_msg()
            else:
                # Non-blocking wait for message
                c.check_msg()
                # Then need to sleep to avoid 100% CPU usage (in a real
                # app other useful actions would be performed instead)
                time.sleep(1)

        c.disconnect()


if __name__ == "__main__":
    main()

image

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions