Ir al contenido

Configuración previa

Antes de intentar 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, instala la biblioteca RN2483-Arduino-Library para propósitos LoRaWAN.


Fragmento de código

Para PLCs basados en ESP32, utiliza el siguiente ejemplo de la biblioteca RN2483 que descargaste previamente:

#include <rn2xx3.h>

//create an instance of the rn2xx3 library
rn2xx3 myLora(SerialExp2);

// the setup routine runs once when you press reset:
void setup() {

  // Open serial communications and wait for port to open:
  Serial.begin(115200); //serial port to computer
  SerialExp2.begin(57600); //serial port to radio
  Serial.println("Startup");

  initialize_radio();

  //transmit a startup message
  myLora.tx("TTN Mapper on TTN Enschede node");

  delay(2000);
}

void initialize_radio() {
  //reset rn2483
  pinMode(EXP2_RST, OUTPUT);
  digitalWrite(EXP2_RST, LOW);
  delay(1000);
  digitalWrite(EXP2_RST, HIGH);
  delay(1000); //wait for the RN2xx3's startup message

  SerialExp2.flush();

  Serial.println("RN2xx3 firmware version:");
  Serial.println(myLora.sysver());

  //configure your keys and join the network
  Serial.println("Trying to join TTN");
  bool join_result = false;

  // DevEUI, AppEUI, and AppKey for LoRaWAN
  const char *devEui = "0011223344556677";
  const char *appEui = "0000000000000000";
  const char *appKey = "00112233445566778899AABBCCDDEEFF";

  join_result = myLora.initOTAA(appEui, appKey, devEui);

  // Retry joining the network until successful
  while(!join_result) {
    Serial.println("Unable to join. Are your keys correct, and do you have TTN coverage?");
    delay(60000); //delay a minute before retry
    join_result = myLora.init();
  }
  Serial.println("Successfully joined TTN");
}

// the loop routine runs over and over again forever:
void loop() {

    Serial.println("TXing");
    myLora.tx("!"); //one byte, blocking function

    delay(60000); // wait 60 s between uplinks; check the duty cycle limits of your region
}

Como el dispositivo final está configurado para trabajar con OTAA, reemplaza las 3 variables devEui, appEui y appKey con las tuyas y sube el código. En este caso, copia y pega las claves tal como se muestran en la consola de TTN, en formato Big-Endian o MSB.

Calificación
0 0

No hay comentarios por ahora.

para ser el primero en comentar.