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