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 platform for your connected Things. With the latest Espressif chipset the LoPy offers a perfect combination of power, friendliness and flexibility.

For more information just click here.

Requirements

  • LoPy Board.
  • Expansion Board.
  • Cable micro-USB
  • MultiTech MultiConnect Conduit AEP previously configured with the Ubidots LoRaWAN network server. If you don’t already have it, just click here.

Get started with your LoPy

Hardware Setup

Before beginning, it is important check the points below to make sure your board is programmed correctly:

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

1. Using Pymakr Plugin - (Atom) 

To make it as easy as possible, the Pycom Team developed a series of tools known as the Pymakr Plugins, which allow you to connect to and program your Pycom devices.

To get started with the Pymakr, please reference the Pycom official documentation and follow all the steps provided.

Once finished the "Initial Configuration", and when are able to communicate with your Pycom LoPy using the Pymakr Plugin, you can continue with this guide.

Connect to Ubidots

1. Log in to your Ubidots account.

2. Select Devices and click on Add Device.

3. Once you created the device select Enable LoRa.

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

  • 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, just write the following commands.

Getting the DevEUI

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

The LoPy let’s you set the DevEUI if you need or desire.

  • Application EUI (AppEUI): The AppEUI identifies the application during the JOIN request. This will be generated automatically.
  • Application key (AppKey): The AppKey encrypts the data during the JOIN request. This will be generated automatically.

4. You will see a windows with the parameters that you need to assign on the code.

Setting up the 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 file "ubidots" we are going to manage the libraries(if there are need it, in this sample case not) and main/boot codes. Please reference and follow the structure below to build your project properly: 

> 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. 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 where is indicated in the code:  

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.

Results

As you may see, we just send time and pressure data to Ubidots using LoRaWan in just a few minutes.

Did this answer your question?