LoRa Gateway, interface to Raspberry - Working ok

Dependencies:   BufferedSerial SX1276GenericLib mbed

Fork of DISCO-L072CZ-LRWAN1_LoRa_server by Santiago Gil

Committer:
sagilar
Date:
Mon Oct 08 16:10:32 2018 +0000
Revision:
14:fe73b77cf1c0
Parent:
12:fdf2b9912649
EIoT LoRa Gateway

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Helmut64 0:c43b6919ae15 1 /*
Helmut64 0:c43b6919ae15 2 * This file contains a copy of the master content sx1276PingPong
Helmut64 0:c43b6919ae15 3 * with adaption for the SX1276Generic environment
Helmut64 0:c43b6919ae15 4 * (c) 2017 Helmut Tschemernjak
Helmut64 0:c43b6919ae15 5 * 30826 Garbsen (Hannover) Germany
Helmut64 0:c43b6919ae15 6 */
sagilar 12:fdf2b9912649 7
Helmut64 0:c43b6919ae15 8 #include "mbed.h"
Helmut64 0:c43b6919ae15 9 #include "PinMap.h"
Helmut64 0:c43b6919ae15 10 #include "GenericPingPong.h"
Helmut64 0:c43b6919ae15 11 #include "sx1276-mbed-hal.h"
Helmut64 0:c43b6919ae15 12 #include "main.h"
sagilar 12:fdf2b9912649 13 #include <string>
Helmut64 0:c43b6919ae15 14
Helmut64 0:c43b6919ae15 15 #ifdef FEATURE_LORA
Helmut64 0:c43b6919ae15 16
Helmut64 0:c43b6919ae15 17 /* Set this flag to '1' to display debug messages on the console */
Helmut64 0:c43b6919ae15 18 #define DEBUG_MESSAGE 1
Helmut64 0:c43b6919ae15 19
Helmut64 0:c43b6919ae15 20 /* Set this flag to '1' to use the LoRa modulation or to '0' to use FSK modulation */
Helmut64 0:c43b6919ae15 21 #define USE_MODEM_LORA 1
Helmut64 0:c43b6919ae15 22 #define USE_MODEM_FSK !USE_MODEM_LORA
sagilar 12:fdf2b9912649 23 #define RF_FREQUENCY RF_FREQUENCY_915_0 // Hz
sagilar 12:fdf2b9912649 24 #define TX_OUTPUT_POWER 20 // 20 dBm
Helmut64 0:c43b6919ae15 25
Helmut64 0:c43b6919ae15 26 #if USE_MODEM_LORA == 1
Helmut64 0:c43b6919ae15 27
Helmut64 7:6a8a82bfb0c6 28 #define LORA_BANDWIDTH 125000 // LoRa default, details in SX1276::BandwidthMap
Helmut64 0:c43b6919ae15 29 #define LORA_SPREADING_FACTOR LORA_SF7
Helmut64 0:c43b6919ae15 30 #define LORA_CODINGRATE LORA_ERROR_CODING_RATE_4_5
Helmut64 0:c43b6919ae15 31
Helmut64 0:c43b6919ae15 32 #define LORA_PREAMBLE_LENGTH 8 // Same for Tx and Rx
Helmut64 0:c43b6919ae15 33 #define LORA_SYMBOL_TIMEOUT 5 // Symbols
Helmut64 0:c43b6919ae15 34 #define LORA_FIX_LENGTH_PAYLOAD_ON false
sagilar 12:fdf2b9912649 35 #define LORA_FHSS_ENABLED true
sagilar 12:fdf2b9912649 36 #define LORA_NB_SYMB_HOP 4
Helmut64 0:c43b6919ae15 37 #define LORA_IQ_INVERSION_ON false
Helmut64 0:c43b6919ae15 38 #define LORA_CRC_ENABLED true
sagilar 12:fdf2b9912649 39
Helmut64 0:c43b6919ae15 40 #elif USE_MODEM_FSK == 1
Helmut64 0:c43b6919ae15 41
Helmut64 0:c43b6919ae15 42 #define FSK_FDEV 25000 // Hz
Helmut64 0:c43b6919ae15 43 #define FSK_DATARATE 19200 // bps
Helmut64 0:c43b6919ae15 44 #define FSK_BANDWIDTH 50000 // Hz
Helmut64 0:c43b6919ae15 45 #define FSK_AFC_BANDWIDTH 83333 // Hz
Helmut64 0:c43b6919ae15 46 #define FSK_PREAMBLE_LENGTH 5 // Same for Tx and Rx
Helmut64 0:c43b6919ae15 47 #define FSK_FIX_LENGTH_PAYLOAD_ON false
Helmut64 0:c43b6919ae15 48 #define FSK_CRC_ENABLED true
sagilar 12:fdf2b9912649 49
Helmut64 0:c43b6919ae15 50 #else
sagilar 12:fdf2b9912649 51 #error "Please define a modem in the compiler options."
sagilar 12:fdf2b9912649 52 #endif
Helmut64 0:c43b6919ae15 53
Helmut64 0:c43b6919ae15 54
Helmut64 0:c43b6919ae15 55 #define RX_TIMEOUT_VALUE 3500 // in ms
Helmut64 0:c43b6919ae15 56
Helmut64 0:c43b6919ae15 57 //#define BUFFER_SIZE 32 // Define the payload size here
sagilar 14:fe73b77cf1c0 58 #define BUFFER_SIZE 256 // Define the payload size here
Helmut64 0:c43b6919ae15 59
Helmut64 0:c43b6919ae15 60 /*
Helmut64 0:c43b6919ae15 61 * Global variables declarations
Helmut64 0:c43b6919ae15 62 */
sagilar 12:fdf2b9912649 63 typedef enum {
Helmut64 0:c43b6919ae15 64 LOWPOWER = 0,
Helmut64 0:c43b6919ae15 65 IDLE,
sagilar 12:fdf2b9912649 66
Helmut64 0:c43b6919ae15 67 RX,
Helmut64 0:c43b6919ae15 68 RX_TIMEOUT,
Helmut64 0:c43b6919ae15 69 RX_ERROR,
sagilar 12:fdf2b9912649 70
Helmut64 0:c43b6919ae15 71 TX,
Helmut64 0:c43b6919ae15 72 TX_TIMEOUT,
sagilar 12:fdf2b9912649 73
Helmut64 0:c43b6919ae15 74 CAD,
Helmut64 0:c43b6919ae15 75 CAD_DONE
Helmut64 0:c43b6919ae15 76 } AppStates_t;
Helmut64 0:c43b6919ae15 77
Helmut64 0:c43b6919ae15 78 volatile AppStates_t State = LOWPOWER;
Helmut64 0:c43b6919ae15 79
Helmut64 0:c43b6919ae15 80 /*!
Helmut64 0:c43b6919ae15 81 * Radio events function pointer
Helmut64 0:c43b6919ae15 82 */
Helmut64 0:c43b6919ae15 83 static RadioEvents_t RadioEvents;
Helmut64 0:c43b6919ae15 84
Helmut64 0:c43b6919ae15 85 /*
Helmut64 0:c43b6919ae15 86 * Global variables declarations
Helmut64 0:c43b6919ae15 87 */
Helmut64 0:c43b6919ae15 88 SX1276Generic *Radio;
Helmut64 0:c43b6919ae15 89
Helmut64 0:c43b6919ae15 90
sagilar 12:fdf2b9912649 91 //const uint8_t PingMsg[] = { 0xff, 0xff, 0x00, 0x00, 'P', 'I', 'N', 'G'};// "PING";
sagilar 12:fdf2b9912649 92 //const uint8_t PongMsg[] = { 0xff, 0xff, 0x00, 0x00, 'P', 'O', 'N', 'G'};// "PONG";
sagilar 12:fdf2b9912649 93 //const uint8_t PingMsg[] = { 'M', 'S', 'J', ' ','G','I','L'};// "PING";
sagilar 12:fdf2b9912649 94 //const uint8_t PongMsg[] = { 'R', 'P', 'T', 'A',' ','G','I','L'};// "PONG";
Helmut64 0:c43b6919ae15 95
sagilar 12:fdf2b9912649 96 //construccion de la trama
sagilar 12:fdf2b9912649 97 const char EUI[] = "0A01";// 4 bytes que definen el identificador del dispositivo (copiar los bytes en string)
sagilar 12:fdf2b9912649 98 const char AppEUI[] = "AAAA";// 4 bytes que definen el identificador de la aplicacion (copiar los bytes en string)
sagilar 12:fdf2b9912649 99 const char AppKey[] = "1A1B";// 4 bytes que definen la clave de la aplicacion (copiar los bytes en string) - El protocolo LoRaWAN establece la clave de 16 bytes pero para efectos de prueba se hara de 4
sagilar 14:fe73b77cf1c0 100 char MsgTX[256] = "";// Mensaje de transmision, se pueden usar los 52 bytes faltantes para completar el payload de 64 bytes. Se puede poner directamente en string.
sagilar 14:fe73b77cf1c0 101 char MsgRX[256] = "";// Mensaje de recepcion, carga el payload entrante a esta cadena.
sagilar 12:fdf2b9912649 102 char MsgRet[] = "RECIBIDO";
sagilar 12:fdf2b9912649 103 char DestEUI[4] = "";
sagilar 12:fdf2b9912649 104 char euiAux[4]="";
Helmut64 0:c43b6919ae15 105 uint16_t BufferSize = BUFFER_SIZE;
Helmut64 0:c43b6919ae15 106 uint8_t *Buffer;
sagilar 12:fdf2b9912649 107 string msjDeco="";
sagilar 12:fdf2b9912649 108 char *retParse;
sagilar 12:fdf2b9912649 109 char *srcEUI;
sagilar 12:fdf2b9912649 110 char *msjDestEUI;
sagilar 12:fdf2b9912649 111 char *msjContent;
sagilar 12:fdf2b9912649 112 Serial serRPi(PA_9, PA_10);
Helmut64 0:c43b6919ae15 113
Helmut64 0:c43b6919ae15 114 DigitalOut *led3;
Helmut64 0:c43b6919ae15 115
Helmut64 0:c43b6919ae15 116
sagilar 12:fdf2b9912649 117 int SX1276PingPong()
Helmut64 0:c43b6919ae15 118 {
Helmut64 0:c43b6919ae15 119 #if( defined ( TARGET_KL25Z ) || defined ( TARGET_LPC11U6X ) )
Helmut64 0:c43b6919ae15 120 DigitalOut *led = new DigitalOut(LED2);
bcostm 11:9d7409ebfa57 121 #elif defined(TARGET_NUCLEO_L073RZ) || defined(TARGET_DISCO_L072CZ_LRWAN1)
Helmut64 0:c43b6919ae15 122 DigitalOut *led = new DigitalOut(LED4); // RX red
Helmut64 0:c43b6919ae15 123 led3 = new DigitalOut(LED3); // TX blue
Helmut64 0:c43b6919ae15 124 #else
Helmut64 0:c43b6919ae15 125 DigitalOut *led = new DigitalOut(LED1);
Helmut64 0:c43b6919ae15 126 led3 = led;
Helmut64 0:c43b6919ae15 127 #endif
sagilar 12:fdf2b9912649 128
Helmut64 0:c43b6919ae15 129 Buffer = new uint8_t[BUFFER_SIZE];
Helmut64 0:c43b6919ae15 130 *led3 = 1;
Helmut64 0:c43b6919ae15 131
Helmut64 0:c43b6919ae15 132 #ifdef B_L072Z_LRWAN1_LORA
Helmut64 0:c43b6919ae15 133 Radio = new SX1276Generic(NULL, MURATA_SX1276,
sagilar 12:fdf2b9912649 134 LORA_SPI_MOSI, LORA_SPI_MISO, LORA_SPI_SCLK, LORA_CS, LORA_RESET,
sagilar 12:fdf2b9912649 135 LORA_DIO0, LORA_DIO1, LORA_DIO2, LORA_DIO3, LORA_DIO4, LORA_DIO5,
sagilar 12:fdf2b9912649 136 LORA_ANT_RX, LORA_ANT_TX, LORA_ANT_BOOST, LORA_TCXO);
Helmut64 0:c43b6919ae15 137 #else // RFM95
Helmut64 0:c43b6919ae15 138 Radio = new SX1276Generic(NULL, RFM95_SX1276,
sagilar 12:fdf2b9912649 139 LORA_SPI_MOSI, LORA_SPI_MISO, LORA_SPI_SCLK, LORA_CS, LORA_RESET,
sagilar 12:fdf2b9912649 140 LORA_DIO0, LORA_DIO1, LORA_DIO2, LORA_DIO3, LORA_DIO4, LORA_DIO5);
Helmut64 0:c43b6919ae15 141
Helmut64 0:c43b6919ae15 142 #endif
sagilar 12:fdf2b9912649 143 //serRPi = new BufferedSerial(PA_9, PA_10);
sagilar 12:fdf2b9912649 144 serRPi.baud(115200);
sagilar 12:fdf2b9912649 145 serRPi.format(8);
Helmut64 0:c43b6919ae15 146 uint8_t i;
sagilar 12:fdf2b9912649 147 bool isServer = true;
sagilar 12:fdf2b9912649 148
sagilar 12:fdf2b9912649 149 dprintf("Aplicacion de comunicacion LoRa punto a punto" );
sagilar 12:fdf2b9912649 150 dprintf("Frecuencia: %.1f", (double)RF_FREQUENCY/1000000.0);
Helmut64 0:c43b6919ae15 151 dprintf("TXPower: %d dBm", TX_OUTPUT_POWER);
Helmut64 0:c43b6919ae15 152 #if USE_MODEM_LORA == 1
Helmut64 8:3b0d7b4ff28f 153 dprintf("Bandwidth: %d Hz", LORA_BANDWIDTH);
Helmut64 0:c43b6919ae15 154 dprintf("Spreading factor: SF%d", LORA_SPREADING_FACTOR);
Helmut64 0:c43b6919ae15 155 #elif USE_MODEM_FSK == 1
Helmut64 0:c43b6919ae15 156 dprintf("Bandwidth: %d kHz", FSK_BANDWIDTH);
Helmut64 0:c43b6919ae15 157 dprintf("Baudrate: %d", FSK_DATARATE);
Helmut64 0:c43b6919ae15 158 #endif
Helmut64 0:c43b6919ae15 159 // Initialize Radio driver
Helmut64 0:c43b6919ae15 160 RadioEvents.TxDone = OnTxDone;
Helmut64 0:c43b6919ae15 161 RadioEvents.RxDone = OnRxDone;
Helmut64 0:c43b6919ae15 162 RadioEvents.RxError = OnRxError;
Helmut64 0:c43b6919ae15 163 RadioEvents.TxTimeout = OnTxTimeout;
sagilar 12:fdf2b9912649 164 RadioEvents.RxTimeout = OnRxTimeout;
Helmut64 6:1b598b0e52e4 165 if (Radio->Init( &RadioEvents ) == false) {
Helmut64 6:1b598b0e52e4 166 while(1) {
sagilar 12:fdf2b9912649 167 dprintf("Radio could not be detected!");
sagilar 12:fdf2b9912649 168 wait( 1 );
Helmut64 6:1b598b0e52e4 169 }
Helmut64 0:c43b6919ae15 170 }
Helmut64 6:1b598b0e52e4 171
sagilar 12:fdf2b9912649 172
Helmut64 0:c43b6919ae15 173 switch(Radio->DetectBoardType()) {
Helmut64 0:c43b6919ae15 174 case SX1276MB1LAS:
Helmut64 0:c43b6919ae15 175 if (DEBUG_MESSAGE)
Helmut64 0:c43b6919ae15 176 dprintf(" > Board Type: SX1276MB1LAS <");
Helmut64 0:c43b6919ae15 177 break;
Helmut64 0:c43b6919ae15 178 case SX1276MB1MAS:
Helmut64 0:c43b6919ae15 179 if (DEBUG_MESSAGE)
Helmut64 0:c43b6919ae15 180 dprintf(" > Board Type: SX1276MB1LAS <");
Helmut64 0:c43b6919ae15 181 case MURATA_SX1276:
Helmut64 0:c43b6919ae15 182 if (DEBUG_MESSAGE)
sagilar 12:fdf2b9912649 183 dprintf(" > Board Type: MURATA_SX1276_STM32L0 <");
Helmut64 0:c43b6919ae15 184 break;
Helmut64 0:c43b6919ae15 185 case RFM95_SX1276:
Helmut64 0:c43b6919ae15 186 if (DEBUG_MESSAGE)
Helmut64 0:c43b6919ae15 187 dprintf(" > HopeRF RFM95xx <");
Helmut64 0:c43b6919ae15 188 break;
Helmut64 0:c43b6919ae15 189 default:
Helmut64 0:c43b6919ae15 190 dprintf(" > Board Type: unknown <");
Helmut64 0:c43b6919ae15 191 }
Helmut64 0:c43b6919ae15 192
sagilar 12:fdf2b9912649 193 Radio->SetChannel(RF_FREQUENCY );
Helmut64 0:c43b6919ae15 194
Helmut64 0:c43b6919ae15 195 #if USE_MODEM_LORA == 1
sagilar 12:fdf2b9912649 196
Helmut64 0:c43b6919ae15 197 if (LORA_FHSS_ENABLED)
Helmut64 0:c43b6919ae15 198 dprintf(" > LORA FHSS Mode <");
Helmut64 0:c43b6919ae15 199 if (!LORA_FHSS_ENABLED)
Helmut64 0:c43b6919ae15 200 dprintf(" > LORA Mode <");
Helmut64 0:c43b6919ae15 201
Helmut64 0:c43b6919ae15 202 Radio->SetTxConfig( MODEM_LORA, TX_OUTPUT_POWER, 0, LORA_BANDWIDTH,
sagilar 12:fdf2b9912649 203 LORA_SPREADING_FACTOR, LORA_CODINGRATE,
sagilar 12:fdf2b9912649 204 LORA_PREAMBLE_LENGTH, LORA_FIX_LENGTH_PAYLOAD_ON,
sagilar 12:fdf2b9912649 205 LORA_CRC_ENABLED, LORA_FHSS_ENABLED, LORA_NB_SYMB_HOP,
sagilar 12:fdf2b9912649 206 LORA_IQ_INVERSION_ON, 2000 );
sagilar 12:fdf2b9912649 207
Helmut64 0:c43b6919ae15 208 Radio->SetRxConfig( MODEM_LORA, LORA_BANDWIDTH, LORA_SPREADING_FACTOR,
sagilar 12:fdf2b9912649 209 LORA_CODINGRATE, 0, LORA_PREAMBLE_LENGTH,
sagilar 12:fdf2b9912649 210 LORA_SYMBOL_TIMEOUT, LORA_FIX_LENGTH_PAYLOAD_ON, 0,
sagilar 12:fdf2b9912649 211 LORA_CRC_ENABLED, LORA_FHSS_ENABLED, LORA_NB_SYMB_HOP,
sagilar 12:fdf2b9912649 212 LORA_IQ_INVERSION_ON, true );
sagilar 12:fdf2b9912649 213
Helmut64 0:c43b6919ae15 214 #elif USE_MODEM_FSK == 1
Helmut64 0:c43b6919ae15 215
Helmut64 0:c43b6919ae15 216 dprintf(" > FSK Mode <");
Helmut64 0:c43b6919ae15 217 Radio->SetTxConfig( MODEM_FSK, TX_OUTPUT_POWER, FSK_FDEV, 0,
sagilar 12:fdf2b9912649 218 FSK_DATARATE, 0,
sagilar 12:fdf2b9912649 219 FSK_PREAMBLE_LENGTH, FSK_FIX_LENGTH_PAYLOAD_ON,
sagilar 12:fdf2b9912649 220 FSK_CRC_ENABLED, 0, 0, 0, 2000 );
sagilar 12:fdf2b9912649 221
Helmut64 0:c43b6919ae15 222 Radio->SetRxConfig( MODEM_FSK, FSK_BANDWIDTH, FSK_DATARATE,
sagilar 12:fdf2b9912649 223 0, FSK_AFC_BANDWIDTH, FSK_PREAMBLE_LENGTH,
sagilar 12:fdf2b9912649 224 0, FSK_FIX_LENGTH_PAYLOAD_ON, 0, FSK_CRC_ENABLED,
sagilar 12:fdf2b9912649 225 0, 0, false, true );
sagilar 12:fdf2b9912649 226
Helmut64 0:c43b6919ae15 227 #else
Helmut64 0:c43b6919ae15 228
Helmut64 0:c43b6919ae15 229 #error "Please define a modem in the compiler options."
Helmut64 0:c43b6919ae15 230
Helmut64 0:c43b6919ae15 231 #endif
Helmut64 0:c43b6919ae15 232
sagilar 12:fdf2b9912649 233 if (DEBUG_MESSAGE)
sagilar 12:fdf2b9912649 234 dprintf("Inicializando el servidor");
sagilar 12:fdf2b9912649 235 dprintf("EUI (ID): %s",EUI);
sagilar 12:fdf2b9912649 236
Helmut64 0:c43b6919ae15 237 Radio->Rx( RX_TIMEOUT_VALUE );
sagilar 12:fdf2b9912649 238
sagilar 12:fdf2b9912649 239 while( 1 ) {
Helmut64 0:c43b6919ae15 240 #ifdef TARGET_STM32L4
Helmut64 0:c43b6919ae15 241 WatchDogUpdate();
Helmut64 0:c43b6919ae15 242 #endif
sagilar 12:fdf2b9912649 243
sagilar 12:fdf2b9912649 244 switch( State ) {
sagilar 12:fdf2b9912649 245 case RX:
sagilar 12:fdf2b9912649 246 //dprintf( "Recibiendo informacion" );
sagilar 12:fdf2b9912649 247 *led = !*led;
sagilar 14:fe73b77cf1c0 248 dprintf("Mensaje para depurar: %s",MsgRX);
sagilar 12:fdf2b9912649 249 //strcpy(DestEUI,"A010");
sagilar 12:fdf2b9912649 250 msjDeco=MsgRX;
sagilar 12:fdf2b9912649 251 //vector<string> x = split(msjDeco, '|');
sagilar 12:fdf2b9912649 252 splitOnPosition(MsgRX, 0);
sagilar 12:fdf2b9912649 253 //msjDestEUI=splitOnPosition(MsgRX, 3);
sagilar 12:fdf2b9912649 254 //msjContent=splitOnPosition(MsgRX, 4);
sagilar 12:fdf2b9912649 255
sagilar 12:fdf2b9912649 256 dprintf("Source EUI: %s, Destination EUI: %s, Content: %s",srcEUI,msjDestEUI,msjContent);
sagilar 12:fdf2b9912649 257 if(strcmp(EUI,msjDestEUI) == 0) {
sagilar 12:fdf2b9912649 258 dprintf("Mismo EUI, Soy el destinatario");
sagilar 12:fdf2b9912649 259
sagilar 12:fdf2b9912649 260 } else {
sagilar 12:fdf2b9912649 261 dprintf("Diferente EUI, ignorar mensaje");
sagilar 12:fdf2b9912649 262 Radio->Rx( RX_TIMEOUT_VALUE );
sagilar 12:fdf2b9912649 263 wait_ms( 500 );
sagilar 12:fdf2b9912649 264 *led = !*led;
sagilar 12:fdf2b9912649 265 State = LOWPOWER;
sagilar 12:fdf2b9912649 266 break;
Helmut64 0:c43b6919ae15 267 }
sagilar 12:fdf2b9912649 268 strcpy(MsgRet,"RECIBIDO");
sagilar 12:fdf2b9912649 269 strcpy(MsgTX, EUI);
sagilar 12:fdf2b9912649 270 strcat(MsgTX, "|");
sagilar 12:fdf2b9912649 271 strcat(MsgTX, AppEUI);
sagilar 12:fdf2b9912649 272 strcat(MsgTX, "|");
sagilar 12:fdf2b9912649 273 strcat(MsgTX, AppKey);
sagilar 12:fdf2b9912649 274 strcat(MsgTX, "|");
sagilar 12:fdf2b9912649 275 strcat(MsgTX, srcEUI);
sagilar 12:fdf2b9912649 276 strcat(MsgTX, "|");
sagilar 12:fdf2b9912649 277 strcat(MsgTX, MsgRet);
sagilar 12:fdf2b9912649 278 //strcat(MsgTX, ";");
sagilar 12:fdf2b9912649 279 //MsgRX = "RECIBIDO";
sagilar 12:fdf2b9912649 280 memcpy(Buffer, MsgTX, sizeof(MsgTX));
sagilar 12:fdf2b9912649 281 // Se llena el buffer con la informacion a enviar
sagilar 12:fdf2b9912649 282 for( i = sizeof(MsgTX); i < BufferSize; i++ ) {
sagilar 12:fdf2b9912649 283 Buffer[i] = i - sizeof(MsgTX);
sagilar 12:fdf2b9912649 284 }
sagilar 12:fdf2b9912649 285 //dprintf("enviando mensaje a raspberry");
sagilar 12:fdf2b9912649 286 serRPi.printf("contenido: %s \r\n",msjContent);
sagilar 12:fdf2b9912649 287 Radio->Send( Buffer, BufferSize );
sagilar 12:fdf2b9912649 288 Radio->Rx( RX_TIMEOUT_VALUE );
sagilar 12:fdf2b9912649 289 wait_ms( 500 );
sagilar 12:fdf2b9912649 290
sagilar 12:fdf2b9912649 291 /*Enviar contenido a la raspberry por puerto serial*/
sagilar 12:fdf2b9912649 292
sagilar 12:fdf2b9912649 293
sagilar 12:fdf2b9912649 294 /***************************************************/
sagilar 12:fdf2b9912649 295
sagilar 12:fdf2b9912649 296 *led = !*led;
sagilar 12:fdf2b9912649 297 State = LOWPOWER;
sagilar 12:fdf2b9912649 298 break;
sagilar 12:fdf2b9912649 299 case TX:
sagilar 12:fdf2b9912649 300 *led3 = !*led3;
sagilar 12:fdf2b9912649 301 if( isServer == true ) {
sagilar 12:fdf2b9912649 302 //dprintf("Enviando respuesta al nodo" );
sagilar 12:fdf2b9912649 303 //dprintf("Mensaje a enviar: %s",MsgTX);
Helmut64 0:c43b6919ae15 304 }
sagilar 12:fdf2b9912649 305 wait_ms( 500 );
sagilar 12:fdf2b9912649 306 *led3 = !*led3;
sagilar 12:fdf2b9912649 307 Radio->Rx( RX_TIMEOUT_VALUE );
sagilar 12:fdf2b9912649 308 State = LOWPOWER;
sagilar 12:fdf2b9912649 309 break;
sagilar 12:fdf2b9912649 310 case RX_TIMEOUT:
sagilar 12:fdf2b9912649 311
sagilar 12:fdf2b9912649 312 Radio->Rx( RX_TIMEOUT_VALUE );
sagilar 12:fdf2b9912649 313 State = LOWPOWER;
sagilar 12:fdf2b9912649 314 break;
sagilar 12:fdf2b9912649 315 case RX_ERROR:
sagilar 12:fdf2b9912649 316 // We have received a Packet with a CRC error, send reply as if packet was correct
sagilar 14:fe73b77cf1c0 317 /*strcpy(MsgRet,"ERROR");
sagilar 12:fdf2b9912649 318 strcpy(MsgTX, EUI);
sagilar 12:fdf2b9912649 319 strcat(MsgTX, "|");
sagilar 12:fdf2b9912649 320 strcat(MsgTX, AppEUI);
sagilar 12:fdf2b9912649 321 strcat(MsgTX, "|");
sagilar 12:fdf2b9912649 322 strcat(MsgTX, AppKey);
sagilar 12:fdf2b9912649 323 strcat(MsgTX, "|");
sagilar 12:fdf2b9912649 324 strcat(MsgTX, "A010");
sagilar 12:fdf2b9912649 325 strcat(MsgTX, "|");
sagilar 12:fdf2b9912649 326 strcat(MsgTX, MsgRet);
sagilar 12:fdf2b9912649 327 //MsgRX = "ERROR";
sagilar 12:fdf2b9912649 328 if( isServer == true ) {
sagilar 12:fdf2b9912649 329 // Se llena el buffer para el envio del error
sagilar 12:fdf2b9912649 330 memcpy(Buffer, MsgTX, sizeof(MsgTX));
sagilar 12:fdf2b9912649 331 for( i = sizeof(MsgTX); i < BufferSize; i++ ) {
sagilar 12:fdf2b9912649 332 Buffer[i] = i - sizeof(MsgTX);
sagilar 12:fdf2b9912649 333 }
sagilar 12:fdf2b9912649 334 wait_ms( 1000 );
sagilar 12:fdf2b9912649 335 Radio->Send( Buffer, BufferSize );
sagilar 14:fe73b77cf1c0 336 }*/
sagilar 14:fe73b77cf1c0 337 Radio->Rx( RX_TIMEOUT_VALUE );
sagilar 12:fdf2b9912649 338 State = LOWPOWER;
sagilar 12:fdf2b9912649 339 break;
sagilar 12:fdf2b9912649 340 case TX_TIMEOUT:
sagilar 12:fdf2b9912649 341 Radio->Rx( RX_TIMEOUT_VALUE );
sagilar 12:fdf2b9912649 342 State = LOWPOWER;
sagilar 12:fdf2b9912649 343 break;
sagilar 12:fdf2b9912649 344 case LOWPOWER:
sagilar 12:fdf2b9912649 345 sleep();
sagilar 12:fdf2b9912649 346 break;
sagilar 12:fdf2b9912649 347 default:
sagilar 12:fdf2b9912649 348 State = LOWPOWER;
sagilar 12:fdf2b9912649 349 break;
sagilar 12:fdf2b9912649 350 }
Helmut64 0:c43b6919ae15 351 }
Helmut64 0:c43b6919ae15 352 }
Helmut64 0:c43b6919ae15 353
sagilar 12:fdf2b9912649 354 char *splitOnPosition(char *msj, int pos)
sagilar 12:fdf2b9912649 355 {
sagilar 12:fdf2b9912649 356
sagilar 12:fdf2b9912649 357 int i=0;
sagilar 12:fdf2b9912649 358 char *substring = strtok (msj,"|");
sagilar 12:fdf2b9912649 359 char *strOutput="";
sagilar 12:fdf2b9912649 360 while (substring != NULL) {
sagilar 12:fdf2b9912649 361 //dprintf ("substring: %s, index: %d",substring,i);
sagilar 12:fdf2b9912649 362
sagilar 12:fdf2b9912649 363 if(i == 0) {
sagilar 12:fdf2b9912649 364 srcEUI = substring;
sagilar 12:fdf2b9912649 365 } else if(i == 3) {
sagilar 12:fdf2b9912649 366 msjDestEUI = substring;
sagilar 12:fdf2b9912649 367 } else if(i == 4) {
sagilar 12:fdf2b9912649 368 msjContent = substring;
sagilar 12:fdf2b9912649 369 } else if(i > 4) {
sagilar 12:fdf2b9912649 370 strcat(msjContent," ");
sagilar 12:fdf2b9912649 371 strcat(msjContent,substring);
sagilar 12:fdf2b9912649 372 //dprintf ("contenido: %s, substring: %s, i: %d",msjContent, substring, i);
sagilar 12:fdf2b9912649 373 }
sagilar 12:fdf2b9912649 374 substring = strtok (NULL, "|");
sagilar 12:fdf2b9912649 375 i++;
sagilar 12:fdf2b9912649 376 }
sagilar 12:fdf2b9912649 377 return strOutput;
sagilar 12:fdf2b9912649 378 }
sagilar 12:fdf2b9912649 379
bcostm 11:9d7409ebfa57 380 void OnTxDone(void *radio, void *userThisPtr, void *userData)
Helmut64 0:c43b6919ae15 381 {
Helmut64 0:c43b6919ae15 382 Radio->Sleep( );
Helmut64 0:c43b6919ae15 383 State = TX;
Helmut64 0:c43b6919ae15 384 if (DEBUG_MESSAGE)
Helmut64 0:c43b6919ae15 385 dprintf("> OnTxDone");
Helmut64 0:c43b6919ae15 386 }
Helmut64 0:c43b6919ae15 387
bcostm 11:9d7409ebfa57 388 void OnRxDone(void *radio, void *userThisPtr, void *userData, uint8_t *payload, uint16_t size, int16_t rssi, int8_t snr)
Helmut64 0:c43b6919ae15 389 {
Helmut64 0:c43b6919ae15 390 Radio->Sleep( );
Helmut64 0:c43b6919ae15 391 BufferSize = size;
Helmut64 0:c43b6919ae15 392 memcpy( Buffer, payload, BufferSize );
Helmut64 0:c43b6919ae15 393 State = RX;
Helmut64 0:c43b6919ae15 394 if (DEBUG_MESSAGE)
Helmut64 0:c43b6919ae15 395 dprintf("> OnRxDone: RssiValue=%d dBm, SnrValue=%d", rssi, snr);
sagilar 12:fdf2b9912649 396 //dump("Data:", payload, size);
sagilar 12:fdf2b9912649 397 strcpy(MsgRX,(char*)payload);
sagilar 12:fdf2b9912649 398 //dprintf("Msj: %s", MsgRX);
Helmut64 0:c43b6919ae15 399 }
Helmut64 0:c43b6919ae15 400
bcostm 11:9d7409ebfa57 401 void OnTxTimeout(void *radio, void *userThisPtr, void *userData)
Helmut64 0:c43b6919ae15 402 {
Helmut64 0:c43b6919ae15 403 *led3 = 0;
Helmut64 0:c43b6919ae15 404 Radio->Sleep( );
Helmut64 0:c43b6919ae15 405 State = TX_TIMEOUT;
Helmut64 0:c43b6919ae15 406 if(DEBUG_MESSAGE)
Helmut64 0:c43b6919ae15 407 dprintf("> OnTxTimeout");
Helmut64 0:c43b6919ae15 408 }
Helmut64 0:c43b6919ae15 409
bcostm 11:9d7409ebfa57 410 void OnRxTimeout(void *radio, void *userThisPtr, void *userData)
Helmut64 0:c43b6919ae15 411 {
Helmut64 0:c43b6919ae15 412 *led3 = 0;
Helmut64 0:c43b6919ae15 413 Radio->Sleep( );
Helmut64 0:c43b6919ae15 414 Buffer[BufferSize-1] = 0;
Helmut64 0:c43b6919ae15 415 State = RX_TIMEOUT;
Helmut64 0:c43b6919ae15 416 if (DEBUG_MESSAGE)
Helmut64 0:c43b6919ae15 417 dprintf("> OnRxTimeout");
Helmut64 0:c43b6919ae15 418 }
Helmut64 0:c43b6919ae15 419
bcostm 11:9d7409ebfa57 420 void OnRxError(void *radio, void *userThisPtr, void *userData)
Helmut64 0:c43b6919ae15 421 {
Helmut64 0:c43b6919ae15 422 Radio->Sleep( );
Helmut64 0:c43b6919ae15 423 State = RX_ERROR;
Helmut64 0:c43b6919ae15 424 if (DEBUG_MESSAGE)
Helmut64 0:c43b6919ae15 425 dprintf("> OnRxError");
Helmut64 0:c43b6919ae15 426 }
Helmut64 0:c43b6919ae15 427
Helmut64 0:c43b6919ae15 428 #endif