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
Diff: SX1276GenericPingPong/GenericPingPong.cpp
- Revision:
- 12:eaa8be101e07
- Parent:
- 11:9d7409ebfa57
--- a/SX1276GenericPingPong/GenericPingPong.cpp Fri Aug 18 07:45:44 2017 +0000 +++ b/SX1276GenericPingPong/GenericPingPong.cpp Wed Aug 08 22:34:30 2018 +0000 @@ -4,12 +4,14 @@ * (c) 2017 Helmut Tschemernjak * 30826 Garbsen (Hannover) Germany */ - + #include "mbed.h" #include "PinMap.h" #include "GenericPingPong.h" #include "sx1276-mbed-hal.h" #include "main.h" +#include <string> +#include "rtos.h" #ifdef FEATURE_LORA @@ -19,8 +21,8 @@ /* Set this flag to '1' to use the LoRa modulation or to '0' to use FSK modulation */ #define USE_MODEM_LORA 1 #define USE_MODEM_FSK !USE_MODEM_LORA -#define RF_FREQUENCY RF_FREQUENCY_868_1 // Hz -#define TX_OUTPUT_POWER 14 // 14 dBm +#define RF_FREQUENCY RF_FREQUENCY_915_0 // Hz +#define TX_OUTPUT_POWER 20 // 20 dBm #if USE_MODEM_LORA == 1 @@ -31,11 +33,11 @@ #define LORA_PREAMBLE_LENGTH 8 // Same for Tx and Rx #define LORA_SYMBOL_TIMEOUT 5 // Symbols #define LORA_FIX_LENGTH_PAYLOAD_ON false -#define LORA_FHSS_ENABLED false -#define LORA_NB_SYMB_HOP 4 +#define LORA_FHSS_ENABLED true +#define LORA_NB_SYMB_HOP 4 #define LORA_IQ_INVERSION_ON false #define LORA_CRC_ENABLED true - + #elif USE_MODEM_FSK == 1 #define FSK_FDEV 25000 // Hz @@ -45,32 +47,31 @@ #define FSK_PREAMBLE_LENGTH 5 // Same for Tx and Rx #define FSK_FIX_LENGTH_PAYLOAD_ON false #define FSK_CRC_ENABLED true - + #else - #error "Please define a modem in the compiler options." -#endif +#error "Please define a modem in the compiler options." +#endif #define RX_TIMEOUT_VALUE 3500 // in ms //#define BUFFER_SIZE 32 // Define the payload size here #define BUFFER_SIZE 64 // Define the payload size here - +#define RETRIES 3 // Se define la cantidad de intentos de envio despues de recibir error /* * Global variables declarations */ -typedef enum -{ +typedef enum { LOWPOWER = 0, IDLE, - + RX, RX_TIMEOUT, RX_ERROR, - + TX, TX_TIMEOUT, - + CAD, CAD_DONE } AppStates_t; @@ -88,17 +89,43 @@ SX1276Generic *Radio; -const uint8_t PingMsg[] = { 0xff, 0xff, 0x00, 0x00, 'P', 'I', 'N', 'G'};// "PING"; -const uint8_t PongMsg[] = { 0xff, 0xff, 0x00, 0x00, 'P', 'O', 'N', 'G'};// "PONG"; - +//const uint8_t PingMsg[] = { 0xff, 0xff, 0x00, 0x00, 'P', 'I', 'N', 'G'};// "PING"; +//const uint8_t PongMsg[] = { 0xff, 0xff, 0x00, 0x00, 'P', 'O', 'N', 'G'};// "PONG"; +//const uint8_t PingMsg[] = { 'M', 'S', 'J', ' ','G','I','L'};// "PING"; +//const uint8_t PongMsg[] = { 'R', 'P', 'T', 'A',' ','G','I','L'};// "PONG"; +const char EUI[] = "0A12";// 4 bytes que definen el identificador del dispositivo (copiar los bytes en string) +const char AppEUI[] = "AAAA";// 4 bytes que definen el identificador de la aplicacion (copiar los bytes en string) +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 +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. +char MsgRX[64] = "";// Mensaje de recepcion, carga el payload entrante a esta cadena. +char MsgRet[] = "RECIBIDO";// Para verificar el resultado del envio +char DestEUI[] = "0A01"; //Gateway Server +string strRecepcion = ""; uint16_t BufferSize = BUFFER_SIZE; uint8_t *Buffer; +int reintentos=0; +string msjDeco=""; +char *retParse; +char *srcEUI; +char *msjDestEUI; +char *msjContent; +AnalogIn analog_value(A0); +float meas_r; +float meas_v; + DigitalOut *led3; -int SX1276PingPong() +int SX1276PingPong() { + + + Ticker tick; + tick.attach(&readAnalog,25.0); + uint8_t i; + bool isMaster = true; + #if( defined ( TARGET_KL25Z ) || defined ( TARGET_LPC11U6X ) ) DigitalOut *led = new DigitalOut(LED2); #elif defined(TARGET_NUCLEO_L073RZ) || defined(TARGET_DISCO_L072CZ_LRWAN1) @@ -108,27 +135,27 @@ DigitalOut *led = new DigitalOut(LED1); led3 = led; #endif - + Buffer = new uint8_t[BUFFER_SIZE]; *led3 = 1; #ifdef B_L072Z_LRWAN1_LORA Radio = new SX1276Generic(NULL, MURATA_SX1276, - LORA_SPI_MOSI, LORA_SPI_MISO, LORA_SPI_SCLK, LORA_CS, LORA_RESET, - LORA_DIO0, LORA_DIO1, LORA_DIO2, LORA_DIO3, LORA_DIO4, LORA_DIO5, - LORA_ANT_RX, LORA_ANT_TX, LORA_ANT_BOOST, LORA_TCXO); + LORA_SPI_MOSI, LORA_SPI_MISO, LORA_SPI_SCLK, LORA_CS, LORA_RESET, + LORA_DIO0, LORA_DIO1, LORA_DIO2, LORA_DIO3, LORA_DIO4, LORA_DIO5, + LORA_ANT_RX, LORA_ANT_TX, LORA_ANT_BOOST, LORA_TCXO); #else // RFM95 Radio = new SX1276Generic(NULL, RFM95_SX1276, - LORA_SPI_MOSI, LORA_SPI_MISO, LORA_SPI_SCLK, LORA_CS, LORA_RESET, - LORA_DIO0, LORA_DIO1, LORA_DIO2, LORA_DIO3, LORA_DIO4, LORA_DIO5); + LORA_SPI_MOSI, LORA_SPI_MISO, LORA_SPI_SCLK, LORA_CS, LORA_RESET, + LORA_DIO0, LORA_DIO1, LORA_DIO2, LORA_DIO3, LORA_DIO4, LORA_DIO5); #endif - - uint8_t i; - bool isMaster = true; - - dprintf("SX1276 Ping Pong Demo Application" ); - dprintf("Freqency: %.1f", (double)RF_FREQUENCY/1000000.0); + + + + + dprintf("Aplicacion de comunicacion LoRa punto a punto" ); + dprintf("Frecuencia: %.1f", (double)RF_FREQUENCY/1000000.0); dprintf("TXPower: %d dBm", TX_OUTPUT_POWER); #if USE_MODEM_LORA == 1 dprintf("Bandwidth: %d Hz", LORA_BANDWIDTH); @@ -142,15 +169,15 @@ RadioEvents.RxDone = OnRxDone; RadioEvents.RxError = OnRxError; RadioEvents.TxTimeout = OnTxTimeout; - RadioEvents.RxTimeout = OnRxTimeout; + RadioEvents.RxTimeout = OnRxTimeout; if (Radio->Init( &RadioEvents ) == false) { while(1) { - dprintf("Radio could not be detected!"); - wait( 1 ); + dprintf("Radio could not be detected!"); + wait( 1 ); } } - + switch(Radio->DetectBoardType()) { case SX1276MB1LAS: if (DEBUG_MESSAGE) @@ -161,7 +188,7 @@ dprintf(" > Board Type: SX1276MB1LAS <"); case MURATA_SX1276: if (DEBUG_MESSAGE) - dprintf(" > Board Type: MURATA_SX1276_STM32L0 <"); + dprintf(" > Board Type: MURATA_SX1276_STM32L0 <"); break; case RFM95_SX1276: if (DEBUG_MESSAGE) @@ -171,198 +198,287 @@ dprintf(" > Board Type: unknown <"); } - Radio->SetChannel(RF_FREQUENCY ); + Radio->SetChannel(RF_FREQUENCY ); #if USE_MODEM_LORA == 1 - + if (LORA_FHSS_ENABLED) dprintf(" > LORA FHSS Mode <"); if (!LORA_FHSS_ENABLED) dprintf(" > LORA Mode <"); Radio->SetTxConfig( MODEM_LORA, TX_OUTPUT_POWER, 0, LORA_BANDWIDTH, - LORA_SPREADING_FACTOR, LORA_CODINGRATE, - LORA_PREAMBLE_LENGTH, LORA_FIX_LENGTH_PAYLOAD_ON, - LORA_CRC_ENABLED, LORA_FHSS_ENABLED, LORA_NB_SYMB_HOP, - LORA_IQ_INVERSION_ON, 2000 ); - + LORA_SPREADING_FACTOR, LORA_CODINGRATE, + LORA_PREAMBLE_LENGTH, LORA_FIX_LENGTH_PAYLOAD_ON, + LORA_CRC_ENABLED, LORA_FHSS_ENABLED, LORA_NB_SYMB_HOP, + LORA_IQ_INVERSION_ON, 2000 ); + Radio->SetRxConfig( MODEM_LORA, LORA_BANDWIDTH, LORA_SPREADING_FACTOR, - LORA_CODINGRATE, 0, LORA_PREAMBLE_LENGTH, - LORA_SYMBOL_TIMEOUT, LORA_FIX_LENGTH_PAYLOAD_ON, 0, - LORA_CRC_ENABLED, LORA_FHSS_ENABLED, LORA_NB_SYMB_HOP, - LORA_IQ_INVERSION_ON, true ); - + LORA_CODINGRATE, 0, LORA_PREAMBLE_LENGTH, + LORA_SYMBOL_TIMEOUT, LORA_FIX_LENGTH_PAYLOAD_ON, 0, + LORA_CRC_ENABLED, LORA_FHSS_ENABLED, LORA_NB_SYMB_HOP, + LORA_IQ_INVERSION_ON, true ); + #elif USE_MODEM_FSK == 1 dprintf(" > FSK Mode <"); Radio->SetTxConfig( MODEM_FSK, TX_OUTPUT_POWER, FSK_FDEV, 0, - FSK_DATARATE, 0, - FSK_PREAMBLE_LENGTH, FSK_FIX_LENGTH_PAYLOAD_ON, - FSK_CRC_ENABLED, 0, 0, 0, 2000 ); - + FSK_DATARATE, 0, + FSK_PREAMBLE_LENGTH, FSK_FIX_LENGTH_PAYLOAD_ON, + FSK_CRC_ENABLED, 0, 0, 0, 2000 ); + Radio->SetRxConfig( MODEM_FSK, FSK_BANDWIDTH, FSK_DATARATE, - 0, FSK_AFC_BANDWIDTH, FSK_PREAMBLE_LENGTH, - 0, FSK_FIX_LENGTH_PAYLOAD_ON, 0, FSK_CRC_ENABLED, - 0, 0, false, true ); - + 0, FSK_AFC_BANDWIDTH, FSK_PREAMBLE_LENGTH, + 0, FSK_FIX_LENGTH_PAYLOAD_ON, 0, FSK_CRC_ENABLED, + 0, 0, false, true ); + #else #error "Please define a modem in the compiler options." #endif - - if (DEBUG_MESSAGE) - dprintf("Starting Ping-Pong loop"); - + if (DEBUG_MESSAGE) + dprintf("Inicializando nodo"); + dprintf("EUI (ID): %s",EUI); + Radio->Rx( RX_TIMEOUT_VALUE ); - - while( 1 ) - { + + while( 1 ) { #ifdef TARGET_STM32L4 WatchDogUpdate(); #endif - - switch( State ) - { - case RX: - *led3 = 0; - if( isMaster == true ) - { - if( BufferSize > 0 ) - { - if( memcmp(Buffer, PongMsg, sizeof(PongMsg)) == 0 ) - { - *led = !*led; - dprintf( "...Pong" ); - // Send the next PING frame - memcpy(Buffer, PingMsg, sizeof(PingMsg)); - // We fill the buffer with numbers for the payload - for( i = sizeof(PingMsg); i < BufferSize; i++ ) - { - Buffer[i] = i - sizeof(PingMsg); - } - wait_ms( 10 ); - Radio->Send( Buffer, BufferSize ); + /* + const char EUI[] = "0A10";// 4 bytes que definen el identificador del dispositivo (copiar los bytes en string) + const char AppEUI[] = "AAAA";// 4 bytes que definen el identificador de la aplicacion (copiar los bytes en string) + 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 + 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. + char MsgRX[64] = "";// Mensaje de recepcion, carga el payload entrante a esta cadena. + char MsgRet[] = "RECIBIDO";// Para verificar el resultado del envio + char DestEUI[] = "0A01"; + string strRecepcion = ""; + uint16_t BufferSize = BUFFER_SIZE; + uint8_t *Buffer; + int reintentos=0; + string msjDeco=""; + char *retParse; + char *srcEUI; + char *msjDestEUI; + char *msjContent;*/ + + switch( State ) { + case RX: + reintentos=0; + *led = !*led; + //dprintf("Mensaje para depurar: %s",MsgRX); + msjDeco = MsgRX; + splitOnPosition(MsgRX, 0); + + dprintf("Source EUI: %s, Destination EUI: %s, Content: %s",srcEUI,msjDestEUI,msjContent); + strRecepcion = msjContent; + if(strcmp(EUI,msjDestEUI) == 0) { + dprintf("Mismo EUI, Soy el destinatario"); + + } else { + dprintf("Diferente EUI, ignorar mensaje"); + wait_ms( 500 ); + *led = !*led; + State = LOWPOWER; + break; + } + + if( BufferSize > 0 ) { + + if (strstr(msjContent, "RECIBIDO") != NULL) { + dprintf( "Mensaje recibido por el servidor correctamente" ); + } + if (strstr(msjContent, "ERROR") != NULL) { + dprintf( "Mensaje no fue recibido por el servidor correctamente" ); + } + if (strstr(msjContent, "DENIED") != NULL) { + dprintf( "Mensaje rechazado por el servidor correctamente" ); } - else if( memcmp(Buffer, PingMsg, sizeof(PingMsg)) == 0 ) - { // A master already exists then become a slave - dprintf( "...Ping" ); - *led = !*led; - isMaster = false; - // Send the next PONG frame - memcpy(Buffer, PongMsg, sizeof(PongMsg)); - // We fill the buffer with numbers for the payload - for( i = sizeof(PongMsg); i < BufferSize; i++ ) - { - Buffer[i] = i - sizeof(PongMsg); - } - wait_ms( 10 ); - Radio->Send( Buffer, BufferSize ); - } - else // valid reception but neither a PING or a PONG message - { // Set device as master ans start again - isMaster = true; - Radio->Rx( RX_TIMEOUT_VALUE ); - } + /*if( RecFound == true ) { + + dprintf( "Mensaje recibido por el servidor correctamente" ); + } else if(ErrorFound == true) { // Error en la recepcion + dprintf( "Mensaje no fue recibido por el servidor correctamente" ); + + } else if(DenFound == true) { // Negacion en la recepcion + dprintf( "Mensaje rechazado por el servidor correctamente" ); + + }*/ + } + wait_ms( 500 ); + *led = !*led; + State = LOWPOWER; + break; + case TX: + + //dprintf("Mensaje a enviar: %s",MsgTX); + *led3 = !*led3; + + if(reintentos<RETRIES) { + Radio->Rx( RX_TIMEOUT_VALUE ); + reintentos++; } - } - else - { - if( BufferSize > 0 ) - { - if( memcmp(Buffer, PingMsg, sizeof(PingMsg)) == 0 ) - { - *led = !*led; - dprintf( "...Ping" ); - // Send the reply to the PING string - memcpy(Buffer, PongMsg, sizeof(PongMsg)); - // We fill the buffer with numbers for the payload - for( i = sizeof(PongMsg); i < BufferSize; i++ ) - { - Buffer[i] = i - sizeof(PongMsg); - } - wait_ms( 10 ); - Radio->Send( Buffer, BufferSize ); + wait_ms( 500 ); + *led = !*led; + State = LOWPOWER; + break; + case RX_TIMEOUT: + if( isMaster == true ) { + // Send the next PING frame + memcpy(Buffer, MsgTX, sizeof(MsgTX)); + for( i = sizeof(MsgTX); i < BufferSize; i++ ) { + Buffer[i] = i - sizeof(MsgTX); } - else // valid reception but not a PING as expected - { // Set device as master and start again - isMaster = true; - Radio->Rx( RX_TIMEOUT_VALUE ); - } + wait_ms( 1000 ); + Radio->Send( Buffer, BufferSize ); + } else { + Radio->Rx( RX_TIMEOUT_VALUE ); + } + State = LOWPOWER; + break; + case RX_ERROR: + // We have received a Packet with a CRC error, send reply as if packet was correct + if( isMaster == true ) { + // Send the next PING frame + memcpy(Buffer, MsgTX, sizeof(MsgTX)); + for( i = 4; i < BufferSize; i++ ) { + Buffer[i] = i - 4; + } + wait_ms( 1000 ); + Radio->Send( Buffer, BufferSize ); + } else { + // Send the next PONG frame + memcpy(Buffer, MsgTX, sizeof(MsgTX)); + for( i = sizeof(MsgTX); i < BufferSize; i++ ) { + Buffer[i] = i - sizeof(MsgTX); + } + wait_ms( 1000 ); + Radio->Send( Buffer, BufferSize ); } - } - State = LOWPOWER; - break; - case TX: - *led3 = 1; - if( isMaster == true ) - { - dprintf("Ping..." ); - } - else - { - dprintf("Pong..." ); - } - Radio->Rx( RX_TIMEOUT_VALUE ); - State = LOWPOWER; - break; - case RX_TIMEOUT: - if( isMaster == true ) - { - // Send the next PING frame - memcpy(Buffer, PingMsg, sizeof(PingMsg)); - for( i = sizeof(PingMsg); i < BufferSize; i++ ) - { - Buffer[i] = i - sizeof(PingMsg); - } - wait_ms( 10 ); - Radio->Send( Buffer, BufferSize ); - } - else - { - Radio->Rx( RX_TIMEOUT_VALUE ); - } - State = LOWPOWER; - break; - case RX_ERROR: - // We have received a Packet with a CRC error, send reply as if packet was correct - if( isMaster == true ) - { - // Send the next PING frame - memcpy(Buffer, PingMsg, sizeof(PingMsg)); - for( i = 4; i < BufferSize; i++ ) - { - Buffer[i] = i - 4; - } - wait_ms( 10 ); - Radio->Send( Buffer, BufferSize ); - } - else - { - // Send the next PONG frame - memcpy(Buffer, PongMsg, sizeof(PongMsg)); - for( i = sizeof(PongMsg); i < BufferSize; i++ ) - { - Buffer[i] = i - sizeof(PongMsg); - } - wait_ms( 10 ); - Radio->Send( Buffer, BufferSize ); - } - State = LOWPOWER; - break; - case TX_TIMEOUT: - Radio->Rx( RX_TIMEOUT_VALUE ); - State = LOWPOWER; - break; - case LOWPOWER: - sleep(); - break; - default: - State = LOWPOWER; - break; - } + State = LOWPOWER; + break; + case TX_TIMEOUT: + Radio->Rx( RX_TIMEOUT_VALUE ); + State = LOWPOWER; + break; + case LOWPOWER: + sleep(); + break; + default: + State = LOWPOWER; + break; + } + } +} + +char *splitOnPosition(char *msj, int pos) +{ + int i=0; + char *substring = strtok (msj,"|"); + char *strOutput=""; + while (substring != NULL) { + //dprintf ("substring: %s, index: %d",substring,i); + + if(i == 0) { + srcEUI = substring; + } else if(i == 3) { + msjDestEUI = substring; + } else if(i == 4) { + msjContent = substring; + } else if(i > 4) { + strcat(msjContent," "); + strcat(msjContent,substring); + } + + if(i == pos) { + strOutput = substring; + } + substring = strtok (NULL, "|"); + i++; } + return strOutput; +} + +void onButtonEvent() +{ + reintentos=0; + float value = 4.85; + char valueStr[]=""; + sprintf(valueStr,"%.2f",value); + char variable[] = "temperatura"; + char dispositivo[] = "refrigerador1"; + strcpy(MsgTX, EUI); + strcat(MsgTX, "|"); + strcat(MsgTX, AppEUI); + strcat(MsgTX, "|"); + strcat(MsgTX, AppKey); + strcat(MsgTX, "|"); + strcat(MsgTX, DestEUI); + strcat(MsgTX, "|"); + strcat(MsgTX, valueStr); + strcat(MsgTX, " "); + strcat(MsgTX, variable); + strcat(MsgTX, " "); + strcat(MsgTX, dispositivo); + //strcat(MsgTX, ";"); + //*led = !*led; + //dprintf( "Enviando contenido al server" ); + memcpy(Buffer, MsgTX, sizeof(MsgTX)); + // Se llena el buffer con la informacion a enviar + for( int i = sizeof(MsgTX); i < BufferSize; i++ ) { + Buffer[i] = i - sizeof(MsgTX); + } + //wait_ms( 100 ); + Radio->Send( Buffer, BufferSize ); + Radio->Rx( RX_TIMEOUT_VALUE ); + +} + +void readAnalog() +{ + + + //meas_r = analog_value.read(); // Read the analog input value (value from 0.0 to 1.0 = full ADC conversion range) + meas_r = (float) rand()/RAND_MAX; // Para este caso se va a usar un numero aleatorio + //dprintf("random value: %.2f",meas_r); + //meas_r = 0.12; + meas_v = meas_r * 5.0; // Convierte el valor de 0 a 5 (Corriente) + reintentos=0; + char valueStr[]=""; + sprintf(valueStr,"%.2f",meas_v); + char variable[] = "corriente"; + char dispositivo[] = "nodo2_refrig_LoRa"; + strcpy(MsgTX, EUI); + strcat(MsgTX, "|"); + strcat(MsgTX, AppEUI); + strcat(MsgTX, "|"); + strcat(MsgTX, AppKey); + strcat(MsgTX, "|"); + strcat(MsgTX, DestEUI); + strcat(MsgTX, "|"); + strcat(MsgTX, valueStr); + strcat(MsgTX, " "); + strcat(MsgTX, variable); + strcat(MsgTX, " "); + strcat(MsgTX, dispositivo); + //strcat(MsgTX, ";"); + //*led = !*led; + //dprintf( "Enviando contenido al server" ); + memcpy(Buffer, MsgTX, sizeof(MsgTX)); + // Se llena el buffer con la informacion a enviar + for( int i = sizeof(MsgTX); i < BufferSize; i++ ) { + Buffer[i] = i - sizeof(MsgTX); + } + //wait_ms( 100 ); + Radio->Send( Buffer, BufferSize ); + Radio->Rx( RX_TIMEOUT_VALUE ); + + //wait(10); + } void OnTxDone(void *radio, void *userThisPtr, void *userData) @@ -381,7 +497,9 @@ State = RX; if (DEBUG_MESSAGE) dprintf("> OnRxDone: RssiValue=%d dBm, SnrValue=%d", rssi, snr); - dump("Data:", payload, size); + //dump("Data:", payload, size); + strcpy(MsgRX,(char*)payload); + //dprintf("Msj: %s", MsgRX); } void OnTxTimeout(void *radio, void *userThisPtr, void *userData)