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

nRF24_Transmit.ino (2589B)

      1 /*
      2    RadioLib nRF24 Transmit Example
      3 
      4    This example transmits packets using nRF24 2.4 GHz radio module.
      5    Each packet contains up to 32 bytes of data, in the form of:
      6     - Arduino String
      7     - null-terminated char array (C-string)
      8     - arbitrary binary data (byte array)
      9 
     10    Packet delivery is automatically acknowledged by the receiver.
     11 
     12    For default module settings, see the wiki page
     13    https://github.com/jgromes/RadioLib/wiki/Default-configuration#nrf24
     14 
     15    For full API reference, see the GitHub Pages
     16    https://jgromes.github.io/RadioLib/
     17 */
     18 
     19 // include the library
     20 #include <RadioLib.h>
     21 
     22 // nRF24 has the following connections:
     23 // CS pin:    10
     24 // IRQ pin:   2
     25 // CE pin:    3
     26 nRF24 radio = new Module(10, 2, 3);
     27 
     28 // or using RadioShield
     29 // https://github.com/jgromes/RadioShield
     30 //nRF24 radio = RadioShield.ModuleA;
     31 
     32 void setup() {
     33   Serial.begin(9600);
     34 
     35   // initialize nRF24 with default settings
     36   Serial.print(F("[nRF24] Initializing ... "));
     37   int state = radio.begin();
     38   if(state == RADIOLIB_ERR_NONE) {
     39     Serial.println(F("success!"));
     40   } else {
     41     Serial.print(F("failed, code "));
     42     Serial.println(state);
     43     while(true);
     44   }
     45 
     46   // set transmit address
     47   // NOTE: address width in bytes MUST be equal to the
     48   //       width set in begin() or setAddressWidth()
     49   //       methods (5 by default)
     50   byte addr[] = {0x01, 0x23, 0x45, 0x67, 0x89};
     51   Serial.print(F("[nRF24] Setting transmit pipe ... "));
     52   state = radio.setTransmitPipe(addr);
     53   if(state == RADIOLIB_ERR_NONE) {
     54     Serial.println(F("success!"));
     55   } else {
     56     Serial.print(F("failed, code "));
     57     Serial.println(state);
     58     while(true);
     59   }
     60 }
     61 
     62 void loop() {
     63   Serial.print(F("[nRF24] Transmitting packet ... "));
     64 
     65   // you can transmit C-string or Arduino string up to
     66   // 32 characters long
     67   int state = radio.transmit("Hello World!");
     68 
     69   if (state == RADIOLIB_ERR_NONE) {
     70     // the packet was successfully transmitted
     71     Serial.println(F("success!"));
     72 
     73   } else if (state == RADIOLIB_ERR_PACKET_TOO_LONG) {
     74     // the supplied packet was longer than 32 bytes
     75     Serial.println(F("too long!"));
     76 
     77   } else if (state == RADIOLIB_ERR_ACK_NOT_RECEIVED) {
     78     // acknowledge from destination module
     79     // was not received within 15 retries
     80     Serial.println(F("ACK not received!"));
     81 
     82   } else if (state == RADIOLIB_ERR_TX_TIMEOUT) {
     83     // timed out while transmitting
     84     Serial.println(F("timeout!"));
     85 
     86   } else {
     87     // some other error occurred
     88     Serial.print(F("failed, code "));
     89     Serial.println(state);
     90 
     91   }
     92 
     93   // wait for a second before transmitting again
     94   delay(1000);
     95 }