Skip to Content

LoRaWAN on Raspberry PLCs

Prior configuration

Before attempting to send messages through LoRaWAN, make sure to have registered your device to the The Things Network Stack, as seen in the LoRaWAN setup chapter.

In addition, download the following files to use LoRaWAN functions with Python:

lora.py

lorawan.py

Code snippet

The code for Raspberry based PLCs is very simple, for instance take a look at the following Python sketch. Remember to install the librpiplc, and change the path of the command variable in the code to adjust your model.

import subprocess
import time
from lorawan import LoRaWAN

debug = True                   # print debug information of current command in REPL
serial_port = "/dev/ttySC0"    # indicate UART port LoRa module is connected to
timeout = 5000                 # timeout of serial and lora reception

def set_exp1_rst(value):
    command = f"~/test/RPIPLC_V4/RPIPLC/set-digital-output EXP1_RST {value}"
    try:
        subprocess.run(command, check=True, shell=True)
    except subprocess.CalledProcessError as e:
        print(f"Failed to set EXP1_RST to {value}: {e}")

set_exp1_rst(0)
time.sleep(2)
set_exp1_rst(1)

time.sleep(5)

appkey = "00112233445566778899AABBCCDDEEFF"
appeui = "0000000000000000"
deveui = "0011223344556677"

loraWAN = LoRaWAN(serial_port, timeout, timeout, debug)

print("------------- LoRaWAN object created")

loraWAN.config_otaa(appkey, appeui, deveui)

print("------------- LoRaWAN OTAA done")

loraWAN.join()

print("------------- Device joined the network")

while True:
    try:
        i = input("Type to send message! ")
        loraWAN.send_uplink(''.join(f'{ord(c):02X}' for c in i))
    except KeyboardInterrupt:
        print("\nProgram interrupted! Exiting gracefully.")
        break

By replacing the variables appkey, appeui and deveui with the one found in the end device console, we can easily send messages over LoRaWAN.

Rating
0 0

There are no comments for now.

to be the first to leave a comment.