In this guide we'll see how to build a Nano Gateway to The Things Network (TTN) using a Pycom device with LoRa: a LoPy4 or a FiPy.

Transcript

G'day, Chris again from Core Electronics. Today we're going to look at another LoRa project. Instead of getting LoRa devices just to talk to each other on the quiet, we're going to get those devices to connect to the internet through a gateway. As usual, there's documentation linked below, take your time to read that. I'll skip over a lot of things in this tutorial, so let's get started, hey?

So when you build a LoRa device, it's a low power, long distance radio technology, brilliant for the internet of things, created for the internet of things. Now, to get your LoRa devices' messages onto the internet, you need two things. You need first to have a gateway device, which speaks LoRa on one side, and some other access to the internet on the other side. Today we're going to use Wi-Fi, which is part of every PiCom device. Behind the gateway, you need some kind of internet service that receives all of the messages coming from your LoRa devices, decrypts them at the other end, aggregates them all in one place.

And some of the things that can happen to LoRa messages is that they can get duplicated, triplicated. If your device sends a message and it happens to be in range of more than one gateway, all of those gateways will send their messages into the aggregation point, the network service, and the network service will identify that that's three copies of the same message. So there's an important feature done both by the gateway to get LoRa messages onto the internet, and by the network service at the back, which is on the internet, and it receives the messages from all of your LoRa devices.

So when you want to build a LoRa internet of things application, you really want to think about your device at the front, talking to a service at the back, and in between you'll have a gateway and a network service that should be transparent. Once set up, you could pretty much forget about them and talk from your device direct to your web server at the end. So we're going to do this today using Pycom devices and the Things Network. The Things Network is a community-based LoRa gateway system. You can go onto the Things Network and register your own gateway and allow other people to use it. Likewise, you can freely use the Things Network gateways that are provided. So community-led effort, just the right kind of thing for the internet of things.

So today on our bench, we have a LoPi4 and a LoPi1. The only difference with the LoPi4 is it supports an extra 433 MHz frequency range, which the LoPi1 doesn't. But effectively, they're just LoPi4s. All right, on the PC here, you can see the documentation that I've started to produce. And the first thing I want to show you there is, here's the LoRa coverage map for where I am in Newcastle. There are actually four radio towers, two almost on top of each other in Newcastle West. And I'm at this red flag point out at Adamstown. And there's no coverage for me here. So I really want my own LoRa gateway. And Pycom makes that possible by a project called a Nanogateway. So we're going to build a Pycom Nanogateway that connects us to the Things Network. Right.

Having been through the coverage map, I'm going to skip a lot of the documentation here. I'll point out here that the link to Internet of Things with Pycom and Adafruit. That will help you through all of the basic things like uploading firmware and getting Atom and the PyMaker extension installed. So pretty much everything is linked from here. All right. There's a piece of code here that is a is a kind of a Things Network convention that when you register your own gateway, you give it its own unique identifier made from the network cards identifier. So we've got Wireless LAN Mac in here twice. So we're going to use this piece of code in a second to basically create a dummy unique device name.

Running down through here, there's a link here to download Pycom libraries. So I'm going to click that and download Pycom libraries. I'm just going to put it in my downloads folder. And here we go. I'll just extract it to here. And this will be the starting code for both our gateway and the node that we can use to connect to the gateway. So going in through Pycom libraries and into examples, don't grab the LoRaNanoGateway. We want LoRaWANNanoGateway. Okay. And from this project, you can see there are four files here that are called node. So they will be used depending on what kind of node you want. And the three files in the middle, config, main and nano gateway, help us with building our gateway. I've already created here a folder called TTNNanoGateway. And I've put a couple of files in there. I'm just going to add these three files to that mix. And now jumping over to Atom, I can open a project.

