All Collections
Connect your Devices
Connect a Pycom LoPy to Ubidots using LoRaWAN over HTTP
Connect a Pycom LoPy to Ubidots using LoRaWAN over HTTP

Learn to connect to LoRaWAN using Pycom + Multitech + Ubidots over HTTP.

S
Written by Sergio M
Updated over a week ago

With LoRa, Wifi, and BLE, the LoPy is the only triple bearer MicroPython enabled micro controller on the market today – the perfect enterprise grade IoT hardware for your connected Things. With the latest Espressif chipset the LoPy offers a perfect combination of power, friendliness, and flexibility.

No matter the board type you are using WiPy, SiPy, LoPy, or FiPy, the entire Pycom family communicates with Ubidots the same. 

For more information about the Pycom family of hardware just click here.

By following this guide you will be able to transmit data from the LoPy devices as a LoRa end nodes to a LoRaWAN Gateway previously configured with the TTN LoRaWAN Network Server. Then, the data received in TTN will be forwarded to Ubidots by using an "HTTP Integration" feature provided by TTN.

Requirements

Step-by-Step

  1. Setting up LoRaWAN Gateway to TTN

  2. Setting up a TTN Application  

  3. Setting up LoRa Nodes - LoPy 

  4. Integrate your TTN data with Ubidots

  5. Summary

1. Setting up LoRaWAN Gateway to TTN

To be able to transmit data from the LoPy to Ubidots over LoRaWAN you will need to setup a LoRaWAN Gateway in order to get your data to the cloud. 

To learn how to setup a Multitech Conduit with the LoRaWAN Network Server offered by The Things Network refer to the guide below:

If you are using a different Gateway for your application, refer to the TTN documentation to find additional details to the TTN supported gateway you are working with.

With the Gateway already configured and connected to your TTN account, you will be able to receive data from the LoRa nodes (LoPy Boards).

2. Setting up a TTN Application 

Now, you need to create an application in TTN in order to configure and generate the credentials (DevEUI, AppEUI, AppKey) which are going to be assigned in the sample code provided below to establish the communication between the hardware and gateways and then on to TTN and Ubidots where the data can be made useful in an end-user ready IoT Application.

1. Go to the "Application" section of your TTN account and press "Add Application"

After assigning all new App ID and Description you will have a screen with something similar to the following:

To finish, press the "Add application" button.

2. Once the application is created, you will be redirect to an Application Overview window. There select "Register Device" from the Device section:

In the following window, assign all the parameters required plus the region where your devices are deployed. At this point, in the Device EUI field, you should assign the one provided by the hardware manufacturer. 

  • Device EUI (DevEUI): The DevEUI identifies the device on the LoRaWAN network during the JOIN request. This is assigned to the device by the chip manufacturer. You will be able to know the DevEUI by entering the executing the command below through the Pymakr console.

>>> import network
>>> from network import LoRa
>>> import binascii
>>> binascii.hexlify(network.LoRa().mac())
b'70b3d5100046a40e'


After assigning all the parameters required you should have as result something like below:

  To finish, press thge "Register" button. 

3. Once the device is properly registered you will be able to get the credentials required to transmit data to the LoRaWAN gateway. As you can see in the image below, the Device ID, Application EUI and App Key were generated by the device register of TTN: 

3. Setting up LoRa Nodes - LoPy 

Before beginning, it is important check the resources below to ensure your board will be programmed correctly:

If this is your first time working with a Pycom board, we highly recommend you reference these getting started materials and then return to this guide for further instruction once you have become familiar with your hardware.

For the Over-The-Air Activation (OTAA) you will have to assign the following parameters to the code AppEUI, AppKey, DevEUI. Where:

  • Device EUI (DevEUI): The DevEUI identifies the device on the LoRaWAN network during the JOIN request. This is assigned to the device by the chip manufacturer. You will be able to know the DevEUI through Pymakr console, simply by executing the following commands.

>>> import network
>>> from network import LoRa
>>> import binascii
>>> binascii.hexlify(network.LoRa().mac())
b'70b3d5100046a40e'

As you can see the DeviceEUI of my device is 70b3d5100046a40e . 

  • Application EUI (AppEUI): The AppEUI identifies the application during the JOIN request. This will be generated by TTN.

  • Application key (AppKey): The AppKey encrypts the data during the JOIN request. This will be generated by TTN.

Setting up Pymakr Project 

1. Open the Atom editor, and activate the Pymakr plugin.

2. Connect the board with the Atom editor. Once the board is connected you will see the message "connected" in the status:

3. To start, create a new directory called "ubidots" to manage the codes and libraries. Once the directory is created, in the Atom editor select Open a Project, and search for the directory "ubidots," which was previously created and open it. 

4. In the directory "ubidots" we are going to manage the libraries and main/boot codes. Please reference and follow the structure below to build your project properly. Please note that the lib will contain the external libraries for your project, if they are needed in your project: 

> ubidots
  - boot.py
  - main.py
  > lib

DEPLOYMENT NOTE: In the case that you need to use an external micro-python library add a folder for the management of the libraries in the Ubidots directory. Right click on the folder "ubidots" and select "New folder", assign "lib" as name.

