In this video of our Meshtastic for Maker course, we are going to be learning how to send custom data from a microcontroller to the Meshtastic network so you can send your sensor data, automated notifications, or whatever your project really needs. We're going to be continuing with our Pico, which we set up previously, and please ensure that before you start sending your information, you create your own private channel so you don't annoy everyone around you. You'll find the playlist containing both of those videos in the description below. Alright, what are we doing? Well, we have a Pico running Meshtastic. Why don't we just read a sensor with this Pico and send that data through the network? Well, there are ways to do that, and we might look at that later in the series, but there's only certain sensors that are supported by this native plug-and-play system. And it's not very easy to just write your own sensor reading code and put it on the Pico because it's already running the Meshtastic firmware. Instead, we're going to use a workaround where we have a second Pico working together with our Meshtastic Pico. So this Pico is going to read our sensor data or gather whatever information we want to send, and then it will send that data to our Meshtastic Pico over you-what. And then the Meshtastic Pico will be set up to just repeat whatever it hears through that you-what connection. If this sounds a little bit tricky, don't worry, it's actually super simple, and we'll go through it step by step. And if you've never touched you up before, we have an introduction video linked below, but if you have some level of MicroPython experience, you're probably going to be able to follow along with just this guide.
Now it may seem a little bit annoying to have to use an extra Pico, but it's an inexpensive board, which is why we love it. And this setup that we're doing is going to give you the greatest amount of freedom in your project, and you can really customize it however you want to fit your needs. So to follow along, you're going to need the Pico and LoRa Hat we set up previously, as well as another Pico, and we're just going to be using some jumper wires and a breadboard to connect everything together. Now in this course, we're going to be using the Pico and MicroPython as our second microcontroller, but if you want to use an ESP32 or an ARTWINO or even C++ instead, it is definitely possible. As long as you know how to send you up commands to the Meshtastic Pico, it's going to pretty much be the same workflow. We're just going to be using MicroPython and the Pico in our code-along examples. Alright, let's shut up our Meshtastic Pico. Head on over to your Meshtastic client, connect your Pico, and we're going to go to CONFIG, and then down here we'll select MODULE CONFIG. All of these tabs up here are Meshtastic modules, and each one can kind of be thought of as an extension that enables new capabilities for your Meshtastic device. There's some very, very cool things in here that you should check out in your own time, but we are going to be using the serial module. Here is just another term that people use for UR. So first things first, we're going to enable it, and then we're going to specify our transmit and receive pins. So our receive pin is going to be 9, and our transmit pin is going to be GPIO8. These are the default UR pins that Meshtastic uses for the Pico, and I would avoid changing them as it will likely just not work. If you do accidentally use the wrong pins, you can also actually soft-brick your Pico where it just gets caught in this forever looping boot cycle. If that does happen in the written guide below, we have a fix for that. Then we're going to set the board rate to 115,000, 200, and this is just the speed that our UR connection is going to operate at. If you need to change it to something else that your project needs, do whatever you want, but for this demo, we're going to be sticking with this value. And finally, we're going to set our MODE to text message-like. So very important on that one. This mode, any time the our Meshtastic device receives a UR message, is just simply going to repeat it and broadcast it to the network. This mode also does the opposite, and if this device receives a message from the Meshtastic network, it's going to repeat it over UR to our Pico. And in the next video, we're going to be using this to control some hardware with Meshtastic messages. And that is all we need to do here, just to ensure you hit save.
Now let's go ahead and connect everything together. First of all, let's place in our second Pico, like so. Now unfortunately, that lower module we're using has the battery connector on the bottom that makes it difficult to place in a breadboard. To fix this, we're using these header stackers to give it a bit more clearance. You can breadboard without it, but it might be a little bit janky. You'll find a link to these in the description below and in the written guide. Just tack the antenna on the side like so with a little bit of blue tech as well. Starting off, we're going to connect a ground pin from one Pico to the other. Any ground pin to ground pin will work. This is just to ensure that they share a common ground. We're going to need to make our UR connections. We're going to connect pin 8 of one Pico to pin 9 of the other Pico. And then we'll connect pin 9 to pin 8. This means we have RX to TX and TX to RX. Previously, we mentioned that you could connect an OLED to your Meshtastic Pico and we're going to use this as an opportunity to do so. Now you can use the standard 1-inch SSD 1306 OLED that you see everywhere. But we're going to spice it up and use this giant 2.4-inch OLED, which is also compatible. Pretty much the same thing just scaled up bigger, same resolution at all. Plug in your power pins and we're going to connect SDA to pin 4 and SCL to pin 5. And if we want to go all the way, we can also plug in a button to let us navigate the menus on this screen. Connect the button from ground to pin 17. Again, both of these are entirely optional, but the more we progress into this series, the more that this OLED can be used to help debug issues, but it's not compulsory.
Alright, let's start programming our second Pico. If this is your first time programming in MicroPython and Thonny, or you just need a refresher, we'll have a video link below to get you going. It's real short and it's beginner friendly. We're pretty loud in the coding here, so we're going to go through and do it from scratch and explain what does what along the way. But if you want to find the full written out code, you can get that on our course page linked below. So starting off, we'll import the libraries we need, which is going to be obviously UR as well as pin and the time library like so. We're going to go ahead and create an instance of our UR. We're just going to call it lowercase UR just for ease of use here. It's going to be using pre-heifer 1, which is what pins 8@9 are plugged into. And then we're going to set our board rate to 1115,200. Make sure that is the same number you set in your Pico Meshtastic board rate they need to match. And then we're going to tell our Pico to use pin 8 to transmit on. And then we're just going to copy that over. And we're going to tell it to use pin 9 to receive on like so. And then we'll create our infinite while true loop like so. write. And then they send a test message like so. sleep for five seconds. And this is it. This is the bare minimum code we need to send messages. We create a UR instance. write, whatever we put in those brackets there or whatever variable we put there, it sends it to the other Pico via UR. And then that sends the message to the Meshtastic network. And then our five seconds sleep here just means that we're going to be sending that message every five seconds. Let's give that all the test. I've powered on our second Pico. By the way, this Meshtastic Pico doesn't need to be plugged into a computer. You can plug it into a USB wall charge or a phone power bank or you can even power it through its GPIO pins. It just needs power to operate. I've also got another Meshtastic device connected to my phone. And if we go ahead and run that script we just write, we should see some messages coming through. There is our test message and it should come through every five seconds. Beautiful. And that is how easy it is to send data from a microcontroller to the Meshtastic network.
I reckon we're going to go through and do one more example of actually reading a sensor and sending that data on. I think very example will use a soil moisture sensor. So we'll go ahead and unplug our Pico's. We don't want to work on it while it's being powered. And we are going to plug this sensor into power and plug it into our ADC. This is just a very basic analog soil moisture sensor we're using. Plug it back in like so. So we're going to go ahead and import the ADC library which we need. Whatever you need to import, you should import up there. And then we're going to go ahead and set up the pin that we will be using for it which is going to be pin 26 our ADC pin. Like so. Beautiful. At the start of the loop we're going to start by taking a reading of our soil sensor like so. I'm saying 16. And then we're going to write that reading to our UR connection. Now there is something very important here which is why we're doing this example. When you read this sensor it's going to come through as an int or a number. And unfortunately when we try and write something to UR it has to be in the format of a string. So if we went ahead and tried to run this code we can see it's not going to work. So if you are getting a number from your sensor reading or whatever you're doing you first need to turn it into a string which can be done with this string conversion line like so. Now if we run it it's perfectly fine. And if we run this code you can see that we are now getting that soil moisture sensor data being pushed through. And with this sleep function we can control how often we want to send that data. Five seconds is probably a bit excessive if we wanted to do 10 minutes we could do 600 seconds. If we wanted to do every hour we could do 3600 seconds. However frequently you want to send your data you can set it by just changing the time in seconds here. And that is how you can send any sort of data you like from a microcontroller to the mesh testing network. You can modify this code to do anything you want read any sensor you want perform any calculation you want. write it will be sent via mesh testing. Well that about wraps that up. In the coming videos we're going to go and do some more with this setup and do the opposite where we receive mesh testing messages on our Pico and do something with it. If you made anything cool with this or you just need a hand with anything we covered in this video you can head on over to our community forums or our makers over there and we're happy to help. Until next time though happy making.
Makers love reviews as much as you do, please follow this link to review the products you have purchased.