Fork of the Simple Ping-Pong demo application between two SX1272MB2xAs demo board. It's now a simple application demonstrating simple Rx (Receive) from a SX1272 boards.

Dependencies:   SX1272Lib mbed

Fork of SX1272PingPong by Semtech

Committer:
Antoine38
Date:
Sat Apr 01 21:46:33 2017 +0000
Revision:
21:03a7d7429cd1
Parent:
20:17d8ea079085
Optimization of received message + conversion (magic formula)

Who changed what in which revision?

UserRevisionLine numberNew contents of line
GregCr 0:1ed39951ab7b 1 #include "mbed.h"
GregCr 4:5ece30264cd9 2 #include "main.h"
GregCr 13:edb9b443c1dd 3 #include "sx1272-hal.h"
GregCr 8:f956dee63a56 4 #include "debug.h"
GregCr 0:1ed39951ab7b 5
GregCr 0:1ed39951ab7b 6 /* Set this flag to '1' to display debug messages on the console */
GregCr 13:edb9b443c1dd 7 #define DEBUG_MESSAGE 1
GregCr 0:1ed39951ab7b 8
GregCr 0:1ed39951ab7b 9
Antoine38 14:6c312f8635fe 10 #define RF_FREQUENCY 868000000 // Hz
GregCr 0:1ed39951ab7b 11
Antoine38 14:6c312f8635fe 12 #define LORA_BANDWIDTH 2 // [0: 125 kHz,
Antoine38 15:dc867bef95d9 13 // 1: 250 kHz,
Antoine38 15:dc867bef95d9 14 // 2: 500 kHz,
Antoine38 15:dc867bef95d9 15 // 3: Reserved]
Antoine38 15:dc867bef95d9 16
Antoine38 14:6c312f8635fe 17 #define LORA_SPREADING_FACTOR 7 // [SF7..SF12]
Antoine38 14:6c312f8635fe 18 #define LORA_CODINGRATE 1 // [1: 4/5,
Antoine38 15:dc867bef95d9 19 // 2: 4/6,
Antoine38 15:dc867bef95d9 20 // 3: 4/7,
Antoine38 15:dc867bef95d9 21 // 4: 4/8]
Antoine38 15:dc867bef95d9 22
Antoine38 15:dc867bef95d9 23 #define LORA_PREAMBLE_LENGTH 8
Antoine38 14:6c312f8635fe 24 #define LORA_SYMBOL_TIMEOUT 5 // Symbols
Antoine38 14:6c312f8635fe 25 #define LORA_FIX_LENGTH_PAYLOAD_ON false
Antoine38 14:6c312f8635fe 26 #define LORA_FHSS_ENABLED false
Antoine38 14:6c312f8635fe 27 #define LORA_NB_SYMB_HOP 4
Antoine38 14:6c312f8635fe 28 #define LORA_IQ_INVERSION_ON false
Antoine38 14:6c312f8635fe 29 #define LORA_CRC_ENABLED true
GregCr 0:1ed39951ab7b 30
Antoine38 14:6c312f8635fe 31 #define RX_TIMEOUT_VALUE 3500000 // in us
Antoine38 21:03a7d7429cd1 32 #define BUFFER_SIZE 48 // Define the payload size here
GregCr 0:1ed39951ab7b 33
GregCr 3:8b9e2a4df4b5 34 DigitalOut led(LED1);
GregCr 3:8b9e2a4df4b5 35
Antoine38 21:03a7d7429cd1 36 /*
Antoine38 21:03a7d7429cd1 37 * Global variables declarations
Antoine38 21:03a7d7429cd1 38 */
Antoine38 21:03a7d7429cd1 39 typedef enum {
Antoine38 21:03a7d7429cd1 40 LOWPOWER = 0,
Antoine38 21:03a7d7429cd1 41
Antoine38 21:03a7d7429cd1 42 RX,
Antoine38 21:03a7d7429cd1 43 RX_TIMEOUT,
Antoine38 21:03a7d7429cd1 44 RX_ERROR
Antoine38 21:03a7d7429cd1 45 } AppStates_t;
Antoine38 21:03a7d7429cd1 46
Antoine38 21:03a7d7429cd1 47 volatile AppStates_t State = LOWPOWER;
Antoine38 21:03a7d7429cd1 48
mluis 10:7af820d1e1df 49 /*!
mluis 10:7af820d1e1df 50 * Radio events function pointer
mluis 10:7af820d1e1df 51 */
mluis 10:7af820d1e1df 52 static RadioEvents_t RadioEvents;
mluis 10:7af820d1e1df 53
mluis 10:7af820d1e1df 54 /*
mluis 10:7af820d1e1df 55 * Global variables declarations
mluis 10:7af820d1e1df 56 */
GregCr 13:edb9b443c1dd 57 SX1272MB2xAS Radio( NULL );
GregCr 0:1ed39951ab7b 58
Antoine38 21:03a7d7429cd1 59 uint16_t BufferSize = 0;
GregCr 0:1ed39951ab7b 60 uint8_t Buffer[BUFFER_SIZE];
GregCr 0:1ed39951ab7b 61
GregCr 5:f2431c4fe3bb 62 int16_t RssiValue = 0.0;
GregCr 5:f2431c4fe3bb 63 int8_t SnrValue = 0.0;
GregCr 0:1ed39951ab7b 64
Antoine38 14:6c312f8635fe 65 int main()
GregCr 0:1ed39951ab7b 66 {
Antoine38 17:c44bd9aea979 67 debug( "\n\n\r iGreenhouse Application - Receiver \n\n\r" );
mluis 10:7af820d1e1df 68
mluis 10:7af820d1e1df 69 // Initialize Radio driver
mluis 10:7af820d1e1df 70 RadioEvents.RxDone = OnRxDone;
mluis 10:7af820d1e1df 71 RadioEvents.RxError = OnRxError;
mluis 10:7af820d1e1df 72 RadioEvents.RxTimeout = OnRxTimeout;
mluis 10:7af820d1e1df 73 Radio.Init( &RadioEvents );
Antoine38 14:6c312f8635fe 74
GregCr 7:c1bbd6c56979 75 // verify the connection with the board
Antoine38 14:6c312f8635fe 76 while( Radio.Read( REG_VERSION ) == 0x00 ) {
GregCr 7:c1bbd6c56979 77 debug( "Radio could not be detected!\n\r", NULL );
GregCr 7:c1bbd6c56979 78 wait( 1 );
GregCr 2:59e108728d71 79 }
Antoine38 14:6c312f8635fe 80
GregCr 13:edb9b443c1dd 81 debug_if( ( DEBUG_MESSAGE & ( Radio.DetectBoardType( ) == SX1272MB2XAS ) ) , "\n\r > Board Type: SX1272MB2xAS < \n\r" );
GregCr 0:1ed39951ab7b 82
Antoine38 14:6c312f8635fe 83 Radio.SetChannel( RF_FREQUENCY );
Antoine38 14:6c312f8635fe 84
Antoine38 14:6c312f8635fe 85
GregCr 7:c1bbd6c56979 86 debug_if( LORA_FHSS_ENABLED, "\n\n\r > LORA FHSS Mode < \n\n\r");
GregCr 7:c1bbd6c56979 87 debug_if( !LORA_FHSS_ENABLED, "\n\n\r > LORA Mode < \n\n\r");
GregCr 7:c1bbd6c56979 88
Antoine38 14:6c312f8635fe 89 Radio.SetRxConfig( MODEM_LORA, LORA_BANDWIDTH, LORA_SPREADING_FACTOR,
Antoine38 14:6c312f8635fe 90 LORA_CODINGRATE, 0, LORA_PREAMBLE_LENGTH,
Antoine38 14:6c312f8635fe 91 LORA_SYMBOL_TIMEOUT, LORA_FIX_LENGTH_PAYLOAD_ON, 0,
Antoine38 14:6c312f8635fe 92 LORA_CRC_ENABLED, LORA_FHSS_ENABLED, LORA_NB_SYMB_HOP,
Antoine38 14:6c312f8635fe 93 LORA_IQ_INVERSION_ON, true );
GregCr 0:1ed39951ab7b 94
Antoine38 14:6c312f8635fe 95
Antoine38 21:03a7d7429cd1 96 debug_if( DEBUG_MESSAGE, "Starting listening loop\r\n\r\n" );
GregCr 0:1ed39951ab7b 97
GregCr 3:8b9e2a4df4b5 98 led = 0;
GregCr 0:1ed39951ab7b 99 Radio.Rx( RX_TIMEOUT_VALUE );
Antoine38 14:6c312f8635fe 100
Antoine38 14:6c312f8635fe 101 while( 1 ) {
Antoine38 21:03a7d7429cd1 102 switch( State ) {
Antoine38 21:03a7d7429cd1 103 case RX:
Antoine38 21:03a7d7429cd1 104 if( BufferSize > 0 ) {
Antoine38 21:03a7d7429cd1 105 debug_if( DEBUG_MESSAGE, "\r\n========\r\nNew Packet\r\n========\r\n" );
Antoine38 21:03a7d7429cd1 106 for(int i = 0; i < BufferSize; i++) {
Antoine38 21:03a7d7429cd1 107 debug_if( DEBUG_MESSAGE, "%x", Buffer[i]);
Antoine38 21:03a7d7429cd1 108 }
Antoine38 21:03a7d7429cd1 109 debug_if( DEBUG_MESSAGE, "\r\n");
Antoine38 21:03a7d7429cd1 110 retrieve_data( Buffer );
Antoine38 21:03a7d7429cd1 111 }
Antoine38 21:03a7d7429cd1 112 BufferSize = 0;
Antoine38 21:03a7d7429cd1 113 Radio.Rx( RX_TIMEOUT_VALUE );
Antoine38 21:03a7d7429cd1 114 State = LOWPOWER;
Antoine38 21:03a7d7429cd1 115 break;
Antoine38 21:03a7d7429cd1 116 case RX_TIMEOUT:
Antoine38 21:03a7d7429cd1 117 Radio.Rx( RX_TIMEOUT_VALUE );
Antoine38 21:03a7d7429cd1 118 State = LOWPOWER;
Antoine38 21:03a7d7429cd1 119 break;
Antoine38 21:03a7d7429cd1 120 case RX_ERROR:
Antoine38 21:03a7d7429cd1 121 // We have received a Packet with a CRC error
Antoine38 21:03a7d7429cd1 122 Radio.Rx( RX_TIMEOUT_VALUE );
Antoine38 21:03a7d7429cd1 123 State = LOWPOWER;
Antoine38 21:03a7d7429cd1 124 break;
Antoine38 21:03a7d7429cd1 125 case LOWPOWER:
Antoine38 21:03a7d7429cd1 126 break;
Antoine38 21:03a7d7429cd1 127 default:
Antoine38 21:03a7d7429cd1 128 State = LOWPOWER;
Antoine38 21:03a7d7429cd1 129 break;
Antoine38 20:17d8ea079085 130 }
GregCr 0:1ed39951ab7b 131 }
GregCr 0:1ed39951ab7b 132 }
GregCr 0:1ed39951ab7b 133
GregCr 4:5ece30264cd9 134 void OnRxDone( uint8_t *payload, uint16_t size, int16_t rssi, int8_t snr)
GregCr 0:1ed39951ab7b 135 {
GregCr 0:1ed39951ab7b 136 Radio.Sleep( );
GregCr 0:1ed39951ab7b 137 BufferSize = size;
GregCr 0:1ed39951ab7b 138 memcpy( Buffer, payload, BufferSize );
GregCr 0:1ed39951ab7b 139 RssiValue = rssi;
GregCr 0:1ed39951ab7b 140 SnrValue = snr;
Antoine38 21:03a7d7429cd1 141 State = RX;
Antoine38 21:03a7d7429cd1 142 debug_if( DEBUG_MESSAGE, "> OnRxDone %d \n\r", RssiValue );
GregCr 0:1ed39951ab7b 143 }
GregCr 0:1ed39951ab7b 144
GregCr 0:1ed39951ab7b 145 void OnRxTimeout( void )
GregCr 0:1ed39951ab7b 146 {
GregCr 0:1ed39951ab7b 147 Radio.Sleep( );
Antoine38 21:03a7d7429cd1 148 BufferSize = 0;
Antoine38 21:03a7d7429cd1 149 State = RX_TIMEOUT;
GregCr 7:c1bbd6c56979 150 debug_if( DEBUG_MESSAGE, "> OnRxTimeout\n\r" );
GregCr 0:1ed39951ab7b 151 }
GregCr 0:1ed39951ab7b 152
GregCr 0:1ed39951ab7b 153 void OnRxError( void )
GregCr 0:1ed39951ab7b 154 {
GregCr 0:1ed39951ab7b 155 Radio.Sleep( );
Antoine38 21:03a7d7429cd1 156 State = RX_ERROR;
GregCr 7:c1bbd6c56979 157 debug_if( DEBUG_MESSAGE, "> OnRxError\n\r" );
GregCr 0:1ed39951ab7b 158 }
GregCr 3:8b9e2a4df4b5 159
Antoine38 21:03a7d7429cd1 160 float u8_to_float(uint8_t x, bool isTemp)
Antoine38 21:03a7d7429cd1 161 {
Antoine38 21:03a7d7429cd1 162 float a = 0.0;
Antoine38 21:03a7d7429cd1 163 if ( isTemp ) {
Antoine38 21:03a7d7429cd1 164 a = 30.0;
Antoine38 21:03a7d7429cd1 165 }
Antoine38 21:03a7d7429cd1 166 return (x/255.0)*100.0 - a;
Antoine38 21:03a7d7429cd1 167 }
Antoine38 21:03a7d7429cd1 168
Antoine38 21:03a7d7429cd1 169 void retrieve_data(uint8_t * payload)
Antoine38 21:03a7d7429cd1 170 {
Antoine38 21:03a7d7429cd1 171 uint8_t measurements_type[4];
Antoine38 21:03a7d7429cd1 172 uint8_t greenhouse_info = payload[5];
Antoine38 21:03a7d7429cd1 173 printf("Greenhouse number: %d \r\n", greenhouse_info & 0x0F);
Antoine38 21:03a7d7429cd1 174 printf("Sensors position: %d \r\n", (greenhouse_info & 0x30) >> 4);
Antoine38 21:03a7d7429cd1 175
Antoine38 21:03a7d7429cd1 176 measurements_type[0] = payload[4] & 0x03;
Antoine38 21:03a7d7429cd1 177 measurements_type[1] = (payload[4] >> 2) & 0x03;
Antoine38 21:03a7d7429cd1 178 measurements_type[2] = (payload[4] >> 4) & 0x03;
Antoine38 21:03a7d7429cd1 179 measurements_type[3] = (payload[4] >> 6) & 0x03;
Antoine38 21:03a7d7429cd1 180
Antoine38 21:03a7d7429cd1 181 convert(payload[0], measurements_type[3]);
Antoine38 21:03a7d7429cd1 182 convert(payload[1], measurements_type[2]);
Antoine38 21:03a7d7429cd1 183 convert(payload[2], measurements_type[1]);
Antoine38 21:03a7d7429cd1 184 convert(payload[3], measurements_type[0]);
Antoine38 21:03a7d7429cd1 185 }
Antoine38 21:03a7d7429cd1 186
Antoine38 21:03a7d7429cd1 187 void convert(uint8_t m, uint8_t t)
Antoine38 21:03a7d7429cd1 188 {
Antoine38 21:03a7d7429cd1 189 if( t & 0x01 == 1 ) {
Antoine38 21:03a7d7429cd1 190 // Air
Antoine38 21:03a7d7429cd1 191 if( (t >> 1) & 0x01 == 1 ) {
Antoine38 21:03a7d7429cd1 192 // Humidity
Antoine38 21:03a7d7429cd1 193 printf("HumiA=%3.2f \r\n", (float) m);
Antoine38 21:03a7d7429cd1 194 } else {
Antoine38 21:03a7d7429cd1 195 // Temperature
Antoine38 21:03a7d7429cd1 196 printf("TempA=%3.2f \r\n", (float) m);
Antoine38 21:03a7d7429cd1 197 }
Antoine38 21:03a7d7429cd1 198 } else {
Antoine38 21:03a7d7429cd1 199 // Soil
Antoine38 21:03a7d7429cd1 200 if( (t >> 1) & 0x01 == 1 ) {
Antoine38 21:03a7d7429cd1 201 // Humidity
Antoine38 21:03a7d7429cd1 202 printf("HumiS=%3.2f \r\n", u8_to_float(m, false));
Antoine38 21:03a7d7429cd1 203 } else {
Antoine38 21:03a7d7429cd1 204 // Temperature
Antoine38 21:03a7d7429cd1 205 printf("TempS=%3.2f \r\n", u8_to_float(m, true));
Antoine38 21:03a7d7429cd1 206 }
Antoine38 21:03a7d7429cd1 207 }
Antoine38 21:03a7d7429cd1 208 }