Comments (15)
See #186 for a fix which will resubscribe to all existing topics if a reconnect is made.
from micropython-lib.
In the screenshot above, I have restarted the broker after two successful MQTT publish messages (two temperature values in this case)
from micropython-lib.
I use code to avoid such problem:
try: retries = 0 while (retries < 20): retries += 1; client.check_msg() time.sleep(1); except OSError: connect_and_subscribe()
def connect_and_subscribe():
global client
client = MQTTClient(CONFIG['client_id'], CONFIG['broker'], CONFIG['port'], "user", "pass", 120)
client.set_callback(callback)
client.connect(False)
print("Conn to {}".format(CONFIG['broker']))
client.subscribe(b"MBI/CURRENT_DATETIME")
time.sleep(1);
client.check_msg()
.....
So I always check if there any OsError and reconnect to MQTT.
You need to upgrade the robust by this PR: [https://github.com//pull/117]
from micropython-lib.
Is there a better way to go about handling this situation where the broker 'disappears' and then 're-appears' at some later time? The current implementation of umqtt.robust is not robust at all, even with the reconnect. check_msg never works even though it seems the client reconnected.
Should the umqtt.robust object include a list of subscribed topics to auto-resubscribe in the reconnect() function?
from micropython-lib.
That last message as a microPython newby worries me... I don't want to start coding in microPython on the basis of unreliable MQTT as we have reliable MQTT in C......
Can someone give an example of this "robust" code which WILL stay connected and which will resubscribe on reconnection - i.e. so that it just works in the background.
Is this possible?
from micropython-lib.
Is there a better way to go about handling this situation where the broker 'disappears' and then 're-appears' at some later time?
Yes, the current implementation of umqtt.robust does not handle the case when the broker is restarted and forgets all of its state (at least the state related to your client).
Should the umqtt.robust object include a list of subscribed topics to auto-resubscribe in the reconnect() function?
Prehaps. This is indeed how other libraries work (eg https://github.com/fusesource/mqtt-client). The robust MQTTClient class would need to override the "subscribe" method to record the topics (and qos), and then in the reconnect() method it would call subscribe() again after reconnecting.
from micropython-lib.
Damn, I was in the process of writing a fix for this. You win the day, sir!
from micropython-lib.
@craftyguy I'd be interested to see your solution. And also if you want to test my solution and give feedback that would be great.
from micropython-lib.
I literally started about less than an hour ago, but my approach was pretty much the same as yours. Your solution looks to be more elegant/robust. I'll give this a shot possibly as early as tomorrow, since I'm tired of hacking together a more robust robust mqtt 😄
from micropython-lib.
I tried your patch (#186), and it doesn't seem to work with this simple test program:
import machine
from umqtt.robust import MQTTClient
import utime
MQTT_SERVER = '1.1.1.1'
IN = 'in'
OUT = 'out'
# mqtt subscription callback
def sub_cb(topic, msg):
t = topic.decode('ASCII')
m = msg.decode('ASCII')
print("received new topic/msg: %s / %s" % (t, m))
if t == IN:
print("IN: %s" % m)
umqtt_client = MQTTClient("test_client", MQTT_SERVER)
umqtt_client.DEBUG = True
umqtt_client.set_callback(sub_cb)
umqtt_client.connect(clean_session=False)
umqtt_client.subscribe(IN)
print("Connected to MQTT broker: %s" % MQTT_SERVER)
def main():
global umqtt_client
while True:
utime.sleep(1)
umqtt_client.check_msg()
umqtt_client.publish(OUT, b'hi!')
I should note that I am invoking the main() function here from main.py.
When I restart the mqtt broker, I get an mqtt: OSError(-1,)
printed to console. I can see the client reconnects to the broker since there's a message in the broker log about this, but the client doesn't respond to messages published to the IN topic, nor does it publish anything else to OUT topic.
If my test is an invalid use of umqtt, please let me know since I am relatively new to using mqtt!
from micropython-lib.
@craftyguy for your example to work I think you need to connect with clean_session=True, because you'll be explicitly resubscribing upon reconnection.
from micropython-lib.
@dpgeorge I see, thank you for pointing that out. I also see the PR was merged :)
I will give it another try!
from micropython-lib.
I also see the PR was merged
@craftyguy No it wasn't, so you'll need to pull the PR explicitly to test it.
from micropython-lib.
from micropython-lib.
I tried the PR and it does recover from a broker restart but when the broker loses power and comes back, it doesn't recover. It is completely reproducible, happens every time I take the power out from my raspberry pi zero running the mosquitto broker. Does anyone else face this behaviour?
The problem seems to be that umqtt.robust does raise an error when killing my mqtt broker from the terminal but doesn't raise any error when broker dies from a power outage.
from micropython-lib.
Related Issues (20)
- Deque is not a deque HOT 2
- aiohttp TLS websocket fails for continously sending packages on ESP32 HOT 11
- aiohttp sends Sec-WebSocket-Key with in wrong with b'' envelope HOT 2
- BLE: Bluetooth Low Energy HOT 4
- python-ecosys/requests can not post Chinese, why not add `; charset=UTF-8` to Content-Type? HOT 2
- Why can't I install umqtt? HOT 1
- Handle MemoryError in aioble_central.py scanner event HOT 1
- 'TypeError: can't convert NoneType to int' using aioble on Pico W HOT 1
- Handling the # TODO: Handle failure in the _IRQ_ENCRYPTION_UPDATE event
- Feature request: support for the HTTP Digest authentication on the requests and/or the aiohttp HOT 2
- SX1262 LORA driver error: RuntimeError: Internal radio Status (2, 1) OpError 0x20 HOT 2
- OSError: no SD card HOT 1
- HID keyboard won't release keys when called via schedule HOT 6
- Writing to a second serial connection in a thread has errors HOT 4
- Cannot init USB devices in boot.py HOT 5
- The order of bt_addr passed up in the ScanResult HOT 1
- Serial connections dropping characters, not flushing buffers HOT 2
- Use Emulation to Run RP2 PIO Code on Other Ports HOT 3
- Unable to save service discovery during bonding makes it impossible to get data from some bonded devices on reconnect HOT 3
- Can someone add a mini version of web3.py library to execute some blockchain transactions
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from micropython-lib.