Setup LoPy modules
Place the Lopy modules on the Expansion Board and Pytrack boards. Setup these modules and the Expansion boards by installing required drivers and update the latest firmware. Instructions are available in the pycom website –¬†
Expansion Board FTDI driver and Firmware Tool for LoPy :¬†
Pytrack board USB Serial driver :¬†
Pytrack board Firmware¬†:
LoPy with the Expansion Board is to be used as the Gateway in this example. Henceforth referred to as ‘LoPy Gateway’. LoPy combined with¬†Pytrack is the mobile unit that communicates with the Gateway and sends messages via the LoRaWAN network. Henceforth referred to as ‘LoPy Tracker’
Connect to the Gateway LoPy Unit via USB Serial. Find out the COM port to be used for the connection from the Device Manager.¬†
Open a Putty Terminal and connect using correct Serial COM port with Baud rate of 115200.
We need to know the MAC address of the LoPy module which is to be used during the registration in TTN. Although any 8 byte valid Hexadecimal sequence can be used as Id. PyCom LoPy modules are running MicroPython firmware hence enter the following python commands for this purpose –
WIFI_MAC = binascii.hexlify(machine.unique_id()).upper()
GATEWAY_ID = WIFI_MAC[:6] + “FFFE” + WIFI_MAC[6:12]
This would give the generated MAC Id of the LoPy. Make a note of this output.
Add a LoRaWAN Nano gateway to TTN
The instructions for this step are redirected to the Pycom standard documentation for registering LoRaWAN Nano Gateway –¬†
Create the TTN account.
Go to the Gateway section –¬†
Click on the ‘register gateway’ link –¬†
Fill in the details –¬†
Paste the GATEWAY_ID generated from the LoPy module in previous step in the 8 byte EUI field. Ensure to select the checkbox for ‘I’m using the legacy packet forwarder’. Choose frequency plan as per the region applicable. The¬†Router is chosen based upon the Frequency plan but it can be altered if needed. Make note of the router name.
Select location on the map for the location coordinates of the Gateway and choose the antenna placement location as applicable. Finish the registration by clicking ‘Register Gateway’ –¬†
Now it’s time to configure the application and devices.
Create an Application in TTN
From the TTN website console click on the Application Icon –¬†
From the Application Listing page click on the ‘add application’ link –¬†
Fill in the details as required and choose the appropriate provider –¬†
Please note that the Application Id field has specific restriction on the value to be used. It guides the user accordingly on the restriction while filling this field. Application EUI would be generated.¬†
The created application details can be seen from the application overview screen –
Next step is to add devices to this application.
Register Devices in the Application
Scroll down on the application overview page to the devices section and click on the ‘register device’ link –¬†
Fill in the device id. Note that the value for this field has certain restriction and the site guides on what to be used in this place –¬†
At this screen the default option is to enter a Device EUI. This can be changed to generate mode by clicking on the crossover arrow link at the beginning of the field. The icon changes to a pencil. Click on the ‘Register’ button to finish registration of the device.
The registered device can be seen on the list of registered devices (Note that the following is depiction from my own TTN console which has 2 devices) –¬†
The first column is the device id and the second column is the generated¬†Device EUI. The green dot next to them is the status indicating whether the device was connected ever or not. Depending on this the first time entries would have a gray dot. Click on the row containing the device to see details –¬†
Please note that the above screenshot is from an actual registration hence the details are overlayed with green for privacy purposes.¬†During the actual implementation please make a copy¬†of the following information to be used during the LoPy¬†Device configuration –¬†
- Device EUI
- Application EUI
- App Key
Note that the activation method set for this device is default OTAA (Over The Air Activation) for LoRa.
Configure LoPy Gateway in LoRaWAN nano Gateway mode
This step assumes that the LoPy Units are firmware upgraded and USB drivers to connect with them are properly installed on the computer used to configure them.¬†
Download the following files –¬†
Edit the config.py in a text editor such as Notepad for Windows and update the following lines with appropriate value –¬†
WIFI_SSID = ‘your-wifi-ssid‘
WIFI_PASS = ‘your-wiki-key‘
Connect the LoPy Gateway module¬†to the PC using a USB cable. Note the COM port from Device Manager. Launch Putty and open a serial terminal as depicted in Step 1. Execute the following commands (after changing the WiFi credentials) in order to connect the LoPy to the local WiFi Network –¬†
from network import WLAN
wlan = WLAN()
wlan.connect(‘your-wifi-ssid’, auth=(WLAN.WPA2, ‘your-wiki-key’), timeout=10000)
Wait 5¬†seconds and run the following command to¬†check the assigned Ip address.
This would print out the IP address along with the subnet mask and router IP for the local WiFi.¬†Take note of the assigned IP address.
Launch FileZilla and go to File -> Site Manager. Click on the ‘New Site’ Button and add the configuration –¬†
Update Host¬†IP (XX.XX.XX.XX) as obtained running the wlan.ifconfig() command previously. User Id to be used is ‘micro‘. Password is not needed to be entered now, only when the connection is initiated FileZilla would prompt for password. Note that LoPy devices only support passive FTP hence in the Transfer Settings section configure the following –¬†
LoPy only supports 1 Simultaneous connection hence the checkbox needs to be selected. Once finished, click on the ‘Connect’ button to initiate connection. FileZilla now asks for Password. Enter the default password for FTP for LoPy, which is ‘python‘ and click ‘OK’ –¬†
FileZilla shows connection progress –¬†
The file system of the LoPy module can be seen –¬†
Sync the following files to device along with the updated config.py –
Reboot¬†the device via the reset button on LoPy or simply plug-out the USB cable and plug back in. Allow a minute or two before the WiFi connection and LoRa initiation completes. Check the TTN console to see¬†that the Gateway has connected –¬†
Configure LoPy Tracking node
Download the following files –¬†
Edit the main.py in a text editor and update the following lines –¬†
dev_eui = binascii.unhexlify(‘your-dev-id’.replace(‘ ‘,”))
app_eui = binascii.unhexlify(‘your-app-id’.replace(‘ ‘,”))
app_key = binascii.unhexlify(‘your-app-key’.replace(‘ ‘,”))
Update the¬†‘your-dev-id’, ‘your-app-id’, ‘your-app-key’ values from the information obtained in Step 4.¬†
In order for this node to send location and accelerometer¬†data from Pytrack sensors the following additional bits of snippet needs to be added in the main.py –¬†
Further, in order to read the sensors and send the data following snippet would replace the existing line of ‘ s.send(b’PKT #’ + bytes([i]))‘ with –
Finally update the last line for the time delay in the for loop to a reasonable value e.g. ‘time.sleep(60)‘. Save the main.py.
Connect to the LoPy Tracking node via USB connection¬†in the similar manner as described in previous step for LoPy Gateway and sync the boot.py and the updated main.py.
Reset the LoPy module on the Tracker and check TTN console for connection. It takes a bit a of a time before LoRa¬†device OTAA succeeds and then actual message payload starts coming through.
Check the status of the device in TTN console and it should look something similar –¬†
Configure Node-RED on Bluemix to Receive data from TTN
Launch the Bluemix Node-RED¬†application. Click on the Menu and select ‘Manage palette’ –¬†
Select the Install tab and type the node to search as ‘node-red-contrib-ttn‘ –¬†
Click on the small install button to¬†add the ttn node to the palette.¬†After install, it would look like this –¬†
These nodes can now be used in the Node-RED flow. Create a new flow and drag and drop the ‘ttn message’ node onto it. Double click to edit the same –
Click on the pencil button next to the App field to add¬†the Application configuration as defined in TTN console.¬†
Update the details as seen from the TTN console.¬†Note that App Id is not the same as App EUI.¬†For Region or Broker refers to the last piece of text following ‘ttn-handler-‘ in the ‘Handler’ name in Application Overview (Step 3) of TTN console. In this case it was ‘eu‘. Click ‘Add’ to finish the configuration.
This takes the screen back to the ttn-message node configuration which now has the¬†App field filled in. Provide a suitable name and update the ‘Device ID’ from the Device Overview in the TTN console (Step 4). Leave the content of the ‘Field’ blank. Click on ‘Done’ to finish the configuration.
msg.payload = msg.payload.toString();
Click ‘Done’ to finish configuration.
Connect the output of the TTN Message node to the input of the function node. Drag and drop ¬†JSON node ¬†and double click to edit it. Provide a suitable name to it and click on ‘Done’
Connect the output of the function node to the input of the JSON node. Drag and drop a output Debug node and make sure it is set to output¬†msg.payload. Connect the output of the JSON node to the input of Debug node. Click on Deploy to save changes and deploy the node RED application configuration.
¬†After OTAA is success (this takes a while) data would start flowing in the debug view of Node-RED every 60 seconds –¬†
This concludes the description for integration between TTN and Bluemix. The heart of this integration is the TTN Node-RED node which bridges the two environments to send the data over. This integration can be further extended to include components from the Watson IoT to process the incoming data, integrate cognitive analytics and IoT device actuation.