1-
2- // If your using PlatformIO you will need to set _CLASS_C_ using the -D flag in the build_flags options
3- # include < Arduino.h >
1+ /*
2+ * Example of OTAA class C device
3+ */
44#include < lorawan.h>
55
66// OTAA credentials
7- const char *devEui = " 0 " ;
8- const char *appEui = " 0 " ;
9- const char *appKey = " 0 " ;
7+ const char *devEui = " 0000000000000000 " ;
8+ const char *appEui = " 0000000000000000 " ;
9+ const char *appKey = " 00000000000000000000000000000000 " ;
1010
11- const unsigned long interval = 15 * 1000 ; // 10 s interval to send message
11+ const unsigned long interval = 10000 ; // 10 s interval to send message
1212unsigned long previousMillis = 0 ; // will store last time message sent
1313unsigned int counter = 0 ; // message counter
1414
1515char myStr[50 ];
16+ char outStr[255 ];
17+ byte recvStatus = 0 ;
1618
1719const sRFM_pins RFM_pins = {
18- .CS = 7 ,
19- .RST = 3 ,
20- .DIO0 = 18 ,
21- .DIO1 = 19 ,
20+ .CS = SS,
21+ .RST = RFM_RST,
22+ .DIO0 = RFM_DIO0,
23+ .DIO1 = RFM_DIO1,
24+ .DIO2 = RFM_DIO2,
25+ .DIO5 = RFM_DIO5,
2226};
2327
2428void setup () {
@@ -31,12 +35,15 @@ void setup() {
3135 return ;
3236 }
3337
38+ // Set LoRaWAN Class change CLASS_A or CLASS_C
3439 lora.setDeviceClass (CLASS_C);
40+
3541 // Set Data Rate
3642 lora.setDataRate (SF8BW125);
3743
3844 // set channel to random
3945 lora.setChannel (MULTI);
46+
4047 // Put OTAA Key and DevAddress here
4148 lora.setDevEUI (devEui);
4249 lora.setAppEUI (appEui);
@@ -49,42 +56,36 @@ void setup() {
4956 isJoined = lora.join ();
5057
5158 // wait for 10s to try again
52- delay (7000 );
59+ delay (10000 );
5360 }while (!isJoined);
5461 Serial.println (" Joined to network" );
55-
56- delay (1000 );
57-
58- sprintf (myStr, " First Message" );
59-
60- Serial.print (" Sending: " );
61- Serial.println (myStr);
62-
63- lora.sendUplink (myStr, strlen (myStr), 0 , 1 );
64- counter++;
65-
6662}
6763
6864void loop () {
69-
7065 // Check interval overflow
7166 if (millis () - previousMillis > interval){
72- delay (1000 );
73-
7467 previousMillis = millis ();
7568
7669 sprintf (myStr, " Counter-%d" , counter);
7770
7871 Serial.print (" Sending: " );
7972 Serial.println (myStr);
80-
81- lora.sendUplink (myStr, strlen (myStr), 1 , 1 );
82- counter++;
8373
74+ // To send the string, the message is confirmed, using Port1
75+ lora.sendUplink (myStr, strlen (myStr), 1 , 1 );
76+ counter++;
8477 }
8578
79+ // If any downliks are received from the server, proceed to read and print it, as well as the reception power
80+ recvStatus = lora.readData (outStr);
81+ if (recvStatus) {
82+ Serial.println (lora.getRssi ());
83+ Serial.println (outStr);
84+ }
85+
86+ // Check Lora RX
8687 lora.update ();
8788
89+ // To receive the "Acknowlede" message from the server, since our Uplink was a confirmed one
8890 if (lora.readAck ()) Serial.println (" ack received" );
89-
9091}
0 commit comments