Right. So on the bottom here, I have a node project. So this is going to be used to test out our gateway and make sure that it works. I'll just leave that off the screen for now. Here we are in the top looking at the, excuse me, looking at the gateway code. All right. So whenever a Pycom project is running, whenever a Pycom device boots, it looks for boot.py first. If that's not found, doesn't matter. It goes to main.py. And here we have a main.py file. I'll just make that a little larger. We can see that's all the code in main.py. And what it does is it imports config and nano gateway, which happened to be the names of the two other files that we grabbed. And then it creates from nano gateway, an object that uses all these things from config, gateway ID, LoRa frequency, et cetera, et cetera. So let's have a look at this nano gateway first. It's quite a long file. It imports a lot of things here at the top, including LoRa and wireless LAN. And as we run down, it doesn't look like there's anything here that we need to change. And we get to the constructor for the class. So there's a class here called nano gateway. And this __init__ is how you create a new nano gateway. And it passes in things like what's its identifier, its frequency, its data rate, SSID, all the things that we just saw in the main.py file. Okay. So it looks like config holds the keys to everything. So let's jump over to the config.py. And here we have the code that I mentioned earlier in the documentation. We invent a unique identifier for this gateway using the FFFE in the middle. That's a things network convention. If we read through some more of the documentation, which I've got open up there on the next half of the screen, I'll show you that we're going to make a few changes.For one, we're going to change our router from Europe to Australia. And just to make sure we get a local NTP, network time protocol server, we're going to add AU.in there. And as this is our gateway, sorry, not node, we need connection to the internet. So under the bench here, I have a little Wi-Fi router whose name is IOT. And the password is... There we go. Now, down here, we have some settings specifically for Europe's 868 MHz frequency range. Now, because we're not using Europe and we're not allowed to use Europe, we need to change this to the AU915. And we need to fix this aura frequency. I've gone into this in detail in the documentation, but just for now, I'm going to scream down through the documentation and there's our frequency, 916.8 megahertz. And so we'll replace that with that. So that's all of the config file. We've now got an Australian network server for the things network. We've got a local network time protocol server. We've got Wi-Fi credentials to get to the internet. And we've got some setup that's specific for LoRa in Australia. So that should work for us.

So what I need to do is get connected to this device. It's on COM 11, so it should be there. When I do an upload, it'll copy all of the files from the project onto the device that aren't already there and already that version. And then it does a reset. And the main.py file should boot first. Here it goes, starting a LoRa WAN nano gateway. And that's it running. Now we've left something out. We need to go to the things network. If you haven't got a things network account, it's free to sign up. You just need your email address and a few bits of information. Once in, you can go to the console and the console has these two major sections. Applications, we'll look at very briefly at the end. Gateways are the things that we're interested in right now. So if we go to gateways, we can create a new gateway. Now, as per Pycom's documentation, we need to tick this box. Do the second thing first. I'm using a legacy packet forwarder. And now I need the extended unique identification for this device. So what I really want to do is come over to the console and come over to my Atom project again and run this bit of code. There we go. We've interrupted the gateway, but we did print out down here in the bottom left, 30AEA blah, blah, blah. And we've pasted that into the TTN console. The description is anything you like. I'm just going to call this a LoPi gateway. Oops, gateway. And because we're in Australia, the frequency plan on the devices was set when I did the last firmware update. And if you haven't done a firmware update, that's how you do it. All right. So we're going to change this Asia Southeast router to US West. I don't know why Asia Southeast doesn't work for me, but we're going to switch to US West and I'll register this gateway. There's more information you can put in there, including putting a pin on the map to say where you are. Obviously, if you're building a real gateway, you want to broadcast to people, hey, look, there's a gateway here. Here's where it's located. Here's what the setup is. And here's how you can contact me about it. But we won't do that right now.

