How to publish a disconnected client to another mqtt client (ESP32 - Arduino)

User avatar
Pyshco
Posts: 28
Joined: Wed Jul 19, 2017 2:36 pm

How to publish a disconnected client to another mqtt client (ESP32 - Arduino)

Postby Pyshco » Mon Aug 07, 2017 8:44 pm

I have a ESP32, a mosquitto server and a Android phone with Linear MQTT Dashboard installed. The thing is: I need to advice the phone about the state of the ESP32 specifically when is disconnected, I'm using the PubSubClient library.

Any help?

User avatar
kolban
Posts: 1683
Joined: Mon Nov 16, 2015 4:43 pm
Location: Texas, USA

Re: How to publish a disconnected client to another mqtt client (ESP32 - Arduino)

Postby kolban » Mon Aug 07, 2017 9:27 pm

An ESP32 can publish message on MQTT. These can be received by an MQTT broker which in turn can make them available to an MQTT subscriber. If the messages contain ESP32 status, then that will result in status information being supplied to the MQTT subscriber.
Free book on ESP32 available here: https://leanpub.com/kolban-ESP32

tele_player
Posts: 90
Joined: Sun Jul 02, 2017 3:38 am

Re: How to publish a disconnected client to another mqtt client (ESP32 - Arduino)

Postby tele_player » Tue Aug 08, 2017 4:20 am

If you're running your own MQTT server, for instance, mosquitto on Linux, you could write a script which publishes when a specific client hasn't published in a while.

User avatar
Pyshco
Posts: 28
Joined: Wed Jul 19, 2017 2:36 pm

Re: How to publish a disconnected client to another mqtt client (ESP32 - Arduino)

Postby Pyshco » Tue Aug 08, 2017 2:06 pm

Hey there Neil and tele_player!

Thanks for the quick replies and answers!

tele_player
Posts: 90
Joined: Sun Jul 02, 2017 3:38 am

Re: How to publish a disconnected client to another mqtt client (ESP32 - Arduino)

Postby tele_player » Tue Aug 08, 2017 3:17 pm

I haven't tried to do this, but I expect this could be done using a shell script, mosquitto_pub and mosquitto_sub, or a python script.

User avatar
kolban
Posts: 1683
Joined: Mon Nov 16, 2015 4:43 pm
Location: Texas, USA

Re: How to publish a disconnected client to another mqtt client (ESP32 - Arduino)

Postby kolban » Tue Aug 08, 2017 5:45 pm

@Pyscho,

lets see if we can clarify some of your needs.

You said:
I have a ESP32, a mosquitto server and a Android phone with Linear MQTT Dashboard installed. The thing is: I need to advice the phone about the state of the ESP32 specifically when is disconnected, I'm using the PubSubClient library.
You have an ESP32. Got that.

You have a mosquitto server. For others, this is an MQTT open source server. On what platform is the mosquitto server running?

You have an Android phone. Got that.

You have something called "Linear MQTT Dashboard" installed. Don't know what that is... but I'll assume its an MQTT client capable of receiving messages from an MQTT broker.

Now we get into the crux of the story. If I am understanding, you need to have the ESP32 post periodic MQTT messages on a topic which will then be delivered to Mosquitto server. An MQTT client that happens to be running on your Android phone will receive them.

Then you said "about the state of the ESP32 specifically when is disconnected".

In that statement ... when "what" is disconnected and disconnected from what?

Can you also paint some back story on the design/plan of why this function is needed. Putting it in context can commonly help.

In addition, can you answer whether this is a work project, a personal project or a school assignment?
Free book on ESP32 available here: https://leanpub.com/kolban-ESP32

User avatar
Pyshco
Posts: 28
Joined: Wed Jul 19, 2017 2:36 pm

Re: How to publish a disconnected client to another mqtt client (ESP32 - Arduino)

Postby Pyshco » Tue Aug 08, 2017 6:12 pm

Back story: I'm on a internship, and they need a ESP32 reading constantly the voltage of a battery to get the actual "%" of it , the ESP32 needs to be connected to a MQTT server (Mosquitto in this case) and they need to get notifications on a phone or Gmail (in this case i choose the phone), I have all done, but, they need a notification if the ESP is disconnected of the network or else (example: if was unplugged).

PD: Yes, Linear MQTT Dashboard is a MQTT client on android.

User avatar
kolban
Posts: 1683
Joined: Mon Nov 16, 2015 4:43 pm
Location: Texas, USA

Re: How to publish a disconnected client to another mqtt client (ESP32 - Arduino)

Postby kolban » Tue Aug 08, 2017 7:21 pm

If I am understanding correctly, the ESP32 is producing messages on a regular basis ... but if the ESP32 should be "lost" from the network, then you wont to know that. What about detecting that a message was NOT published by the ESP32 after a period of time when we should have expected the ESP32 to publish a new message? Would that be a sufficient algorithm?
Free book on ESP32 available here: https://leanpub.com/kolban-ESP32

User avatar
Pyshco
Posts: 28
Joined: Wed Jul 19, 2017 2:36 pm

Re: How to publish a disconnected client to another mqtt client (ESP32 - Arduino)

Postby Pyshco » Tue Aug 08, 2017 7:33 pm

I think so, but, how do i do that algorithm? *I'm new on programming to*.

tele_player
Posts: 90
Joined: Sun Jul 02, 2017 3:38 am

Re: How to publish a disconnected client to another mqtt client (ESP32 - Arduino)

Postby tele_player » Tue Aug 08, 2017 7:57 pm

It's simpler than I thought. Google 'mqtt last will'. No additional script needed.

Who is online

Users browsing this forum: No registered users and 79 guests