DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Because the DevOps movement has redefined engineering responsibilities, SREs now have to become stewards of observability strategy.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Related

  • IoT Needs To Get Serious About Security
  • Sending Sensor Data From Raspberry Pi Pico W to HiveMQ Cloud
  • Blink a LED on a Raspberry Pi With Vaadin
  • Java on Raspberry Pi

Trending

  • Designing for Sustainability: The Rise of Green Software
  • Designing AI Multi-Agent Systems in Java
  • Power BI Embedded Analytics — Part 3: Power BI Embedded Demo
  • Assessing Bias in AI Chatbot Responses
  1. DZone
  2. Data Engineering
  3. IoT
  4. MQTT Messaging With Java and Raspberry Pi

MQTT Messaging With Java and Raspberry Pi

In this article, learn how to send and visualize sensor data using multiple types of Raspberry Pi boards, MQTT, and HiveMQ. Includes video tutorials.

By 
Frank Delporte user avatar
Frank Delporte
·
Mar. 18, 22 · Tutorial
Likes (6)
Comment
Save
Tweet
Share
10.7K Views

Join the DZone community and get the full member experience.

Join For Free

In December 2021, I had the chance to write a three-post blog series for HiveMQ. They provide an MQTT compatible message broker you can install yourself, or through a cloud service they provide. That cloud service is free to connect up to 100 devices! Even for the most enthusiastic maker, that’s a lot of microcontrollers or computers! 

You can find example code for many programming languages on the HiveMQ site, but as a Java and Raspberry Pi fan, I wanted to add some extra examples. The full descriptions are available on the HiveMQ blog, so let me give you a short overview here on DZone to give you an idea of the possibilities.

Sensor Data From Raspberry Pi to HiveMQ Cloud With Java and Pi4J 

The first blog describes a data-publisher application and the full sources are available on GitHub. We will read the data from different sensors and send their values to HiveMQ Cloud. Because every “maker” will have his/her own ideas and wishes for a project, we will not focus on components and wiring, but use an easy starter kit that is perfect for this demo project: the CrowPi. This pre-wired kit makes it very easy to get started with electronics programming as all the components are pre-assembled and wired. At least that’s one thing you don’t need to worry about or can mess up, if the sensor doesn't work as expected, it's most probably an error in your code...

CrowPi

By combining the CrowPi with example Java-code from the Pi4J project and the HiveMQ MQTT-library, it becomes very easy to send values from the sensors either periodically or on events.


Visualize Sensor Data on a TilesFX Dashboard

In the second blog post, a JavaFX dashboard application is created that connects to the message broker and visualizes all the data in a nice overview. 

JavaFX dashboard showing sensor data

This application doesn't use any specific functionality of the Raspberry Pi as it doesn't connect directly to the sensors, so it can also run on any PC. The advantage to run on a Raspberry Pi is the price. Imagine you want to have this dashboard always on to monitor a set of devices. In that case, a cheap version of the Raspberry Pi and a small screen are sufficient to run this application.


Sending Sensor Data From Raspberry Pi Pico

The third post uses a completely different type of board: the Raspberry Pi Pico that was launched in early 2021. This isn't a full Linux PC like the other Raspberry Pi boards, but a microcontroller, comparable to an Arduino. The Pico is a small and versatile board with the RP2040 microcontroller chip that has been developed by Raspberry Pi itself. This RP2040 features a dual-core Arm Cortex-M0+ processor with 264KB internal RAM and support for up to 16MB of off-chip Flash, combined with a wide range of GPIOs (supporting I2C, SPI, and programmable I/O (PIO)). But the biggest surprise of this Pico: the price of only $4!

By using an "Adafruit AirLift WiFi" module, a distance sensor, and CircuitPython, it is possible to send sensor values with MQTT to HiveMQ cloud with a single and simple source file.

Breadboard setup with Pico, Wifi module, and distance sensor

The original article contains a lot of extra info, the sources, etc. and the end result looks like this:


Conclusion

MQTT is a great way to exchange data between devices. If you want to run this locally, you can install Mosquitto (a light-weight MQTT Message Broker) on a Raspberry Pi. But if you are looking for an installation/update-free solution, HiveMQ Cloud is worth investigating as it is free for up to 100 clients.

raspberry pi MQTT Java (programming language)

Opinions expressed by DZone contributors are their own.

Related

  • IoT Needs To Get Serious About Security
  • Sending Sensor Data From Raspberry Pi Pico W to HiveMQ Cloud
  • Blink a LED on a Raspberry Pi With Vaadin
  • Java on Raspberry Pi

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!