Okay. So we have the gateway ID, its name, it belongs to me and it says connected. So in the last scene, just down here, we can say that it was seen seven seconds ago. So actually in the background here, the gateway is still running, although I can't see it. So I'm just going to do another upload, the trigger a reset and start it running again so that we can see what's happening. All right. So this is as it was before. It goes through the process of starting up and now we can say last seen now. And we've got messages on the screen in the bottom left there. It's pushing an acknowledgement. So there's a little conversation that goes on here between the gateway and the things network. Where it says, you know, I'm here. It's the other end says, Hey, I'm here. And they just occasionally talk backwards and forwards to each other. So you should be able to see that once your gateway is running, this last scene never gets older than about 30 seconds because another message will go through and reset it back to zero. Now, there's not a lot of point having a gateway unless we know that it works. So I'm going to use some pre-cooked code here. This is my node, which again, I've taken from the code that's provided by Pycom. Just going to disconnect and reconnect. All right. Now I'm going to do an upload there to make sure that config file is on there. This project doesn't have a main.py file. So it's only going to start the code file that I request. So I'm going to go into node.py, ABP node.py, I should say, which has been set up for Australia. And I've got some security information here that I need to match to the Things Network. Now, just to explain, there are two kinds of ways to get communicating with the Things Network. There's activation by personalization, ABP, and that's what we're going to use in this case. The better one is OTAA, over-the-air activation. So ABP is a set of keys, encryption keys, where you go into the Things Network console and encryption keys are generated for you. Then you go over to your code that will run on the device and you program in the encryption keys, you paste them into the program. And that is what's used to secure the information from end to end, from the Things Network website all the way through to your device. Ideally, you don't want to have to go into every device's code and program in encryption keys. The better way to do this is over-the-air activation. With that, all you need to do is give the Things Network the actual device extended unique identifier, which you can get from all of them in advance. You could basically paste in an entire file and say, here are my 300 devices and all of their unique identifiers. And then the Things Network knows that those devices belong to your application. Then when the device actually wakes up and starts to communicate on LoRa, the device says, hey, Things Network, can I talk to you? And the Things Network already agrees to talk to it because it has its EUI. And then at the start of the communication, the encryption keys are invented. So the two ends agree on what the encryption keys will be and then they use that. Now, due to an issue I'm having, getting my OTAA code to work, I haven't actually got that working in this example.It won't hold you back. ABP works just fine for people who are tinkering with Internet of Things or makers who have a single project that they're working on. But the ideal here is that you will build a gateway that other people can use and you would use OTAA authentication for that.

All right, so back to the code. If I go into the applications in the Things Network console, I haven't showed you how to create an application, but I've done it in advance. This will be covered in a future video. Under this application, we have one device registered, which is my other LoPi device. So if I go in here, I can see the LoPi device's EUI is this number. And the number here is the actual LoRa MAC address. So this is not an invented one that was used, that we use the network's MAC address to create.

Okay, so each time you create a device or mess with the application settings, these can change. So here we go. We can see that the device address is correct on the code as it is in the console. And if I have a look at this one, this one seems to be the same. And if I have a look at app session, that one seems to be the same as well. Okay, good.

Now at the bottom here, I'm gonna reset frame counters. One of the security aspects here is a frame counter that prevents replay attacks. So frame counters will get you stuck if you stop and restart your application over and over. So I'm just gonna reset the frame counter here and then go to the ABP node code for our node. And I'm gonna run that. No file open to run. All right, okay.

Now what we see here in the bottom left is the node is sending a packet and the packet contains the binary value zero. That's the slash X zero zero. The next one contains the binary value zero one, which is just the number one. And you can see above that the gateway here is receiving a packet on the Lauren side of its network and it's passing it on to the things network. So we can see here on the things network that the device's status is that it was seen now one second ago. If we switch to the data panel, we can see the data that's coming in from the device. And we get more information as we open it up. And we can actually see the radio frequency that came in on, et cetera, et cetera.

All right, so there will be future videos that extend on from this. I definitely wanna get OTAA working so that I can show you that in a future project. But we'll cover the development of a node and we'll show you also how to add onto the back end of your things network another service so that you can talk directly from your device to things like if this, then that. So thank you again for watching. Again, the link is down below for all the documentation. See ya.

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.