LoRa node acquiring random float value and sending to LoRa Server - Working ok

Dependencies:   BufferedSerial SX1276GenericLib_node2 mbed

Fork of DISCO-L072CZ-LRWAN1_LoRa_PingPong by ST

Committer:
sagilar
Date:
Wed Aug 08 22:34:30 2018 +0000
Revision:
12:eaa8be101e07
Parent:
11:9d7409ebfa57
LoRa node acquiring random float value and sending to LoRa Server - Working ok

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:eaa8be101e07 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:eaa8be101e07 13 #include <string>
sagilar 12:eaa8be101e07 14 #include "rtos.h"
Helmut64 0:c43b6919ae15 15
Helmut64 0:c43b6919ae15 16 #ifdef FEATURE_LORA
Helmut64 0:c43b6919ae15 17
Helmut64 0:c43b6919ae15 18 /* Set this flag to '1' to display debug messages on the console */
Helmut64 0:c43b6919ae15 19 #define DEBUG_MESSAGE 1
Helmut64 0:c43b6919ae15 20
Helmut64 0:c43b6919ae15 21 /* Set this flag to '1' to use the LoRa modulation or to '0' to use FSK modulation */
Helmut64 0:c43b6919ae15 22 #define USE_MODEM_LORA 1
Helmut64 0:c43b6919ae15 23 #define USE_MODEM_FSK !USE_MODEM_LORA
sagilar 12:eaa8be101e07 24 #define RF_FREQUENCY RF_FREQUENCY_915_0 // Hz
sagilar 12:eaa8be101e07 25 #define TX_OUTPUT_POWER 20 // 20 dBm
Helmut64 0:c43b6919ae15 26
Helmut64 0:c43b6919ae15 27 #if USE_MODEM_LORA == 1
Helmut64 0:c43b6919ae15 28
Helmut64 7:6a8a82bfb0c6 29 #define LORA_BANDWIDTH 125000 // LoRa default, details in SX1276::BandwidthMap
Helmut64 0:c43b6919ae15 30 #define LORA_SPREADING_FACTOR LORA_SF7
Helmut64 0:c43b6919ae15 31 #define LORA_CODINGRATE LORA_ERROR_CODING_RATE_4_5
Helmut64 0:c43b6919ae15 32
Helmut64 0:c43b6919ae15 33 #define LORA_PREAMBLE_LENGTH 8 // Same for Tx and Rx
Helmut64 0:c43b6919ae15 34 #define LORA_SYMBOL_TIMEOUT 5 // Symbols
Helmut64 0:c43b6919ae15 35 #define LORA_FIX_LENGTH_PAYLOAD_ON false
sagilar 12:eaa8be101e07 36 #define LORA_FHSS_ENABLED true
sagilar 12:eaa8be101e07 37 #define LORA_NB_SYMB_HOP 4
Helmut64 0:c43b6919ae15 38 #define LORA_IQ_INVERSION_ON false
Helmut64 0:c43b6919ae15 39 #define LORA_CRC_ENABLED true
sagilar 12:eaa8be101e07 40
Helmut64 0:c43b6919ae15 41 #elif USE_MODEM_FSK == 1
Helmut64 0:c43b6919ae15 42
Helmut64 0:c43b6919ae15 43 #define FSK_FDEV 25000 // Hz
Helmut64 0:c43b6919ae15 44 #define FSK_DATARATE 19200 // bps
Helmut64 0:c43b6919ae15 45 #define FSK_BANDWIDTH 50000 // Hz
Helmut64 0:c43b6919ae15 46 #define FSK_AFC_BANDWIDTH 83333 // Hz
Helmut64 0:c43b6919ae15 47 #define FSK_PREAMBLE_LENGTH 5 // Same for Tx and Rx
Helmut64 0:c43b6919ae15 48 #define FSK_FIX_LENGTH_PAYLOAD_ON false
Helmut64 0:c43b6919ae15 49 #define FSK_CRC_ENABLED true
sagilar 12:eaa8be101e07 50
Helmut64 0:c43b6919ae15 51 #else
sagilar 12:eaa8be101e07 52 #error "Please define a modem in the compiler options."
sagilar 12:eaa8be101e07 53 #endif
Helmut64 0:c43b6919ae15 54
Helmut64 0:c43b6919ae15 55
Helmut64 0:c43b6919ae15 56 #define RX_TIMEOUT_VALUE 3500 // in ms
Helmut64 0:c43b6919ae15 57
Helmut64 0:c43b6919ae15 58 //#define BUFFER_SIZE 32 // Define the payload size here
Helmut64 0:c43b6919ae15 59 #define BUFFER_SIZE 64 // Define the payload size here
sagilar 12:eaa8be101e07 60 #define RETRIES 3 // Se define la cantidad de intentos de envio despues de recibir error
Helmut64 0:c43b6919ae15 61 /*
Helmut64 0:c43b6919ae15 62 * Global variables declarations
Helmut64 0:c43b6919ae15 63 */
sagilar 12:eaa8be101e07 64 typedef enum {
Helmut64 0:c43b6919ae15 65 LOWPOWER = 0,
Helmut64 0:c43b6919ae15 66 IDLE,
sagilar 12:eaa8be101e07 67
Helmut64 0:c43b6919ae15 68 RX,
Helmut64 0:c43b6919ae15 69 RX_TIMEOUT,
Helmut64 0:c43b6919ae15 70 RX_ERROR,
sagilar 12:eaa8be101e07 71
Helmut64 0:c43b6919ae15 72 TX,
Helmut64 0:c43b6919ae15 73 TX_TIMEOUT,
sagilar 12:eaa8be101e07 74
Helmut64 0:c43b6919ae15 75 CAD,
Helmut64 0:c43b6919ae15 76 CAD_DONE
Helmut64 0:c43b6919ae15 77 } AppStates_t;
Helmut64 0:c43b6919ae15 78
Helmut64 0:c43b6919ae15 79 volatile AppStates_t State = LOWPOWER;
Helmut64 0:c43b6919ae15 80
Helmut64 0:c43b6919ae15 81 /*!
Helmut64 0:c43b6919ae15 82 * Radio events function pointer
Helmut64 0:c43b6919ae15 83 */
Helmut64 0:c43b6919ae15 84 static RadioEvents_t RadioEvents;
Helmut64 0:c43b6919ae15 85
Helmut64 0:c43b6919ae15 86 /*
Helmut64 0:c43b6919ae15 87 * Global variables declarations
Helmut64 0:c43b6919ae15 88 */
Helmut64 0:c43b6919ae15 89 SX1276Generic *Radio;
Helmut64 0:c43b6919ae15 90
Helmut64 0:c43b6919ae15 91
sagilar 12:eaa8be101e07 92 //const uint8_t PingMsg[] = { 0xff, 0xff, 0x00, 0x00, 'P', 'I', 'N', 'G'};// "PING";
sagilar 12:eaa8be101e07 93 //const uint8_t PongMsg[] = { 0xff, 0xff, 0x00, 0x00, 'P', 'O', 'N', 'G'};// "PONG";
sagilar 12:eaa8be101e07 94 //const uint8_t PingMsg[] = { 'M', 'S', 'J', ' ','G','I','L'};// "PING";
sagilar 12:eaa8be101e07 95 //const uint8_t PongMsg[] = { 'R', 'P', 'T', 'A',' ','G','I','L'};// "PONG";
sagilar 12:eaa8be101e07 96 const char EUI[] = "0A12";// 4 bytes que definen el identificador del dispositivo (copiar los bytes en string)
sagilar 12:eaa8be101e07 97 const char AppEUI[] = "AAAA";// 4 bytes que definen el identificador de la aplicacion (copiar los bytes en string)
sagilar 12:eaa8be101e07 98 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 12:eaa8be101e07 99 char MsgTX[64] = "";// Mensaje de transmision, se pueden usar los 52 bytes faltantes para completar el payload de 64 bytes. Se puede poner directamente en string.
sagilar 12:eaa8be101e07 100 char MsgRX[64] = "";// Mensaje de recepcion, carga el payload entrante a esta cadena.
sagilar 12:eaa8be101e07 101 char MsgRet[] = "RECIBIDO";// Para verificar el resultado del envio
sagilar 12:eaa8be101e07 102 char DestEUI[] = "0A01"; //Gateway Server
sagilar 12:eaa8be101e07 103 string strRecepcion = "";
Helmut64 0:c43b6919ae15 104 uint16_t BufferSize = BUFFER_SIZE;
Helmut64 0:c43b6919ae15 105 uint8_t *Buffer;
sagilar 12:eaa8be101e07 106 int reintentos=0;
sagilar 12:eaa8be101e07 107 string msjDeco="";
sagilar 12:eaa8be101e07 108 char *retParse;
sagilar 12:eaa8be101e07 109 char *srcEUI;
sagilar 12:eaa8be101e07 110 char *msjDestEUI;
sagilar 12:eaa8be101e07 111 char *msjContent;
sagilar 12:eaa8be101e07 112 AnalogIn analog_value(A0);
sagilar 12:eaa8be101e07 113 float meas_r;
sagilar 12:eaa8be101e07 114 float meas_v;
sagilar 12:eaa8be101e07 115
Helmut64 0:c43b6919ae15 116
Helmut64 0:c43b6919ae15 117 DigitalOut *led3;
Helmut64 0:c43b6919ae15 118
Helmut64 0:c43b6919ae15 119
sagilar 12:eaa8be101e07 120 int SX1276PingPong()
Helmut64 0:c43b6919ae15 121 {
sagilar 12:eaa8be101e07 122
sagilar 12:eaa8be101e07 123
sagilar 12:eaa8be101e07 124 Ticker tick;
sagilar 12:eaa8be101e07 125 tick.attach(&readAnalog,25.0);
sagilar 12:eaa8be101e07 126 uint8_t i;
sagilar 12:eaa8be101e07 127 bool isMaster = true;
sagilar 12:eaa8be101e07 128
Helmut64 0:c43b6919ae15 129 #if( defined ( TARGET_KL25Z ) || defined ( TARGET_LPC11U6X ) )
Helmut64 0:c43b6919ae15 130 DigitalOut *led = new DigitalOut(LED2);
bcostm 11:9d7409ebfa57 131 #elif defined(TARGET_NUCLEO_L073RZ) || defined(TARGET_DISCO_L072CZ_LRWAN1)
Helmut64 0:c43b6919ae15 132 DigitalOut *led = new DigitalOut(LED4); // RX red
Helmut64 0:c43b6919ae15 133 led3 = new DigitalOut(LED3); // TX blue
Helmut64 0:c43b6919ae15 134 #else
Helmut64 0:c43b6919ae15 135 DigitalOut *led = new DigitalOut(LED1);
Helmut64 0:c43b6919ae15 136 led3 = led;
Helmut64 0:c43b6919ae15 137 #endif
sagilar 12:eaa8be101e07 138
Helmut64 0:c43b6919ae15 139 Buffer = new uint8_t[BUFFER_SIZE];
Helmut64 0:c43b6919ae15 140 *led3 = 1;
Helmut64 0:c43b6919ae15 141
Helmut64 0:c43b6919ae15 142 #ifdef B_L072Z_LRWAN1_LORA
Helmut64 0:c43b6919ae15 143 Radio = new SX1276Generic(NULL, MURATA_SX1276,
sagilar 12:eaa8be101e07 144 LORA_SPI_MOSI, LORA_SPI_MISO, LORA_SPI_SCLK, LORA_CS, LORA_RESET,
sagilar 12:eaa8be101e07 145 LORA_DIO0, LORA_DIO1, LORA_DIO2, LORA_DIO3, LORA_DIO4, LORA_DIO5,
sagilar 12:eaa8be101e07 146 LORA_ANT_RX, LORA_ANT_TX, LORA_ANT_BOOST, LORA_TCXO);
Helmut64 0:c43b6919ae15 147 #else // RFM95
Helmut64 0:c43b6919ae15 148 Radio = new SX1276Generic(NULL, RFM95_SX1276,
sagilar 12:eaa8be101e07 149 LORA_SPI_MOSI, LORA_SPI_MISO, LORA_SPI_SCLK, LORA_CS, LORA_RESET,
sagilar 12:eaa8be101e07 150 LORA_DIO0, LORA_DIO1, LORA_DIO2, LORA_DIO3, LORA_DIO4, LORA_DIO5);
Helmut64 0:c43b6919ae15 151
Helmut64 0:c43b6919ae15 152 #endif
sagilar 12:eaa8be101e07 153
sagilar 12:eaa8be101e07 154
sagilar 12:eaa8be101e07 155
sagilar 12:eaa8be101e07 156
sagilar 12:eaa8be101e07 157 dprintf("Aplicacion de comunicacion LoRa punto a punto" );
sagilar 12:eaa8be101e07 158 dprintf("Frecuencia: %.1f", (double)RF_FREQUENCY/1000000.0);
Helmut64 0:c43b6919ae15 159 dprintf("TXPower: %d dBm", TX_OUTPUT_POWER);
Helmut64 0:c43b6919ae15 160 #if USE_MODEM_LORA == 1
Helmut64 8:3b0d7b4ff28f 161 dprintf("Bandwidth: %d Hz", LORA_BANDWIDTH);
Helmut64 0:c43b6919ae15 162 dprintf("Spreading factor: SF%d", LORA_SPREADING_FACTOR);
Helmut64 0:c43b6919ae15 163 #elif USE_MODEM_FSK == 1
Helmut64 0:c43b6919ae15 164 dprintf("Bandwidth: %d kHz", FSK_BANDWIDTH);
Helmut64 0:c43b6919ae15 165 dprintf("Baudrate: %d", FSK_DATARATE);
Helmut64 0:c43b6919ae15 166 #endif
Helmut64 0:c43b6919ae15 167 // Initialize Radio driver
Helmut64 0:c43b6919ae15 168 RadioEvents.TxDone = OnTxDone;
Helmut64 0:c43b6919ae15 169 RadioEvents.RxDone = OnRxDone;
Helmut64 0:c43b6919ae15 170 RadioEvents.RxError = OnRxError;
Helmut64 0:c43b6919ae15 171 RadioEvents.TxTimeout = OnTxTimeout;
sagilar 12:eaa8be101e07 172 RadioEvents.RxTimeout = OnRxTimeout;
Helmut64 6:1b598b0e52e4 173 if (Radio->Init( &RadioEvents ) == false) {
Helmut64 6:1b598b0e52e4 174 while(1) {
sagilar 12:eaa8be101e07 175 dprintf("Radio could not be detected!");
sagilar 12:eaa8be101e07 176 wait( 1 );
Helmut64 6:1b598b0e52e4 177 }
Helmut64 0:c43b6919ae15 178 }
Helmut64 6:1b598b0e52e4 179
sagilar 12:eaa8be101e07 180
Helmut64 0:c43b6919ae15 181 switch(Radio->DetectBoardType()) {
Helmut64 0:c43b6919ae15 182 case SX1276MB1LAS:
Helmut64 0:c43b6919ae15 183 if (DEBUG_MESSAGE)
Helmut64 0:c43b6919ae15 184 dprintf(" > Board Type: SX1276MB1LAS <");
Helmut64 0:c43b6919ae15 185 break;
Helmut64 0:c43b6919ae15 186 case SX1276MB1MAS:
Helmut64 0:c43b6919ae15 187 if (DEBUG_MESSAGE)
Helmut64 0:c43b6919ae15 188 dprintf(" > Board Type: SX1276MB1LAS <");
Helmut64 0:c43b6919ae15 189 case MURATA_SX1276:
Helmut64 0:c43b6919ae15 190 if (DEBUG_MESSAGE)
sagilar 12:eaa8be101e07 191 dprintf(" > Board Type: MURATA_SX1276_STM32L0 <");
Helmut64 0:c43b6919ae15 192 break;
Helmut64 0:c43b6919ae15 193 case RFM95_SX1276:
Helmut64 0:c43b6919ae15 194 if (DEBUG_MESSAGE)
Helmut64 0:c43b6919ae15 195 dprintf(" > HopeRF RFM95xx <");
Helmut64 0:c43b6919ae15 196 break;
Helmut64 0:c43b6919ae15 197 default:
Helmut64 0:c43b6919ae15 198 dprintf(" > Board Type: unknown <");
Helmut64 0:c43b6919ae15 199 }
Helmut64 0:c43b6919ae15 200
sagilar 12:eaa8be101e07 201 Radio->SetChannel(RF_FREQUENCY );
Helmut64 0:c43b6919ae15 202
Helmut64 0:c43b6919ae15 203 #if USE_MODEM_LORA == 1
sagilar 12:eaa8be101e07 204
Helmut64 0:c43b6919ae15 205 if (LORA_FHSS_ENABLED)
Helmut64 0:c43b6919ae15 206 dprintf(" > LORA FHSS Mode <");
Helmut64 0:c43b6919ae15 207 if (!LORA_FHSS_ENABLED)
Helmut64 0:c43b6919ae15 208 dprintf(" > LORA Mode <");
Helmut64 0:c43b6919ae15 209
Helmut64 0:c43b6919ae15 210 Radio->SetTxConfig( MODEM_LORA, TX_OUTPUT_POWER, 0, LORA_BANDWIDTH,
sagilar 12:eaa8be101e07 211 LORA_SPREADING_FACTOR, LORA_CODINGRATE,
sagilar 12:eaa8be101e07 212 LORA_PREAMBLE_LENGTH, LORA_FIX_LENGTH_PAYLOAD_ON,
sagilar 12:eaa8be101e07 213 LORA_CRC_ENABLED, LORA_FHSS_ENABLED, LORA_NB_SYMB_HOP,
sagilar 12:eaa8be101e07 214 LORA_IQ_INVERSION_ON, 2000 );
sagilar 12:eaa8be101e07 215
Helmut64 0:c43b6919ae15 216 Radio->SetRxConfig( MODEM_LORA, LORA_BANDWIDTH, LORA_SPREADING_FACTOR,
sagilar 12:eaa8be101e07 217 LORA_CODINGRATE, 0, LORA_PREAMBLE_LENGTH,
sagilar 12:eaa8be101e07 218 LORA_SYMBOL_TIMEOUT, LORA_FIX_LENGTH_PAYLOAD_ON, 0,
sagilar 12:eaa8be101e07 219 LORA_CRC_ENABLED, LORA_FHSS_ENABLED, LORA_NB_SYMB_HOP,
sagilar 12:eaa8be101e07 220 LORA_IQ_INVERSION_ON, true );
sagilar 12:eaa8be101e07 221
Helmut64 0:c43b6919ae15 222 #elif USE_MODEM_FSK == 1
Helmut64 0:c43b6919ae15 223
Helmut64 0:c43b6919ae15 224 dprintf(" > FSK Mode <");
Helmut64 0:c43b6919ae15 225 Radio->SetTxConfig( MODEM_FSK, TX_OUTPUT_POWER, FSK_FDEV, 0,
sagilar 12:eaa8be101e07 226 FSK_DATARATE, 0,
sagilar 12:eaa8be101e07 227 FSK_PREAMBLE_LENGTH, FSK_FIX_LENGTH_PAYLOAD_ON,
sagilar 12:eaa8be101e07 228 FSK_CRC_ENABLED, 0, 0, 0, 2000 );
sagilar 12:eaa8be101e07 229
Helmut64 0:c43b6919ae15 230 Radio->SetRxConfig( MODEM_FSK, FSK_BANDWIDTH, FSK_DATARATE,
sagilar 12:eaa8be101e07 231 0, FSK_AFC_BANDWIDTH, FSK_PREAMBLE_LENGTH,
sagilar 12:eaa8be101e07 232 0, FSK_FIX_LENGTH_PAYLOAD_ON, 0, FSK_CRC_ENABLED,
sagilar 12:eaa8be101e07 233 0, 0, false, true );
sagilar 12:eaa8be101e07 234
Helmut64 0:c43b6919ae15 235 #else
Helmut64 0:c43b6919ae15 236
Helmut64 0:c43b6919ae15 237 #error "Please define a modem in the compiler options."
Helmut64 0:c43b6919ae15 238
Helmut64 0:c43b6919ae15 239 #endif
Helmut64 0:c43b6919ae15 240
sagilar 12:eaa8be101e07 241 if (DEBUG_MESSAGE)
sagilar 12:eaa8be101e07 242 dprintf("Inicializando nodo");
sagilar 12:eaa8be101e07 243 dprintf("EUI (ID): %s",EUI);
sagilar 12:eaa8be101e07 244
Helmut64 0:c43b6919ae15 245 Radio->Rx( RX_TIMEOUT_VALUE );
sagilar 12:eaa8be101e07 246
sagilar 12:eaa8be101e07 247 while( 1 ) {
Helmut64 0:c43b6919ae15 248 #ifdef TARGET_STM32L4
Helmut64 0:c43b6919ae15 249 WatchDogUpdate();
Helmut64 0:c43b6919ae15 250 #endif
sagilar 12:eaa8be101e07 251 /*
sagilar 12:eaa8be101e07 252 const char EUI[] = "0A10";// 4 bytes que definen el identificador del dispositivo (copiar los bytes en string)
sagilar 12:eaa8be101e07 253 const char AppEUI[] = "AAAA";// 4 bytes que definen el identificador de la aplicacion (copiar los bytes en string)
sagilar 12:eaa8be101e07 254 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 12:eaa8be101e07 255 char MsgTX[64] = "";// Mensaje de transmision, se pueden usar los 52 bytes faltantes para completar el payload de 64 bytes. Se puede poner directamente en string.
sagilar 12:eaa8be101e07 256 char MsgRX[64] = "";// Mensaje de recepcion, carga el payload entrante a esta cadena.
sagilar 12:eaa8be101e07 257 char MsgRet[] = "RECIBIDO";// Para verificar el resultado del envio
sagilar 12:eaa8be101e07 258 char DestEUI[] = "0A01";
sagilar 12:eaa8be101e07 259 string strRecepcion = "";
sagilar 12:eaa8be101e07 260 uint16_t BufferSize = BUFFER_SIZE;
sagilar 12:eaa8be101e07 261 uint8_t *Buffer;
sagilar 12:eaa8be101e07 262 int reintentos=0;
sagilar 12:eaa8be101e07 263 string msjDeco="";
sagilar 12:eaa8be101e07 264 char *retParse;
sagilar 12:eaa8be101e07 265 char *srcEUI;
sagilar 12:eaa8be101e07 266 char *msjDestEUI;
sagilar 12:eaa8be101e07 267 char *msjContent;*/
sagilar 12:eaa8be101e07 268
sagilar 12:eaa8be101e07 269 switch( State ) {
sagilar 12:eaa8be101e07 270 case RX:
sagilar 12:eaa8be101e07 271 reintentos=0;
sagilar 12:eaa8be101e07 272 *led = !*led;
sagilar 12:eaa8be101e07 273 //dprintf("Mensaje para depurar: %s",MsgRX);
sagilar 12:eaa8be101e07 274 msjDeco = MsgRX;
sagilar 12:eaa8be101e07 275 splitOnPosition(MsgRX, 0);
sagilar 12:eaa8be101e07 276
sagilar 12:eaa8be101e07 277 dprintf("Source EUI: %s, Destination EUI: %s, Content: %s",srcEUI,msjDestEUI,msjContent);
sagilar 12:eaa8be101e07 278 strRecepcion = msjContent;
sagilar 12:eaa8be101e07 279 if(strcmp(EUI,msjDestEUI) == 0) {
sagilar 12:eaa8be101e07 280 dprintf("Mismo EUI, Soy el destinatario");
sagilar 12:eaa8be101e07 281
sagilar 12:eaa8be101e07 282 } else {
sagilar 12:eaa8be101e07 283 dprintf("Diferente EUI, ignorar mensaje");
sagilar 12:eaa8be101e07 284 wait_ms( 500 );
sagilar 12:eaa8be101e07 285 *led = !*led;
sagilar 12:eaa8be101e07 286 State = LOWPOWER;
sagilar 12:eaa8be101e07 287 break;
sagilar 12:eaa8be101e07 288 }
sagilar 12:eaa8be101e07 289
sagilar 12:eaa8be101e07 290 if( BufferSize > 0 ) {
sagilar 12:eaa8be101e07 291
sagilar 12:eaa8be101e07 292 if (strstr(msjContent, "RECIBIDO") != NULL) {
sagilar 12:eaa8be101e07 293 dprintf( "Mensaje recibido por el servidor correctamente" );
sagilar 12:eaa8be101e07 294 }
sagilar 12:eaa8be101e07 295 if (strstr(msjContent, "ERROR") != NULL) {
sagilar 12:eaa8be101e07 296 dprintf( "Mensaje no fue recibido por el servidor correctamente" );
sagilar 12:eaa8be101e07 297 }
sagilar 12:eaa8be101e07 298 if (strstr(msjContent, "DENIED") != NULL) {
sagilar 12:eaa8be101e07 299 dprintf( "Mensaje rechazado por el servidor correctamente" );
Helmut64 0:c43b6919ae15 300 }
sagilar 12:eaa8be101e07 301 /*if( RecFound == true ) {
sagilar 12:eaa8be101e07 302
sagilar 12:eaa8be101e07 303 dprintf( "Mensaje recibido por el servidor correctamente" );
sagilar 12:eaa8be101e07 304 } else if(ErrorFound == true) { // Error en la recepcion
sagilar 12:eaa8be101e07 305 dprintf( "Mensaje no fue recibido por el servidor correctamente" );
sagilar 12:eaa8be101e07 306
sagilar 12:eaa8be101e07 307 } else if(DenFound == true) { // Negacion en la recepcion
sagilar 12:eaa8be101e07 308 dprintf( "Mensaje rechazado por el servidor correctamente" );
sagilar 12:eaa8be101e07 309
sagilar 12:eaa8be101e07 310 }*/
sagilar 12:eaa8be101e07 311 }
sagilar 12:eaa8be101e07 312 wait_ms( 500 );
sagilar 12:eaa8be101e07 313 *led = !*led;
sagilar 12:eaa8be101e07 314 State = LOWPOWER;
sagilar 12:eaa8be101e07 315 break;
sagilar 12:eaa8be101e07 316 case TX:
sagilar 12:eaa8be101e07 317
sagilar 12:eaa8be101e07 318 //dprintf("Mensaje a enviar: %s",MsgTX);
sagilar 12:eaa8be101e07 319 *led3 = !*led3;
sagilar 12:eaa8be101e07 320
sagilar 12:eaa8be101e07 321 if(reintentos<RETRIES) {
sagilar 12:eaa8be101e07 322 Radio->Rx( RX_TIMEOUT_VALUE );
sagilar 12:eaa8be101e07 323 reintentos++;
Helmut64 0:c43b6919ae15 324 }
sagilar 12:eaa8be101e07 325 wait_ms( 500 );
sagilar 12:eaa8be101e07 326 *led = !*led;
sagilar 12:eaa8be101e07 327 State = LOWPOWER;
sagilar 12:eaa8be101e07 328 break;
sagilar 12:eaa8be101e07 329 case RX_TIMEOUT:
sagilar 12:eaa8be101e07 330 if( isMaster == true ) {
sagilar 12:eaa8be101e07 331 // Send the next PING frame
sagilar 12:eaa8be101e07 332 memcpy(Buffer, MsgTX, sizeof(MsgTX));
sagilar 12:eaa8be101e07 333 for( i = sizeof(MsgTX); i < BufferSize; i++ ) {
sagilar 12:eaa8be101e07 334 Buffer[i] = i - sizeof(MsgTX);
Helmut64 0:c43b6919ae15 335 }
sagilar 12:eaa8be101e07 336 wait_ms( 1000 );
sagilar 12:eaa8be101e07 337 Radio->Send( Buffer, BufferSize );
sagilar 12:eaa8be101e07 338 } else {
sagilar 12:eaa8be101e07 339 Radio->Rx( RX_TIMEOUT_VALUE );
sagilar 12:eaa8be101e07 340 }
sagilar 12:eaa8be101e07 341 State = LOWPOWER;
sagilar 12:eaa8be101e07 342 break;
sagilar 12:eaa8be101e07 343 case RX_ERROR:
sagilar 12:eaa8be101e07 344 // We have received a Packet with a CRC error, send reply as if packet was correct
sagilar 12:eaa8be101e07 345 if( isMaster == true ) {
sagilar 12:eaa8be101e07 346 // Send the next PING frame
sagilar 12:eaa8be101e07 347 memcpy(Buffer, MsgTX, sizeof(MsgTX));
sagilar 12:eaa8be101e07 348 for( i = 4; i < BufferSize; i++ ) {
sagilar 12:eaa8be101e07 349 Buffer[i] = i - 4;
sagilar 12:eaa8be101e07 350 }
sagilar 12:eaa8be101e07 351 wait_ms( 1000 );
sagilar 12:eaa8be101e07 352 Radio->Send( Buffer, BufferSize );
sagilar 12:eaa8be101e07 353 } else {
sagilar 12:eaa8be101e07 354 // Send the next PONG frame
sagilar 12:eaa8be101e07 355 memcpy(Buffer, MsgTX, sizeof(MsgTX));
sagilar 12:eaa8be101e07 356 for( i = sizeof(MsgTX); i < BufferSize; i++ ) {
sagilar 12:eaa8be101e07 357 Buffer[i] = i - sizeof(MsgTX);
sagilar 12:eaa8be101e07 358 }
sagilar 12:eaa8be101e07 359 wait_ms( 1000 );
sagilar 12:eaa8be101e07 360 Radio->Send( Buffer, BufferSize );
Helmut64 0:c43b6919ae15 361 }
sagilar 12:eaa8be101e07 362 State = LOWPOWER;
sagilar 12:eaa8be101e07 363 break;
sagilar 12:eaa8be101e07 364 case TX_TIMEOUT:
sagilar 12:eaa8be101e07 365 Radio->Rx( RX_TIMEOUT_VALUE );
sagilar 12:eaa8be101e07 366 State = LOWPOWER;
sagilar 12:eaa8be101e07 367 break;
sagilar 12:eaa8be101e07 368 case LOWPOWER:
sagilar 12:eaa8be101e07 369 sleep();
sagilar 12:eaa8be101e07 370 break;
sagilar 12:eaa8be101e07 371 default:
sagilar 12:eaa8be101e07 372 State = LOWPOWER;
sagilar 12:eaa8be101e07 373 break;
sagilar 12:eaa8be101e07 374 }
sagilar 12:eaa8be101e07 375 }
sagilar 12:eaa8be101e07 376 }
sagilar 12:eaa8be101e07 377
sagilar 12:eaa8be101e07 378 char *splitOnPosition(char *msj, int pos)
sagilar 12:eaa8be101e07 379 {
sagilar 12:eaa8be101e07 380 int i=0;
sagilar 12:eaa8be101e07 381 char *substring = strtok (msj,"|");
sagilar 12:eaa8be101e07 382 char *strOutput="";
sagilar 12:eaa8be101e07 383 while (substring != NULL) {
sagilar 12:eaa8be101e07 384 //dprintf ("substring: %s, index: %d",substring,i);
sagilar 12:eaa8be101e07 385
sagilar 12:eaa8be101e07 386 if(i == 0) {
sagilar 12:eaa8be101e07 387 srcEUI = substring;
sagilar 12:eaa8be101e07 388 } else if(i == 3) {
sagilar 12:eaa8be101e07 389 msjDestEUI = substring;
sagilar 12:eaa8be101e07 390 } else if(i == 4) {
sagilar 12:eaa8be101e07 391 msjContent = substring;
sagilar 12:eaa8be101e07 392 } else if(i > 4) {
sagilar 12:eaa8be101e07 393 strcat(msjContent," ");
sagilar 12:eaa8be101e07 394 strcat(msjContent,substring);
sagilar 12:eaa8be101e07 395 }
sagilar 12:eaa8be101e07 396
sagilar 12:eaa8be101e07 397 if(i == pos) {
sagilar 12:eaa8be101e07 398 strOutput = substring;
sagilar 12:eaa8be101e07 399 }
sagilar 12:eaa8be101e07 400 substring = strtok (NULL, "|");
sagilar 12:eaa8be101e07 401 i++;
Helmut64 0:c43b6919ae15 402 }
sagilar 12:eaa8be101e07 403 return strOutput;
sagilar 12:eaa8be101e07 404 }
sagilar 12:eaa8be101e07 405
sagilar 12:eaa8be101e07 406 void onButtonEvent()
sagilar 12:eaa8be101e07 407 {
sagilar 12:eaa8be101e07 408 reintentos=0;
sagilar 12:eaa8be101e07 409 float value = 4.85;
sagilar 12:eaa8be101e07 410 char valueStr[]="";
sagilar 12:eaa8be101e07 411 sprintf(valueStr,"%.2f",value);
sagilar 12:eaa8be101e07 412 char variable[] = "temperatura";
sagilar 12:eaa8be101e07 413 char dispositivo[] = "refrigerador1";
sagilar 12:eaa8be101e07 414 strcpy(MsgTX, EUI);
sagilar 12:eaa8be101e07 415 strcat(MsgTX, "|");
sagilar 12:eaa8be101e07 416 strcat(MsgTX, AppEUI);
sagilar 12:eaa8be101e07 417 strcat(MsgTX, "|");
sagilar 12:eaa8be101e07 418 strcat(MsgTX, AppKey);
sagilar 12:eaa8be101e07 419 strcat(MsgTX, "|");
sagilar 12:eaa8be101e07 420 strcat(MsgTX, DestEUI);
sagilar 12:eaa8be101e07 421 strcat(MsgTX, "|");
sagilar 12:eaa8be101e07 422 strcat(MsgTX, valueStr);
sagilar 12:eaa8be101e07 423 strcat(MsgTX, " ");
sagilar 12:eaa8be101e07 424 strcat(MsgTX, variable);
sagilar 12:eaa8be101e07 425 strcat(MsgTX, " ");
sagilar 12:eaa8be101e07 426 strcat(MsgTX, dispositivo);
sagilar 12:eaa8be101e07 427 //strcat(MsgTX, ";");
sagilar 12:eaa8be101e07 428 //*led = !*led;
sagilar 12:eaa8be101e07 429 //dprintf( "Enviando contenido al server" );
sagilar 12:eaa8be101e07 430 memcpy(Buffer, MsgTX, sizeof(MsgTX));
sagilar 12:eaa8be101e07 431 // Se llena el buffer con la informacion a enviar
sagilar 12:eaa8be101e07 432 for( int i = sizeof(MsgTX); i < BufferSize; i++ ) {
sagilar 12:eaa8be101e07 433 Buffer[i] = i - sizeof(MsgTX);
sagilar 12:eaa8be101e07 434 }
sagilar 12:eaa8be101e07 435 //wait_ms( 100 );
sagilar 12:eaa8be101e07 436 Radio->Send( Buffer, BufferSize );
sagilar 12:eaa8be101e07 437 Radio->Rx( RX_TIMEOUT_VALUE );
sagilar 12:eaa8be101e07 438
sagilar 12:eaa8be101e07 439 }
sagilar 12:eaa8be101e07 440
sagilar 12:eaa8be101e07 441 void readAnalog()
sagilar 12:eaa8be101e07 442 {
sagilar 12:eaa8be101e07 443
sagilar 12:eaa8be101e07 444
sagilar 12:eaa8be101e07 445 //meas_r = analog_value.read(); // Read the analog input value (value from 0.0 to 1.0 = full ADC conversion range)
sagilar 12:eaa8be101e07 446 meas_r = (float) rand()/RAND_MAX; // Para este caso se va a usar un numero aleatorio
sagilar 12:eaa8be101e07 447 //dprintf("random value: %.2f",meas_r);
sagilar 12:eaa8be101e07 448 //meas_r = 0.12;
sagilar 12:eaa8be101e07 449 meas_v = meas_r * 5.0; // Convierte el valor de 0 a 5 (Corriente)
sagilar 12:eaa8be101e07 450 reintentos=0;
sagilar 12:eaa8be101e07 451 char valueStr[]="";
sagilar 12:eaa8be101e07 452 sprintf(valueStr,"%.2f",meas_v);
sagilar 12:eaa8be101e07 453 char variable[] = "corriente";
sagilar 12:eaa8be101e07 454 char dispositivo[] = "nodo2_refrig_LoRa";
sagilar 12:eaa8be101e07 455 strcpy(MsgTX, EUI);
sagilar 12:eaa8be101e07 456 strcat(MsgTX, "|");
sagilar 12:eaa8be101e07 457 strcat(MsgTX, AppEUI);
sagilar 12:eaa8be101e07 458 strcat(MsgTX, "|");
sagilar 12:eaa8be101e07 459 strcat(MsgTX, AppKey);
sagilar 12:eaa8be101e07 460 strcat(MsgTX, "|");
sagilar 12:eaa8be101e07 461 strcat(MsgTX, DestEUI);
sagilar 12:eaa8be101e07 462 strcat(MsgTX, "|");
sagilar 12:eaa8be101e07 463 strcat(MsgTX, valueStr);
sagilar 12:eaa8be101e07 464 strcat(MsgTX, " ");
sagilar 12:eaa8be101e07 465 strcat(MsgTX, variable);
sagilar 12:eaa8be101e07 466 strcat(MsgTX, " ");
sagilar 12:eaa8be101e07 467 strcat(MsgTX, dispositivo);
sagilar 12:eaa8be101e07 468 //strcat(MsgTX, ";");
sagilar 12:eaa8be101e07 469 //*led = !*led;
sagilar 12:eaa8be101e07 470 //dprintf( "Enviando contenido al server" );
sagilar 12:eaa8be101e07 471 memcpy(Buffer, MsgTX, sizeof(MsgTX));
sagilar 12:eaa8be101e07 472 // Se llena el buffer con la informacion a enviar
sagilar 12:eaa8be101e07 473 for( int i = sizeof(MsgTX); i < BufferSize; i++ ) {
sagilar 12:eaa8be101e07 474 Buffer[i] = i - sizeof(MsgTX);
sagilar 12:eaa8be101e07 475 }
sagilar 12:eaa8be101e07 476 //wait_ms( 100 );
sagilar 12:eaa8be101e07 477 Radio->Send( Buffer, BufferSize );
sagilar 12:eaa8be101e07 478 Radio->Rx( RX_TIMEOUT_VALUE );
sagilar 12:eaa8be101e07 479
sagilar 12:eaa8be101e07 480 //wait(10);
sagilar 12:eaa8be101e07 481
Helmut64 0:c43b6919ae15 482 }
Helmut64 0:c43b6919ae15 483
bcostm 11:9d7409ebfa57 484 void OnTxDone(void *radio, void *userThisPtr, void *userData)
Helmut64 0:c43b6919ae15 485 {
Helmut64 0:c43b6919ae15 486 Radio->Sleep( );
Helmut64 0:c43b6919ae15 487 State = TX;
Helmut64 0:c43b6919ae15 488 if (DEBUG_MESSAGE)
Helmut64 0:c43b6919ae15 489 dprintf("> OnTxDone");
Helmut64 0:c43b6919ae15 490 }
Helmut64 0:c43b6919ae15 491
bcostm 11:9d7409ebfa57 492 void OnRxDone(void *radio, void *userThisPtr, void *userData, uint8_t *payload, uint16_t size, int16_t rssi, int8_t snr)
Helmut64 0:c43b6919ae15 493 {
Helmut64 0:c43b6919ae15 494 Radio->Sleep( );
Helmut64 0:c43b6919ae15 495 BufferSize = size;
Helmut64 0:c43b6919ae15 496 memcpy( Buffer, payload, BufferSize );
Helmut64 0:c43b6919ae15 497 State = RX;
Helmut64 0:c43b6919ae15 498 if (DEBUG_MESSAGE)
Helmut64 0:c43b6919ae15 499 dprintf("> OnRxDone: RssiValue=%d dBm, SnrValue=%d", rssi, snr);
sagilar 12:eaa8be101e07 500 //dump("Data:", payload, size);
sagilar 12:eaa8be101e07 501 strcpy(MsgRX,(char*)payload);
sagilar 12:eaa8be101e07 502 //dprintf("Msj: %s", MsgRX);
Helmut64 0:c43b6919ae15 503 }
Helmut64 0:c43b6919ae15 504
bcostm 11:9d7409ebfa57 505 void OnTxTimeout(void *radio, void *userThisPtr, void *userData)
Helmut64 0:c43b6919ae15 506 {
Helmut64 0:c43b6919ae15 507 *led3 = 0;
Helmut64 0:c43b6919ae15 508 Radio->Sleep( );
Helmut64 0:c43b6919ae15 509 State = TX_TIMEOUT;
Helmut64 0:c43b6919ae15 510 if(DEBUG_MESSAGE)
Helmut64 0:c43b6919ae15 511 dprintf("> OnTxTimeout");
Helmut64 0:c43b6919ae15 512 }
Helmut64 0:c43b6919ae15 513
bcostm 11:9d7409ebfa57 514 void OnRxTimeout(void *radio, void *userThisPtr, void *userData)
Helmut64 0:c43b6919ae15 515 {
Helmut64 0:c43b6919ae15 516 *led3 = 0;
Helmut64 0:c43b6919ae15 517 Radio->Sleep( );
Helmut64 0:c43b6919ae15 518 Buffer[BufferSize-1] = 0;
Helmut64 0:c43b6919ae15 519 State = RX_TIMEOUT;
Helmut64 0:c43b6919ae15 520 if (DEBUG_MESSAGE)
Helmut64 0:c43b6919ae15 521 dprintf("> OnRxTimeout");
Helmut64 0:c43b6919ae15 522 }
Helmut64 0:c43b6919ae15 523
bcostm 11:9d7409ebfa57 524 void OnRxError(void *radio, void *userThisPtr, void *userData)
Helmut64 0:c43b6919ae15 525 {
Helmut64 0:c43b6919ae15 526 Radio->Sleep( );
Helmut64 0:c43b6919ae15 527 State = RX_ERROR;
Helmut64 0:c43b6919ae15 528 if (DEBUG_MESSAGE)
Helmut64 0:c43b6919ae15 529 dprintf("> OnRxError");
Helmut64 0:c43b6919ae15 530 }
Helmut64 0:c43b6919ae15 531
Helmut64 0:c43b6919ae15 532 #endif