acid-drop

- Unnamed repository; edit this file 'description' to name the repository.
git clone git://git.acid.vegas/-c.git
Log | Files | Refs | Archive | README | LICENSE

lora.cpp (4469B)

      1 #include <RadioLib.h>
      2 #include "pins.h"
      3 
      4 
      5 SX1262 radio = new Module(RADIO_CS_PIN, RADIO_DIO1_PIN, RADIO_RST_PIN, RADIO_BUSY_PIN);
      6 
      7 
      8 bool setupRadio() {
      9     pinMode(RADIO_CS_PIN, OUTPUT);
     10     digitalWrite(RADIO_CS_PIN, HIGH);
     11 
     12     int state = radio.begin(RADIO_FREQ);
     13     
     14     if (state == RADIOLIB_ERR_NONE) {
     15         Serial.println("Start Radio success!");
     16     } else {
     17         Serial.print("Start Radio failed,code:");
     18         Serial.println(state);
     19         return false;
     20     }
     21 
     22     if (radio.setFrequency(RADIO_FREQ) == RADIOLIB_ERR_INVALID_FREQUENCY) {
     23         Serial.println(F("Selected frequency is invalid for this module!"));
     24         return false;
     25     }
     26 
     27     if (radio.setBandwidth(125.0) == RADIOLIB_ERR_INVALID_BANDWIDTH) {
     28         Serial.println(F("Selected bandwidth is invalid for this module!"));
     29         return false;
     30     }
     31 
     32     if (radio.setSpreadingFactor(10) == RADIOLIB_ERR_INVALID_SPREADING_FACTOR) {
     33         Serial.println(F("Selected spreading factor is invalid for this module!"));
     34         return false;
     35     }
     36 
     37     if (radio.setCodingRate(6) == RADIOLIB_ERR_INVALID_CODING_RATE) {
     38         Serial.println(F("Selected coding rate is invalid for this module!"));
     39         return false;
     40     }
     41 
     42     if (radio.setSyncWord(0xAB) != RADIOLIB_ERR_NONE) {
     43         Serial.println(F("Unable to set sync word!"));
     44         return false;
     45     }
     46 
     47     // set output power to 10 dBm (accepted range is -17 - 22 dBm)
     48     if (radio.setOutputPower(17) == RADIOLIB_ERR_INVALID_OUTPUT_POWER) {
     49         Serial.println(F("Selected output power is invalid for this module!"));
     50         return false;
     51     }
     52 
     53     // set over current protection limit to 140 mA (accepted range is 45 - 140 mA) (set value to 0 to disable overcurrent protection)
     54     if (radio.setCurrentLimit(140) == RADIOLIB_ERR_INVALID_CURRENT_LIMIT) {
     55         Serial.println(F("Selected current limit is invalid for this module!"));
     56         return false;
     57     }
     58 
     59     // set LoRa preamble length to 15 symbols (accepted range is 0 - 65535)
     60     if (radio.setPreambleLength(15) == RADIOLIB_ERR_INVALID_PREAMBLE_LENGTH) {
     61         Serial.println(F("Selected preamble length is invalid for this module!"));
     62         return false;
     63     }
     64 
     65     if (radio.setCRC(false) == RADIOLIB_ERR_INVALID_CRC_CONFIGURATION) {
     66         Serial.println(F("Selected CRC is invalid for this module!"));
     67         return false;
     68     }
     69 
     70     // set the function that will be called when new packet is received
     71     //radio.setDio1Action(setFlag);
     72 
     73     return true;
     74 }
     75 
     76 bool transmit() {
     77     int state = radio.transmit("Hello World!");
     78 
     79     // you can also transmit byte array up to 256 bytes long
     80     /*
     81         byte byteArr[] = {0x01, 0x23, 0x45, 0x56, 0x78, 0xAB, 0xCD, 0xEF};
     82         int state = radio.transmit(byteArr, 8);
     83     */
     84 
     85     if (state == RADIOLIB_ERR_NONE) {
     86         Serial.println(F("Radio tramsmittion successful!"));
     87         Serial.print(F("[SX1262] Datarate:\t"));
     88         Serial.print(radio.getDataRate());
     89         Serial.println(F(" bps"));
     90         return true;
     91     } else if (state == RADIOLIB_ERR_PACKET_TOO_LONG) {
     92         Serial.println(F("Radio packet too long")); // 256 bytes is the maximum packet length
     93         return false;
     94     } else if (state == RADIOLIB_ERR_TX_TIMEOUT) {
     95         Serial.println(F("Radio timeout"));
     96         return false;
     97     } else {
     98         Serial.print(F("Radio error failed, code "));
     99         Serial.println(state);
    100         return false;
    101     }
    102 }
    103 
    104 
    105 void recvLoop() {
    106     String recv;
    107 
    108     while (true) {
    109         if (radio.available()) {
    110             int state = radio.readData(recv);
    111 
    112             if (state == RADIOLIB_ERR_NONE) {
    113                 Serial.print(F("[RADIO] Received packet!"));
    114 
    115                 Serial.print(F(" Data:"));
    116                 Serial.print(recv);
    117 
    118                 Serial.print(F(" RSSI:"));
    119                 Serial.print(radio.getRSSI());
    120                 Serial.print(F(" dBm"));
    121                 // snprintf(dispRecvicerBuff[1], sizeof(dispRecvicerBuff[1]), "RSSI:%.2f dBm", radio.getRSSI());
    122 
    123                 Serial.print(F("  SNR:"));
    124                 Serial.print(radio.getSNR());
    125                 Serial.println(F(" dB"));
    126             } else if (state ==  RADIOLIB_ERR_CRC_MISMATCH) {
    127                 Serial.println(F("CRC error!"));
    128             } else {
    129                 Serial.print(F("failed, code "));
    130                 Serial.println(state);
    131             }
    132         } else {
    133             Serial.println(F("Radio became unavailable!"));
    134             break;
    135         }
    136     }
    137 }