Projects STRLCPY CatSniffer Commits 6c8f8fb3
🤬
  • ■ ■ ■ ■ ■ ■
    firmware/LoRaCatSniffer/LoRaCatSniffer.ino
     1 +/*
     2 + CatSniffer - Use LoRa for communication with the SX1262 module
     3 +
     4 + Andres Sabas @ Electronic Cats
     5 + Original Creation Date: Jal 23, 2021
     6 + This code is beerware; if you see me (or any other Electronic Cats
     7 + member) at the local, and you've found our code helpful,
     8 + please buy us a round!
     9 + Distributed as-is; no warranty is given.
     10 +*/
     11 + 
     12 +#define SERIALCOMMAND_HARDWAREONLY
     13 +
     14 +#include <SerialCommand.h>
     15 +#include <RadioLib.h>
     16 + 
     17 +#define CTF1 14
     18 +#define CTF2 11
     19 +#define CTF3 10
     20 + 
     21 +SerialCommand SCmd;
     22 + 
     23 +float fwVersion= 0.1;
     24 + 
     25 +float frequency = 915;
     26 +int spreadFactor = 8;
     27 +int bwReference = 7;
     28 +int codingRate = 5;
     29 +byte syncWord = 0x12;
     30 +int preambleLength = 8;
     31 +int outputPower = 20;
     32 +int channel = 0;
     33 +bool rx_status = false;
     34 + 
     35 +// SX1262 has the following connections:
     36 +// NSS pin: 17
     37 +// DIO1 pin: 3
     38 +// NRST pin: 8
     39 +// BUSY pin: 2
     40 +SX1262 radio = new Module(17, 3, 8, 2);
     41 + 
     42 +// or using RadioShield
     43 +// https://github.com/jgromes/RadioShield
     44 +//SX1262 radio = RadioShield.ModuleA;
     45 + 
     46 +void setup() {
     47 + Serial.begin(9600);
     48 + while(!Serial);
     49 + 
     50 + Serial.println("Welcome to the LoRa Sniffer CLI " + String(fwVersion,1) + "v\n");
     51 + Serial.println("With this sketch you can scan the LoRa spectrum");
     52 + Serial.println("Changing the Frequency, Spreading Factor, BandWidth or the IQ signals of the radio.");
     53 + Serial.println("Type help to get the available commands.");
     54 + Serial.println("Electronic Cats ® 2020");
     55 + 
     56 + // Setup callbacks for SerialCommand commands
     57 + SCmd.addCommand("help",help);
     58 + SCmd.addCommand("set_rx",set_rx);
     59 + SCmd.addCommand("set_tx",set_tx0);
     60 + SCmd.addCommand("set_tx_hex",set_tx1);
     61 + SCmd.addCommand("set_tx_ascii",set_tx2);
     62 + SCmd.addCommand("set_freq",set_freq);
     63 + SCmd.addCommand("set_sf",set_sf);
     64 + SCmd.addCommand("set_bw",set_bw);
     65 + SCmd.addCommand("set_cr",set_cr);
     66 + SCmd.addCommand("set_chann",set_chann);
     67 + SCmd.addCommand("set_sw",set_sw);
     68 + SCmd.addCommand("set_op",set_op);
     69 + SCmd.addCommand("set_pl",set_pl);
     70 + 
     71 + SCmd.addCommand("set_rx",set_rx);
     72 + 
     73 + SCmd.addCommand("get_config",get_config);
     74 + SCmd.addCommand("get_freq",get_freq);
     75 + SCmd.addCommand("get_sf",get_sf);
     76 + SCmd.addCommand("get_bw",get_bw);
     77 + SCmd.addCommand("get_cr",get_cr);
     78 + SCmd.addCommand("get_op",get_op);
     79 + SCmd.addCommand("get_sw",get_sw);
     80 + SCmd.addCommand("get_pl",get_pl);
     81 +
     82 + SCmd.setDefaultHandler(unrecognized); // Handler for command that isn't matched (says "What?")
     83 + 
     84 + 
     85 + pinMode(CTF1, OUTPUT);
     86 + pinMode(CTF2, OUTPUT);
     87 + pinMode(CTF3, OUTPUT);
     88 + 
     89 + digitalWrite(CTF1, HIGH);
     90 + digitalWrite(CTF2, LOW);
     91 + digitalWrite(CTF3, LOW);
     92 + 
     93 +
     94 + // initialize SX1262 with default settings
     95 + Serial.print(F("[SX1262] Initializing ... "));
     96 + //debing(float freq, float bw, uint8_t sf, uint8_t cr, uint8_t syncWord, int8_t power, uint16_t preambleLength, float tcxoVoltage, bool useRegulatorLDO)
     97 + 
     98 + int state = radio.begin(915.0, 250, 7, 5, 0x12, 20, 8, 0, false);
     99 + 
     100 + if (state == ERR_NONE) {
     101 + Serial.println(F("success!"));
     102 + } else {
     103 + Serial.print(F("failed, code "));
     104 + Serial.println(state);
     105 + while (true);
     106 + }
     107 + 
     108 + // set the function that will be called
     109 + // when new packet is received
     110 + radio.setDio1Action(setFlag);
     111 + 
     112 + rx_status = false;
     113 +
     114 + // some modules have an external RF switch
     115 + // controlled via two pins (RX enable, TX enable)
     116 + // to enable automatic control of the switch,
     117 + // call the following method
     118 + // RX enable: 4
     119 + // TX enable: 5
     120 +
     121 + radio.setRfSwitchPins(16, 15);
     122 +
     123 +}
     124 + 
     125 +// flag to indicate that a packet was received
     126 +volatile bool receivedFlag = false;
     127 + 
     128 +// disable interrupt when it's not needed
     129 +volatile bool enableInterrupt = true;
     130 + 
     131 +// this function is called when a complete packet
     132 +// is received by the module
     133 +// IMPORTANT: this function MUST be 'void' type
     134 +// and MUST NOT have any arguments!
     135 +void setFlag(void) {
     136 + // check if the interrupt is enabled
     137 + if(!enableInterrupt) {
     138 + return;
     139 + }
     140 + 
     141 + // we got a packet, set the flag
     142 + receivedFlag = true;
     143 +}
     144 + 
     145 +void loop() {
     146 + 
     147 + SCmd.readSerial(); // We don't do much, just process serial commands
     148 + 
     149 + // check if the flag is set
     150 + if(receivedFlag && rx_status) {
     151 + // disable the interrupt service routine while
     152 + // processing the data
     153 + enableInterrupt = false;
     154 + 
     155 + // reset flag
     156 + receivedFlag = false;
     157 + 
     158 + // you can read received data as an Arduino String
     159 + String str;
     160 + int state = radio.readData(str);
     161 + 
     162 + // you can also read received data as byte array
     163 + /*
     164 + byte byteArr[8];
     165 + int state = radio.readData(byteArr, 8);
     166 + */
     167 + 
     168 + if (state == ERR_NONE) {
     169 + // packet was successfully received
     170 + Serial.println(F("[SX1262] Received packet!"));
     171 + 
     172 + // print data of the packet
     173 + Serial.print(F("[SX1262] Data:\t\t"));
     174 + Serial.println(str);
     175 + 
     176 + // print RSSI (Received Signal Strength Indicator)
     177 + Serial.print(F("[SX1262] RSSI:\t\t"));
     178 + Serial.print(radio.getRSSI());
     179 + Serial.println(F(" dBm"));
     180 + 
     181 + // print SNR (Signal-to-Noise Ratio)
     182 + Serial.print(F("[SX1262] SNR:\t\t"));
     183 + Serial.print(radio.getSNR());
     184 + Serial.println(F(" dB"));
     185 + 
     186 + } else if (state == ERR_CRC_MISMATCH) {
     187 + // packet was received, but is malformed
     188 + Serial.println(F("CRC error!"));
     189 + 
     190 + } else {
     191 + // some other error occurred
     192 + Serial.print(F("failed, code "));
     193 + Serial.println(state);
     194 + 
     195 + }
     196 + 
     197 + // put module back to listen mode
     198 + radio.startReceive();
     199 + 
     200 + // we're ready to receive more packets,
     201 + // enable interrupt service routine
     202 + enableInterrupt = true;
     203 + }
     204 + 
     205 +}
     206 +void help(){
     207 + Serial.println("Fw version: " + String(fwVersion,1)+"v");
     208 + Serial.println("\tConfiguration commands:");
     209 + Serial.println("\tset_rx");
     210 + Serial.println("\tset_tx");
     211 + Serial.println("\tset_tx_hex");
     212 + Serial.println("\tset_tx_ascii");
     213 + Serial.println("\tset_chann");
     214 + Serial.println("\tset_freq");
     215 + Serial.println("\tset_sf");
     216 + Serial.println("\tset_bw");
     217 + Serial.println("\tset_cr");
     218 + Serial.println("\tset_sw");
     219 + Serial.println("\tset_pl");
     220 + Serial.println("\tset_op");
     221 + 
     222 + Serial.println("Monitor commands:");
     223 + Serial.println("\tget_freq");
     224 + Serial.println("\tget_sf");
     225 + Serial.println("\tget_bw");
     226 + Serial.println("\tget_cr");
     227 + Serial.println("\tget_sw");
     228 + Serial.println("\tget_pl");
     229 + Serial.println("\tget_op");
     230 + Serial.println("\tget_config");
     231 + 
     232 + Serial.println("..help");
     233 + 
     234 +}
     235 + 
     236 +void set_tx0(){
     237 + char *arg;
     238 + byte data[64];
     239 + int i;
     240 + 
     241 + arg = SCmd.next(); // Get the next argument from the SerialCommand object buffer
     242 + if(arg != NULL){
     243 + for(i = 0; arg != NULL; i++){
     244 + if((arg[0] > 47 && arg[0]< 58) && (arg[1] > 47 && arg[1]< 58) && (arg[2] > 47 && arg[2]< 58) && arg[3] == 0){
     245 +
     246 + data[i] = (byte)strtoul(arg, NULL, 10);
     247 + //Serial.println(data[i],BIN);
     248 + }
     249 + else {
     250 + Serial.println("Use a series of xxx values separated by spaces. The value xxx represents a 3-digit number < 255. ");
     251 + return;
     252 + }
     253 + arg = SCmd.next(); // Get the next argument from the SerialCommand object buffer
     254 + }
     255 +
     256 + for(int j = 0; j < i; j++){
     257 + Serial.print(data[j]);
     258 + Serial.print(" ");
     259 + }
     260 +
     261 + int state = radio.transmit(data, i);
     262 + 
     263 + if (state == ERR_NONE) {
     264 + // the packet was successfully transmitted
     265 + Serial.println(F("success!"));
     266 +
     267 + // print measured data rate
     268 + Serial.print(F("[SX1262] Datarate:\t"));
     269 + Serial.print(radio.getDataRate());
     270 + Serial.println(F(" bps"));
     271 +
     272 + } else if (state == ERR_PACKET_TOO_LONG) {
     273 + // the supplied packet was longer than 256 bytes
     274 + Serial.println(F("too long!"));
     275 +
     276 + } else if (state == ERR_TX_TIMEOUT) {
     277 + // timeout occured while transmitting packet
     278 + Serial.println(F("timeout!"));
     279 +
     280 + } else {
     281 + // some other error occurred
     282 + Serial.print(F("failed, code "));
     283 + Serial.println(state);
     284 +
     285 + }
     286 + 
     287 + Serial.println();
     288 + Serial.print(i);
     289 + Serial.println(" byte(s) sent");
     290 +
     291 + rx_status = false;
     292 + }
     293 + else {
     294 + Serial.println("No argument");
     295 + }
     296 +}
     297 + 
     298 +void set_tx1(){
     299 + char *arg;
     300 + byte data[64];
     301 + int i;
     302 + 
     303 + arg = SCmd.next(); // Get the next argument from the SerialCommand object buffer
     304 + if(arg != NULL){
     305 + for(i = 0; arg != NULL; i++){
     306 +
     307 + if((arg[0] > 64 && arg[0]< 71 || arg[0] > 47 && arg[0]< 58) && (arg[1] > 64 && arg[1]< 71 || arg[1] > 47 && arg[1]< 58) && arg[2] == 0){
     308 +
     309 + data[i] = 0;
     310 + data[i] = nibble(*(arg))<<4;
     311 + data[i] = data[i]|nibble(*(arg + 1));
     312 + }
     313 + else{
     314 + Serial.println("Use a series of yy values separated by spaces. The value yy represents any pair of hexadecimal digits. ");
     315 + return;
     316 + }
     317 + arg = SCmd.next(); // Get the next argument from the SerialCommand object buffer
     318 + }
     319 +
     320 + for(int j = 0; j < i; j++){
     321 + Serial.print(data[j], HEX);
     322 + Serial.print(" ");
     323 + }
     324 + 
     325 + int state = radio.transmit(data, i);
     326 + 
     327 + if (state == ERR_NONE) {
     328 + // the packet was successfully transmitted
     329 + Serial.println(F("success!"));
     330 +
     331 + // print measured data rate
     332 + Serial.print(F("[SX1262] Datarate:\t"));
     333 + Serial.print(radio.getDataRate());
     334 + Serial.println(F(" bps"));
     335 +
     336 + } else if (state == ERR_PACKET_TOO_LONG) {
     337 + // the supplied packet was longer than 256 bytes
     338 + Serial.println(F("too long!"));
     339 +
     340 + } else if (state == ERR_TX_TIMEOUT) {
     341 + // timeout occured while transmitting packet
     342 + Serial.println(F("timeout!"));
     343 +
     344 + } else {
     345 + // some other error occurred
     346 + Serial.print(F("failed, code "));
     347 + Serial.println(state);
     348 +
     349 + }
     350 + 
     351 +
     352 + Serial.println();
     353 + Serial.print(i);
     354 + Serial.println(" byte(s) sent");
     355 +
     356 + rx_status = false;
     357 + 
     358 + }
     359 + else {
     360 + Serial.println("No argument");
     361 + }
     362 +}
     363 + 
     364 + 
     365 +void set_tx2(){
     366 + char *arg;
     367 + arg = SCmd.next(); // Get the next argument from the SerialCommand object buffer
     368 + if (arg != NULL){
     369 +
     370 + Serial.print(F("[SX1262] Transmitting packet ... "));
     371 +
     372 + // you can transmit C-string or Arduino string up to
     373 + // 256 characters long
     374 + // NOTE: transmit() is a blocking method!
     375 + // See example SX126x_Transmit_Interrupt for details
     376 + // on non-blocking transmission method.
     377 + int state = radio.transmit("Hello World!");
     378 +
     379 + // you can also transmit byte array up to 256 bytes long
     380 + /*
     381 + byte byteArr[] = {0x01, 0x23, 0x45, 0x56, 0x78, 0xAB, 0xCD, 0xEF};
     382 + int state = radio.transmit(byteArr, 8);
     383 + */
     384 +
     385 + if (state == ERR_NONE) {
     386 + // the packet was successfully transmitted
     387 + Serial.println(F(" Success!, ASCII message sent"));
     388 +
     389 + // print measured data rate
     390 + Serial.print(F("[SX1262] Datarate:\t"));
     391 + Serial.print(radio.getDataRate());
     392 + Serial.println(F(" bps"));
     393 +
     394 + } else if (state == ERR_PACKET_TOO_LONG) {
     395 + // the supplied packet was longer than 256 bytes
     396 + Serial.println(F("too long!"));
     397 +
     398 + } else if (state == ERR_TX_TIMEOUT) {
     399 + // timeout occured while transmitting packet
     400 + Serial.println(F("timeout!"));
     401 +
     402 + } else {
     403 + // some other error occurred
     404 + Serial.print(F("failed, code "));
     405 + Serial.println(state);
     406 +
     407 + }
     408 +
     409 + rx_status = false;
     410 + }
     411 + else {
     412 + Serial.println("No argument");
     413 + }
     414 +}
     415 + 
     416 +void set_freq(){
     417 + char *arg;
     418 + arg = SCmd.next();
     419 + frequency = atof(arg);
     420 + if (arg != NULL){
     421 + if(frequency > 902 && frequency < 923){
     422 + 
     423 + if (radio.setFrequency(frequency) == ERR_INVALID_FREQUENCY) {
     424 + Serial.println(F("Selected frequency is invalid for this module!"));
     425 + return;
     426 + }
     427 +
     428 + Serial.println("Frequency set to " + String(frequency) + " MHz");
     429 + rx_status = false;
     430 + }
     431 + else{
     432 + Serial.println("Error setting the frequency");
     433 + Serial.println("Value must be between 902 MHz and 923 MHz");
     434 + }
     435 + }
     436 + else {
     437 + Serial.println("No argument");
     438 + }
     439 +}
     440 + 
     441 +void set_chann(){
     442 + char *arg;
     443 + arg = SCmd.next(); // Get the next argument from the SerialCommand object buffer
     444 + channel = atoi(arg);
     445 + if (arg != NULL){
     446 + if(channel > -1 && channel < 64){
     447 + long freq = 902300000 + channel*125000;
     448 + frequency = (float)freq/1000000;
     449 +
     450 + //LoRa.setFrequency(freq);
     451 + if (radio.setFrequency(frequency) == ERR_INVALID_FREQUENCY) {
     452 + Serial.println(F("Selected frequency is invalid for this module!"));
     453 + return;
     454 + }
     455 +
     456 + Serial.println("Uplink channel set to " + String(channel));
     457 + rx_status = false;
     458 + }
     459 + else if(channel > 63 && channel < 72){
     460 + long freq = 903000000 + (channel - 64)*500000;
     461 + frequency = (float)freq/1000000;
     462 + 
     463 + if (radio.setFrequency(frequency) == ERR_INVALID_FREQUENCY) {
     464 + Serial.println(F("Selected frequency is invalid for this module!"));
     465 + return;
     466 + }
     467 +
     468 + Serial.println("Downlink channel set to " + String(channel));
     469 + rx_status = false;
     470 + }
     471 + else{
     472 + Serial.println("Error setting the channel");
     473 + Serial.println("Value must be between 0 and 63");
     474 + }
     475 + }
     476 + else {
     477 + Serial.println("No argument");
     478 + }
     479 +}
     480 + 
     481 + 
     482 +void set_sf(){
     483 + char *arg;
     484 + arg = SCmd.next();
     485 + if (arg != NULL){
     486 + spreadFactor = atoi(arg);
     487 + if(spreadFactor < 6 || spreadFactor > 12){
     488 + Serial.println("Error setting the Spreading factor");
     489 + Serial.println("Value must be between 6 and 12");
     490 + return;
     491 + }
     492 + else{
     493 + 
     494 + if (radio.setSpreadingFactor(spreadFactor) == ERR_INVALID_SPREADING_FACTOR) {
     495 + Serial.println(F("Selected spreading factor is invalid for this module!"));
     496 + return;
     497 + }
     498 + Serial.println("Spreading factor set to " + String(spreadFactor));
     499 + rx_status = false;
     500 + }
     501 + 
     502 + }
     503 + else {
     504 + Serial.println("No argument");
     505 + }
     506 +}
     507 + 
     508 +void set_cr(){
     509 + char *arg;
     510 + arg = SCmd.next();
     511 + if (arg != NULL){
     512 + codingRate = atoi(arg);
     513 + if(codingRate > 8 || codingRate < 5){
     514 + Serial.println("Error setting the Coding Rate");
     515 + Serial.println("Value must be between 5 and 8");
     516 + return;
     517 + }
     518 + else{
     519 + 
     520 + if (radio.setCodingRate(codingRate) == ERR_INVALID_CODING_RATE) {
     521 + Serial.println(F("Selected coding rate is invalid for this module!"));
     522 + return;
     523 + }
     524 + Serial.println("CodingRate set to 4/" + String(codingRate));
     525 + rx_status = false;
     526 + }
     527 + 
     528 + }
     529 + else {
     530 + Serial.println("No argument");
     531 + }
     532 +}
     533 + 
     534 +void set_bw(){
     535 + char *arg;
     536 + arg = SCmd.next(); // Get the next argument from the SerialCommand object buffer
     537 + int bwRefResp = bwReference; //save the previous data
     538 + bwReference = atoi(arg);
     539 + if (arg != NULL){
     540 + switch (bwReference){
     541 + case 0:
     542 + if (radio.setBandwidth(7.8) == ERR_INVALID_BANDWIDTH) {
     543 + Serial.println(F("Selected bandwidth is invalid for this module!"));
     544 + return;
     545 + }
     546 + rx_status = false;
     547 + Serial.println("Bandwidth set to 7.8 kHz");
     548 + break;
     549 + case 1:
     550 + if (radio.setBandwidth(10.4) == ERR_INVALID_BANDWIDTH) {
     551 + Serial.println(F("Selected bandwidth is invalid for this module!"));
     552 + return;
     553 + }
     554 + rx_status = false;
     555 + Serial.println("Bandwidth set to 10.4 kHz");
     556 + break;
     557 + case 2:
     558 + //LoRa.setSignalBandwidth(15.6E3);
     559 + if (radio.setBandwidth(15.6) == ERR_INVALID_BANDWIDTH) {
     560 + Serial.println(F("Selected bandwidth is invalid for this module!"));
     561 + return;
     562 + }
     563 + rx_status = false;
     564 + Serial.println("Bandwidth set to 15.6 kHz");
     565 + break;
     566 + case 3:
     567 + if (radio.setBandwidth(20.8) == ERR_INVALID_BANDWIDTH) {
     568 + Serial.println(F("Selected bandwidth is invalid for this module!"));
     569 + return;
     570 + }
     571 + rx_status = false;
     572 + Serial.println("Bandwidth set to 20.8 kHz");
     573 + break;
     574 + case 4:
     575 + if (radio.setBandwidth(31.25) == ERR_INVALID_BANDWIDTH) {
     576 + Serial.println(F("Selected bandwidth is invalid for this module!"));
     577 + return;
     578 + }
     579 + rx_status = false;
     580 + Serial.println("Bandwidth set to 31.25 kHz");
     581 + break;
     582 + case 5:
     583 + if (radio.setBandwidth(41.7) == ERR_INVALID_BANDWIDTH) {
     584 + Serial.println(F("Selected bandwidth is invalid for this module!"));
     585 + return;
     586 + }
     587 + rx_status = false;
     588 + Serial.println("Bandwidth set to 41.7 kHz");
     589 + break;
     590 + case 6:
     591 + if (radio.setBandwidth(62.5) == ERR_INVALID_BANDWIDTH) {
     592 + Serial.println(F("Selected bandwidth is invalid for this module!"));
     593 + return;
     594 + }
     595 + rx_status = false;
     596 + Serial.println("Bandwidth set to 62.5 kHz");
     597 + break;
     598 + case 7:
     599 + if (radio.setBandwidth(125) == ERR_INVALID_BANDWIDTH) {
     600 + Serial.println(F("Selected bandwidth is invalid for this module!"));
     601 + return;
     602 + }
     603 + rx_status = false;
     604 + Serial.println("Bandwidth set to 125 kHz");
     605 + break;
     606 + case 8:
     607 + if (radio.setBandwidth(250.0) == ERR_INVALID_BANDWIDTH) {
     608 + Serial.println(F("Selected bandwidth is invalid for this module!"));
     609 + return;
     610 + }
     611 + rx_status = false;
     612 + Serial.println("Bandwidth set to 250 kHz");
     613 + break;
     614 + 
     615 + default:
     616 + Serial.println("Error setting the bandwidth value must be between 0-8");
     617 + bwReference = bwRefResp; //if there's no valid data restore previous value
     618 + break;
     619 + }
     620 + }
     621 + else {
     622 + Serial.println("No argument");
     623 + }
     624 +}
     625 + 
     626 +void set_op(){
     627 + char *arg;
     628 + arg = SCmd.next();
     629 + if (arg != NULL){
     630 + outputPower = atoi(arg);
     631 + if(outputPower > -16 || outputPower < 23){
     632 + Serial.println("Error setting the output power");
     633 + Serial.println("Value must be between -17 and 22 dBm");
     634 + return;
     635 + }
     636 + else{
     637 + 
     638 + if (radio.setOutputPower(outputPower) == ERR_INVALID_OUTPUT_POWER) {
     639 + Serial.println(F("Selected output power is invalid for this module!"));
     640 + return;
     641 + }
     642 + Serial.println("Output power set to " + String(outputPower));
     643 + rx_status = false;
     644 + }
     645 + 
     646 + }
     647 + else {
     648 + Serial.println("No argument");
     649 + }
     650 +}
     651 + 
     652 +byte nibble(char c)
     653 +{
     654 + if (c >= '0' && c <= '9')
     655 + return c - '0';
     656 + 
     657 + if (c >= 'a' && c <= 'f')
     658 + return c - 'a' + 10;
     659 + 
     660 + if (c >= 'A' && c <= 'F')
     661 + return c - 'A' + 10;
     662 + 
     663 + return 0; // Not a valid hexadecimal character
     664 +}
     665 + 
     666 +void set_sw(){
     667 + char *arg;
     668 + byte data;
     669 + int i;
     670 + 
     671 + arg = SCmd.next(); // Get the next argument from the SerialCommand object buffer
     672 + if(arg != NULL){
     673 +
     674 + if((arg[0] > 64 && arg[0]< 71 || arg[0] > 47 && arg[0]< 58) && (arg[1] > 64 && arg[1]< 71 || arg[1] > 47 && arg[1]< 58) && arg[2] == 0){
     675 +
     676 + data = 0;
     677 + data = nibble(*(arg))<<4;
     678 + data = data|nibble(*(arg + 1));
     679 + if (radio.setSyncWord(data) != ERR_NONE) {
     680 + Serial.println(F("Unable to set sync word!"));
     681 + return;
     682 + }
     683 + Serial.println("Sync word set to 0x" + String(data));
     684 + }
     685 + else{
     686 + Serial.println("Use yy value. The value yy represents any pair of hexadecimal digits. ");
     687 + return;
     688 + }
     689 + }
     690 + else {
     691 + Serial.println("No argument");
     692 + }
     693 +}
     694 + 
     695 +void set_pl(){
     696 + char *arg;
     697 + arg = SCmd.next();
     698 + if (arg != NULL){
     699 + preambleLength = atoi(arg);
     700 + if(preambleLength > -1 || preambleLength < 65536){
     701 + Serial.println("Error setting the preamble length");
     702 + Serial.println("Value must be between 0 and 65535");
     703 + return;
     704 + }
     705 + else{
     706 + 
     707 + if (radio.setPreambleLength(preambleLength) == ERR_INVALID_CODING_RATE) {
     708 + Serial.println(F("Selected preamble length is invalid for this module!"));
     709 + return;
     710 + }
     711 + 
     712 + Serial.println("Preamble length set to " + String(preambleLength));
     713 + rx_status = false;
     714 + }
     715 + 
     716 + }
     717 + else {
     718 + Serial.println("No argument");
     719 + }
     720 +}
     721 + 
     722 +void set_rx(){
     723 + char *arg;
     724 + arg = SCmd.next();
     725 + if (arg != NULL){
     726 + frequency = atof(arg);
     727 + if(frequency > 902 && frequency < 923){
     728 + 
     729 + if (radio.setFrequency(frequency) == ERR_INVALID_FREQUENCY) {
     730 + Serial.println(F("Selected frequency is invalid for this module!"));
     731 + return;
     732 + }
     733 + 
     734 + Serial.println("LoRa radio receiving at " + String(frequency) + " MHz");
     735 +
     736 + // start listening for LoRa packets
     737 + Serial.print(F("[SX1262] Starting to listen ... "));
     738 + int state = radio.startReceive();
     739 + if (state == ERR_NONE) {
     740 + Serial.println(F("success!"));
     741 + } else {
     742 + Serial.print(F("failed, code "));
     743 + Serial.println(state);
     744 + while (true);
     745 + }
     746 +
     747 + rx_status = true;
     748 + }
     749 + else{
     750 + Serial.println("Error setting the frequency");
     751 + Serial.println("Value must be between 902 MHz and 923 MHz");
     752 + }
     753 + }
     754 + else {
     755 + Serial.println("LoRa radio receiving at " + String(frequency) + " MHz");
     756 + // start listening for LoRa packets
     757 + Serial.print(F("[SX1262] Starting to listen ... "));
     758 + int state = radio.startReceive();
     759 + if (state == ERR_NONE) {
     760 + Serial.println(F("success!"));
     761 + } else {
     762 + Serial.print(F("failed, code "));
     763 + Serial.println(state);
     764 + while (true);
     765 + }
     766 + rx_status = true;
     767 + }
     768 +}
     769 + 
     770 +/**********Get information**************/
     771 + 
     772 +void get_freq(){
     773 + Serial.print("Frequency = ");
     774 + Serial.println(frequency);
     775 +}
     776 + 
     777 +void get_sf(){
     778 + Serial.print("Spreading factor = ");
     779 + Serial.println(spreadFactor);
     780 +}
     781 + 
     782 +void get_cr(){
     783 + Serial.print("Coding Rate = ");
     784 + Serial.println(codingRate);
     785 +}
     786 + 
     787 +void get_sw(){
     788 + Serial.print("Sync Word = 0x");
     789 + Serial.println(syncWord, HEX);
     790 +}
     791 + 
     792 +void get_pl(){
     793 + Serial.print("Preamble Length = ");
     794 + Serial.println(preambleLength);
     795 +}
     796 + 
     797 +void get_op(){
     798 + Serial.print("Output Power = ");
     799 + Serial.println(outputPower);
     800 +}
     801 + 
     802 +void get_bw(){
     803 + Serial.println("Bandwidth = ");
     804 + switch (bwReference){
     805 + case 0:
     806 + Serial.println("7.8 kHz");
     807 + break;
     808 + case 1:
     809 + Serial.println("10.4 kHz");
     810 + break;
     811 + case 2:
     812 + Serial.println("15.6 kHz");
     813 + break;
     814 + case 3:
     815 + Serial.println("20.8 kHz");
     816 + break;
     817 + case 4:
     818 + Serial.println("31.25 kHz");
     819 + break;
     820 + case 5:
     821 + Serial.println("41.7 kHz");
     822 + break;
     823 + case 6:
     824 + Serial.println("62.5 kHz");
     825 + break;
     826 + case 7:
     827 + Serial.println("125 kHz");
     828 + break;
     829 + case 8:
     830 + Serial.println("250 kHz");
     831 + break;
     832 + default:
     833 + Serial.println("Error setting the bandwidth value must be between 0-8");
     834 + break;
     835 + }
     836 +}
     837 + 
     838 +void get_config(){
     839 + Serial.println("\nRadio configurations: ");
     840 + Serial.println("Frequency = " + String(frequency) + " MHz");
     841 + Serial.print("Bandwidth = ");
     842 + switch (bwReference){
     843 + case 0:
     844 + Serial.println("7.8 kHz");
     845 + break;
     846 + case 1:
     847 + Serial.println("10.4 kHz");
     848 + break;
     849 + case 2:
     850 + Serial.println("15.6 kHz");
     851 + break;
     852 + case 3:
     853 + Serial.println("20.8 kHz");
     854 + break;
     855 + case 4:
     856 + Serial.println("31.25 kHz");
     857 + break;
     858 + case 5:
     859 + Serial.println("41.7 kHz");
     860 + break;
     861 + case 6:
     862 + Serial.println("62.5 kHz");
     863 + break;
     864 + case 7:
     865 + Serial.println("125 kHz");
     866 + break;
     867 + case 8:
     868 + Serial.println("250 kHz");
     869 + break;
     870 + }
     871 + Serial.println("Spreading Factor = " + String(spreadFactor));
     872 + Serial.println("Coding Rate = 4/" + String(codingRate));
     873 + Serial.print("Sync Word = 0x");
     874 + Serial.println(syncWord, HEX);
     875 + Serial.println("Preamble Length = " + String(preambleLength));
     876 + Serial.println("Output Power = " + String(outputPower));
     877 + Serial.println("Rx active = " + String(rx_status));
     878 +}
     879 + 
     880 +// This gets set as the default handler, and gets called when no other command matches.
     881 +void unrecognized(const char *command) {
     882 + Serial.println("Command not found, type help to get the valid commands");
     883 +}
     884 + 
Please wait...
Page is in error, reload to recover