In our two previous Pycom articles we covered Building a LoRaWAN Nano Gateway to The Things Network, and creating Your First LoRaWAN Node on The Things Network. In these articles we used the mighty powerful Pycom LoPy (part of the Pycom range) to build two nodes of a LoRaWAN network connected to The Things Network.

Transcript

For the project today, I've invented a little scenario about a farmer. The farmers got a paddock, that's maybe ten kilometres from the house, where he's got pump water into a trough to give to the animals. How's he going to manage that. Well, I guess he could put up a solar panel and a battery and run a little pump, run it a few minutes a day, but what happens if it stops working, he's going to have to ride out there on a motorbike and check it every day, otherwise the animals would suffer. So, you might think to invent some little bit of electronics that look at the water level in the trough and pumps water when it needs to, great but then what happens if that stops working you have no way of knowing again. You're driving out there and having a look at it every day, what if we could create a sensor that would look at the water level in the trough, another actuator that would sit over at the pump to turn it on and off and those things although they're ten kilometres away were connected to the Internet. What if you could control it all from your house or better yet from your phone, while you're not even home. That's what we've got in our scenario today. So, let's have a look at the hardware on the bench on the right here.

 

We're using a PyCom LoPy as a LoRa gateway. It's just the way that we've done it in the last two projects, it's still doing the same job, it's using both its LoRa antenna to talk to the LoRa network and its built-in Wi-Fi antenna to talk to a little Wi-Fi access point I have under the desk simulating a gateway, that you would install at home. On the left, we have another LoPy device this is simulating our other two devices that would be running the pump and measuring the water level. So, the lights are just gone red on there, that's saying that the pump just stopped when the lights green the pump is on

 

Over here, we've added a Raspberry Pi and put a Node-RED on that, you don't have to install your own Raspberry Pi to run this system. You can use Node-RED on a PC or you could use it from a website, have a look at the documentation there are links in there for that and then we'll go back to the PC and go through in more detail how the whole thing operates.

 

All right not much to look at with the Gateway, as we've said it runs as it has done in the last two projects, good to see there that things are working, we can see messages going backwards and forwards. In our Node though, we might just make this code screen a little larger, we've added some code to the Node. I'm just going to scroll up here a bit, so we've added four variables here, one for whether the pump is running or not true or false, we want the water level that we can measure, what's the water level when the thing is empty, what's the water level when it's full and then we've taken some of the code that we need to run this and put it into functions just to group together a single feature of this program. So, this function is called check downlink messages and that's all it does. It needs to look at the pump running variable. So, we must call that in explicitly, that's the one that's defined up above the function. So, what it does is it looks for a message received on the LoRaWAN network, if there's not a message then return that, just says stop nothing more to do get out quick the function. If there is a message this is a little confusing here, if there is a message look at the first number in the message. So, position zero is the first number in the message and because we're doing an "if" on that, "if" that first number is not zero, then this part of the program runs. It's a little awkward, that we're talking about, two zeros, there one is a position and one is logic. So, if there is a number given to us that's not zero, start the pump, so we mark the pump that it's running if the number is zero, we take that as a command to stop the pump and now the pump running is false.

 

We have a send level function here and this is run every 10 seconds in the program, it would normally measure the actual water level. So, you could use an ultrasonic sensor, which you would position above the water you bounce the ultra-sonic wave off the water and measures the height of the water. So, you get a continuous measurement from empty to full, or you could use something, like a conductance probe which, would sit at the top and as the water level comes up and touches the probe, you know to stop the pump there because that's the top right. So, we measure the water level and here we're just faking it we've got a pretend, how that's working so we're taking our water level and sending it up to the Things network.

 

Another function here adjusts the water level, so this is if the pumps running, we pretend that the water level is increased by one. When the pumps running or the water level is decreased by one when the pump is not running and then if you're familiar with the Node code, we've had before we've got a wild true loop which just goes around and round forever and controls everything that happens. So, at the top here every 10 seconds, we adjust the water level we send the water level off to the Things network and then for 10 seconds we look every second for a downlink message and if the downlink message has turned the pump on or off. Then we update the LED to green or red, that's where we would turn the pump on and off. Right, so each of these loops has a one-second delay. So, 10 loops of 1 second that's a 10-second delay, so the next time adjust water level runs will be 10 seconds after the previous. We can see the messages in here sending water level 2, 3, 4, 5, 5 and stopping the pump and then we see 4 and stopping the pump again.

 

