stlehmann / Flask-MQTT

Flask Extension for the MQTT protocol
MIT License
207 stars 71 forks source link

Any implementation of will_set()? #13

Closed gregsvo closed 7 years ago

gregsvo commented 7 years ago

GREAT work here. I'm actually presenting it as part of my presentation at the PyOhio conference next weekend.

I'd like to see MQTT's "last will" implemented if possible. I attempted to do it myself in the init file, as the will_set() function must be called before client.connect(). It doesn't seem to be working, but wasn't having any luck getting it to work. init.py file changes below:

   `def init_app(self, app):
        # type: (Flask) -> None
        self.username = app.config.get('MQTT_USERNAME')
        self.password = app.config.get('MQTT_PASSWORD')
        self.broker_url = app.config.get('MQTT_BROKER_URL', 'localhost')
        self.broker_port = app.config.get('MQTT_BROKER_PORT', 1883)
        self.tls_enabled = app.config.get('MQTT_TLS_ENABLED', False)
        self.keepalive = app.config.get('MQTT_KEEPALIVE', 60)
        self.last_will_topic = app.config.get('LAST_WILL_TOPIC')    <-------------HERE
        self.last_will_message = app.config.get('LAST_WILL_MESSAGE')  <-------------HERE

    if self.tls_enabled:
        self.tls_ca_certs = app.config['MQTT_TLS_CA_CERTS']
        self.tls_certfile = app.config.get('MQTT_TLS_CERTFILE')
        self.tls_keyfile = app.config.get('MQTT_TLS_KEYFILE')
        self.tls_cert_reqs = app.config.get('MQTT_TLS_CERT_REQS', ssl.CERT_REQUIRED)
        self.tls_version = app.config.get('MQTT_TLS_VERSION', ssl.PROTOCOL_TLSv1)
        self.tls_ciphers = app.config.get('MQTT_TLS_CIPHERS')
        self.tls_insecure = app.config.get('MQTT_TLS_INSECURE', False)
    # set last will
    self.client.will_set(self.last_will_topic, self.last_will_topic, 0, False)  <-------------HERE
    self._connect()`
stlehmann commented 7 years ago

Happy to hear your positive feedback and great to see it as part of your presentation. What will be the topic of your presentation? I'm from Germany so I sadly won't make it to PyOhio.

I will have a look at the last will implementation. As paho mqtt already provides this function it should be no problem to implement it. The way you implemented it seems OK to me. I'll see if I can find out why it's not working.

stlehmann commented 7 years ago

I've included last will functionality into Flask-MQTT according to your proposal. I've also added the configuration variables MQTT_LAST_WILL_QOS and MQTT_LAST_WILL_RETAIN for further configuration. Just give me feedback if it's working for you.

gregsvo commented 7 years ago

Fantastic! I'll give it a test this evening.

The talk is on writing IoT applications in a Python stack (MicroPython/Flask/Mqtt). The video is posted on YouTube a day or so after the event, I'll make sure to send you a link. Thanks again for the great work!

stlehmann commented 7 years ago

Great, just come back to me if you get any issues.

I would really appreciate you sending me this link to the video. I look forward to it and which you all the best for the presentation at Pyohio.