![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
EIoT LoRa node 2 - Working ok
Dependencies: BufferedSerial SX1276GenericLib-node1 mbed
Fork of DISCO-L072CZ-LRWAN1_LoRa_node by
SX1276GenericPingPong/GenericPingPong.cpp@13:c2c91cc0467d, 2018-10-08 (annotated)
- Committer:
- sagilar
- Date:
- Mon Oct 08 16:11:05 2018 +0000
- Revision:
- 13:c2c91cc0467d
- Parent:
- 12:a6a37ba1feff
EIoT LoRa node 2
Who changed what in which revision?
User | Revision | Line number | New 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:a6a37ba1feff | 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:a6a37ba1feff | 13 | #include <string> |
sagilar | 12:a6a37ba1feff | 14 | #include "rtos.h" |
sagilar | 13:c2c91cc0467d | 15 | #include "ACS712.h" |
sagilar | 13:c2c91cc0467d | 16 | #include <math.h> |
Helmut64 | 0:c43b6919ae15 | 17 | |
Helmut64 | 0:c43b6919ae15 | 18 | #ifdef FEATURE_LORA |
Helmut64 | 0:c43b6919ae15 | 19 | |
Helmut64 | 0:c43b6919ae15 | 20 | /* Set this flag to '1' to display debug messages on the console */ |
Helmut64 | 0:c43b6919ae15 | 21 | #define DEBUG_MESSAGE 1 |
Helmut64 | 0:c43b6919ae15 | 22 | |
Helmut64 | 0:c43b6919ae15 | 23 | /* Set this flag to '1' to use the LoRa modulation or to '0' to use FSK modulation */ |
Helmut64 | 0:c43b6919ae15 | 24 | #define USE_MODEM_LORA 1 |
Helmut64 | 0:c43b6919ae15 | 25 | #define USE_MODEM_FSK !USE_MODEM_LORA |
sagilar | 12:a6a37ba1feff | 26 | #define RF_FREQUENCY RF_FREQUENCY_915_0 // Hz |
sagilar | 12:a6a37ba1feff | 27 | #define TX_OUTPUT_POWER 20 // 20 dBm |
Helmut64 | 0:c43b6919ae15 | 28 | |
Helmut64 | 0:c43b6919ae15 | 29 | #if USE_MODEM_LORA == 1 |
Helmut64 | 0:c43b6919ae15 | 30 | |
Helmut64 | 7:6a8a82bfb0c6 | 31 | #define LORA_BANDWIDTH 125000 // LoRa default, details in SX1276::BandwidthMap |
Helmut64 | 0:c43b6919ae15 | 32 | #define LORA_SPREADING_FACTOR LORA_SF7 |
Helmut64 | 0:c43b6919ae15 | 33 | #define LORA_CODINGRATE LORA_ERROR_CODING_RATE_4_5 |
Helmut64 | 0:c43b6919ae15 | 34 | |
Helmut64 | 0:c43b6919ae15 | 35 | #define LORA_PREAMBLE_LENGTH 8 // Same for Tx and Rx |
Helmut64 | 0:c43b6919ae15 | 36 | #define LORA_SYMBOL_TIMEOUT 5 // Symbols |
Helmut64 | 0:c43b6919ae15 | 37 | #define LORA_FIX_LENGTH_PAYLOAD_ON false |
sagilar | 12:a6a37ba1feff | 38 | #define LORA_FHSS_ENABLED true |
sagilar | 12:a6a37ba1feff | 39 | #define LORA_NB_SYMB_HOP 4 |
Helmut64 | 0:c43b6919ae15 | 40 | #define LORA_IQ_INVERSION_ON false |
Helmut64 | 0:c43b6919ae15 | 41 | #define LORA_CRC_ENABLED true |
sagilar | 12:a6a37ba1feff | 42 | |
Helmut64 | 0:c43b6919ae15 | 43 | #elif USE_MODEM_FSK == 1 |
Helmut64 | 0:c43b6919ae15 | 44 | |
Helmut64 | 0:c43b6919ae15 | 45 | #define FSK_FDEV 25000 // Hz |
Helmut64 | 0:c43b6919ae15 | 46 | #define FSK_DATARATE 19200 // bps |
Helmut64 | 0:c43b6919ae15 | 47 | #define FSK_BANDWIDTH 50000 // Hz |
Helmut64 | 0:c43b6919ae15 | 48 | #define FSK_AFC_BANDWIDTH 83333 // Hz |
Helmut64 | 0:c43b6919ae15 | 49 | #define FSK_PREAMBLE_LENGTH 5 // Same for Tx and Rx |
Helmut64 | 0:c43b6919ae15 | 50 | #define FSK_FIX_LENGTH_PAYLOAD_ON false |
Helmut64 | 0:c43b6919ae15 | 51 | #define FSK_CRC_ENABLED true |
sagilar | 12:a6a37ba1feff | 52 | |
Helmut64 | 0:c43b6919ae15 | 53 | #else |
sagilar | 12:a6a37ba1feff | 54 | #error "Please define a modem in the compiler options." |
sagilar | 12:a6a37ba1feff | 55 | #endif |
Helmut64 | 0:c43b6919ae15 | 56 | |
Helmut64 | 0:c43b6919ae15 | 57 | |
Helmut64 | 0:c43b6919ae15 | 58 | #define RX_TIMEOUT_VALUE 3500 // in ms |
Helmut64 | 0:c43b6919ae15 | 59 | |
Helmut64 | 0:c43b6919ae15 | 60 | //#define BUFFER_SIZE 32 // Define the payload size here |
Helmut64 | 0:c43b6919ae15 | 61 | #define BUFFER_SIZE 64 // Define the payload size here |
sagilar | 12:a6a37ba1feff | 62 | #define RETRIES 3 // Se define la cantidad de intentos de envio despues de recibir error |
Helmut64 | 0:c43b6919ae15 | 63 | /* |
Helmut64 | 0:c43b6919ae15 | 64 | * Global variables declarations |
Helmut64 | 0:c43b6919ae15 | 65 | */ |
sagilar | 12:a6a37ba1feff | 66 | typedef enum { |
Helmut64 | 0:c43b6919ae15 | 67 | LOWPOWER = 0, |
Helmut64 | 0:c43b6919ae15 | 68 | IDLE, |
sagilar | 12:a6a37ba1feff | 69 | |
Helmut64 | 0:c43b6919ae15 | 70 | RX, |
Helmut64 | 0:c43b6919ae15 | 71 | RX_TIMEOUT, |
Helmut64 | 0:c43b6919ae15 | 72 | RX_ERROR, |
sagilar | 12:a6a37ba1feff | 73 | |
Helmut64 | 0:c43b6919ae15 | 74 | TX, |
Helmut64 | 0:c43b6919ae15 | 75 | TX_TIMEOUT, |
sagilar | 12:a6a37ba1feff | 76 | |
Helmut64 | 0:c43b6919ae15 | 77 | CAD, |
Helmut64 | 0:c43b6919ae15 | 78 | CAD_DONE |
Helmut64 | 0:c43b6919ae15 | 79 | } AppStates_t; |
Helmut64 | 0:c43b6919ae15 | 80 | |
Helmut64 | 0:c43b6919ae15 | 81 | volatile AppStates_t State = LOWPOWER; |
Helmut64 | 0:c43b6919ae15 | 82 | |
Helmut64 | 0:c43b6919ae15 | 83 | /*! |
Helmut64 | 0:c43b6919ae15 | 84 | * Radio events function pointer |
Helmut64 | 0:c43b6919ae15 | 85 | */ |
Helmut64 | 0:c43b6919ae15 | 86 | static RadioEvents_t RadioEvents; |
Helmut64 | 0:c43b6919ae15 | 87 | |
Helmut64 | 0:c43b6919ae15 | 88 | /* |
Helmut64 | 0:c43b6919ae15 | 89 | * Global variables declarations |
Helmut64 | 0:c43b6919ae15 | 90 | */ |
Helmut64 | 0:c43b6919ae15 | 91 | SX1276Generic *Radio; |
Helmut64 | 0:c43b6919ae15 | 92 | |
Helmut64 | 0:c43b6919ae15 | 93 | |
sagilar | 12:a6a37ba1feff | 94 | //const uint8_t PingMsg[] = { 0xff, 0xff, 0x00, 0x00, 'P', 'I', 'N', 'G'};// "PING"; |
sagilar | 12:a6a37ba1feff | 95 | //const uint8_t PongMsg[] = { 0xff, 0xff, 0x00, 0x00, 'P', 'O', 'N', 'G'};// "PONG"; |
sagilar | 12:a6a37ba1feff | 96 | //const uint8_t PingMsg[] = { 'M', 'S', 'J', ' ','G','I','L'};// "PING"; |
sagilar | 12:a6a37ba1feff | 97 | //const uint8_t PongMsg[] = { 'R', 'P', 'T', 'A',' ','G','I','L'};// "PONG"; |
sagilar | 13:c2c91cc0467d | 98 | const char EUI[] = "0E20";// 4 bytes que definen el identificador del dispositivo (copiar los bytes en string) |
sagilar | 13:c2c91cc0467d | 99 | const char nombreDispoitivo[] = "nodo2_EIoT_LoRa"; |
sagilar | 12:a6a37ba1feff | 100 | const char AppEUI[] = "AAAA";// 4 bytes que definen el identificador de la aplicacion (copiar los bytes en string) |
sagilar | 12:a6a37ba1feff | 101 | 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:a6a37ba1feff | 102 | 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:a6a37ba1feff | 103 | char MsgRX[64] = "";// Mensaje de recepcion, carga el payload entrante a esta cadena. |
sagilar | 12:a6a37ba1feff | 104 | char MsgRet[] = "RECIBIDO";// Para verificar el resultado del envio |
sagilar | 12:a6a37ba1feff | 105 | char DestEUI[] = "0A01"; //Gateway Server |
sagilar | 12:a6a37ba1feff | 106 | string strRecepcion = ""; |
Helmut64 | 0:c43b6919ae15 | 107 | uint16_t BufferSize = BUFFER_SIZE; |
Helmut64 | 0:c43b6919ae15 | 108 | uint8_t *Buffer; |
sagilar | 12:a6a37ba1feff | 109 | int reintentos=0; |
sagilar | 12:a6a37ba1feff | 110 | string msjDeco=""; |
sagilar | 12:a6a37ba1feff | 111 | char *retParse; |
sagilar | 12:a6a37ba1feff | 112 | char *srcEUI; |
sagilar | 12:a6a37ba1feff | 113 | char *msjDestEUI; |
sagilar | 12:a6a37ba1feff | 114 | char *msjContent; |
sagilar | 13:c2c91cc0467d | 115 | |
sagilar | 13:c2c91cc0467d | 116 | //ACS712 cur_sensor(A0,1.0,5); |
sagilar | 13:c2c91cc0467d | 117 | //ACS712 cur_sensor(A0); |
sagilar | 13:c2c91cc0467d | 118 | AnalogIn analog_value_current(A0); |
sagilar | 13:c2c91cc0467d | 119 | AnalogIn analog_value_temp(A2); |
sagilar | 12:a6a37ba1feff | 120 | //RtosTimer analog_read_timer(readAnalog, osTimerPeriodic); |
sagilar | 12:a6a37ba1feff | 121 | |
sagilar | 12:a6a37ba1feff | 122 | |
sagilar | 13:c2c91cc0467d | 123 | float meas_r1 = 0.0; |
sagilar | 13:c2c91cc0467d | 124 | float meas_v1 = 0.0; |
sagilar | 13:c2c91cc0467d | 125 | float meas_r2 = 0.0; |
sagilar | 13:c2c91cc0467d | 126 | float meas_v2 = 0.0; |
sagilar | 13:c2c91cc0467d | 127 | float inst_current = 0.0; |
sagilar | 13:c2c91cc0467d | 128 | float inst_current_d1 = 0.0; |
sagilar | 13:c2c91cc0467d | 129 | float mean_current = 0.0; |
sagilar | 13:c2c91cc0467d | 130 | float max_inst_current = 0.0; |
sagilar | 13:c2c91cc0467d | 131 | int conteoSenal = 0; |
sagilar | 13:c2c91cc0467d | 132 | float inst_power = 0.0; |
sagilar | 13:c2c91cc0467d | 133 | float mean_power = 0.0; |
sagilar | 13:c2c91cc0467d | 134 | float max_power = 0.0; |
sagilar | 13:c2c91cc0467d | 135 | float min_power = 0.0; |
sagilar | 13:c2c91cc0467d | 136 | float temperature = 0.0; |
sagilar | 13:c2c91cc0467d | 137 | const float FACTOR_CUR = 1.48; |
sagilar | 13:c2c91cc0467d | 138 | const float RMS_VOLTAGE = 9.4; |
sagilar | 13:c2c91cc0467d | 139 | const float SENS = 185.0; |
sagilar | 13:c2c91cc0467d | 140 | const float VDC_STATIC_MV = 1540.0; |
sagilar | 13:c2c91cc0467d | 141 | Timer tiempo; |
sagilar | 13:c2c91cc0467d | 142 | //const int SAMP_RATIO = 50; |
sagilar | 13:c2c91cc0467d | 143 | bool sending_temp = false; |
sagilar | 13:c2c91cc0467d | 144 | bool sending_electric = false; |
sagilar | 13:c2c91cc0467d | 145 | Ticker tick_current; |
sagilar | 13:c2c91cc0467d | 146 | Ticker tick_send_electric; |
sagilar | 13:c2c91cc0467d | 147 | Ticker tick_temp; |
sagilar | 13:c2c91cc0467d | 148 | DigitalOut *led3; |
Helmut64 | 0:c43b6919ae15 | 149 | |
sagilar | 13:c2c91cc0467d | 150 | //Filtro peak @ 3000 Hz de muestreo |
sagilar | 13:c2c91cc0467d | 151 | double v_sub1 = 0.0; |
sagilar | 13:c2c91cc0467d | 152 | double v_sub2 = 0.0; |
sagilar | 13:c2c91cc0467d | 153 | double a0= 0.031474551558414499, a1= 0, a2 = -0.031474551558414499, b0 = 1, b1 = -1.9275262288479234, b2 = 0.937050896883171, gc= 1.0; |
sagilar | 13:c2c91cc0467d | 154 | // Filtro Pasa alta @ 3000 Hz de muestreo fc = 5Hz; |
sagilar | 13:c2c91cc0467d | 155 | double vh_sub1 = 0.0; |
sagilar | 13:c2c91cc0467d | 156 | double vh_sub2 = 0.0; |
sagilar | 13:c2c91cc0467d | 157 | double ah0= 1, ah1= -2, ah2 = 1, bh0 = 1, bh1 = -1.9851906578962613, bh2 = 0.98529951312821451, gh= 0.99262254275611894; |
sagilar | 13:c2c91cc0467d | 158 | //Filtro pasa baja @ 3000 Hz de muestreo, fc = 65Hz; |
sagilar | 13:c2c91cc0467d | 159 | double vl_sub1 = 0.0; |
sagilar | 13:c2c91cc0467d | 160 | double vl_sub2 = 0.0; |
sagilar | 13:c2c91cc0467d | 161 | double al0= 1, al1= 2, al2 = 1, bl0 = 1, bl1 = -0.62020410288672889,bl2 = 0.24040820577345759, gl = 0.1550510257216822; |
Helmut64 | 0:c43b6919ae15 | 162 | |
Helmut64 | 0:c43b6919ae15 | 163 | |
sagilar | 12:a6a37ba1feff | 164 | int SX1276PingPong() |
Helmut64 | 0:c43b6919ae15 | 165 | { |
sagilar | 13:c2c91cc0467d | 166 | |
sagilar | 13:c2c91cc0467d | 167 | //tick_current.attach(&readCurrent,1.0/(60.0 * SAMP_RATIO)); |
sagilar | 13:c2c91cc0467d | 168 | tick_current.attach(&readCurrent,5.0); |
sagilar | 12:a6a37ba1feff | 169 | |
sagilar | 13:c2c91cc0467d | 170 | tick_send_electric.attach(&sendElectric,60.0); |
sagilar | 13:c2c91cc0467d | 171 | |
sagilar | 13:c2c91cc0467d | 172 | tick_temp.attach(&readTemperature,120.0); |
sagilar | 12:a6a37ba1feff | 173 | uint8_t i; |
sagilar | 12:a6a37ba1feff | 174 | bool isMaster = true; |
sagilar | 12:a6a37ba1feff | 175 | |
sagilar | 12:a6a37ba1feff | 176 | |
sagilar | 12:a6a37ba1feff | 177 | |
Helmut64 | 0:c43b6919ae15 | 178 | #if( defined ( TARGET_KL25Z ) || defined ( TARGET_LPC11U6X ) ) |
Helmut64 | 0:c43b6919ae15 | 179 | DigitalOut *led = new DigitalOut(LED2); |
bcostm | 11:9d7409ebfa57 | 180 | #elif defined(TARGET_NUCLEO_L073RZ) || defined(TARGET_DISCO_L072CZ_LRWAN1) |
Helmut64 | 0:c43b6919ae15 | 181 | DigitalOut *led = new DigitalOut(LED4); // RX red |
Helmut64 | 0:c43b6919ae15 | 182 | led3 = new DigitalOut(LED3); // TX blue |
Helmut64 | 0:c43b6919ae15 | 183 | #else |
Helmut64 | 0:c43b6919ae15 | 184 | DigitalOut *led = new DigitalOut(LED1); |
Helmut64 | 0:c43b6919ae15 | 185 | led3 = led; |
Helmut64 | 0:c43b6919ae15 | 186 | #endif |
sagilar | 12:a6a37ba1feff | 187 | |
Helmut64 | 0:c43b6919ae15 | 188 | Buffer = new uint8_t[BUFFER_SIZE]; |
Helmut64 | 0:c43b6919ae15 | 189 | *led3 = 1; |
Helmut64 | 0:c43b6919ae15 | 190 | |
Helmut64 | 0:c43b6919ae15 | 191 | #ifdef B_L072Z_LRWAN1_LORA |
Helmut64 | 0:c43b6919ae15 | 192 | Radio = new SX1276Generic(NULL, MURATA_SX1276, |
sagilar | 12:a6a37ba1feff | 193 | LORA_SPI_MOSI, LORA_SPI_MISO, LORA_SPI_SCLK, LORA_CS, LORA_RESET, |
sagilar | 12:a6a37ba1feff | 194 | LORA_DIO0, LORA_DIO1, LORA_DIO2, LORA_DIO3, LORA_DIO4, LORA_DIO5, |
sagilar | 12:a6a37ba1feff | 195 | LORA_ANT_RX, LORA_ANT_TX, LORA_ANT_BOOST, LORA_TCXO); |
Helmut64 | 0:c43b6919ae15 | 196 | #else // RFM95 |
Helmut64 | 0:c43b6919ae15 | 197 | Radio = new SX1276Generic(NULL, RFM95_SX1276, |
sagilar | 12:a6a37ba1feff | 198 | LORA_SPI_MOSI, LORA_SPI_MISO, LORA_SPI_SCLK, LORA_CS, LORA_RESET, |
sagilar | 12:a6a37ba1feff | 199 | LORA_DIO0, LORA_DIO1, LORA_DIO2, LORA_DIO3, LORA_DIO4, LORA_DIO5); |
Helmut64 | 0:c43b6919ae15 | 200 | |
Helmut64 | 0:c43b6919ae15 | 201 | #endif |
sagilar | 12:a6a37ba1feff | 202 | |
sagilar | 12:a6a37ba1feff | 203 | dprintf("Aplicacion de comunicacion LoRa punto a punto" ); |
sagilar | 12:a6a37ba1feff | 204 | dprintf("Frecuencia: %.1f", (double)RF_FREQUENCY/1000000.0); |
Helmut64 | 0:c43b6919ae15 | 205 | dprintf("TXPower: %d dBm", TX_OUTPUT_POWER); |
Helmut64 | 0:c43b6919ae15 | 206 | #if USE_MODEM_LORA == 1 |
Helmut64 | 8:3b0d7b4ff28f | 207 | dprintf("Bandwidth: %d Hz", LORA_BANDWIDTH); |
Helmut64 | 0:c43b6919ae15 | 208 | dprintf("Spreading factor: SF%d", LORA_SPREADING_FACTOR); |
Helmut64 | 0:c43b6919ae15 | 209 | #elif USE_MODEM_FSK == 1 |
Helmut64 | 0:c43b6919ae15 | 210 | dprintf("Bandwidth: %d kHz", FSK_BANDWIDTH); |
Helmut64 | 0:c43b6919ae15 | 211 | dprintf("Baudrate: %d", FSK_DATARATE); |
Helmut64 | 0:c43b6919ae15 | 212 | #endif |
Helmut64 | 0:c43b6919ae15 | 213 | // Initialize Radio driver |
Helmut64 | 0:c43b6919ae15 | 214 | RadioEvents.TxDone = OnTxDone; |
Helmut64 | 0:c43b6919ae15 | 215 | RadioEvents.RxDone = OnRxDone; |
Helmut64 | 0:c43b6919ae15 | 216 | RadioEvents.RxError = OnRxError; |
Helmut64 | 0:c43b6919ae15 | 217 | RadioEvents.TxTimeout = OnTxTimeout; |
sagilar | 12:a6a37ba1feff | 218 | RadioEvents.RxTimeout = OnRxTimeout; |
Helmut64 | 6:1b598b0e52e4 | 219 | if (Radio->Init( &RadioEvents ) == false) { |
Helmut64 | 6:1b598b0e52e4 | 220 | while(1) { |
sagilar | 12:a6a37ba1feff | 221 | dprintf("Radio could not be detected!"); |
sagilar | 12:a6a37ba1feff | 222 | wait( 1 ); |
Helmut64 | 6:1b598b0e52e4 | 223 | } |
Helmut64 | 0:c43b6919ae15 | 224 | } |
Helmut64 | 6:1b598b0e52e4 | 225 | |
sagilar | 12:a6a37ba1feff | 226 | |
Helmut64 | 0:c43b6919ae15 | 227 | switch(Radio->DetectBoardType()) { |
Helmut64 | 0:c43b6919ae15 | 228 | case SX1276MB1LAS: |
Helmut64 | 0:c43b6919ae15 | 229 | if (DEBUG_MESSAGE) |
Helmut64 | 0:c43b6919ae15 | 230 | dprintf(" > Board Type: SX1276MB1LAS <"); |
Helmut64 | 0:c43b6919ae15 | 231 | break; |
Helmut64 | 0:c43b6919ae15 | 232 | case SX1276MB1MAS: |
Helmut64 | 0:c43b6919ae15 | 233 | if (DEBUG_MESSAGE) |
Helmut64 | 0:c43b6919ae15 | 234 | dprintf(" > Board Type: SX1276MB1LAS <"); |
Helmut64 | 0:c43b6919ae15 | 235 | case MURATA_SX1276: |
Helmut64 | 0:c43b6919ae15 | 236 | if (DEBUG_MESSAGE) |
sagilar | 12:a6a37ba1feff | 237 | dprintf(" > Board Type: MURATA_SX1276_STM32L0 <"); |
Helmut64 | 0:c43b6919ae15 | 238 | break; |
Helmut64 | 0:c43b6919ae15 | 239 | case RFM95_SX1276: |
Helmut64 | 0:c43b6919ae15 | 240 | if (DEBUG_MESSAGE) |
Helmut64 | 0:c43b6919ae15 | 241 | dprintf(" > HopeRF RFM95xx <"); |
Helmut64 | 0:c43b6919ae15 | 242 | break; |
Helmut64 | 0:c43b6919ae15 | 243 | default: |
Helmut64 | 0:c43b6919ae15 | 244 | dprintf(" > Board Type: unknown <"); |
Helmut64 | 0:c43b6919ae15 | 245 | } |
Helmut64 | 0:c43b6919ae15 | 246 | |
sagilar | 12:a6a37ba1feff | 247 | Radio->SetChannel(RF_FREQUENCY ); |
Helmut64 | 0:c43b6919ae15 | 248 | |
Helmut64 | 0:c43b6919ae15 | 249 | #if USE_MODEM_LORA == 1 |
sagilar | 12:a6a37ba1feff | 250 | |
Helmut64 | 0:c43b6919ae15 | 251 | if (LORA_FHSS_ENABLED) |
Helmut64 | 0:c43b6919ae15 | 252 | dprintf(" > LORA FHSS Mode <"); |
Helmut64 | 0:c43b6919ae15 | 253 | if (!LORA_FHSS_ENABLED) |
Helmut64 | 0:c43b6919ae15 | 254 | dprintf(" > LORA Mode <"); |
Helmut64 | 0:c43b6919ae15 | 255 | |
Helmut64 | 0:c43b6919ae15 | 256 | Radio->SetTxConfig( MODEM_LORA, TX_OUTPUT_POWER, 0, LORA_BANDWIDTH, |
sagilar | 12:a6a37ba1feff | 257 | LORA_SPREADING_FACTOR, LORA_CODINGRATE, |
sagilar | 12:a6a37ba1feff | 258 | LORA_PREAMBLE_LENGTH, LORA_FIX_LENGTH_PAYLOAD_ON, |
sagilar | 12:a6a37ba1feff | 259 | LORA_CRC_ENABLED, LORA_FHSS_ENABLED, LORA_NB_SYMB_HOP, |
sagilar | 12:a6a37ba1feff | 260 | LORA_IQ_INVERSION_ON, 2000 ); |
sagilar | 12:a6a37ba1feff | 261 | |
Helmut64 | 0:c43b6919ae15 | 262 | Radio->SetRxConfig( MODEM_LORA, LORA_BANDWIDTH, LORA_SPREADING_FACTOR, |
sagilar | 12:a6a37ba1feff | 263 | LORA_CODINGRATE, 0, LORA_PREAMBLE_LENGTH, |
sagilar | 12:a6a37ba1feff | 264 | LORA_SYMBOL_TIMEOUT, LORA_FIX_LENGTH_PAYLOAD_ON, 0, |
sagilar | 12:a6a37ba1feff | 265 | LORA_CRC_ENABLED, LORA_FHSS_ENABLED, LORA_NB_SYMB_HOP, |
sagilar | 12:a6a37ba1feff | 266 | LORA_IQ_INVERSION_ON, true ); |
sagilar | 12:a6a37ba1feff | 267 | |
Helmut64 | 0:c43b6919ae15 | 268 | #elif USE_MODEM_FSK == 1 |
Helmut64 | 0:c43b6919ae15 | 269 | |
Helmut64 | 0:c43b6919ae15 | 270 | dprintf(" > FSK Mode <"); |
Helmut64 | 0:c43b6919ae15 | 271 | Radio->SetTxConfig( MODEM_FSK, TX_OUTPUT_POWER, FSK_FDEV, 0, |
sagilar | 12:a6a37ba1feff | 272 | FSK_DATARATE, 0, |
sagilar | 12:a6a37ba1feff | 273 | FSK_PREAMBLE_LENGTH, FSK_FIX_LENGTH_PAYLOAD_ON, |
sagilar | 12:a6a37ba1feff | 274 | FSK_CRC_ENABLED, 0, 0, 0, 2000 ); |
sagilar | 12:a6a37ba1feff | 275 | |
Helmut64 | 0:c43b6919ae15 | 276 | Radio->SetRxConfig( MODEM_FSK, FSK_BANDWIDTH, FSK_DATARATE, |
sagilar | 12:a6a37ba1feff | 277 | 0, FSK_AFC_BANDWIDTH, FSK_PREAMBLE_LENGTH, |
sagilar | 12:a6a37ba1feff | 278 | 0, FSK_FIX_LENGTH_PAYLOAD_ON, 0, FSK_CRC_ENABLED, |
sagilar | 12:a6a37ba1feff | 279 | 0, 0, false, true ); |
sagilar | 12:a6a37ba1feff | 280 | |
Helmut64 | 0:c43b6919ae15 | 281 | #else |
Helmut64 | 0:c43b6919ae15 | 282 | |
Helmut64 | 0:c43b6919ae15 | 283 | #error "Please define a modem in the compiler options." |
Helmut64 | 0:c43b6919ae15 | 284 | |
Helmut64 | 0:c43b6919ae15 | 285 | #endif |
Helmut64 | 0:c43b6919ae15 | 286 | |
sagilar | 12:a6a37ba1feff | 287 | if (DEBUG_MESSAGE) |
sagilar | 12:a6a37ba1feff | 288 | dprintf("Inicializando nodo"); |
sagilar | 12:a6a37ba1feff | 289 | dprintf("EUI (ID): %s",EUI); |
sagilar | 12:a6a37ba1feff | 290 | |
Helmut64 | 0:c43b6919ae15 | 291 | Radio->Rx( RX_TIMEOUT_VALUE ); |
sagilar | 12:a6a37ba1feff | 292 | |
sagilar | 12:a6a37ba1feff | 293 | while( 1 ) { |
Helmut64 | 0:c43b6919ae15 | 294 | #ifdef TARGET_STM32L4 |
Helmut64 | 0:c43b6919ae15 | 295 | WatchDogUpdate(); |
Helmut64 | 0:c43b6919ae15 | 296 | #endif |
sagilar | 12:a6a37ba1feff | 297 | /* |
sagilar | 12:a6a37ba1feff | 298 | const char EUI[] = "0A10";// 4 bytes que definen el identificador del dispositivo (copiar los bytes en string) |
sagilar | 12:a6a37ba1feff | 299 | const char AppEUI[] = "AAAA";// 4 bytes que definen el identificador de la aplicacion (copiar los bytes en string) |
sagilar | 12:a6a37ba1feff | 300 | 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:a6a37ba1feff | 301 | 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:a6a37ba1feff | 302 | char MsgRX[64] = "";// Mensaje de recepcion, carga el payload entrante a esta cadena. |
sagilar | 12:a6a37ba1feff | 303 | char MsgRet[] = "RECIBIDO";// Para verificar el resultado del envio |
sagilar | 12:a6a37ba1feff | 304 | char DestEUI[] = "0A01"; |
sagilar | 12:a6a37ba1feff | 305 | string strRecepcion = ""; |
sagilar | 12:a6a37ba1feff | 306 | uint16_t BufferSize = BUFFER_SIZE; |
sagilar | 12:a6a37ba1feff | 307 | uint8_t *Buffer; |
sagilar | 12:a6a37ba1feff | 308 | int reintentos=0; |
sagilar | 12:a6a37ba1feff | 309 | string msjDeco=""; |
sagilar | 12:a6a37ba1feff | 310 | char *retParse; |
sagilar | 12:a6a37ba1feff | 311 | char *srcEUI; |
sagilar | 12:a6a37ba1feff | 312 | char *msjDestEUI; |
sagilar | 12:a6a37ba1feff | 313 | char *msjContent;*/ |
sagilar | 12:a6a37ba1feff | 314 | |
sagilar | 12:a6a37ba1feff | 315 | switch( State ) { |
sagilar | 12:a6a37ba1feff | 316 | case RX: |
sagilar | 12:a6a37ba1feff | 317 | reintentos=0; |
sagilar | 12:a6a37ba1feff | 318 | *led = !*led; |
sagilar | 12:a6a37ba1feff | 319 | //dprintf("Mensaje para depurar: %s",MsgRX); |
sagilar | 12:a6a37ba1feff | 320 | msjDeco = MsgRX; |
sagilar | 12:a6a37ba1feff | 321 | splitOnPosition(MsgRX, 0); |
sagilar | 12:a6a37ba1feff | 322 | |
sagilar | 12:a6a37ba1feff | 323 | dprintf("Source EUI: %s, Destination EUI: %s, Content: %s",srcEUI,msjDestEUI,msjContent); |
sagilar | 12:a6a37ba1feff | 324 | strRecepcion = msjContent; |
sagilar | 12:a6a37ba1feff | 325 | if(strcmp(EUI,msjDestEUI) == 0) { |
sagilar | 12:a6a37ba1feff | 326 | dprintf("Mismo EUI, Soy el destinatario"); |
sagilar | 12:a6a37ba1feff | 327 | |
sagilar | 12:a6a37ba1feff | 328 | } else { |
sagilar | 12:a6a37ba1feff | 329 | dprintf("Diferente EUI, ignorar mensaje"); |
sagilar | 12:a6a37ba1feff | 330 | wait_ms( 500 ); |
sagilar | 12:a6a37ba1feff | 331 | *led = !*led; |
sagilar | 12:a6a37ba1feff | 332 | State = LOWPOWER; |
sagilar | 12:a6a37ba1feff | 333 | break; |
sagilar | 12:a6a37ba1feff | 334 | } |
sagilar | 12:a6a37ba1feff | 335 | |
sagilar | 12:a6a37ba1feff | 336 | |
sagilar | 12:a6a37ba1feff | 337 | if( BufferSize > 0 ) { |
sagilar | 12:a6a37ba1feff | 338 | |
sagilar | 12:a6a37ba1feff | 339 | if (strstr(msjContent, "RECIBIDO") != NULL) { |
sagilar | 12:a6a37ba1feff | 340 | dprintf( "Mensaje recibido por el servidor correctamente" ); |
sagilar | 12:a6a37ba1feff | 341 | } |
sagilar | 12:a6a37ba1feff | 342 | if (strstr(msjContent, "ERROR") != NULL) { |
sagilar | 12:a6a37ba1feff | 343 | dprintf( "Mensaje no fue recibido por el servidor correctamente" ); |
sagilar | 12:a6a37ba1feff | 344 | } |
sagilar | 12:a6a37ba1feff | 345 | if (strstr(msjContent, "DENIED") != NULL) { |
sagilar | 12:a6a37ba1feff | 346 | dprintf( "Mensaje rechazado por el servidor correctamente" ); |
Helmut64 | 0:c43b6919ae15 | 347 | } |
sagilar | 12:a6a37ba1feff | 348 | /*if( RecFound == true ) { |
sagilar | 12:a6a37ba1feff | 349 | |
sagilar | 12:a6a37ba1feff | 350 | dprintf( "Mensaje recibido por el servidor correctamente" ); |
sagilar | 12:a6a37ba1feff | 351 | } else if(ErrorFound == true) { // Error en la recepcion |
sagilar | 12:a6a37ba1feff | 352 | dprintf( "Mensaje no fue recibido por el servidor correctamente" ); |
sagilar | 12:a6a37ba1feff | 353 | |
sagilar | 12:a6a37ba1feff | 354 | } else if(DenFound == true) { // Negacion en la recepcion |
sagilar | 12:a6a37ba1feff | 355 | dprintf( "Mensaje rechazado por el servidor correctamente" ); |
sagilar | 12:a6a37ba1feff | 356 | |
sagilar | 12:a6a37ba1feff | 357 | }*/ |
sagilar | 12:a6a37ba1feff | 358 | } |
sagilar | 12:a6a37ba1feff | 359 | wait_ms( 500 ); |
sagilar | 12:a6a37ba1feff | 360 | *led = !*led; |
sagilar | 12:a6a37ba1feff | 361 | State = LOWPOWER; |
sagilar | 12:a6a37ba1feff | 362 | break; |
sagilar | 12:a6a37ba1feff | 363 | case TX: |
sagilar | 12:a6a37ba1feff | 364 | |
sagilar | 12:a6a37ba1feff | 365 | //dprintf("Mensaje a enviar: %s",MsgTX); |
sagilar | 12:a6a37ba1feff | 366 | *led3 = !*led3; |
sagilar | 12:a6a37ba1feff | 367 | |
sagilar | 12:a6a37ba1feff | 368 | if(reintentos<RETRIES) { |
sagilar | 12:a6a37ba1feff | 369 | Radio->Rx( RX_TIMEOUT_VALUE ); |
sagilar | 12:a6a37ba1feff | 370 | reintentos++; |
Helmut64 | 0:c43b6919ae15 | 371 | } |
sagilar | 12:a6a37ba1feff | 372 | wait_ms( 500 ); |
sagilar | 12:a6a37ba1feff | 373 | *led = !*led; |
sagilar | 12:a6a37ba1feff | 374 | State = LOWPOWER; |
sagilar | 12:a6a37ba1feff | 375 | break; |
sagilar | 12:a6a37ba1feff | 376 | case RX_TIMEOUT: |
sagilar | 13:c2c91cc0467d | 377 | if(sending_electric == true) { |
sagilar | 13:c2c91cc0467d | 378 | //sendElectric(); |
sagilar | 13:c2c91cc0467d | 379 | } else if(sending_temp == true) { |
sagilar | 13:c2c91cc0467d | 380 | //readTemperature(); |
sagilar | 12:a6a37ba1feff | 381 | } |
sagilar | 13:c2c91cc0467d | 382 | |
sagilar | 12:a6a37ba1feff | 383 | State = LOWPOWER; |
sagilar | 12:a6a37ba1feff | 384 | break; |
sagilar | 12:a6a37ba1feff | 385 | case RX_ERROR: |
sagilar | 12:a6a37ba1feff | 386 | // We have received a Packet with a CRC error, send reply as if packet was correct |
sagilar | 12:a6a37ba1feff | 387 | if( isMaster == true ) { |
sagilar | 12:a6a37ba1feff | 388 | // Send the next PING frame |
sagilar | 12:a6a37ba1feff | 389 | memcpy(Buffer, MsgTX, sizeof(MsgTX)); |
sagilar | 12:a6a37ba1feff | 390 | for( i = 4; i < BufferSize; i++ ) { |
sagilar | 12:a6a37ba1feff | 391 | Buffer[i] = i - 4; |
sagilar | 12:a6a37ba1feff | 392 | } |
sagilar | 12:a6a37ba1feff | 393 | wait_ms( 1000 ); |
sagilar | 12:a6a37ba1feff | 394 | Radio->Send( Buffer, BufferSize ); |
sagilar | 12:a6a37ba1feff | 395 | } else { |
sagilar | 12:a6a37ba1feff | 396 | // Send the next PONG frame |
sagilar | 12:a6a37ba1feff | 397 | memcpy(Buffer, MsgTX, sizeof(MsgTX)); |
sagilar | 12:a6a37ba1feff | 398 | for( i = sizeof(MsgTX); i < BufferSize; i++ ) { |
sagilar | 12:a6a37ba1feff | 399 | Buffer[i] = i - sizeof(MsgTX); |
sagilar | 12:a6a37ba1feff | 400 | } |
sagilar | 12:a6a37ba1feff | 401 | wait_ms( 1000 ); |
sagilar | 12:a6a37ba1feff | 402 | Radio->Send( Buffer, BufferSize ); |
Helmut64 | 0:c43b6919ae15 | 403 | } |
sagilar | 12:a6a37ba1feff | 404 | State = LOWPOWER; |
sagilar | 12:a6a37ba1feff | 405 | break; |
sagilar | 12:a6a37ba1feff | 406 | case TX_TIMEOUT: |
sagilar | 12:a6a37ba1feff | 407 | Radio->Rx( RX_TIMEOUT_VALUE ); |
sagilar | 12:a6a37ba1feff | 408 | State = LOWPOWER; |
sagilar | 12:a6a37ba1feff | 409 | break; |
sagilar | 12:a6a37ba1feff | 410 | case LOWPOWER: |
sagilar | 12:a6a37ba1feff | 411 | sleep(); |
sagilar | 12:a6a37ba1feff | 412 | break; |
sagilar | 12:a6a37ba1feff | 413 | default: |
sagilar | 12:a6a37ba1feff | 414 | State = LOWPOWER; |
sagilar | 12:a6a37ba1feff | 415 | break; |
sagilar | 12:a6a37ba1feff | 416 | } |
sagilar | 12:a6a37ba1feff | 417 | } |
sagilar | 12:a6a37ba1feff | 418 | } |
sagilar | 12:a6a37ba1feff | 419 | |
sagilar | 12:a6a37ba1feff | 420 | char *splitOnPosition(char *msj, int pos) |
sagilar | 12:a6a37ba1feff | 421 | { |
sagilar | 12:a6a37ba1feff | 422 | int i=0; |
sagilar | 12:a6a37ba1feff | 423 | char *substring = strtok (msj,"|"); |
sagilar | 12:a6a37ba1feff | 424 | char *strOutput=""; |
sagilar | 12:a6a37ba1feff | 425 | while (substring != NULL) { |
sagilar | 12:a6a37ba1feff | 426 | //dprintf ("substring: %s, index: %d",substring,i); |
sagilar | 12:a6a37ba1feff | 427 | |
sagilar | 12:a6a37ba1feff | 428 | if(i == 0) { |
sagilar | 12:a6a37ba1feff | 429 | srcEUI = substring; |
sagilar | 12:a6a37ba1feff | 430 | } else if(i == 3) { |
sagilar | 12:a6a37ba1feff | 431 | msjDestEUI = substring; |
sagilar | 12:a6a37ba1feff | 432 | } else if(i == 4) { |
sagilar | 12:a6a37ba1feff | 433 | msjContent = substring; |
sagilar | 12:a6a37ba1feff | 434 | } else if(i > 4) { |
sagilar | 12:a6a37ba1feff | 435 | strcat(msjContent," "); |
sagilar | 12:a6a37ba1feff | 436 | strcat(msjContent,substring); |
sagilar | 12:a6a37ba1feff | 437 | } |
sagilar | 12:a6a37ba1feff | 438 | |
sagilar | 12:a6a37ba1feff | 439 | if(i == pos) { |
sagilar | 12:a6a37ba1feff | 440 | strOutput = substring; |
sagilar | 12:a6a37ba1feff | 441 | } |
sagilar | 12:a6a37ba1feff | 442 | substring = strtok (NULL, "|"); |
sagilar | 12:a6a37ba1feff | 443 | i++; |
Helmut64 | 0:c43b6919ae15 | 444 | } |
sagilar | 12:a6a37ba1feff | 445 | return strOutput; |
sagilar | 12:a6a37ba1feff | 446 | } |
sagilar | 12:a6a37ba1feff | 447 | |
sagilar | 13:c2c91cc0467d | 448 | void readCurrent() |
sagilar | 12:a6a37ba1feff | 449 | { |
sagilar | 13:c2c91cc0467d | 450 | float vread; |
sagilar | 13:c2c91cc0467d | 451 | float vfilt = 0.0; |
sagilar | 13:c2c91cc0467d | 452 | float vh_ac = 0.0; |
sagilar | 13:c2c91cc0467d | 453 | int cont = 0; |
sagilar | 13:c2c91cc0467d | 454 | tiempo.start(); |
sagilar | 13:c2c91cc0467d | 455 | float sum = 0.0; |
sagilar | 13:c2c91cc0467d | 456 | while(tiempo < 1.0) |
sagilar | 13:c2c91cc0467d | 457 | |
sagilar | 13:c2c91cc0467d | 458 | { |
sagilar | 13:c2c91cc0467d | 459 | |
sagilar | 13:c2c91cc0467d | 460 | vread = analog_value_current.read(); |
sagilar | 13:c2c91cc0467d | 461 | vread = vread * 3300.0; |
sagilar | 13:c2c91cc0467d | 462 | |
sagilar | 13:c2c91cc0467d | 463 | //vh_ac = gh*filter2o(vread, vh_sub1,vh_sub2, ah0, ah1, ah2, bh0, bh1, bh2); |
sagilar | 13:c2c91cc0467d | 464 | //vfilt = filter2o(vh_ac, v_sub1,v_sub2, a0, a1, a2, b0, b1, b2); |
sagilar | 13:c2c91cc0467d | 465 | //vfilt = gh*filter2o(vread, vh_sub1,vh_sub2, ah0, ah1, ah2, bh0, bh1, bh2); |
sagilar | 13:c2c91cc0467d | 466 | |
sagilar | 13:c2c91cc0467d | 467 | //vfilt = gl*filter2o(vh_ac, vl_sub1,vl_sub2, al0, al1, al2, bl0, bl1, bl2); |
sagilar | 13:c2c91cc0467d | 468 | vfilt = gc*filter2o(vread, v_sub1,v_sub2, a0, a1, a2, b0, b1, b2); |
sagilar | 13:c2c91cc0467d | 469 | //dprintf("%.3f",vfilt); |
sagilar | 13:c2c91cc0467d | 470 | sum = sum + pow(vfilt,2); |
sagilar | 13:c2c91cc0467d | 471 | cont++; |
sagilar | 13:c2c91cc0467d | 472 | |
sagilar | 12:a6a37ba1feff | 473 | } |
sagilar | 13:c2c91cc0467d | 474 | float VrmsSensor = sqrt(sum/cont); |
sagilar | 13:c2c91cc0467d | 475 | inst_current = VrmsSensor/SENS; |
sagilar | 13:c2c91cc0467d | 476 | dprintf("%.3f",inst_current); |
sagilar | 13:c2c91cc0467d | 477 | //dprintf("voltajerms: %.3f, corrientersm: %.3f, vread: %.3f, conteo: %d",VrmsSensor,inst_current,vread, cont); |
sagilar | 13:c2c91cc0467d | 478 | tiempo.stop(); |
sagilar | 13:c2c91cc0467d | 479 | tiempo.reset(); |
sagilar | 13:c2c91cc0467d | 480 | |
sagilar | 13:c2c91cc0467d | 481 | if(mean_current != 0.0) { |
sagilar | 13:c2c91cc0467d | 482 | mean_current = (mean_current + inst_current)/2.0; |
sagilar | 13:c2c91cc0467d | 483 | } else { |
sagilar | 13:c2c91cc0467d | 484 | mean_current = inst_current; |
sagilar | 13:c2c91cc0467d | 485 | |
sagilar | 13:c2c91cc0467d | 486 | } |
sagilar | 13:c2c91cc0467d | 487 | |
sagilar | 13:c2c91cc0467d | 488 | inst_power = inst_current * RMS_VOLTAGE; |
sagilar | 12:a6a37ba1feff | 489 | |
sagilar | 13:c2c91cc0467d | 490 | if(mean_power != 0.0) { |
sagilar | 13:c2c91cc0467d | 491 | mean_power = (mean_power + inst_power)/2.0; |
sagilar | 13:c2c91cc0467d | 492 | |
sagilar | 13:c2c91cc0467d | 493 | } else { |
sagilar | 13:c2c91cc0467d | 494 | mean_power = inst_power; |
sagilar | 13:c2c91cc0467d | 495 | } |
sagilar | 13:c2c91cc0467d | 496 | |
sagilar | 13:c2c91cc0467d | 497 | if(inst_power > max_power) { |
sagilar | 13:c2c91cc0467d | 498 | max_power = inst_power; |
sagilar | 13:c2c91cc0467d | 499 | } |
sagilar | 13:c2c91cc0467d | 500 | if(min_power != 0) { |
sagilar | 13:c2c91cc0467d | 501 | min_power = inst_power; |
sagilar | 13:c2c91cc0467d | 502 | } else { |
sagilar | 13:c2c91cc0467d | 503 | if(inst_power < min_power) { |
sagilar | 13:c2c91cc0467d | 504 | min_power = inst_power; |
sagilar | 13:c2c91cc0467d | 505 | } |
sagilar | 13:c2c91cc0467d | 506 | } |
sagilar | 12:a6a37ba1feff | 507 | } |
sagilar | 12:a6a37ba1feff | 508 | |
sagilar | 13:c2c91cc0467d | 509 | void sendElectric() |
sagilar | 12:a6a37ba1feff | 510 | { |
sagilar | 13:c2c91cc0467d | 511 | tick_current.detach(); |
sagilar | 13:c2c91cc0467d | 512 | //dprintf("Voltaje Cin: Vcin=%f", meas_v1); |
sagilar | 13:c2c91cc0467d | 513 | sending_electric = true; |
sagilar | 12:a6a37ba1feff | 514 | reintentos=0; |
sagilar | 12:a6a37ba1feff | 515 | char valueStr[]=""; |
sagilar | 13:c2c91cc0467d | 516 | sprintf(valueStr,"%.2f",mean_current); |
sagilar | 13:c2c91cc0467d | 517 | //sprintf(valueStr,"%.2f",(float)cur_sensor); |
sagilar | 13:c2c91cc0467d | 518 | char variable[] = "corriente_promedio"; |
sagilar | 12:a6a37ba1feff | 519 | //char MsgEnvio[64] = ""; |
sagilar | 12:a6a37ba1feff | 520 | strcpy(MsgTX, EUI); |
sagilar | 12:a6a37ba1feff | 521 | strcat(MsgTX, "|"); |
sagilar | 12:a6a37ba1feff | 522 | strcat(MsgTX, AppEUI); |
sagilar | 12:a6a37ba1feff | 523 | strcat(MsgTX, "|"); |
sagilar | 12:a6a37ba1feff | 524 | strcat(MsgTX, AppKey); |
sagilar | 12:a6a37ba1feff | 525 | strcat(MsgTX, "|"); |
sagilar | 12:a6a37ba1feff | 526 | strcat(MsgTX, DestEUI); |
sagilar | 12:a6a37ba1feff | 527 | strcat(MsgTX, "|"); |
sagilar | 12:a6a37ba1feff | 528 | strcat(MsgTX, valueStr); |
sagilar | 12:a6a37ba1feff | 529 | strcat(MsgTX, " "); |
sagilar | 12:a6a37ba1feff | 530 | strcat(MsgTX, variable); |
sagilar | 12:a6a37ba1feff | 531 | strcat(MsgTX, " "); |
sagilar | 13:c2c91cc0467d | 532 | strcat(MsgTX, nombreDispoitivo); |
sagilar | 12:a6a37ba1feff | 533 | memcpy(Buffer, MsgTX, sizeof(MsgTX)); |
sagilar | 13:c2c91cc0467d | 534 | for( int i = sizeof(MsgTX); i < BufferSize; i++ ) { |
sagilar | 13:c2c91cc0467d | 535 | Buffer[i] = i - sizeof(MsgTX); |
sagilar | 13:c2c91cc0467d | 536 | } |
sagilar | 13:c2c91cc0467d | 537 | Radio->Send( Buffer, BufferSize ); |
sagilar | 13:c2c91cc0467d | 538 | Radio->Rx( RX_TIMEOUT_VALUE ); |
sagilar | 13:c2c91cc0467d | 539 | //dprintf("Enviando corriente: Cmean=%f", mean_current); |
sagilar | 13:c2c91cc0467d | 540 | wait_ms( 4000 ); |
sagilar | 13:c2c91cc0467d | 541 | |
sagilar | 13:c2c91cc0467d | 542 | sprintf(valueStr,"%.2f",mean_power); |
sagilar | 13:c2c91cc0467d | 543 | strcpy(variable,"potencia_promedio"); |
sagilar | 13:c2c91cc0467d | 544 | //char MsgEnvio[64] = ""; |
sagilar | 13:c2c91cc0467d | 545 | strcpy(MsgTX, EUI); |
sagilar | 13:c2c91cc0467d | 546 | strcat(MsgTX, "|"); |
sagilar | 13:c2c91cc0467d | 547 | strcat(MsgTX, AppEUI); |
sagilar | 13:c2c91cc0467d | 548 | strcat(MsgTX, "|"); |
sagilar | 13:c2c91cc0467d | 549 | strcat(MsgTX, AppKey); |
sagilar | 13:c2c91cc0467d | 550 | strcat(MsgTX, "|"); |
sagilar | 13:c2c91cc0467d | 551 | strcat(MsgTX, DestEUI); |
sagilar | 13:c2c91cc0467d | 552 | strcat(MsgTX, "|"); |
sagilar | 13:c2c91cc0467d | 553 | strcat(MsgTX, valueStr); |
sagilar | 13:c2c91cc0467d | 554 | strcat(MsgTX, " "); |
sagilar | 13:c2c91cc0467d | 555 | strcat(MsgTX, variable); |
sagilar | 13:c2c91cc0467d | 556 | strcat(MsgTX, " "); |
sagilar | 13:c2c91cc0467d | 557 | strcat(MsgTX, nombreDispoitivo); |
sagilar | 13:c2c91cc0467d | 558 | memcpy(Buffer, MsgTX, sizeof(MsgTX)); |
sagilar | 13:c2c91cc0467d | 559 | for( int i = sizeof(MsgTX); i < BufferSize; i++ ) { |
sagilar | 13:c2c91cc0467d | 560 | Buffer[i] = i - sizeof(MsgTX); |
sagilar | 13:c2c91cc0467d | 561 | } |
sagilar | 13:c2c91cc0467d | 562 | Radio->Send( Buffer, BufferSize ); |
sagilar | 13:c2c91cc0467d | 563 | Radio->Rx( RX_TIMEOUT_VALUE ); |
sagilar | 13:c2c91cc0467d | 564 | //dprintf("Enviando potencia prom: Pmean=%f", mean_power); |
sagilar | 13:c2c91cc0467d | 565 | wait_ms( 4000 ); |
sagilar | 13:c2c91cc0467d | 566 | |
sagilar | 13:c2c91cc0467d | 567 | sprintf(valueStr,"%.2f",max_power); |
sagilar | 13:c2c91cc0467d | 568 | strcpy(variable,"potencia_max"); |
sagilar | 13:c2c91cc0467d | 569 | //char MsgEnvio[64] = ""; |
sagilar | 13:c2c91cc0467d | 570 | strcpy(MsgTX, EUI); |
sagilar | 13:c2c91cc0467d | 571 | strcat(MsgTX, "|"); |
sagilar | 13:c2c91cc0467d | 572 | strcat(MsgTX, AppEUI); |
sagilar | 13:c2c91cc0467d | 573 | strcat(MsgTX, "|"); |
sagilar | 13:c2c91cc0467d | 574 | strcat(MsgTX, AppKey); |
sagilar | 13:c2c91cc0467d | 575 | strcat(MsgTX, "|"); |
sagilar | 13:c2c91cc0467d | 576 | strcat(MsgTX, DestEUI); |
sagilar | 13:c2c91cc0467d | 577 | strcat(MsgTX, "|"); |
sagilar | 13:c2c91cc0467d | 578 | strcat(MsgTX, valueStr); |
sagilar | 13:c2c91cc0467d | 579 | strcat(MsgTX, " "); |
sagilar | 13:c2c91cc0467d | 580 | strcat(MsgTX, variable); |
sagilar | 13:c2c91cc0467d | 581 | strcat(MsgTX, " "); |
sagilar | 13:c2c91cc0467d | 582 | strcat(MsgTX, nombreDispoitivo); |
sagilar | 13:c2c91cc0467d | 583 | memcpy(Buffer, MsgTX, sizeof(MsgTX)); |
sagilar | 12:a6a37ba1feff | 584 | for( int i = sizeof(MsgTX); i < BufferSize; i++ ) { |
sagilar | 12:a6a37ba1feff | 585 | Buffer[i] = i - sizeof(MsgTX); |
sagilar | 12:a6a37ba1feff | 586 | } |
sagilar | 13:c2c91cc0467d | 587 | Radio->Send( Buffer, BufferSize ); |
sagilar | 13:c2c91cc0467d | 588 | Radio->Rx( RX_TIMEOUT_VALUE ); |
sagilar | 13:c2c91cc0467d | 589 | //dprintf("Enviando potencia max: Pmax=%f", max_power); |
sagilar | 13:c2c91cc0467d | 590 | wait_ms( 3000 ); |
sagilar | 13:c2c91cc0467d | 591 | |
sagilar | 13:c2c91cc0467d | 592 | /*sprintf(valueStr,"%.2f",min_power); |
sagilar | 13:c2c91cc0467d | 593 | strcpy(variable,"potencia_min"); |
sagilar | 13:c2c91cc0467d | 594 | //char MsgEnvio[64] = ""; |
sagilar | 13:c2c91cc0467d | 595 | strcpy(MsgTX, EUI); |
sagilar | 13:c2c91cc0467d | 596 | strcat(MsgTX, "|"); |
sagilar | 13:c2c91cc0467d | 597 | strcat(MsgTX, AppEUI); |
sagilar | 13:c2c91cc0467d | 598 | strcat(MsgTX, "|"); |
sagilar | 13:c2c91cc0467d | 599 | strcat(MsgTX, AppKey); |
sagilar | 13:c2c91cc0467d | 600 | strcat(MsgTX, "|"); |
sagilar | 13:c2c91cc0467d | 601 | strcat(MsgTX, DestEUI); |
sagilar | 13:c2c91cc0467d | 602 | strcat(MsgTX, "|"); |
sagilar | 13:c2c91cc0467d | 603 | strcat(MsgTX, valueStr); |
sagilar | 13:c2c91cc0467d | 604 | strcat(MsgTX, " "); |
sagilar | 13:c2c91cc0467d | 605 | strcat(MsgTX, variable); |
sagilar | 13:c2c91cc0467d | 606 | strcat(MsgTX, " "); |
sagilar | 13:c2c91cc0467d | 607 | strcat(MsgTX, nombreDispoitivo); |
sagilar | 13:c2c91cc0467d | 608 | memcpy(Buffer, MsgTX, sizeof(MsgTX)); |
sagilar | 13:c2c91cc0467d | 609 | for( int i = sizeof(MsgTX); i < BufferSize; i++ ) { |
sagilar | 13:c2c91cc0467d | 610 | Buffer[i] = i - sizeof(MsgTX); |
sagilar | 13:c2c91cc0467d | 611 | } |
sagilar | 13:c2c91cc0467d | 612 | Radio->Send( Buffer, BufferSize ); |
sagilar | 13:c2c91cc0467d | 613 | Radio->Rx( RX_TIMEOUT_VALUE ); |
sagilar | 13:c2c91cc0467d | 614 | //dprintf("Enviando potencia min: Pmin=%f", min_power); |
sagilar | 13:c2c91cc0467d | 615 | wait_ms( 3000 );*/ |
sagilar | 13:c2c91cc0467d | 616 | |
sagilar | 13:c2c91cc0467d | 617 | max_power = 0.0; |
sagilar | 13:c2c91cc0467d | 618 | min_power = 0.0; |
sagilar | 13:c2c91cc0467d | 619 | max_inst_current = 0.0; |
sagilar | 13:c2c91cc0467d | 620 | mean_current = 0.0; |
sagilar | 13:c2c91cc0467d | 621 | |
sagilar | 13:c2c91cc0467d | 622 | sending_electric = false; |
sagilar | 13:c2c91cc0467d | 623 | tick_current.attach(&readCurrent,5.0); |
sagilar | 13:c2c91cc0467d | 624 | } |
sagilar | 13:c2c91cc0467d | 625 | |
sagilar | 13:c2c91cc0467d | 626 | void readTemperature() |
sagilar | 13:c2c91cc0467d | 627 | { |
sagilar | 13:c2c91cc0467d | 628 | tick_current.detach(); |
sagilar | 13:c2c91cc0467d | 629 | sending_temp = true; |
sagilar | 13:c2c91cc0467d | 630 | meas_r2 = analog_value_temp.read(); // Read the analog input value (value from 0.0 to 1.0 = full ADC conversion range) |
sagilar | 13:c2c91cc0467d | 631 | meas_v2 = meas_r2 * 3.3; |
sagilar | 13:c2c91cc0467d | 632 | temperature = meas_v2 * 100.0; |
sagilar | 13:c2c91cc0467d | 633 | |
sagilar | 13:c2c91cc0467d | 634 | char valueStr[]=""; |
sagilar | 13:c2c91cc0467d | 635 | sprintf(valueStr,"%.2f",temperature); |
sagilar | 13:c2c91cc0467d | 636 | char variable[] = "temperatura"; |
sagilar | 13:c2c91cc0467d | 637 | strcpy(MsgTX, EUI); |
sagilar | 13:c2c91cc0467d | 638 | strcat(MsgTX, "|"); |
sagilar | 13:c2c91cc0467d | 639 | strcat(MsgTX, AppEUI); |
sagilar | 13:c2c91cc0467d | 640 | strcat(MsgTX, "|"); |
sagilar | 13:c2c91cc0467d | 641 | strcat(MsgTX, AppKey); |
sagilar | 13:c2c91cc0467d | 642 | strcat(MsgTX, "|"); |
sagilar | 13:c2c91cc0467d | 643 | strcat(MsgTX, DestEUI); |
sagilar | 13:c2c91cc0467d | 644 | strcat(MsgTX, "|"); |
sagilar | 13:c2c91cc0467d | 645 | strcat(MsgTX, valueStr); |
sagilar | 13:c2c91cc0467d | 646 | strcat(MsgTX, " "); |
sagilar | 13:c2c91cc0467d | 647 | strcat(MsgTX, variable); |
sagilar | 13:c2c91cc0467d | 648 | strcat(MsgTX, " "); |
sagilar | 13:c2c91cc0467d | 649 | strcat(MsgTX, nombreDispoitivo); |
sagilar | 13:c2c91cc0467d | 650 | memcpy(Buffer, MsgTX, sizeof(MsgTX)); |
sagilar | 13:c2c91cc0467d | 651 | for( int i = sizeof(MsgTX); i < BufferSize; i++ ) { |
sagilar | 13:c2c91cc0467d | 652 | Buffer[i] = i - sizeof(MsgTX); |
sagilar | 13:c2c91cc0467d | 653 | } |
sagilar | 12:a6a37ba1feff | 654 | Radio->Send( Buffer, BufferSize ); |
sagilar | 12:a6a37ba1feff | 655 | Radio->Rx( RX_TIMEOUT_VALUE ); |
sagilar | 12:a6a37ba1feff | 656 | |
sagilar | 13:c2c91cc0467d | 657 | dprintf("Enviando temperatura: T=%f", temperature); |
sagilar | 13:c2c91cc0467d | 658 | sending_temp = false; |
sagilar | 13:c2c91cc0467d | 659 | tick_current.attach(&readCurrent,5.0); |
Helmut64 | 0:c43b6919ae15 | 660 | } |
Helmut64 | 0:c43b6919ae15 | 661 | |
sagilar | 13:c2c91cc0467d | 662 | |
bcostm | 11:9d7409ebfa57 | 663 | void OnTxDone(void *radio, void *userThisPtr, void *userData) |
Helmut64 | 0:c43b6919ae15 | 664 | { |
Helmut64 | 0:c43b6919ae15 | 665 | Radio->Sleep( ); |
Helmut64 | 0:c43b6919ae15 | 666 | State = TX; |
Helmut64 | 0:c43b6919ae15 | 667 | if (DEBUG_MESSAGE) |
Helmut64 | 0:c43b6919ae15 | 668 | dprintf("> OnTxDone"); |
Helmut64 | 0:c43b6919ae15 | 669 | } |
Helmut64 | 0:c43b6919ae15 | 670 | |
bcostm | 11:9d7409ebfa57 | 671 | void OnRxDone(void *radio, void *userThisPtr, void *userData, uint8_t *payload, uint16_t size, int16_t rssi, int8_t snr) |
Helmut64 | 0:c43b6919ae15 | 672 | { |
Helmut64 | 0:c43b6919ae15 | 673 | Radio->Sleep( ); |
Helmut64 | 0:c43b6919ae15 | 674 | BufferSize = size; |
Helmut64 | 0:c43b6919ae15 | 675 | memcpy( Buffer, payload, BufferSize ); |
Helmut64 | 0:c43b6919ae15 | 676 | State = RX; |
Helmut64 | 0:c43b6919ae15 | 677 | if (DEBUG_MESSAGE) |
Helmut64 | 0:c43b6919ae15 | 678 | dprintf("> OnRxDone: RssiValue=%d dBm, SnrValue=%d", rssi, snr); |
sagilar | 12:a6a37ba1feff | 679 | //dump("Data:", payload, size); |
sagilar | 12:a6a37ba1feff | 680 | strcpy(MsgRX,(char*)payload); |
sagilar | 12:a6a37ba1feff | 681 | //dprintf("Msj: %s", MsgRX); |
Helmut64 | 0:c43b6919ae15 | 682 | } |
Helmut64 | 0:c43b6919ae15 | 683 | |
bcostm | 11:9d7409ebfa57 | 684 | void OnTxTimeout(void *radio, void *userThisPtr, void *userData) |
Helmut64 | 0:c43b6919ae15 | 685 | { |
Helmut64 | 0:c43b6919ae15 | 686 | *led3 = 0; |
Helmut64 | 0:c43b6919ae15 | 687 | Radio->Sleep( ); |
Helmut64 | 0:c43b6919ae15 | 688 | State = TX_TIMEOUT; |
Helmut64 | 0:c43b6919ae15 | 689 | if(DEBUG_MESSAGE) |
Helmut64 | 0:c43b6919ae15 | 690 | dprintf("> OnTxTimeout"); |
Helmut64 | 0:c43b6919ae15 | 691 | } |
Helmut64 | 0:c43b6919ae15 | 692 | |
bcostm | 11:9d7409ebfa57 | 693 | void OnRxTimeout(void *radio, void *userThisPtr, void *userData) |
Helmut64 | 0:c43b6919ae15 | 694 | { |
Helmut64 | 0:c43b6919ae15 | 695 | *led3 = 0; |
Helmut64 | 0:c43b6919ae15 | 696 | Radio->Sleep( ); |
Helmut64 | 0:c43b6919ae15 | 697 | Buffer[BufferSize-1] = 0; |
Helmut64 | 0:c43b6919ae15 | 698 | State = RX_TIMEOUT; |
Helmut64 | 0:c43b6919ae15 | 699 | if (DEBUG_MESSAGE) |
Helmut64 | 0:c43b6919ae15 | 700 | dprintf("> OnRxTimeout"); |
Helmut64 | 0:c43b6919ae15 | 701 | } |
Helmut64 | 0:c43b6919ae15 | 702 | |
bcostm | 11:9d7409ebfa57 | 703 | void OnRxError(void *radio, void *userThisPtr, void *userData) |
Helmut64 | 0:c43b6919ae15 | 704 | { |
Helmut64 | 0:c43b6919ae15 | 705 | Radio->Sleep( ); |
Helmut64 | 0:c43b6919ae15 | 706 | State = RX_ERROR; |
Helmut64 | 0:c43b6919ae15 | 707 | if (DEBUG_MESSAGE) |
Helmut64 | 0:c43b6919ae15 | 708 | dprintf("> OnRxError"); |
Helmut64 | 0:c43b6919ae15 | 709 | } |
Helmut64 | 0:c43b6919ae15 | 710 | |
sagilar | 13:c2c91cc0467d | 711 | double filter2o(double u, double &v1, double &v2, const double a0, const double a1, const double a2, const double b0, const double b1, const double b2) |
sagilar | 13:c2c91cc0467d | 712 | { |
sagilar | 13:c2c91cc0467d | 713 | //notch 60Hz a 12Hz Frecuencia de muestreo params: [num]/[den] = [ 0.99866961055330528, -1.9963536547718377, 0.99866961055330528]/[ 1,-1.9963536547718377, 0.99733922110661055] |
sagilar | 13:c2c91cc0467d | 714 | double v = b0*u-b1*v1-b2*v2; |
sagilar | 13:c2c91cc0467d | 715 | double y = a0*v+a1*v1+a2*v2; //Forma directa II |
sagilar | 13:c2c91cc0467d | 716 | v2=v1; |
sagilar | 13:c2c91cc0467d | 717 | v1=v; |
sagilar | 13:c2c91cc0467d | 718 | return y; |
sagilar | 13:c2c91cc0467d | 719 | |
sagilar | 13:c2c91cc0467d | 720 | } |
sagilar | 13:c2c91cc0467d | 721 | |
Helmut64 | 0:c43b6919ae15 | 722 | #endif |