| 
									
										
										
										
											2020-04-07 11:30:05 +00:00
										 |  |  | /*
 | 
					
						
							|  |  |  |    RadioLib SX128x Transmit with Interrupts Example | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    This example transmits LoRa packets with one second delays | 
					
						
							|  |  |  |    between them. Each packet contains up to 256 bytes | 
					
						
							|  |  |  |    of data, in the form of: | 
					
						
							|  |  |  |     - Arduino String | 
					
						
							|  |  |  |     - null-terminated char array (C-string) | 
					
						
							|  |  |  |     - arbitrary binary data (byte array) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    Other modules from SX128x family can also be used. | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-04 09:47:24 +00:00
										 |  |  |    For default module settings, see the wiki page | 
					
						
							|  |  |  |    https://github.com/jgromes/RadioLib/wiki/Default-configuration#sx128x---lora-modem
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-07 11:30:05 +00:00
										 |  |  |    For full API reference, see the GitHub Pages | 
					
						
							|  |  |  |    https://jgromes.github.io/RadioLib/
 | 
					
						
							|  |  |  | */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // include the library
 | 
					
						
							|  |  |  | #include <RadioLib.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // SX1280 has the following connections:
 | 
					
						
							|  |  |  | // NSS pin:   10
 | 
					
						
							|  |  |  | // DIO1 pin:  2
 | 
					
						
							|  |  |  | // NRST pin:  3
 | 
					
						
							|  |  |  | // BUSY pin:  9
 | 
					
						
							| 
									
										
										
										
											2020-07-04 09:47:24 +00:00
										 |  |  | SX1280 radio = new Module(10, 2, 3, 9); | 
					
						
							| 
									
										
										
										
											2020-04-07 11:30:05 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | // or using RadioShield
 | 
					
						
							|  |  |  | // https://github.com/jgromes/RadioShield
 | 
					
						
							| 
									
										
										
										
											2020-07-04 09:47:24 +00:00
										 |  |  | //SX1280 radio = RadioShield.ModuleA;
 | 
					
						
							| 
									
										
										
										
											2020-04-07 11:30:05 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | // save transmission state between loops
 | 
					
						
							| 
									
										
										
										
											2021-11-14 10:42:36 +00:00
										 |  |  | int transmissionState = RADIOLIB_ERR_NONE; | 
					
						
							| 
									
										
										
										
											2020-04-07 11:30:05 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | void setup() { | 
					
						
							|  |  |  |   Serial.begin(9600); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // initialize SX1280 with default settings
 | 
					
						
							|  |  |  |   Serial.print(F("[SX1280] Initializing ... ")); | 
					
						
							| 
									
										
										
										
											2020-07-04 09:47:24 +00:00
										 |  |  |   int state = radio.begin(); | 
					
						
							| 
									
										
										
										
											2021-11-14 10:42:36 +00:00
										 |  |  |   if (state == RADIOLIB_ERR_NONE) { | 
					
						
							| 
									
										
										
										
											2020-04-07 11:30:05 +00:00
										 |  |  |     Serial.println(F("success!")); | 
					
						
							|  |  |  |   } else { | 
					
						
							|  |  |  |     Serial.print(F("failed, code ")); | 
					
						
							|  |  |  |     Serial.println(state); | 
					
						
							|  |  |  |     while (true); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // set the function that will be called
 | 
					
						
							|  |  |  |   // when packet transmission is finished
 | 
					
						
							| 
									
										
										
										
											2020-07-04 09:47:24 +00:00
										 |  |  |   radio.setDio1Action(setFlag); | 
					
						
							| 
									
										
										
										
											2020-04-07 11:30:05 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |   // start transmitting the first packet
 | 
					
						
							|  |  |  |   Serial.print(F("[SX1280] Sending first packet ... ")); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // you can transmit C-string or Arduino string up to
 | 
					
						
							|  |  |  |   // 256 characters long
 | 
					
						
							| 
									
										
										
										
											2020-07-04 09:47:24 +00:00
										 |  |  |   transmissionState = radio.startTransmit("Hello World!"); | 
					
						
							| 
									
										
										
										
											2020-04-07 11:30:05 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |   // you can also transmit byte array up to 256 bytes long
 | 
					
						
							|  |  |  |   /*
 | 
					
						
							|  |  |  |     byte byteArr[] = {0x01, 0x23, 0x45, 0x67, | 
					
						
							|  |  |  |                       0x89, 0xAB, 0xCD, 0xEF}; | 
					
						
							| 
									
										
										
										
											2020-07-04 09:47:24 +00:00
										 |  |  |     state = radio.startTransmit(byteArr, 8); | 
					
						
							| 
									
										
										
										
											2020-04-07 11:30:05 +00:00
										 |  |  |   */ | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // flag to indicate that a packet was sent
 | 
					
						
							|  |  |  | volatile bool transmittedFlag = false; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // this function is called when a complete packet
 | 
					
						
							|  |  |  | // is transmitted by the module
 | 
					
						
							|  |  |  | // IMPORTANT: this function MUST be 'void' type
 | 
					
						
							|  |  |  | //            and MUST NOT have any arguments!
 | 
					
						
							| 
									
										
										
										
											2021-12-29 07:49:22 +00:00
										 |  |  | #if defined(ESP8266) || defined(ESP32)
 | 
					
						
							|  |  |  |   ICACHE_RAM_ATTR | 
					
						
							|  |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2020-04-07 11:30:05 +00:00
										 |  |  | void setFlag(void) { | 
					
						
							|  |  |  |   // we sent a packet, set the flag
 | 
					
						
							|  |  |  |   transmittedFlag = true; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void loop() { | 
					
						
							|  |  |  |   // check if the previous transmission finished
 | 
					
						
							|  |  |  |   if(transmittedFlag) { | 
					
						
							|  |  |  |     // reset flag
 | 
					
						
							|  |  |  |     transmittedFlag = false; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-14 10:42:36 +00:00
										 |  |  |     if (transmissionState == RADIOLIB_ERR_NONE) { | 
					
						
							| 
									
										
										
										
											2020-04-07 11:30:05 +00:00
										 |  |  |       // packet was successfully sent
 | 
					
						
							|  |  |  |       Serial.println(F("transmission finished!")); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     } else { | 
					
						
							|  |  |  |       Serial.print(F("failed, code ")); | 
					
						
							|  |  |  |       Serial.println(transmissionState); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-18 14:15:07 +00:00
										 |  |  |     // clean up after transmission is finished
 | 
					
						
							|  |  |  |     // this will ensure transmitter is disabled,
 | 
					
						
							|  |  |  |     // RF switch is powered down etc.
 | 
					
						
							|  |  |  |     radio.finishTransmit(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-07 11:30:05 +00:00
										 |  |  |     // wait a second before transmitting again
 | 
					
						
							|  |  |  |     delay(1000); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // send another one
 | 
					
						
							|  |  |  |     Serial.print(F("[SX1280] Sending another packet ... ")); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // you can transmit C-string or Arduino string up to
 | 
					
						
							|  |  |  |     // 256 characters long
 | 
					
						
							| 
									
										
										
										
											2020-07-04 09:47:24 +00:00
										 |  |  |     transmissionState = radio.startTransmit("Hello World!"); | 
					
						
							| 
									
										
										
										
											2020-04-07 11:30:05 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     // you can also transmit byte array up to 256 bytes long
 | 
					
						
							|  |  |  |     /*
 | 
					
						
							|  |  |  |       byte byteArr[] = {0x01, 0x23, 0x45, 0x67, | 
					
						
							|  |  |  |                         0x89, 0xAB, 0xCD, 0xEF}; | 
					
						
							| 
									
										
										
										
											2020-07-04 09:47:24 +00:00
										 |  |  |       int state = radio.startTransmit(byteArr, 8); | 
					
						
							| 
									
										
										
										
											2020-04-07 11:30:05 +00:00
										 |  |  |     */ | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | } |