| 
									
										
										
										
											2019-02-10 10:48:21 +00:00
										 |  |  | /*
 | 
					
						
							|  |  |  |    RadioLib CC1101 Receive with Interrupts Example | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    This example listens for FSK transmissions and tries to | 
					
						
							|  |  |  |    receive them. Once a packet is received, an interrupt is | 
					
						
							|  |  |  |    triggered. | 
					
						
							| 
									
										
										
										
											2019-06-02 12:09:10 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |    To successfully receive data, the following settings have to be the same | 
					
						
							|  |  |  |    on both transmitter and receiver: | 
					
						
							|  |  |  |     - carrier frequency | 
					
						
							|  |  |  |     - bit rate | 
					
						
							|  |  |  |     - frequency deviation | 
					
						
							|  |  |  |     - sync word | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-04 09:22:47 +00:00
										 |  |  |    For default module settings, see the wiki page | 
					
						
							|  |  |  |    https://github.com/jgromes/RadioLib/wiki/Default-configuration#cc1101
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    For full API reference, see the GitHub Pages | 
					
						
							|  |  |  |    https://jgromes.github.io/RadioLib/
 | 
					
						
							| 
									
										
										
										
											2019-02-10 10:48:21 +00:00
										 |  |  | */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // include the library
 | 
					
						
							|  |  |  | #include <RadioLib.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-02 12:09:10 +00:00
										 |  |  | // CC1101 has the following connections:
 | 
					
						
							| 
									
										
										
										
											2019-12-27 12:16:31 +00:00
										 |  |  | // CS pin:    10
 | 
					
						
							| 
									
										
										
										
											2019-06-02 12:09:10 +00:00
										 |  |  | // GDO0 pin:  2
 | 
					
						
							| 
									
										
										
										
											2019-12-27 12:16:31 +00:00
										 |  |  | // RST pin:   unused
 | 
					
						
							|  |  |  | // GDO2 pin:  3 (optional)
 | 
					
						
							| 
									
										
										
										
											2020-07-04 09:22:47 +00:00
										 |  |  | CC1101 radio = new Module(10, 2, RADIOLIB_NC, 3); | 
					
						
							| 
									
										
										
										
											2019-06-02 12:09:10 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | // or using RadioShield
 | 
					
						
							|  |  |  | // https://github.com/jgromes/RadioShield
 | 
					
						
							| 
									
										
										
										
											2020-07-04 09:22:47 +00:00
										 |  |  | //CC1101 radio = RadioShield.ModuleA;
 | 
					
						
							| 
									
										
										
										
											2019-02-10 10:48:21 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | void setup() { | 
					
						
							|  |  |  |   Serial.begin(9600); | 
					
						
							| 
									
										
										
										
											2019-05-24 11:58:49 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-10 10:48:21 +00:00
										 |  |  |   // initialize CC1101 with default settings
 | 
					
						
							|  |  |  |   Serial.print(F("[CC1101] Initializing ... ")); | 
					
						
							| 
									
										
										
										
											2020-07-04 09:22:47 +00:00
										 |  |  |   int state = radio.begin(); | 
					
						
							| 
									
										
										
										
											2019-02-10 10:48:21 +00:00
										 |  |  |   if (state == ERR_NONE) { | 
					
						
							|  |  |  |     Serial.println(F("success!")); | 
					
						
							|  |  |  |   } else { | 
					
						
							|  |  |  |     Serial.print(F("failed, code ")); | 
					
						
							|  |  |  |     Serial.println(state); | 
					
						
							|  |  |  |     while (true); | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2019-05-24 11:58:49 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |   // set the function that will be called
 | 
					
						
							| 
									
										
										
										
											2019-02-10 10:48:21 +00:00
										 |  |  |   // when new packet is received
 | 
					
						
							| 
									
										
										
										
											2020-07-04 09:22:47 +00:00
										 |  |  |   radio.setGdo0Action(setFlag); | 
					
						
							| 
									
										
										
										
											2019-05-24 11:58:49 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-10 10:48:21 +00:00
										 |  |  |   // start listening for packets
 | 
					
						
							|  |  |  |   Serial.print(F("[CC1101] Starting to listen ... ")); | 
					
						
							| 
									
										
										
										
											2020-07-04 09:22:47 +00:00
										 |  |  |   state = radio.startReceive(); | 
					
						
							| 
									
										
										
										
											2019-02-10 10:48:21 +00:00
										 |  |  |   if (state == ERR_NONE) { | 
					
						
							|  |  |  |     Serial.println(F("success!")); | 
					
						
							|  |  |  |   } else { | 
					
						
							|  |  |  |     Serial.print(F("failed, code ")); | 
					
						
							|  |  |  |     Serial.println(state); | 
					
						
							|  |  |  |     while (true); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // if needed, 'listen' mode can be disabled by calling
 | 
					
						
							|  |  |  |   // any of the following methods:
 | 
					
						
							|  |  |  |   //
 | 
					
						
							| 
									
										
										
										
											2020-07-04 09:22:47 +00:00
										 |  |  |   // radio.standby()
 | 
					
						
							|  |  |  |   // radio.sleep()
 | 
					
						
							|  |  |  |   // radio.transmit();
 | 
					
						
							|  |  |  |   // radio.receive();
 | 
					
						
							|  |  |  |   // radio.readData();
 | 
					
						
							| 
									
										
										
										
											2019-02-10 10:48:21 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // flag to indicate that a packet was received
 | 
					
						
							|  |  |  | volatile bool receivedFlag = false; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // disable interrupt when it's not needed
 | 
					
						
							|  |  |  | volatile bool enableInterrupt = true; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // this function is called when a complete packet
 | 
					
						
							|  |  |  | // is received by the module
 | 
					
						
							|  |  |  | // IMPORTANT: this function MUST be 'void' type
 | 
					
						
							|  |  |  | //            and MUST NOT have any arguments!
 | 
					
						
							|  |  |  | void setFlag(void) { | 
					
						
							|  |  |  |   // check if the interrupt is enabled
 | 
					
						
							|  |  |  |   if(!enableInterrupt) { | 
					
						
							|  |  |  |     return; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // we got a packet, set the flag
 | 
					
						
							|  |  |  |   receivedFlag = true; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void loop() { | 
					
						
							|  |  |  |   // check if the flag is set
 | 
					
						
							|  |  |  |   if(receivedFlag) { | 
					
						
							|  |  |  |     // disable the interrupt service routine while
 | 
					
						
							|  |  |  |     // processing the data
 | 
					
						
							|  |  |  |     enableInterrupt = false; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // reset flag
 | 
					
						
							|  |  |  |     receivedFlag = false; | 
					
						
							| 
									
										
										
										
											2019-05-24 11:58:49 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-10 10:48:21 +00:00
										 |  |  |     // you can read received data as an Arduino String
 | 
					
						
							| 
									
										
										
										
											2020-04-14 13:01:19 +00:00
										 |  |  |     String str; | 
					
						
							| 
									
										
										
										
											2020-07-04 09:22:47 +00:00
										 |  |  |     int state = radio.readData(str); | 
					
						
							| 
									
										
										
										
											2019-05-24 11:58:49 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-10 10:48:21 +00:00
										 |  |  |     // you can also read received data as byte array
 | 
					
						
							| 
									
										
										
										
											2020-04-14 13:01:19 +00:00
										 |  |  |     /*
 | 
					
						
							| 
									
										
										
										
											2019-02-10 10:48:21 +00:00
										 |  |  |       byte byteArr[8]; | 
					
						
							| 
									
										
										
										
											2020-07-04 09:22:47 +00:00
										 |  |  |       int state = radio.readData(byteArr, 8); | 
					
						
							| 
									
										
										
										
											2020-04-14 13:01:19 +00:00
										 |  |  |     */ | 
					
						
							| 
									
										
										
										
											2019-05-24 11:58:49 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-10 10:48:21 +00:00
										 |  |  |     if (state == ERR_NONE) { | 
					
						
							|  |  |  |       // packet was successfully received
 | 
					
						
							| 
									
										
										
										
											2019-05-24 11:58:49 +00:00
										 |  |  |       Serial.println(F("[CC1101] Received packet!")); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-10 10:48:21 +00:00
										 |  |  |       // print data of the packet
 | 
					
						
							| 
									
										
										
										
											2020-04-14 13:01:19 +00:00
										 |  |  |       Serial.print(F("[CC1101] Data:\t\t")); | 
					
						
							|  |  |  |       Serial.println(str); | 
					
						
							| 
									
										
										
										
											2019-02-10 10:48:21 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-24 11:58:49 +00:00
										 |  |  |       // print RSSI (Received Signal Strength Indicator)
 | 
					
						
							| 
									
										
										
										
											2019-02-10 10:48:21 +00:00
										 |  |  |       // of the last received packet
 | 
					
						
							| 
									
										
										
										
											2019-05-24 11:58:49 +00:00
										 |  |  |       Serial.print(F("[CC1101] RSSI:\t\t")); | 
					
						
							| 
									
										
										
										
											2020-07-04 09:22:47 +00:00
										 |  |  |       Serial.print(radio.getRSSI()); | 
					
						
							| 
									
										
										
										
											2019-05-24 11:58:49 +00:00
										 |  |  |       Serial.println(F(" dBm")); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       // print LQI (Link Quality Indicator)
 | 
					
						
							| 
									
										
										
										
											2019-02-10 10:48:21 +00:00
										 |  |  |       // of the last received packet, lower is better
 | 
					
						
							| 
									
										
										
										
											2019-05-24 11:58:49 +00:00
										 |  |  |       Serial.print(F("[CC1101] LQI:\t\t")); | 
					
						
							| 
									
										
										
										
											2020-07-04 09:22:47 +00:00
										 |  |  |       Serial.println(radio.getLQI()); | 
					
						
							| 
									
										
										
										
											2019-06-02 12:09:10 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     } else if (state == ERR_CRC_MISMATCH) { | 
					
						
							|  |  |  |       // packet was received, but is malformed
 | 
					
						
							|  |  |  |       Serial.println(F("CRC error!")); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     } else { | 
					
						
							|  |  |  |       // some other error occurred
 | 
					
						
							|  |  |  |       Serial.print(F("failed, code ")); | 
					
						
							|  |  |  |       Serial.println(state); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-10 10:48:21 +00:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-19 16:56:24 +00:00
										 |  |  |     // put module back to listen mode
 | 
					
						
							| 
									
										
										
										
											2020-07-04 09:22:47 +00:00
										 |  |  |     radio.startReceive(); | 
					
						
							| 
									
										
										
										
											2019-11-19 16:56:24 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-10 10:48:21 +00:00
										 |  |  |     // we're ready to receive more packets,
 | 
					
						
							|  |  |  |     // enable interrupt service routine
 | 
					
						
							|  |  |  |     enableInterrupt = true; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } |