Pycom Pygate setup
Overview
The aim of this lesson is to learn is to learn how to setup a Pycom Pygate LoRa WAN Gateway. This gateway is useful for testing LoRa sensors to ensure that they are working correctly and sending data to The Things Network. Even though LoRa WAN Gateways are installed in several local government areas (and also be hobbyists) it is more reliable to have a LoRa WAN Gateway within close range and/or a gateway where the traffic can be monitored using a computer.
In this application, we are using the Pycom Pygate expansion board and a Pycom WiPy microcontroller. The LoRa antenna is connected to the Pygate expansion board. The WiPy 3.0 connects to a home or school WiFi router so that LoRa data can be sent to The Things Network.
In addition to the instructions in this lesson you can visit the following URL https://docs.pycom.io/tutorials/expansionboards/pygate/
Learning Objectives
- Learn how to update the firmware on the Pycom Pygate and WiPy units.
- Learn how to register a LoRa Gateway on The Things Network.
- Learn how to configure and upload micropython software for the Pygate gateway.
Pycom Pygate Setup
Update the firmware on the Pycome Pygate and WiPy combination
- It is very important to always keep the firmware on a microcontroller updated to the most current stable version. * Firmware is software that is installed on hardware that is similar to a small operating system.
- It should be updated monthly to help ensure that latest updates and security patches are installed.
- Ensure that the Pycom WiPy microcontroller is plugged into the Pycom Pygate board.
- You will need a USB type-C plug for the Pygate expansion board and a USB 2.0 type-A plug for your computer.
- Connect the Pygate assembly to your computer.
- Download and install the Pycom Firmware Updater tool and start the software.
- The Welcome page should appear. Click Continue.
- At the Setup Instructions page click Continue.
- At the Communication page select Type: pygate.
- Click Continue.
- At Pybytes registration click Skip.
- Under Advanced Settings ensure that the settings are the same. Click Continue.
- The Pycom Firmware updater will now update the firmware.
- A notification will appear when the upgrade is complete.
Register the gateway with The Things Network (TTN)
- Login to The Things Stack (The Things Network – TTN)
- Click on Go to gateways.
- Click on the button + Add gateway.
- Enter Gateway ID. This is a name for our new gateway it is quokka-1
- We will need to generate a random Gateway EUI
- The Gateway EUI is a 64 bit extended unique identifier. Each 4 bits will be represented by one hexadecimal number (0,1,2,3,4,5,6,7,8,9,a,b,c,d,e,f) – so we will need 16 hexadecimal numbers in total.
- Open the Terminal on your computer.
- Enter the command python3 to start python
- Load the secrets library with the command import secrets
- To generate 16 hexadecimal numbers we only need to generate the equivalent of 8 characters.
- So the python command is secrets.token_hex(8)
- Copy the 16 hexadecimal numbers.
- Back in The Things Network gateway setup, enter the Gateway EUI and the Gateway name
- Check the Gateway Server address is au1.cloud.thethings.network
- Make the Gateway status public
- Uncheck the Gateway location.
- Under LoRaWAN options select the frequency plan for Australia TTN.
- Keep other settings as the default or as shown below.
- Click on Create gateway to complete the setup of the gateway on the Things Network.
- The quokka-1 LoRa Gateway page will appear as shown.
- Note that the gateway has not connected to The Things Network.
- Click on Global configuration button which shows – Download global_conf.json located at the bottom left of the page.
Using Atom to install the Pygate software on the Pycom WiFi microcontroller
- Go to the following URL https://docs.pycom.io/tutorials/expansionboards/pygate/
- Copy the python code. Note that only a representative sample is shown here. The actual code is 54 lines long.
- Python code reproduced below.
from network import WLAN
import time
import machine
from machine import RTC
import pycom
print('\nStarting LoRaWAN concentrator')
# Disable Hearbeat
pycom.heartbeat(False)
# Define callback function for Pygate events
def machine_cb (arg):
evt = machine.events()
if (evt & machine.PYGATE_START_EVT):
# Green
pycom.rgbled(0x103300)
elif (evt & machine.PYGATE_ERROR_EVT):
# Red
pycom.rgbled(0x331000)
elif (evt & machine.PYGATE_STOP_EVT):
# RGB off
pycom.rgbled(0x000000)
# register callback function
machine.callback(trigger = (machine.PYGATE_START_EVT | machine.PYGATE_STOP_EVT | machine.PYGATE_ERROR_EVT), handler=machine_cb)
print('Connecting to WiFi...', end='')
# Connect to a Wifi Network
wlan = WLAN(mode=WLAN.STA)
wlan.connect(ssid='WIFI-NETWORK', auth=(WLAN.WPA2, "PASSWORD"))
while not wlan.isconnected():
print('.', end='')
time.sleep(1)
print(" OK")
# Sync time via NTP server for GW timestamps on Events
print('Syncing RTC via ntp...', end='')
rtc = RTC()
rtc.ntp_sync(server="pool.ntp.org")
while not rtc.synced():
print('.', end='')
time.sleep(.5)
print(" OK\n")
# Read the GW config file from Filesystem
with open('/flash/global_conf.json','r') as fp:
buf = fp.read()
# Start the Pygate
machine.pygate_init(buf)
# disable degub messages
# machine.pygate_debug_level(1)
- Create a new project folder named Pygate1 on your computer running Atom.
- Past the code copied from the Pygate web site into Atom.
- Save the file as main.py within the Pygate1 folder.
- Within Atom select Add folder (bottom left of screen).
- Find the Pygate1 folder and click Open.
- Both the Pygate1 folder and the main.py file should both appear on the left of screen in Atom.
- Copy the global_conf.json file in the Downloads folder.
- Paste the copied file in the Pygate1 folder.
- Both files should now appear in the Atom project folder viewer.
Editing the main.py and global_conf.json files
- In the main.py file update the SSID (router public name) and PASSWORD settings for your local WiFi home or school router. Keep these secret.
- Make sure that the name of the global_conf.json file is the same as the that referred to in the main.py file.
- In our example the two names were slightly different.
- Save all files and Upload project to device.
- Press on the WiPy reset button to restart the microcontroller to ensure that all changes take effect. A green light will light up on the WiPy microcontroller.
- The output from the Pygate/WiPy in the Atom console (REPL) should appear similar to below.
- Pygate will print a status overview every 30 seconds to the Atom console.
- To stop the Pygate press Ctrl-C on the REPL and run machine.pygate_deinit(). It will take a few seconds to stop the gateway tasks and safely power-off the concentrator. It will not be possible to do this if the Pygate is powered without being connected to a computer running Atom or a Terminal.
Seeing live data on The Things Network
- From the Things Network web site - Live data will start appearing indicating that the Pygate LoRa Gateway is working.
- Last activity status will change with every message received or status report.
- You can click on See all activity to see more details about the Live data.
Setting up Pygate for School
To connect your Pygate to a LoRa server, please follow these steps:
- Attach a WiPy 3, GPy or LoPy 4 to the Pygate. The RGB LED of the development board should be aligned with the USB port of the Pygate.
- For MPPS the GPy module was used with the Pygate board.
- Attach the LoRa Antenna to the Pygate. There is no need to attach a LoRa antenna to the Lopy4 module directly. The antenna should be connected to the Pygate.
- Flash the Pycom Device with with a firmware build where Pygate functionality is enabled. In the firmware update tool, please choose pygate as the firmware type.
- Follow the steps to register the gateway with TTN in our getting started guide and download the global_config.json.
- Create a main.py that creates an uplink (wifi, ethernet or LTE) and runs the LoRa packet forwarder (see example below).
- Upload global_config.json and main.py and reset the board. This file is automatically executed every time the module resets.
- You will see how it creates the uplink connection and then start the LoRa GW. It will print out some debug information while it is running. After some initialization it will print “LoRa GW started” and the LED will turn green.
- Now it is operational. The communication from other LoRa nodes such as a LoPy4 will now reach the gateway and will receive up and downlink messages via the PyGate.
- To stop the Pygate at any time press Ctrl-C on the REPL and run machine.pygate_deinit(). It will take a few seconds to stop the gateway tasks and safely power-off the concentrator.
Flash Pycom Device
Skip Pybytes registration.
Registering on The Things Network
Special Instructions for GPy
- Running the LoRa gateway on a GPy can get you close to the memory limit of the device.
- To avoid running out of memory one should not run the WiFi task and the LTE task at the same time.
- This shouldn’t really restrict your use of the Pygate, since you wouldn’t be using WiFi and LTE at the same time.
- The tasks run when you explicitly initialize them with wlan = WLAN() or lte = LTE(), or when they get automatically started upon boot based on the settings pycom.wifi_on_boot(True) or pycom.lte_modem_en_on_boot(True).
- Bottom line, if you have trouble starting the LoRa packet forwarder, please double check these settings and make sure at least the network that you don’t use is not automatically started.
Code copied from website for PyGate
from network import WLAN
import time
import machine
from machine import RTC
import pycom
print('\nStarting LoRaWAN concentrator')
# Disable Hearbeat
pycom.heartbeat(False)
# Define callback function for Pygate events
def machine_cb (arg):
evt = machine.events()
if (evt & machine.PYGATE_START_EVT):
# Green
pycom.rgbled(0x103300)
elif (evt & machine.PYGATE_ERROR_EVT):
# Red
pycom.rgbled(0x331000)
elif (evt & machine.PYGATE_STOP_EVT):
# RGB off
pycom.rgbled(0x000000)
# register callback function
machine.callback(trigger = (machine.PYGATE_START_EVT | machine.PYGATE_STOP_EVT | machine.PYGATE_ERROR_EVT), handler=machine_cb)
print('Connecting to WiFi...', end='')
# Connect to a Wifi Network
wlan = WLAN(mode=WLAN.STA)
wlan.connect(ssid='<SSID>', auth=(WLAN.WPA2, "<PASSWORD>"))
while not wlan.isconnected():
print('.', end='')
time.sleep(1)
print(" OK")
# Sync time via NTP server for GW timestamps on Events
print('Syncing RTC via ntp...', end='')
rtc = RTC()
rtc.ntp_sync(server="pool.ntp.org")
while not rtc.synced():
print('.', end='')
time.sleep(.5)
print(" OK\n")
# Read the GW config file from Filesystem
with open('/flash/global_config.json','r') as fp:
buf = fp.read()
# Start the Pygate
machine.pygate_init(buf)
# disable degub messages
# machine.pygate_debug_level(1)
Python code implemented on GPy PyGate MPPS
from network import WLAN
import time
import machine
from machine import RTC
import pycom
print('\nStarting LoRaWAN concentrator')
# Disable Hearbeat
pycom.heartbeat(False)
# Define callback function for Pygate events
def machine_cb (arg):
evt = machine.events()
if (evt & machine.PYGATE_START_EVT):
# Green
pycom.rgbled(0x103300)
elif (evt & machine.PYGATE_ERROR_EVT):
# Red
pycom.rgbled(0x331000)
elif (evt & machine.PYGATE_STOP_EVT):
# RGB off
pycom.rgbled(0x000000)
# register callback function
machine.callback(trigger = (machine.PYGATE_START_EVT | machine.PYGATE_STOP_EVT | machine.PYGATE_ERROR_EVT), handler=machine_cb)
print('Connecting to WiFi...', end='')
# Connect to a Wifi Network
wlan = WLAN(mode=WLAN.STA)
wlan.connect(ssid='TPW4G_Cj7G2J', auth=(WLAN.WPA2, "<PASSWORD>"))
while not wlan.isconnected():
print('.', end='')
time.sleep(1)
print(" OK")
# Sync time via NTP server for GW timestamps on Events
print('Syncing RTC via ntp...', end='')
rtc = RTC()
rtc.ntp_sync(server="pool.ntp.org")
while not rtc.synced():
print('.', end='')
time.sleep(.5)
print(" OK\n")
# Read the GW config file from Filesystem
with open('/flash/global_conf.json','r') as fp:
buf = fp.read()
# Start the Pygate
machine.pygate_init(buf)
# disable degub messages
# machine.pygate_debug_level(1)





