So, what's happening is there's a bit of a lag in LoRaWAN Network messages, so the Node-RED server is saying, "hey the water level hasn't dropped yet, you really need to stop that pump", it's getting 2 messages to start or two messages to stop that's just because of the delay inherent in the system, we can fix that with programming. So once the pump has stopped, we see the water level drop 5, 4, 3, 2, 1, 0 and it sits at 0 for a bit before the pump starts, again and again, we get 2 commands to start and we see the water level rise. So that's how our Node is operating that would be two separate devices in the farmer scenario we've invented, let's see how this operates at the Things network.

 

So, any LoRaWAN network needs to have LoRaWAN devices and they need to have something connected to that they must be pre-registered to something that manages the communication through the internet and across the LoRa we're Network. So, we could use PyBites from PyCom there are various other things available but we're using the things Network now and it's proved very good so far. So we've got as we look at the console we can go to gateways, we've registered one gateway, the idea of course is that eventually the Things network will have gateways everywhere and you won't even need your own gateway, you'll just use gateways that exist already down the bottom here we can see when it was last seen and that keeps resetting back to zero, so we know our Gateway is operating in real time. If we go down through applications, we can have a look at the application we created and into the one device we have registered to that application, here it is and it 2 has a status of four seconds ago. So, it's working in real time and being seen by the Things network.

 

Lovely if we go back up to the application and have a look at the data coming through the application. The arrows we have here on the left give us whether a message is a downlink message or an uplink message. Obviously down and up arrows, the counter is keeping track of each message that goes, there all uplink message counters and the data that's coming through, you can see here we have payload 0,2, now 0,1 so the water level is dropping the payload is saying this is the data that's coming from the device it's just sending one number and the number is 1. However, we're now telling the things network to interpret that for us and give it to us in a nicer way. So just below that in the fields box, it says water level 1. So, we're using the Things network to take our binary byte data, which is ugly and difficult to work with and turn it into JSON, which is human readable and makes far more sense and it's much better for us to send on to another device like Node-Red.

 

So that it understands the data, so we have messages going in both directions, downlink messages, if the downlink message just has the water level in it nothing gets sent. If the downlink message has a pump running a command in it, as this one does, that message will be sent out to the device otherwise. We see messages coming in giving us the water level 1,2,3,4 so the water levels about to hit the maximum and the pumps about to stop. So the way that we've converted the binary data into JSON which is far nicer to read is by, using the payload formats which is part of the Things Network, if we go into payload formats on our application, there are four boxes here where we can include a little JavaScript and they come with some default JavaScript, so you show you how they work.

 

So, in the decoder, we've taken the first byte that's coming in from our PyCom Node and we've said let's call that water level, that's far more descriptive and we return that instead. We can also use a converter and a validator function to decide whether the information coming in is good, so I have in the validator said if the number of the water level is higher than five its rubbish it doesn't make sense, if the water level is less than zero likewise it doesn't make sense, so return false we'll drop that message and it will go nowhere. If it passes that then we return true and the message is passed out of the Things network to whatever we've connected it to. In the other direction things coming into the Things network which needs to be passed on to our devices runs through an encoder. So, we need to convert data back into that binary format of numbers, so that the devices can act on it, we're only allowed to send numbers across the LoRaWAN network. So, this time we're looking for a message that has pump underscore running in it and we're looking for the values 0 or the value 1 and if that's what we've got then we create a message after the device that is just one byte and the byte will say 0 or 1, 1 for run and 0 for stop.

 

All right so now we have nicely formatted data coming from our devices it's as if our LoRaWAN devices, don't speak binary anymore, it looks to the rest of our code that they speak JSON which is much nicer. So, if we switch over now to Node-RED, we'll go through how this works. Now, Node-RED is the current coolness, the way that people are building these kinds of projects integrating data from all sources if you want things sent out as emails, if you want to read SMS’s, if you want to fire tweets, if you want to use artificial intelligence, there are inputs and outputs to Node-Red, that do almost anything. So, we've added on to Node-RED TTN which is the Things network. So now we have nodes that we can input and output data directly to and from the Things network. So, Node-Red is connected directly to the Things network and we've had to use appreciated key between the two, so this security setup between them, so information is securely handled in between the Things network and our Node-Red device.

 

