|
| 1 | +/* |
| 2 | + * Example of OTAA class C device |
| 3 | + */ |
| 4 | +#include <lorawan.h> |
| 5 | + |
| 6 | +// OTAA credentials |
| 7 | +const char *devEui = "0000000000000000"; |
| 8 | +const char *appEui = "0000000000000000"; |
| 9 | +const char *appKey = "00000000000000000000000000000000"; |
| 10 | + |
| 11 | +const unsigned long interval = 10000; // 10 s interval to send message |
| 12 | +unsigned long previousMillis = 0; // will store last time message sent |
| 13 | +unsigned int counter = 0; // message counter |
| 14 | + |
| 15 | +char myStr[50]; |
| 16 | +char outStr[255]; |
| 17 | +byte recvStatus = 0; |
| 18 | + |
| 19 | +//Pins used to handle the communication with the LoRa module |
| 20 | +const sRFM_pins RFM_pins = { |
| 21 | + .CS = 7, |
| 22 | + .RST = 3, |
| 23 | + .DIO0 = 18, |
| 24 | + .DIO1 = 19, |
| 25 | + .DIO2 = -1, |
| 26 | + .DIO5 = -1, |
| 27 | +}; |
| 28 | + |
| 29 | +void setup() { |
| 30 | + // Setup loraid access |
| 31 | + Serial.begin(115200); |
| 32 | + while(!Serial); |
| 33 | + if(!lora.init()){ |
| 34 | + Serial.println("RFM95 not detected"); |
| 35 | + delay(10000); |
| 36 | + return; |
| 37 | + } |
| 38 | + |
| 39 | + // Set LoRaWAN Class change CLASS_A or CLASS_C |
| 40 | + lora.setDeviceClass(CLASS_C); |
| 41 | + |
| 42 | + // Set Data Rate |
| 43 | + lora.setDataRate(SF8BW125); |
| 44 | + |
| 45 | + // set channel to random |
| 46 | + lora.setChannel(MULTI); |
| 47 | + |
| 48 | + // Put OTAA Key and DevAddress here |
| 49 | + lora.setDevEUI(devEui); |
| 50 | + lora.setAppEUI(appEui); |
| 51 | + lora.setAppKey(appKey); |
| 52 | + |
| 53 | + // Join procedure |
| 54 | + bool isJoined; |
| 55 | + do { |
| 56 | + Serial.println("Joining..."); |
| 57 | + isJoined = lora.join(); |
| 58 | + |
| 59 | + //wait for 10s to try again |
| 60 | + delay(10000); |
| 61 | + }while(!isJoined); |
| 62 | + Serial.println("Joined to network"); |
| 63 | +} |
| 64 | + |
| 65 | +void loop() { |
| 66 | + // Check interval overflow |
| 67 | + if (millis() - previousMillis > interval){ |
| 68 | + previousMillis = millis(); |
| 69 | + |
| 70 | + sprintf(myStr, "Counter-%d", counter); |
| 71 | + |
| 72 | + Serial.print("Sending: "); |
| 73 | + Serial.println(myStr); |
| 74 | + |
| 75 | + //To send the string, the message is confirmed, using Port1 |
| 76 | + lora.sendUplink(myStr, strlen(myStr), 1, 1); |
| 77 | + counter++; |
| 78 | + } |
| 79 | + |
| 80 | + //If any downliks are received from the server, proceed to read and print it, as well as the reception power |
| 81 | + recvStatus = lora.readData(outStr); |
| 82 | + if(recvStatus) { |
| 83 | + Serial.println(lora.getRssi()); |
| 84 | + Serial.println(outStr); |
| 85 | + } |
| 86 | + |
| 87 | + // Check Lora RX |
| 88 | + lora.update(); |
| 89 | + |
| 90 | + //To receive the "Acknowlede" message from the server, since our Uplink was a confirmed one |
| 91 | + if(lora.readAck()) Serial.println("ack received"); |
| 92 | +} |
0 commit comments