In this video of our Meshtastic for Makers course, we're going to be connecting our mesh of sensors and nodes to other things via MQTT. We'll continue with the node we set up in the previous videos, which you can find in the playlist or the written course linked below. First, let's clarify something. When someone in the Meshtastic community mentions MQTT, they often refer to network tunneling meshes together. This involves having a mesh in one place and using an MQTT server and the internet to connect it to a mesh in another location. There's a public MQTT server for Meshtastic devices, but we are not doing that. Instead, we'll connect our mesh of sensors to an MQTT broker. This will create a connection point from our mesh and nodes to whatever else we want. For example, if you connect it to Adafruit I.O., you'll be able to monitor your sensor data over the internet anywhere in the world. If you send it to your home assistant server, you can integrate your Meshtastic connected devices into your home automation setup. As long as it has MQTT compatibility, you can connect your Meshtastic nodes to it. For this video, we'll demonstrate sending and receiving data with Adafruit I.O., but the method can be used for other MQTT applications. The regular internet tunneling method uses the MQTT module in the Meshtastic client, which is easy to use, but it won't work for us. Instead, we'll use a second microcontroller. Importantly, you'll need a microcontroller that supports Wi-Fi connectivity. For our code along, we'll use the Pico W and MicroPython, but other microcontrollers and languages are possible. Before we begin, we'll need to install an MQTT library for our Pico by searching for micropython-umqtt.simple. Make sure you have the same library, and now we are ready to go. We'll paste in the sample code, which you can find on the course site linked below. Let's quickly go through it. We start by importing all the necessary libraries, then set up some information that will be helpful later, including our Wi-Fi name, password, and MQTT details. I'm using Adafruit I-O, so enter your username and password, and create a unique client ID for this device. I'll call mine JaredMeshTasticDevice1. On Adafruit I-O, I've created a few feeds: temp1, soil1, and light. In my code, I've assigned these feed names to variables. Adafruit I-O is intuitive, but if you need help, we have a guide linked below. The guide is the basis for most of this video. We set up a UART as we did last time and an LED for debugging. The first function connects your Pico to your local Wi-Fi network using the details we provided. It flashes the LED while connecting and turns solid green when connected. Next, we have the process UART message function, which extracts the actual text we want. The MQTT subscription callback function handles messages received from our MQTT broker. Initially, it sends the message via UART to the Meshtastic network. Now, it gets tricky. We're defining an asyncio function, which allows two separate bits of code to run simultaneously. We have two asyncio functions, and then we start running our code. We run the connectWiFi function to connect your Pico to the internet, set up all MQTT information, and set the callback function. We connect to our MQTT broker and subscribe to the MQTT receive topic, which is light. We use asyncio to run handleUART and handleMQTT simultaneously. Inside handleUART, we have a while true loop. We check for a UART message, sort it, and format it. If the message starts with soil1, we publish it to our MQTT client, sending it to our soil topic. On our Adafruit dashboard, we have soil1 and temp1 topics set up. We check if the message starts with temp and send it to the temperature topic. You can add more checks by copying and pasting the code. Running the code should connect to Wi-Fi and our broker. If I send a random message, it ignores it. But if I send soil1 followed by a number, it publishes it to our MQTT broker. Sending another simulated reading updates our dashboard. Our mesh and data from a remote node without internet connection come through to our Wi-Fi connected node via MQTT to a dashboard or wherever we want online. The second loop, handleMQTT, checks for messages from our broker. If a message comes through, it calls the callback function and writes the message to UART. Our other Pico repeats the message to the Meshtastic network. On our Adafruit dashboard, I've created a button to test. In settings, when the button is on, it sends light on, and when off, it sends light off. Update and save the layout. Turning it on sends a message that the light is on, and turning it off sends light off. The message goes from our MQTT broker over Wi-Fi to our Pico, then broadcasts to the Meshtastic network. If a node is set to respond to light on or off, we can remotely control something through the internet, even off the grid, as long as one part of our system is connected to the net. That wraps us up for now. If you made something cool or need help, visit our community forums. Until next time, happy making!
Makers love reviews as much as you do, please follow this link to review the products you have purchased.