So here on this gridded area is where we build a flow to control what's happening, I'll just turn that debug message back on and redeploy and on the left here you can see all of these nodes that can be added onto the flow, there are inputs, there are outputs and there are various other kinds functions, we've used a function in this flow, there's social media, there's a storage analysis and even some Raspberry Pi functions, you can directly control GPIO's on the Raspberry Pi. So, the input we have we've called get water level and if we look at the features on, that all it's doing is connecting to our app on the Things network that's called LoRaWAN app 1. So, all the LoRaWAN app wander is coming in through this node because we only have one device, we don't have to be specific, but you could specify the device it comes from and the field that you're interested in.

 

On the right here at the bottom, we have a send pump control output. So, it's going back to the LoRaWAN app one, on the things network and we've specified which device we want that to go, LoRa node one which is our device on the left. So, it's configured for that in between those two, we have this orange box which performs a function and it has some more JavaScript in it, so information comes into Node-Red in the form of a message and the message has a payload that includes the description for the water level. If the water level is zero, we're going to add on to the message a new description for a thing called pump running and we're going to give it the value one, if the water level is 5, we're going to add a pump running and make it zero. So, if one of those two conditions occur, we'll add value on to our message that's coming in and we'll turn it around and send it out that's the return message. So that's why you'll see in the Things network message is coming in from Node-Red that aren't going to be sent down to our node because they don't contain a command about running the pump, so at the Things network we just say stop there if you're sending a message that's not about the pump we don't care.

 

All right there are a few other boxes here on this flow, we've got a manual start and a manual stop, and we can send messages into the debugging area. So on the right here we have the debugging area and the latest message is at the bottom so we can see the last message to arrive says water level 2 and we've done nothing about that because it needs to be zero or five for us to act on it, the next one is one level three and you can see the same thing being mirrored over here in the ripple box that's attached to our node, so the node is sending the message and we can see that the message is arriving here in Node-Red. Lots of use of word node sorry for the confusion.

 

Of course, there's lots more information in the documentation about this project, so the documentation will be linked below, if you're brand new to Raspberry Pi, no fear, no dread, will run on a PC Node-Red can be used from a website. You can sign up for free use of Node-Red. So, your choice to put a Raspberry Pi into your network is up to you. If you see on our website here, I want to bring attention to a couple of things if you go into the learn area and go into tutorials I'm normally promoting the PyCom tutorials in here but today, have a look at the Raspberry Pi tutorials there are 45 in there and with all of these tutorials there's a new feature on the website here, if you open one of our tutorials you now have at the bottom of the screen ask the author of this guide today.

 

So if you've watched this video and are inspired to go and work through the tutorial documentation great if you get stuck at some point please ask me directly there otherwise, feel free to jump on to support and go into the forum, the forum automatically has a new article added each time we do a video, so you should be able to find exactly this tutorial and ask questions that way as well, they'll come straight to me and you and I can sort them out together.

 

So I hope that's inspired you to do a LoRaWAN Internet of Things project, this is how it works in to end, this is three tutorials worth of work to replicate yourself but I'm sure you'll find it very rewarding and I'm looking forward to doing some major projects around the office using LoRaWAN and we'll put up our own gateway, will reserve this Raspberry Pi as our Node-Red server and we'll connect everything through LoRaWAN. Thanks for watching and see you later.                                                                      

Comments


Loading...
Feedback

Please continue if you would like to leave feedback for any of these topics:

  • Website features/issues
  • Content errors/improvements
  • Missing products/categories
  • Product assignments to categories
  • Search results relevance

For all other inquiries (orders status, stock levels, etc), please contact our support team for quick assistance.

Note: click continue and a draft email will be opened to edit. If you don't have an email client on your device, then send a message via the chat icon on the bottom left of our website.

Makers love reviews as much as you do, please follow this link to review the products you have purchased.