acid-drop- Hacking the planet from a LilyGo T-Deck using custom firmware |
git clone git://git.acid.vegas/acid-drop.git |
Log | Files | Refs | Archive | README | LICENSE |
SX126x_PingPong.ino (4297B)
1 /* 2 RadioLib SX126x Ping-Pong Example 3 4 For default module settings, see the wiki page 5 https://github.com/jgromes/RadioLib/wiki/Default-configuration#sx126x---lora-modem 6 7 For full API reference, see the GitHub Pages 8 https://jgromes.github.io/RadioLib/ 9 */ 10 11 // include the library 12 #include <RadioLib.h> 13 14 // uncomment the following only on one 15 // of the nodes to initiate the pings 16 //#define INITIATING_NODE 17 18 // SX1262 has the following connections: 19 // NSS pin: 10 20 // DIO1 pin: 2 21 // NRST pin: 3 22 // BUSY pin: 9 23 SX1262 radio = new Module(10, 2, 3, 9); 24 25 // or using RadioShield 26 // https://github.com/jgromes/RadioShield 27 //SX1262 radio = RadioShield.ModuleA; 28 29 // or using CubeCell 30 //SX1262 radio = new Module(RADIOLIB_BUILTIN_MODULE); 31 32 // save transmission states between loops 33 int transmissionState = RADIOLIB_ERR_NONE; 34 35 // flag to indicate transmission or reception state 36 bool transmitFlag = false; 37 38 // disable interrupt when it's not needed 39 volatile bool enableInterrupt = true; 40 41 // flag to indicate that a packet was sent or received 42 volatile bool operationDone = false; 43 44 // this function is called when a complete packet 45 // is transmitted or received by the module 46 // IMPORTANT: this function MUST be 'void' type 47 // and MUST NOT have any arguments! 48 void setFlag(void) { 49 // check if the interrupt is enabled 50 if(!enableInterrupt) { 51 return; 52 } 53 54 // we sent aor received packet, set the flag 55 operationDone = true; 56 } 57 58 void setup() { 59 Serial.begin(9600); 60 61 // initialize SX1262 with default settings 62 Serial.print(F("[SX1262] Initializing ... ")); 63 int state = radio.begin(); 64 if (state == RADIOLIB_ERR_NONE) { 65 Serial.println(F("success!")); 66 } else { 67 Serial.print(F("failed, code ")); 68 Serial.println(state); 69 while (true); 70 } 71 72 // set the function that will be called 73 // when new packet is received 74 radio.setDio1Action(setFlag); 75 76 #if defined(INITIATING_NODE) 77 // send the first packet on this node 78 Serial.print(F("[SX1262] Sending first packet ... ")); 79 transmissionState = radio.startTransmit("Hello World!"); 80 transmitFlag = true; 81 #else 82 // start listening for LoRa packets on this node 83 Serial.print(F("[SX1262] Starting to listen ... ")); 84 state = radio.startReceive(); 85 if (state == RADIOLIB_ERR_NONE) { 86 Serial.println(F("success!")); 87 } else { 88 Serial.print(F("failed, code ")); 89 Serial.println(state); 90 while (true); 91 } 92 #endif 93 } 94 95 void loop() { 96 // check if the previous operation finished 97 if(operationDone) { 98 // disable the interrupt service routine while 99 // processing the data 100 enableInterrupt = false; 101 102 // reset flag 103 operationDone = false; 104 105 if(transmitFlag) { 106 // the previous operation was transmission, listen for response 107 // print the result 108 if (transmissionState == RADIOLIB_ERR_NONE) { 109 // packet was successfully sent 110 Serial.println(F("transmission finished!")); 111 112 } else { 113 Serial.print(F("failed, code ")); 114 Serial.println(transmissionState); 115 116 } 117 118 // listen for response 119 radio.startReceive(); 120 transmitFlag = false; 121 122 } else { 123 // the previous operation was reception 124 // print data and send another packet 125 String str; 126 int state = radio.readData(str); 127 128 if (state == RADIOLIB_ERR_NONE) { 129 // packet was successfully received 130 Serial.println(F("[SX1262] Received packet!")); 131 132 // print data of the packet 133 Serial.print(F("[SX1262] Data:\t\t")); 134 Serial.println(str); 135 136 // print RSSI (Received Signal Strength Indicator) 137 Serial.print(F("[SX1262] RSSI:\t\t")); 138 Serial.print(radio.getRSSI()); 139 Serial.println(F(" dBm")); 140 141 // print SNR (Signal-to-Noise Ratio) 142 Serial.print(F("[SX1262] SNR:\t\t")); 143 Serial.print(radio.getSNR()); 144 Serial.println(F(" dB")); 145 146 } 147 148 // wait a second before transmitting again 149 delay(1000); 150 151 // send another one 152 Serial.print(F("[SX1262] Sending another packet ... ")); 153 transmissionState = radio.startTransmit("Hello World!"); 154 transmitFlag = true; 155 } 156 157 // we're ready to process more packets, 158 // enable interrupt service routine 159 enableInterrupt = true; 160 161 } 162 }