Cory Mast / Mbed OS Simple_Tx_SX127x

Dependencies:   MbedJSONValue SX127x sx12xx_hal

Committer:
corymast
Date:
Tue Sep 24 23:09:39 2019 +0000
Revision:
7:803a185d7021
Parent:
6:83bedc32acaf
Child:
8:738ab96d41da
Added support for analog temperature sensors as well as helper functions to convert analog values to actual units.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
LoRaToolbox 0:44fb2db84011 1 #include "radio.h"
corymast 3:5e25ec621190 2 #include "mbed.h"
corymast 3:5e25ec621190 3 #include "platform/CircularBuffer.h"
corymast 3:5e25ec621190 4 #include "MbedJSONValue/MbedJSONValue.h"
LoRaToolbox 0:44fb2db84011 5
LoRaToolbox 1:b277f5a65c1c 6 // Semtech radio definitions for SX127x, SX126x and SX128x
LoRaToolbox 1:b277f5a65c1c 7
LoRaToolbox 0:44fb2db84011 8 #if defined(SX127x_H)
LoRaToolbox 0:44fb2db84011 9 #define BW_KHZ 500
LoRaToolbox 0:44fb2db84011 10 #define SPREADING_FACTOR 11
LoRaToolbox 0:44fb2db84011 11 #define CF_HZ 912000000
LoRaToolbox 0:44fb2db84011 12 #define TX_DBM 20
LoRaToolbox 0:44fb2db84011 13 #elif defined(SX126x_H)
LoRaToolbox 0:44fb2db84011 14 #define BW_KHZ 500
LoRaToolbox 0:44fb2db84011 15 #define SPREADING_FACTOR 10
LoRaToolbox 0:44fb2db84011 16 #define CF_HZ 915000000
LoRaToolbox 0:44fb2db84011 17 #define TX_DBM (Radio::chipType == CHIP_TYPE_SX1262 ? 20 : 14)
LoRaToolbox 0:44fb2db84011 18 #elif defined(SX128x_H)
LoRaToolbox 0:44fb2db84011 19 #define BW_KHZ 200
LoRaToolbox 0:44fb2db84011 20 #define SPREADING_FACTOR 7
LoRaToolbox 0:44fb2db84011 21 #define CF_HZ 2487000000
LoRaToolbox 0:44fb2db84011 22 #define TX_DBM 6
LoRaToolbox 0:44fb2db84011 23 #endif
LoRaToolbox 0:44fb2db84011 24
corymast 3:5e25ec621190 25 // The buffer size should match the tx_buf length in sx12xx.h
corymast 3:5e25ec621190 26 #define BUF_SIZE 256
corymast 3:5e25ec621190 27
LoRaToolbox 1:b277f5a65c1c 28 /******************** Setup radio transmitter ****************************/
LoRaToolbox 1:b277f5a65c1c 29
corymast 7:803a185d7021 30 // Analog Sensor setup
corymast 7:803a185d7021 31 AnalogIn sensor_light(A1);
corymast 7:803a185d7021 32 AnalogIn sensor_temp(A3);
corymast 7:803a185d7021 33 double light_val_analog;
corymast 7:803a185d7021 34 double temperature_analog;
corymast 5:a8b4e0857e8b 35
corymast 4:b2fc780be0b3 36 // Configure the User button as an interrupt
corymast 4:b2fc780be0b3 37 InterruptIn button(USER_BUTTON, PullUp);
corymast 4:b2fc780be0b3 38
corymast 4:b2fc780be0b3 39 // Initialize global variables
corymast 4:b2fc780be0b3 40 uint8_t seq = 0; // Set initial transmit sequence to 0
LoRaToolbox 0:44fb2db84011 41 volatile bool txDone;
corymast 3:5e25ec621190 42 CircularBuffer<uint8_t, BUF_SIZE> msg;
corymast 4:b2fc780be0b3 43 MbedJSONValue message;
corymast 4:b2fc780be0b3 44 Timer t;
LoRaToolbox 0:44fb2db84011 45
corymast 7:803a185d7021 46 // Forward declaration
corymast 7:803a185d7021 47 float convert_analog_light_resistance(double analog_val);
corymast 7:803a185d7021 48 float convert_analog_temperature(double analog_val);
corymast 7:803a185d7021 49
corymast 4:b2fc780be0b3 50 // Transmit Done Callback Handler
LoRaToolbox 0:44fb2db84011 51 void txDoneCB()
LoRaToolbox 0:44fb2db84011 52 {
LoRaToolbox 0:44fb2db84011 53 txDone = true;
corymast 4:b2fc780be0b3 54 printf("Tx Done.\r\n");
LoRaToolbox 0:44fb2db84011 55 }
LoRaToolbox 0:44fb2db84011 56
corymast 4:b2fc780be0b3 57 // Receive Done Callback Handler
LoRaToolbox 0:44fb2db84011 58 void rxDoneCB(uint8_t size, float rssi, float snr)
LoRaToolbox 0:44fb2db84011 59 {
corymast 4:b2fc780be0b3 60 printf("Rx Done.\r\n");
LoRaToolbox 0:44fb2db84011 61 }
LoRaToolbox 0:44fb2db84011 62
corymast 4:b2fc780be0b3 63 // Transmit Timeout Callback Handler
corymast 4:b2fc780be0b3 64 void txTimeoutCB()
corymast 4:b2fc780be0b3 65 {
corymast 4:b2fc780be0b3 66 printf("Tx Timeout.\r\n");
corymast 4:b2fc780be0b3 67 }
LoRaToolbox 1:b277f5a65c1c 68
corymast 4:b2fc780be0b3 69 // Define radio events for transmitter
LoRaToolbox 0:44fb2db84011 70 const RadioEvents_t rev = {
LoRaToolbox 0:44fb2db84011 71 /* Dio0_top_half */ NULL,
LoRaToolbox 0:44fb2db84011 72 /* TxDone_topHalf */ NULL,
LoRaToolbox 0:44fb2db84011 73 /* TxDone_botHalf */ txDoneCB,
corymast 4:b2fc780be0b3 74 /* TxTimeout */ txTimeoutCB,
LoRaToolbox 0:44fb2db84011 75 /* RxDone */ rxDoneCB,
LoRaToolbox 0:44fb2db84011 76 /* RxTimeout */ NULL,
LoRaToolbox 0:44fb2db84011 77 /* RxError */ NULL,
LoRaToolbox 0:44fb2db84011 78 /* FhssChangeChannel */NULL,
LoRaToolbox 0:44fb2db84011 79 /* CadDone */ NULL
LoRaToolbox 0:44fb2db84011 80 };
LoRaToolbox 0:44fb2db84011 81
corymast 4:b2fc780be0b3 82 /**
corymast 4:b2fc780be0b3 83 * Interrupt Handler for the User button.
corymast 4:b2fc780be0b3 84 */
corymast 4:b2fc780be0b3 85 void button_send_json()
corymast 4:b2fc780be0b3 86 {
corymast 4:b2fc780be0b3 87 string s;
corymast 4:b2fc780be0b3 88 seq++;
corymast 4:b2fc780be0b3 89
corymast 4:b2fc780be0b3 90 //fill the object
corymast 4:b2fc780be0b3 91 message["btn_count"] = seq;
corymast 4:b2fc780be0b3 92 message["btn_timer"] = t.read();
corymast 7:803a185d7021 93 message["light_val"] = light_val_analog;
corymast 7:803a185d7021 94 message["light_resistance"] = convert_analog_light_resistance(light_val_analog);
corymast 7:803a185d7021 95 message["temperature"] = convert_analog_temperature(temperature_analog);
corymast 4:b2fc780be0b3 96 message["my_str"] = "Hello World!";
corymast 4:b2fc780be0b3 97 message["my_boolean"] = false;
corymast 4:b2fc780be0b3 98
corymast 4:b2fc780be0b3 99 //serialize it into a JSON string
corymast 4:b2fc780be0b3 100 s = message.serialize();
corymast 4:b2fc780be0b3 101 printf("json: %s\r\n", s.c_str());
corymast 4:b2fc780be0b3 102
corymast 4:b2fc780be0b3 103 memcpy(Radio::radio.tx_buf, s.c_str(), s.length());
corymast 4:b2fc780be0b3 104 txDone = false;
corymast 4:b2fc780be0b3 105 Radio::Send(s.length(), 0, 0, 0); // begin transmission of payload
corymast 4:b2fc780be0b3 106 t.reset();
corymast 4:b2fc780be0b3 107 }
corymast 4:b2fc780be0b3 108
corymast 4:b2fc780be0b3 109 /**
corymast 4:b2fc780be0b3 110 * Function for initializing the SX127x radio.
corymast 4:b2fc780be0b3 111 */
corymast 3:5e25ec621190 112 void radio_init()
corymast 3:5e25ec621190 113 {
LoRaToolbox 1:b277f5a65c1c 114 // Start radio transmitter after POR or reset
LoRaToolbox 0:44fb2db84011 115 Radio::Init(&rev);
LoRaToolbox 0:44fb2db84011 116
LoRaToolbox 1:b277f5a65c1c 117 //Set radio properties for transmitter
LoRaToolbox 0:44fb2db84011 118 Radio::Standby();
LoRaToolbox 0:44fb2db84011 119 Radio::LoRaModemConfig(BW_KHZ, SPREADING_FACTOR, 1);
LoRaToolbox 0:44fb2db84011 120 Radio::SetChannel(CF_HZ);
LoRaToolbox 0:44fb2db84011 121
LoRaToolbox 1:b277f5a65c1c 122 // Set transmitter output power
LoRaToolbox 0:44fb2db84011 123 Radio::set_tx_dbm(TX_DBM);
LoRaToolbox 0:44fb2db84011 124
LoRaToolbox 1:b277f5a65c1c 125 // Setup transmit packet payload -> preambleLen, fixLen, crcOn, invIQ
LoRaToolbox 0:44fb2db84011 126 Radio::LoRaPacketConfig(8, false, true, false);
corymast 3:5e25ec621190 127 }
corymast 3:5e25ec621190 128
corymast 7:803a185d7021 129 float convert_analog_light_resistance(double analog_val)
corymast 7:803a185d7021 130 {
corymast 7:803a185d7021 131 float Rsensor=(float)(1023-analog_val)*10/analog_val;
corymast 7:803a185d7021 132 return Rsensor;
corymast 7:803a185d7021 133 }
corymast 7:803a185d7021 134
corymast 7:803a185d7021 135 float convert_analog_temperature(double analog_val)
corymast 7:803a185d7021 136 {
corymast 7:803a185d7021 137 double a = analog_val*1023;
corymast 7:803a185d7021 138 double resistance=(float)(1023-a)*10000/a;
corymast 7:803a185d7021 139 double temperature=1/(log(resistance/10000)/3975+1/298.15)-276.05;
corymast 7:803a185d7021 140 double temperature_f = (9.0*temperature)/5.0 + 32.0;
corymast 7:803a185d7021 141 return temperature_f;
corymast 7:803a185d7021 142 }
corymast 7:803a185d7021 143
corymast 3:5e25ec621190 144 int main()
corymast 3:5e25ec621190 145 {
corymast 3:5e25ec621190 146 printf("\r\nreset-tx \n");
corymast 4:b2fc780be0b3 147 button.fall(&button_send_json);
LoRaToolbox 0:44fb2db84011 148
corymast 3:5e25ec621190 149 radio_init();
LoRaToolbox 0:44fb2db84011 150
corymast 4:b2fc780be0b3 151 // Start a timer to track time between button presses
corymast 4:b2fc780be0b3 152 t.start();
LoRaToolbox 0:44fb2db84011 153
corymast 4:b2fc780be0b3 154 for (;;)
corymast 4:b2fc780be0b3 155 {
corymast 4:b2fc780be0b3 156 // Transmit message on button press, service radio until tx complete.
corymast 4:b2fc780be0b3 157 while (!txDone)
corymast 4:b2fc780be0b3 158 {
corymast 4:b2fc780be0b3 159 Radio::service();
corymast 7:803a185d7021 160 light_val_analog = sensor_light.read();
corymast 7:803a185d7021 161 temperature_analog = sensor_temp.read();
LoRaToolbox 1:b277f5a65c1c 162 }
corymast 4:b2fc780be0b3 163 }
LoRaToolbox 0:44fb2db84011 164 }