LoRaWAN en Raspberry PLC
Configuración previa
Antes de enviar mensajes a través de LoRaWAN, asegúrate de haber registrado tu dispositivo en The Things Network Stack, como se vio en el capítulo de configuración de LoRaWAN .
Además, descarga los siguientes archivos para usar funciones LoRaWAN con Python:
Fragmento de código
El código para PLCs basados en Raspberry es muy simple. Por ejemplo, mira el siguiente código en Python. Recuerda instalar librpiplc, y cambiar la ruta en la variable "command" según tu modelo.
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
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
Reemplazando las variables appkey, appeui y deveui con las encontradas en la consola del dispositivo final, podemos enviar fácilmente mensajes a través de LoRaWAN.
Calificación
0
0
No hay comentarios por ahora.
Unirse a este curso
para ser el primero en comentar.
1.
En el ejemplo de LoRaWAN en Python, ¿de dónde salen appkey y deveui?