Simple receiver application for SX127x radio.
Dependencies: MbedJSONValue SX127x sx12xx_hal
main.cpp@3:cfbb4ffd0cbd, 2019-09-06 (annotated)
- Committer:
- corymast
- Date:
- Fri Sep 06 23:18:09 2019 +0000
- Revision:
- 3:cfbb4ffd0cbd
- Parent:
- 2:fef452211079
- Child:
- 4:fed69c9a3bbf
Added checks to verify JSON keys exist.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
LoRaToolbox | 0:f843ca4a8e98 | 1 | #include "radio.h" |
corymast | 2:fef452211079 | 2 | #include "MbedJSONValue/MbedJSONValue.h" |
LoRaToolbox | 0:f843ca4a8e98 | 3 | |
LoRaToolbox | 1:8cd5f8492271 | 4 | // Semtech radio definitions for SX127x, SX126x and SX128x |
LoRaToolbox | 1:8cd5f8492271 | 5 | |
LoRaToolbox | 0:f843ca4a8e98 | 6 | #if defined(SX127x_H) |
LoRaToolbox | 0:f843ca4a8e98 | 7 | #define BW_KHZ 500 |
LoRaToolbox | 1:8cd5f8492271 | 8 | #define SPREADING_FACTOR 11 |
LoRaToolbox | 1:8cd5f8492271 | 9 | #define CF_HZ 912000000 |
LoRaToolbox | 0:f843ca4a8e98 | 10 | #elif defined(SX126x_H) |
LoRaToolbox | 0:f843ca4a8e98 | 11 | #define BW_KHZ 500 |
LoRaToolbox | 0:f843ca4a8e98 | 12 | #define SPREADING_FACTOR 10 |
LoRaToolbox | 0:f843ca4a8e98 | 13 | #define CF_HZ 913000000 |
LoRaToolbox | 0:f843ca4a8e98 | 14 | #elif defined(SX128x_H) |
LoRaToolbox | 0:f843ca4a8e98 | 15 | #define BW_KHZ 200 |
LoRaToolbox | 0:f843ca4a8e98 | 16 | #define SPREADING_FACTOR 7 |
LoRaToolbox | 0:f843ca4a8e98 | 17 | #define CF_HZ 2487000000 |
LoRaToolbox | 0:f843ca4a8e98 | 18 | #endif |
LoRaToolbox | 0:f843ca4a8e98 | 19 | |
LoRaToolbox | 0:f843ca4a8e98 | 20 | DigitalOut myled(LED1); |
LoRaToolbox | 0:f843ca4a8e98 | 21 | |
LoRaToolbox | 0:f843ca4a8e98 | 22 | /**********************************************************************/ |
LoRaToolbox | 0:f843ca4a8e98 | 23 | |
LoRaToolbox | 0:f843ca4a8e98 | 24 | void txDoneCB() |
LoRaToolbox | 0:f843ca4a8e98 | 25 | { |
LoRaToolbox | 0:f843ca4a8e98 | 26 | } |
LoRaToolbox | 0:f843ca4a8e98 | 27 | |
LoRaToolbox | 0:f843ca4a8e98 | 28 | void rxDoneCB(uint8_t size, float rssi, float snr) |
LoRaToolbox | 0:f843ca4a8e98 | 29 | { |
corymast | 2:fef452211079 | 30 | MbedJSONValue message; |
LoRaToolbox | 0:f843ca4a8e98 | 31 | printf("%.1fdBm snr:%.1fdB\t", rssi, snr); |
LoRaToolbox | 0:f843ca4a8e98 | 32 | |
LoRaToolbox | 0:f843ca4a8e98 | 33 | myled.write(!myled.read()); // toggle LED |
LoRaToolbox | 0:f843ca4a8e98 | 34 | |
corymast | 2:fef452211079 | 35 | //Determine if the packet is JSON formatted by checking first character |
corymast | 2:fef452211079 | 36 | if(Radio::radio.rx_buf[0] == '{') |
corymast | 2:fef452211079 | 37 | { |
corymast | 2:fef452211079 | 38 | printf("JSON Format!\r\n"); |
corymast | 2:fef452211079 | 39 | // Display payload packet information |
corymast | 2:fef452211079 | 40 | char* json = (char*)Radio::radio.rx_buf; |
corymast | 3:cfbb4ffd0cbd | 41 | printf("Message: %s\r\n", json); |
corymast | 2:fef452211079 | 42 | parse(message, (char*)json); |
corymast | 2:fef452211079 | 43 | printf("\r\n\n"); |
corymast | 3:cfbb4ffd0cbd | 44 | if(message.hasMember("btn_count")) |
corymast | 3:cfbb4ffd0cbd | 45 | { |
corymast | 3:cfbb4ffd0cbd | 46 | printf("btn_count: %d\r\n", message["btn_count"].get<int>()); |
corymast | 3:cfbb4ffd0cbd | 47 | } |
corymast | 3:cfbb4ffd0cbd | 48 | if(message.hasMember("btn_timer")) |
corymast | 3:cfbb4ffd0cbd | 49 | { |
corymast | 3:cfbb4ffd0cbd | 50 | printf("btn_timer: %f\r\n", message["btn_timer"].get<double>()); |
corymast | 3:cfbb4ffd0cbd | 51 | } |
corymast | 3:cfbb4ffd0cbd | 52 | if(message.hasMember("my_str")) |
corymast | 3:cfbb4ffd0cbd | 53 | { |
corymast | 3:cfbb4ffd0cbd | 54 | printf("my_str: %s\r\n", message["my_str"].get<string>().c_str()); |
corymast | 3:cfbb4ffd0cbd | 55 | } |
corymast | 3:cfbb4ffd0cbd | 56 | if(message.hasMember("my_boolean")) |
corymast | 3:cfbb4ffd0cbd | 57 | { |
corymast | 3:cfbb4ffd0cbd | 58 | printf("my_bool: %s\r\n", message["my_boolean"].get<bool>() ? "true" : "false"); |
corymast | 3:cfbb4ffd0cbd | 59 | } |
corymast | 2:fef452211079 | 60 | printf("\r\n\n"); |
LoRaToolbox | 0:f843ca4a8e98 | 61 | } |
corymast | 2:fef452211079 | 62 | else |
corymast | 2:fef452211079 | 63 | { |
corymast | 2:fef452211079 | 64 | printf("Byte Format!\r\n"); |
corymast | 2:fef452211079 | 65 | // Display payload packet information |
corymast | 2:fef452211079 | 66 | for (int i = 0; i < size; i++) { |
corymast | 2:fef452211079 | 67 | printf("%02d ", Radio::radio.rx_buf[i]); // Changed to "%02d \n" |
corymast | 2:fef452211079 | 68 | } |
corymast | 2:fef452211079 | 69 | printf("\r\n\n"); |
corymast | 2:fef452211079 | 70 | } |
corymast | 2:fef452211079 | 71 | |
LoRaToolbox | 0:f843ca4a8e98 | 72 | } |
LoRaToolbox | 0:f843ca4a8e98 | 73 | |
LoRaToolbox | 0:f843ca4a8e98 | 74 | const RadioEvents_t rev = { |
LoRaToolbox | 0:f843ca4a8e98 | 75 | /* Dio0_top_half */ NULL, |
LoRaToolbox | 0:f843ca4a8e98 | 76 | /* TxDone_topHalf */ NULL, |
LoRaToolbox | 0:f843ca4a8e98 | 77 | /* TxDone_botHalf */ txDoneCB, |
LoRaToolbox | 0:f843ca4a8e98 | 78 | /* TxTimeout */ NULL, |
LoRaToolbox | 0:f843ca4a8e98 | 79 | /* RxDone */ rxDoneCB, |
LoRaToolbox | 0:f843ca4a8e98 | 80 | /* RxTimeout */ NULL, |
LoRaToolbox | 0:f843ca4a8e98 | 81 | /* RxError */ NULL, |
LoRaToolbox | 0:f843ca4a8e98 | 82 | /* FhssChangeChannel */NULL, |
LoRaToolbox | 0:f843ca4a8e98 | 83 | /* CadDone */ NULL |
LoRaToolbox | 0:f843ca4a8e98 | 84 | }; |
LoRaToolbox | 0:f843ca4a8e98 | 85 | |
LoRaToolbox | 0:f843ca4a8e98 | 86 | int main() |
LoRaToolbox | 0:f843ca4a8e98 | 87 | { |
LoRaToolbox | 1:8cd5f8492271 | 88 | |
corymast | 2:fef452211079 | 89 | // POR & Reset debug message |
LoRaToolbox | 0:f843ca4a8e98 | 90 | printf("\r\nreset-rx\r\n"); |
LoRaToolbox | 0:f843ca4a8e98 | 91 | |
LoRaToolbox | 0:f843ca4a8e98 | 92 | Radio::Init(&rev); |
LoRaToolbox | 0:f843ca4a8e98 | 93 | |
LoRaToolbox | 1:8cd5f8492271 | 94 | // Radio Start |
LoRaToolbox | 1:8cd5f8492271 | 95 | |
LoRaToolbox | 0:f843ca4a8e98 | 96 | Radio::Standby(); |
LoRaToolbox | 0:f843ca4a8e98 | 97 | Radio::LoRaModemConfig(BW_KHZ, SPREADING_FACTOR, 1); |
LoRaToolbox | 0:f843ca4a8e98 | 98 | Radio::SetChannel(CF_HZ); |
LoRaToolbox | 0:f843ca4a8e98 | 99 | |
LoRaToolbox | 1:8cd5f8492271 | 100 | // preambleLen, fixLen, crcOn, invIQ |
LoRaToolbox | 1:8cd5f8492271 | 101 | |
LoRaToolbox | 0:f843ca4a8e98 | 102 | Radio::LoRaPacketConfig(8, false, true, false); |
LoRaToolbox | 1:8cd5f8492271 | 103 | |
LoRaToolbox | 1:8cd5f8492271 | 104 | // Start radio receiver, wait for packets from transmitter |
LoRaToolbox | 0:f843ca4a8e98 | 105 | |
LoRaToolbox | 0:f843ca4a8e98 | 106 | Radio::Rx(0); |
LoRaToolbox | 0:f843ca4a8e98 | 107 | |
LoRaToolbox | 0:f843ca4a8e98 | 108 | for (;;) { |
LoRaToolbox | 0:f843ca4a8e98 | 109 | Radio::service(); |
LoRaToolbox | 0:f843ca4a8e98 | 110 | } |
LoRaToolbox | 0:f843ca4a8e98 | 111 | } |