简体   繁体   中英

Is it possible with “org.eclipse.paho.client.mqttv3-1.2.0” Library in Java to get more than one MQTT message at a time?

I'm new here at Stackoverflow and have a question.

I want to create an IoT weather station with a ESP8266 that has two sensors (humidity, temperature) and send the data to a broker (Mosquitto) running on a Pi. Beside the easy way to fetch and visualize the data with nodered I want to program a desktop app in Java.

I use the "org.eclipse.paho.client.mqttv3-1.2.0" package to create a client and fetch the messages. With one topic (eg "/test/temperature") it's easily possible to receive the temperature values. But if I add a second client for humidity, it's not possible to receive the two at the same time. When two clients are implemented just the later called client gets its values.

I solved the problem by sending the values from the ESP8266 with a little time shift. But is there a way to fetch to messages at the same time? I thought about Threads but it's not working.

Has anyone had already the same problem or got an idea?

Thanks in advance and don't hesitate to ask further questions.

Cheers.

Robin

No, messages are not fetched from a broker, they pushed by the broker. The broker will push messages as they arrive and they will always be one at once, this is just how MQTT works.

The way to do this is to maintain a state machine that holds the last value for each sensor and use these values to update the output when ever one value changes.

First off, you need to rethink your topic structure. Don't be so restrictive. Hand out topics like houses hand out candy on Halloween. :)

ie

robin/weather/ESP8266/temperature
robin/weather/ESP8266/humidity

A state machine can be almost anything. ie Hashtable, database, etc.. If you want to keep historical data then I would go with a database. ie SQLite , Derby , H2 , etc. I use the “sqlite-jdbc” driver from Taro L. Saito . The JDBC driver works extremely well and he keeps the code in sync with the SQLite code base.

A basic database would have 2 tables: Temperature and Humidity. Have the client subscribe to both topics and when a message arrives, update the appropriate table with the incoming value and the current date and time.

If you want, you could create a 3rd table (ie 'Recent') and it would only ever have 2 rows (one for the current temperature and one for the current humidity) in the table that are constantly replaced.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM