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

Good 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.

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 PyCom 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 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 even between you'll have a gateway on 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 on to 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 LoPy4 and a LoPy1, the only difference with the LoPy4, is it supports an extra 433-megahertz frequency range which the LoPy1 doesn't, but effectively they're just LoPy4.

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 4 radio towers - 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 Nano gateway. So, we're going to build a PyCom Nano gateway that connects us to the things network. Right so having been through the coverage map, I'm going to skip a lot of the documentation here, I'll point out here the link to Internet of Things with PyCom and adafruit, that will help you through all the basic things like uploading firmware and getting Atom and the pymaker extension installed, so pretty much everything is linked from here. Alright there's a piece of code here that 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. Alright so 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 LoRa-Nano-gateway, we want LoRa Wan-Nano-gateway, okay and from this project you can see there are four files here that are called node so, they will be used depending 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 TTN Nano gateway 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 on the top looking at the gateway code. Alright so 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 happen 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, etc., etc. 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, 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 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, 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 megahertz frequency range, now because we're not using Europe and we're not allowed to use Europe, we need to change this to the AU 915 and we need to fix this LoRa 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 is our frequency 916.8 megahertz and so we'll replace that with that. So that's all 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 COM11, 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, there 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 will look out 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 documentation we need to tick this box, do the second thing first, I'm using a legacy packet forward and now I need the extended unique identification for this device. So, what I really wanted to do is 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 30aea4fffe5913b8, and we've pasted that in to the TTN console. The description is anything you like, I'm just going to call this a low Py gateway and because we're in Australia we must select the frequency plan on the devices that was set when I did last firmware update and if you haven't done a firmware update that's how you do it.

Alright 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, the what the setup is and here's how you can contact me about it, but we won't do that right now.

Ok so we have the gateway ID, its name, it belongs to me and it's is connected. So, in the last scene just down here, we can see that was last seen, seven seconds ago. So, 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. Alright so this is as it was before it, goes through the process of starting up and now we can see last seen now and we've got messages on the screen in the bottom left, there it's pushing an acknowledgment, 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, I'm 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 abp_node.py, which has been setup 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 OTA an over-the-air activation. So ABP is a set of keys, encryption keys, where you go into the things network console and the 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 you to secure the information from end to end, from the things that work website all the way through to your device. Ideally, you don't want to have to go into every devices 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 devices 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 OTA a 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 OTA authentication for that.

Alright 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 it under this application, we have one device registered which is my other LoPy device, so if I go in here I can see the LoPy devices EUI is this number and the number here is the actual LoRa MAC address, so this is not an invented one, 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. Now at the bottom here, I'm going to 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 going to reset the frame counter here and then go to the ABP node code, for our node and I'm going to run that, no file open to run, 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 0, that's the /x00, the next one contains the binary value 0,1 which is just the number 1 and you can see above that the Gateway here is receiving a packet on the LoRa Wan 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 devices 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, etc., etc.

Alright so there will be future videos that extend on from this, I definitely want to get OTA a working, so that I can show you that in a future project but we'll cover at the development of a node and we'll show you also how to add on to the back end of your Things network, another service so that you can come talk directly from your device to things like, if, this, then and that.

So, thank you again, for watching, again the link is down below for all the documentation there.

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.