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 |
SX127x_PingPong.ino (4586B)
1 /* 2 RadioLib SX127x Ping-Pong Example 3 4 For default module settings, see the wiki page 5 https://github.com/jgromes/RadioLib/wiki/Default-configuration#sx127xrfm9x---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 // SX1278 has the following connections: 19 // NSS pin: 10 20 // DIO0 pin: 2 21 // NRST pin: 9 22 // DIO1 pin: 3 23 SX1278 radio = new Module(10, 2, 9, 3); 24 25 // or using RadioShield 26 // https://github.com/jgromes/RadioShield 27 //SX1278 radio = RadioShield.ModuleA; 28 29 // save transmission states between loops 30 int transmissionState = RADIOLIB_ERR_NONE; 31 32 // flag to indicate transmission or reception state 33 bool transmitFlag = false; 34 35 // disable interrupt when it's not needed 36 volatile bool enableInterrupt = true; 37 38 // flag to indicate that a packet was sent or received 39 volatile bool operationDone = false; 40 41 // this function is called when a complete packet 42 // is transmitted or received by the module 43 // IMPORTANT: this function MUST be 'void' type 44 // and MUST NOT have any arguments! 45 void setFlag(void) 46 { 47 // check if the interrupt is enabled 48 if (!enableInterrupt) { 49 return; 50 } 51 52 // we sent or received packet, set the flag 53 operationDone = true; 54 } 55 56 void setup() 57 { 58 Serial.begin(9600); 59 60 // initialize SX1278 with default settings 61 Serial.print(F("[SX1278] Initializing ... ")); 62 int state = radio.begin(); 63 if (state == RADIOLIB_ERR_NONE) { 64 Serial.println(F("success!")); 65 } else { 66 Serial.print(F("failed, code ")); 67 Serial.println(state); 68 while (true); 69 } 70 71 // set the function that will be called 72 // when new packet is received 73 radio.setDio0Action(setFlag); 74 75 #if defined(INITIATING_NODE) 76 // send the first packet on this node 77 Serial.print(F("[SX1278] Sending first packet ... ")); 78 transmissionState = radio.startTransmit("Hello World!"); 79 transmitFlag = true; 80 #else 81 // start listening for LoRa packets on this node 82 Serial.print(F("[SX1278] Starting to listen ... ")); 83 state = radio.startReceive(); 84 if (state == RADIOLIB_ERR_NONE) { 85 Serial.println(F("success!")); 86 } else { 87 Serial.print(F("failed, code ")); 88 Serial.println(state); 89 while (true); 90 } 91 #endif 92 } 93 94 void loop() 95 { 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("[SX1278] Received packet!")); 131 132 // print data of the packet 133 Serial.print(F("[SX1278] Data:\t\t")); 134 Serial.println(str); 135 136 // print RSSI (Received Signal Strength Indicator) 137 Serial.print(F("[SX1278] 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("[SX1278] 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("[SX1278] 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 }