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_Transmit.ino (2790B)

      1 /*
      2    RadioLib SX126x Transmit Example
      3 
      4    This example transmits packets using SX1262 LoRa radio module.
      5    Each packet contains up to 256 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    Other modules from SX126x family can also be used.
     11 
     12    For default module settings, see the wiki page
     13    https://github.com/jgromes/RadioLib/wiki/Default-configuration#sx126x---lora-modem
     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 // SX1262 has the following connections:
     23 // NSS pin:   10
     24 // DIO1 pin:  2
     25 // NRST pin:  3
     26 // BUSY pin:  9
     27 SX1262 radio = new Module(10, 2, 3, 9);
     28 
     29 // or using RadioShield
     30 // https://github.com/jgromes/RadioShield
     31 //SX1262 radio = RadioShield.ModuleA;
     32 
     33 // or using CubeCell
     34 //SX1262 radio = new Module(RADIOLIB_BUILTIN_MODULE);
     35 
     36 void setup() {
     37   Serial.begin(9600);
     38 
     39   // initialize SX1262 with default settings
     40   Serial.print(F("[SX1262] Initializing ... "));
     41   int state = radio.begin();
     42   if (state == RADIOLIB_ERR_NONE) {
     43     Serial.println(F("success!"));
     44   } else {
     45     Serial.print(F("failed, code "));
     46     Serial.println(state);
     47     while (true);
     48   }
     49 
     50   // some modules have an external RF switch
     51   // controlled via two pins (RX enable, TX enable)
     52   // to enable automatic control of the switch,
     53   // call the following method
     54   // RX enable:   4
     55   // TX enable:   5
     56   /*
     57     radio.setRfSwitchPins(4, 5);
     58   */
     59 }
     60 
     61 void loop() {
     62   Serial.print(F("[SX1262] Transmitting packet ... "));
     63 
     64   // you can transmit C-string or Arduino string up to
     65   // 256 characters long
     66   // NOTE: transmit() is a blocking method!
     67   //       See example SX126x_Transmit_Interrupt for details
     68   //       on non-blocking transmission method.
     69   int state = radio.transmit("Hello World!");
     70 
     71   // you can also transmit byte array up to 256 bytes long
     72   /*
     73     byte byteArr[] = {0x01, 0x23, 0x45, 0x56, 0x78, 0xAB, 0xCD, 0xEF};
     74     int state = radio.transmit(byteArr, 8);
     75   */
     76 
     77   if (state == RADIOLIB_ERR_NONE) {
     78     // the packet was successfully transmitted
     79     Serial.println(F("success!"));
     80 
     81     // print measured data rate
     82     Serial.print(F("[SX1262] Datarate:\t"));
     83     Serial.print(radio.getDataRate());
     84     Serial.println(F(" bps"));
     85 
     86   } else if (state == RADIOLIB_ERR_PACKET_TOO_LONG) {
     87     // the supplied packet was longer than 256 bytes
     88     Serial.println(F("too long!"));
     89 
     90   } else if (state == RADIOLIB_ERR_TX_TIMEOUT) {
     91     // timeout occured while transmitting packet
     92     Serial.println(F("timeout!"));
     93 
     94   } else {
     95     // some other error occurred
     96     Serial.print(F("failed, code "));
     97     Serial.println(state);
     98 
     99   }
    100 
    101   // wait for a second before transmitting again
    102   delay(1000);
    103 }