Overview

Skill Level: Intermediate

Transfer data from a LoRa enabled IoT device via the LoRaWAN network of TTN (The Things Network) to Bluemix platform using Node Red.

Ingredients

2 x LoPy modules from PyCom

lopySide-800x800

1 x Pycom Expansion Board 2.0

exp2SideTransPXH-510x459

1 x Pytrack 

pytrackSideJune-800x800

2 x PyCom LoRa/SigFox Antenna

atennaNewTransGX

Putty or similar for Serial Communication

Filezilla for FTP transfer

TTN (The Things Network) Account

Bluemix Account with installed Node-RED Starter application. 

 

Optional : 

1 x LiPoly Battery (500 mAh) for outdoor testing

 

Disclaimer : I used a Windows environment to perform all this setup hence the related commands and methods used all relate to Windows only. 

While setting up PyCom LoPy modules, take note of the fact that they are communicating with FTP and Telnet protocols. Hence caution must be taken to ensure that these are setup on a private WiFi network and devices are Physically secured.

This recipe makes use of resources from the following links - 

 

https://docs.pycom.io

https://docs.pycom.io/chapter/tutorials/lopy/lorawan-nano-gateway.html

https://github.com/franckalbinet/iot-smr3128-ictp/blob/master/labs/lorawan-nano-gateway.md

https://www.thethingsnetwork.org/docs/applications/nodered/quick-start.html

 

Step-by-step

  1. 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 : 

    https://docs.pycom.io/chapter/gettingstarted/installation/firmwaretool.html

    Pytrack board USB Serial driver : 

    https://docs.pycom.io/chapter/pytrackpysense/installation/drivers.html

    Pytrack board Firmware :

    https://docs.pycom.io/chapter/pytrackpysense/installation/firmware.html

     

    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.

    PuttySerial

    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 –

    import machine
    import binascii

    WIFI_MAC = binascii.hexlify(machine.unique_id()).upper()
    GATEWAY_ID = WIFI_MAC[:6] + “FFFE” + WIFI_MAC[6:12]

    print(GATEWAY_ID)

    GatewayIDGeneration

    This would give the generated MAC Id of the LoPy. Make a note of this output.

     

  2. Add a LoRaWAN Nano gateway to TTN

    The instructions for this step are redirected to the Pycom standard documentation for registering LoRaWAN Nano Gateway –¬†

    https://docs.pycom.io/chapter/tutorials/lopy/lorawan-nano-gateway.html

     

    Open https://account.thethingsnetwork.org/users/login

    Create the TTN account.

      TTNRegistration

     

     

    Go to the Gateway section –¬†

    GatewayRegistration

    Click on the ‘register gateway’ link –¬†

    GatewayRegistration2

    Fill in the details –¬†

    GatewayRegistrationDetails

    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’ –¬†

    GatewayLocationAndFinalStep

     

    Now it’s time to configure the application and devices.

     

  3. Create an Application in TTN

    From the TTN website console click on the Application Icon –¬†

    CreateApplication

    From the Application Listing page click on the ‘add application’ link –¬†

    CreateApplication2

    Fill in the details as required and choose the appropriate provider –¬†

    ApplicationCreation

    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 –

    CreatedApplication

    Next step is to add devices to this application.

  4. Register Devices in the Application

    Scroll down on the application overview page to the devices section and click on the ‘register device’ link –¬†

    RegisterDevice

    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 –¬†

    RegisterDevice2

    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.

    RegisterDevice3

    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) –¬†

    RegisterdDevices

    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 –¬†

    DeviceOverview

    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.

  5. 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 –¬†

    https://github.com/franckalbinet/iot-smr3128-ictp/blob/master/labs/src/lorawan-nano-gateway/gateway/boot.py

    https://github.com/franckalbinet/iot-smr3128-ictp/blob/master/labs/src/lorawan-nano-gateway/gateway/config.py

    https://github.com/franckalbinet/iot-smr3128-ictp/blob/master/labs/src/lorawan-nano-gateway/gateway/main.py

    https://github.com/franckalbinet/iot-smr3128-ictp/blob/master/labs/src/lorawan-nano-gateway/gateway/nanogateway.py

     

    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.init(mode=WLAN.STA)
    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.

    wlan.ifconfig()

    WiFiConnect

    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 –¬†

    FileZilla1

    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 –¬†

    FileZilla2

    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’ –¬†

    FileZilla3

    FileZilla shows connection progress –¬†

    FileZillaConnected

    The file system of the LoPy module can be seen –¬†

    FlashFileSystem

    Sync the following files to device along with the updated config.py –

    boot.py

    main.py

    nanogateway.py

    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 –¬†

     GatewayConnected

  6. Configure LoPy Tracking node

    Download the following files –¬†

    https://github.com/franckalbinet/iot-smr3128-ictp/blob/master/labs/src/lorawan-nano-gateway/node/boot.py

    https://github.com/franckalbinet/iot-smr3128-ictp/blob/master/labs/src/lorawan-nano-gateway/node/main.py

    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 –¬†

    CodeSnippetTrackerInitialize

    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 –

    CodeSnippetTrackerDataSend

    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 –¬†

    DeviceConnected

  7. Configure Node-RED on Bluemix to Receive data from TTN

    Launch the Bluemix Node-RED¬†application. Click on the Menu and select ‘Manage palette’ –¬†

    NoREDManagePalette

    Select the Install tab and type the node to search as ‘node-red-contrib-ttn‘ –¬†

    NoREDTTNNodeInstall

    Click on the small install button to¬†add the ttn node to the palette.¬†After install, it would look like this –¬†

    TTNNodeInstalled

    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 –

    NodeREDFlowTTNNodeConfig

    Click on the pencil button next to the App field to add the Application configuration as defined in TTN console. 

    NodeREDFlowTTNNodeConfig2

    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.

    NodeREDFlowTTNNodeConfig3

    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.

    NodeREDFlowTTNNodeConfig4

    Drag and drop a function node from palette and double click on it to configure it. Add the following JavaScript snippet and update a suitable name –

    msg.payload = msg.payload.toString();
    return msg;

    Click ‘Done’ to finish configuration.

    NodeREDFlowTTNNodeConfig5

    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’

    NodeREDFlowTTNNodeConfig6

    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.

    NodeREDFlowTTNNodeConfig7

     

    ¬†After OTAA is success (this takes a while) data would start flowing in the debug view of Node-RED every 60 seconds –¬†

    NodeRedMessageReceived4

    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.

Join The Discussion