Now, we're going to add the main and boot files:

  • Right click the folder "ubidots" and select "New File", assign "boot.py" as the name

  • Repeat the above steps to create the main called "main.py":

Once both files are created, the structure of your project will look like this: 

Coding Time 

1. Next, copy and paste the code below into the "boot.py" file: 

from machine import UART
import machine
import os

uart = UART(0, baudrate=115200)
os.dupterm(uart)

machine.main('main.py')

Save your code once you've uploaded to the "boot.py" file.

2. Next, copy and paste the code below into the "main.py" file.  Once  the code is pasted, assign your  AppEUI, AppKey, DevEUI provided and assigned in the device previously registered in TTN where indicated in the code below:  

from network import LoRa
import socket
import time
import binascii
import utime

def select_subband(lora, subband):
    if (type(subband) is int):
        if ((subband<1) or (subband>8)):
            raise ValueError("subband out of range (1-8)")
    else:
        raise TypeError("subband must be 1-8")

    for channel in range(0, 72):
        lora.remove_channel(channel)

    index = (subband-1)*8
    for channel in range(0, 7):
        lora.add_channel(channel, frequency=902300000+index*200000, dr_min=0, dr_max=3)
        index+=1

    index = (subband-1)*8
    for channel in range(8, 15):
        lora.add_channel(channel, frequency=902300000+index*200000, dr_min=0, dr_max=3)
        index+=1

    index = (subband-1)*8
    for channel in range(16, 23):
        lora.add_channel(channel, frequency=902300000+index*200000, dr_min=0, dr_max=3)
        index+=1

    index = (subband-1)*8
    for channel in range(24, 31):
        lora.add_channel(channel, frequency=902300000+index*200000, dr_min=0, dr_max=3)
        index+=1

    index = (subband-1)*8
    for channel in range(32, 39):
        lora.add_channel(channel, frequency=902300000+index*200000, dr_min=0, dr_max=3)
        index+=1

    index = (subband-1)*8
    for channel in range(40, 47):
        lora.add_channel(channel, frequency=902300000+index*200000, dr_min=0, dr_max=3)
        index+=1

    index = (subband-1)*8
    for channel in range(48, 55):
        lora.add_channel(channel, frequency=902300000+index*200000, dr_min=0, dr_max=3)
        index+=1

    index = (subband-1)*8
    for channel in range(56, 63):
        lora.add_channel(channel, frequency=902300000+index*200000, dr_min=0, dr_max=3)
        index+=1

    index = (subband-1)*8
    for channel in range(64, 71):
        lora.add_channel(channel, frequency=902300000+index*200000, dr_min=0, dr_max=3)
        index+=1

lora = LoRa(mode=LoRa.LORAWAN, adr=False, public=True, tx_retries=0)

sb = 1 #Change to desired conduit frequency sub-band

select_subband(lora,sb)

# assign your AppEUI, AppKey, DevEUI
app_eui = binascii.unhexlify('ff ff ba 78 20 0c c2 ee'.replace(' ',''))
app_key = binascii.unhexlify('12 67 ad a4 da e3 ac 12 67 6b ad da e3 ac ff ff'.replace(' ',''))
dev_eui = binascii.unhexlify ('b1 17 11 b5 3d ec b3 44'.replace(' ',''))  

# create an OTAA authentication (DevEUI, AppEUI, AppKey)
auth =(dev_eui, app_eui, app_key)

# create an OTAA authentication (AppEUI, AppKey)
#auth=(app_eui, app_key)

# join a network using OTAA (Over the Air Activation)
lora.join(activation=LoRa.OTAA, auth=auth, timeout=0)
print("Joined...")

while not lora.has_joined():
    time.sleep(5)
    print('Not yet joined on frequency sub-band '+str(sb)+'...')

s = socket.socket(socket.AF_LORA, socket.SOCK_RAW)
s.setblocking(False)
s.setsockopt(socket.SOL_LORA, socket.SO_DR, 3)

pressure = 1    

while lora.has_joined():
    payload = '{"pressure":'+str(pressure)+',"time":'+str(utime.time())+'}'
    print(payload)
    s.send(payload)
    time.sleep(6)

    pressure +=1

    if pressure > 100:
        pressure = 1

Save your code once you've uploaded to the "main.py" file.

3. Now synchronize the project. Press the "Sync" icon from the console, or select Packages –> Pymakr –> Synchronize project.

At this point, your LoPy devices will be able to transmit data to the LoRaWAN Gateway! 

 4. Integrate your TTN data with Ubidots

Now that your devices are transmitting data to TTN, you need to forward the data to Ubidots by using the HTTP Integration feature offered by TTN.

For a detailed explanation of how to setup the HTTP Integration with Ubidots, please refer the guide below:

5. Summary

Now you are able to transmit data from your LoPy devices to Ubidots by using the TTN LoRaWAN Network server. With the data in Ubidots you can transform your raw data into insights and deliver problem solving solutions to end-customers the way they want to see them. 

Other readers have also found useful...

Did this answer your question?