|
| 1 | +/* |
| 2 | + LoRaOTAA.ino |
| 3 | +
|
| 4 | + Establish a connection on a LoRaWAN network in OTAA mode. |
| 5 | + Send a join request and wait the join accept. |
| 6 | + Exchange data to/from the network. |
| 7 | +
|
| 8 | + Extract of the LoRaWAN standard: |
| 9 | +
|
| 10 | + For Over The Air Activation, end-devices must follow a join procedure prior to participating in |
| 11 | + data exchanges with the network server. An end-device has to go through a new join |
| 12 | + procedure every time it has lost the session context information. |
| 13 | +
|
| 14 | + The join procedure requires the end-device to be personalized with the following information |
| 15 | + before its starts the join procedure: a globally unique end-device identifier (DevEUI), the |
| 16 | + application identifier (AppEUI), and an AES-128 key (AppKey). |
| 17 | +
|
| 18 | + USI will burn the unique IEEE EUI64 at factory. |
| 19 | +
|
| 20 | +*/ |
| 21 | + |
| 22 | +#include "LoRaWANNode.h" |
| 23 | + |
| 24 | +#define FRAME_DELAY 300000 // in ms. Every 5 minutes by default. |
| 25 | + |
| 26 | +// Serial port use to communicate with the USI shield. Specific to the board Discovery L475 IoT. |
| 27 | +HardwareSerial SerialLora(PA_1, PA_0); |
| 28 | + |
| 29 | +// AppKey and AppEUI. |
| 30 | +const char appKey[] = "0123456789abcdef0123456789abcdef"; |
| 31 | +const char appEUI[] = "abcdef0123456789abcdef0123456789"; |
| 32 | + |
| 33 | +// Data send |
| 34 | +char frameTx[] = "Hello world!"; |
| 35 | + |
| 36 | +void setup() |
| 37 | +{ |
| 38 | + Serial.begin(9600); |
| 39 | + Serial.println("-- LoRaWAN OTAA sketch --"); |
| 40 | + |
| 41 | + // Enable the USI module and set the radio band. |
| 42 | + while(!loraNode.begin(&SerialLora, LORA_BAND_EU_868)) { |
| 43 | + Serial.println("Lora module not ready"); |
| 44 | + delay(1000); |
| 45 | + } |
| 46 | + |
| 47 | + // Send a join request and wait the join accept |
| 48 | + while(!loraNode.joinOTAA(appKey, appEUI)) { |
| 49 | + Serial.println("joinOTAA failed!!"); |
| 50 | + delay(1000); |
| 51 | + } |
| 52 | + |
| 53 | + Serial.println("Lora module ready, join accepted.\n"); |
| 54 | + |
| 55 | + String str = "Device EUI: "; |
| 56 | + loraNode.getDevEUI(&str); |
| 57 | + Serial.println(str); |
| 58 | + str = "Application key: "; |
| 59 | + loraNode.getAppKey(&str); |
| 60 | + Serial.println(str); |
| 61 | + str = "Application EUI: "; |
| 62 | + loraNode.getAppEUI(&str); |
| 63 | + Serial.println(str); |
| 64 | +} |
| 65 | + |
| 66 | +void loop() |
| 67 | +{ |
| 68 | + receive(); |
| 69 | + transmit(); |
| 70 | + delay(FRAME_DELAY); |
| 71 | +} |
| 72 | + |
| 73 | +void receive(void) { |
| 74 | + uint8_t frameRx[64]; |
| 75 | + uint8_t len; |
| 76 | + uint8_t port; |
| 77 | + |
| 78 | + // Check if data received from a gateway |
| 79 | + if(loraNode.receiveFrame(frameRx, &len, &port)) { |
| 80 | + uint8_t n = 0; |
| 81 | + Serial.print("frame received: 0x"); |
| 82 | + while(len > 0) { |
| 83 | + Serial.print(frameRx[n], HEX); |
| 84 | + Serial.print(','); |
| 85 | + len--; |
| 86 | + n++; |
| 87 | + } |
| 88 | + Serial.print(" on port "); Serial.println(port); |
| 89 | + } else { |
| 90 | + Serial.println("No data"); |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +void transmit(void) { |
| 95 | + // Send unconfirmed data to a gateway (port 1 by default) |
| 96 | + int status = loraNode.sendFrame(frameTx, sizeof(frameTx), UNCONFIRMED); |
| 97 | + if(status == LORA_SEND_ERROR) { |
| 98 | + Serial.println("Send frame failed!!!"); |
| 99 | + } else if(status == LORA_SEND_DELAYED) { |
| 100 | + Serial.println("Module busy or duty cycle"); |
| 101 | + } else { |
| 102 | + Serial.println("Frame sent"); |
| 103 | + } |
| 104 | +} |
0 commit comments