Cory Mast / Mbed OS Simple_Rx_SX127x

Dependencies:   MbedJSONValue SX127x sx12xx_hal

Committer:
corymast
Date:
Fri Sep 06 22:59:55 2019 +0000
Revision:
2:fef452211079
Parent:
1:8cd5f8492271
Child:
3:cfbb4ffd0cbd
Added support for parsing JSON and byte data.

Who changed what in which revision?

UserRevisionLine numberNew 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 2:fef452211079 41 printf("Message: %s\r\n", json);
corymast 2:fef452211079 42
corymast 2:fef452211079 43 parse(message, (char*)json);
corymast 2:fef452211079 44
corymast 2:fef452211079 45 int btn_count = message["btn_count"].get<int>();
corymast 2:fef452211079 46 int btn_timer = message["btn_timer"].get<int>();
corymast 2:fef452211079 47 string my_str = message["my_str"].get<string>();
corymast 2:fef452211079 48 bool my_bool = message["my_boolean"].get<bool>();
corymast 2:fef452211079 49
corymast 2:fef452211079 50 printf("\r\n\n");
corymast 2:fef452211079 51 printf("btn_count: %d\r\n", message["btn_count"].get<int>());
corymast 2:fef452211079 52 printf("btn_timer: %f\r\n", message["btn_timer"].get<double>());
corymast 2:fef452211079 53 printf("my_str: %s\r\n", message["my_str"].get<string>().c_str());
corymast 2:fef452211079 54 printf("my_bool: %s\r\n", message["my_boolean"].get<bool>() ? "true" : "false");
corymast 2:fef452211079 55 printf("\r\n\n");
LoRaToolbox 0:f843ca4a8e98 56 }
corymast 2:fef452211079 57 else
corymast 2:fef452211079 58 {
corymast 2:fef452211079 59 printf("Byte Format!\r\n");
corymast 2:fef452211079 60 // Display payload packet information
corymast 2:fef452211079 61 for (int i = 0; i < size; i++) {
corymast 2:fef452211079 62 printf("%02d ", Radio::radio.rx_buf[i]); // Changed to "%02d \n"
corymast 2:fef452211079 63 }
corymast 2:fef452211079 64 printf("\r\n\n");
corymast 2:fef452211079 65 }
corymast 2:fef452211079 66
LoRaToolbox 0:f843ca4a8e98 67 }
LoRaToolbox 0:f843ca4a8e98 68
LoRaToolbox 0:f843ca4a8e98 69 const RadioEvents_t rev = {
LoRaToolbox 0:f843ca4a8e98 70 /* Dio0_top_half */ NULL,
LoRaToolbox 0:f843ca4a8e98 71 /* TxDone_topHalf */ NULL,
LoRaToolbox 0:f843ca4a8e98 72 /* TxDone_botHalf */ txDoneCB,
LoRaToolbox 0:f843ca4a8e98 73 /* TxTimeout */ NULL,
LoRaToolbox 0:f843ca4a8e98 74 /* RxDone */ rxDoneCB,
LoRaToolbox 0:f843ca4a8e98 75 /* RxTimeout */ NULL,
LoRaToolbox 0:f843ca4a8e98 76 /* RxError */ NULL,
LoRaToolbox 0:f843ca4a8e98 77 /* FhssChangeChannel */NULL,
LoRaToolbox 0:f843ca4a8e98 78 /* CadDone */ NULL
LoRaToolbox 0:f843ca4a8e98 79 };
LoRaToolbox 0:f843ca4a8e98 80
LoRaToolbox 0:f843ca4a8e98 81 int main()
LoRaToolbox 0:f843ca4a8e98 82 {
LoRaToolbox 1:8cd5f8492271 83
corymast 2:fef452211079 84 // POR & Reset debug message
LoRaToolbox 0:f843ca4a8e98 85 printf("\r\nreset-rx\r\n");
LoRaToolbox 0:f843ca4a8e98 86
LoRaToolbox 0:f843ca4a8e98 87 Radio::Init(&rev);
LoRaToolbox 0:f843ca4a8e98 88
LoRaToolbox 1:8cd5f8492271 89 // Radio Start
LoRaToolbox 1:8cd5f8492271 90
LoRaToolbox 0:f843ca4a8e98 91 Radio::Standby();
LoRaToolbox 0:f843ca4a8e98 92 Radio::LoRaModemConfig(BW_KHZ, SPREADING_FACTOR, 1);
LoRaToolbox 0:f843ca4a8e98 93 Radio::SetChannel(CF_HZ);
LoRaToolbox 0:f843ca4a8e98 94
LoRaToolbox 1:8cd5f8492271 95 // preambleLen, fixLen, crcOn, invIQ
LoRaToolbox 1:8cd5f8492271 96
LoRaToolbox 0:f843ca4a8e98 97 Radio::LoRaPacketConfig(8, false, true, false);
LoRaToolbox 1:8cd5f8492271 98
LoRaToolbox 1:8cd5f8492271 99 // Start radio receiver, wait for packets from transmitter
LoRaToolbox 0:f843ca4a8e98 100
LoRaToolbox 0:f843ca4a8e98 101 Radio::Rx(0);
LoRaToolbox 0:f843ca4a8e98 102
LoRaToolbox 0:f843ca4a8e98 103 for (;;) {
LoRaToolbox 0:f843ca4a8e98 104 Radio::service();
LoRaToolbox 0:f843ca4a8e98 105 }
LoRaToolbox 0:f843ca4a8e98 106 }