Programa WARIIOT AIDI421 clase C.

Dependencies:   libxDot-mbed5

Fork of mDot_claseC by Damez

Committer:
gabrielap
Date:
Wed Jun 03 15:25:22 2020 +0000
Revision:
0:293d4729cd57
Programa base para proyecto con mDot en clase C.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
gabrielap 0:293d4729cd57 1 #include "dot_util.h"
gabrielap 0:293d4729cd57 2 #include "RadioEvent.h"
gabrielap 0:293d4729cd57 3
gabrielap 0:293d4729cd57 4
gabrielap 0:293d4729cd57 5 /////////////////////////////////////////////////////////////////////////////
gabrielap 0:293d4729cd57 6 // -------------------- DOT LIBRARY REQUIRED ------------------------------//
gabrielap 0:293d4729cd57 7 // * Because these example programs can be used for both mDot and xDot //
gabrielap 0:293d4729cd57 8 // devices, the LoRa stack is not included. The libmDot library should //
gabrielap 0:293d4729cd57 9 // be imported if building for mDot devices. The libxDot library //
gabrielap 0:293d4729cd57 10 // should be imported if building for xDot devices. //
gabrielap 0:293d4729cd57 11 // * https://developer.mbed.org/teams/MultiTech/code/libmDot-dev-mbed5/ //
gabrielap 0:293d4729cd57 12 // * https://developer.mbed.org/teams/MultiTech/code/libmDot-mbed5/ //
gabrielap 0:293d4729cd57 13 // * https://developer.mbed.org/teams/MultiTech/code/libxDot-dev-mbed5/ //
gabrielap 0:293d4729cd57 14 // * https://developer.mbed.org/teams/MultiTech/code/libxDot-mbed5/ //
gabrielap 0:293d4729cd57 15 /////////////////////////////////////////////////////////////////////////////
gabrielap 0:293d4729cd57 16
gabrielap 0:293d4729cd57 17 /////////////////////////////////////////////////////////////
gabrielap 0:293d4729cd57 18 // * these options must match the settings on your gateway //
gabrielap 0:293d4729cd57 19 // * edit their values to match your configuration //
gabrielap 0:293d4729cd57 20 // * frequency sub band is only relevant for the 915 bands //
gabrielap 0:293d4729cd57 21 // * either the network name and passphrase can be used or //
gabrielap 0:293d4729cd57 22 // the network ID (8 bytes) and KEY (16 bytes) //
gabrielap 0:293d4729cd57 23 /////////////////////////////////////////////////////////////
gabrielap 0:293d4729cd57 24 static std::string network_name = "MultiTech";
gabrielap 0:293d4729cd57 25 static std::string network_passphrase = "MultiTech";
gabrielap 0:293d4729cd57 26 static uint8_t network_id[] = { 0x6C, 0x4E, 0xEF, 0x66, 0xF4, 0x79, 0x86, 0xA6 };
gabrielap 0:293d4729cd57 27 static uint8_t network_key[] = { 0x1F, 0x33, 0xA1, 0x70, 0xA5, 0xF1, 0xFD, 0xA0, 0xAB, 0x69, 0x7A, 0xAE, 0x2B, 0x95, 0x91, 0x6B };
gabrielap 0:293d4729cd57 28 static uint8_t frequency_sub_band = 1;
gabrielap 0:293d4729cd57 29 static lora::NetworkType network_type = lora::PUBLIC_LORAWAN;
gabrielap 0:293d4729cd57 30 static uint8_t join_delay = 5;
gabrielap 0:293d4729cd57 31 static uint8_t ack = 1;
gabrielap 0:293d4729cd57 32 static bool adr = true;
gabrielap 0:293d4729cd57 33
gabrielap 0:293d4729cd57 34 mDot* dot = NULL;
gabrielap 0:293d4729cd57 35 lora::ChannelPlan* plan = NULL;
gabrielap 0:293d4729cd57 36
gabrielap 0:293d4729cd57 37 Serial pc(USBTX, USBRX);
gabrielap 0:293d4729cd57 38
gabrielap 0:293d4729cd57 39 #if !defined(CHANNEL_PLAN)
gabrielap 0:293d4729cd57 40 #define CHANNEL_PLAN CP_AU915
gabrielap 0:293d4729cd57 41 #endif
gabrielap 0:293d4729cd57 42 int TIEMPO_Tx=30;
gabrielap 0:293d4729cd57 43 int TIEMPO_REINTENTOS_CONSECUTIVOS=10;
gabrielap 0:293d4729cd57 44 int cantidad_REINTENTOS=0;
gabrielap 0:293d4729cd57 45 int TIEMPO_RANDOM_SUP=60;
gabrielap 0:293d4729cd57 46 int TIEMPO_RANDOM_INF=20;
gabrielap 0:293d4729cd57 47
gabrielap 0:293d4729cd57 48 Ticker timer_payload;
gabrielap 0:293d4729cd57 49 Ticker timer_Reintento;
gabrielap 0:293d4729cd57 50 Ticker timer_random_rango;
gabrielap 0:293d4729cd57 51
gabrielap 0:293d4729cd57 52 int flag_payload=1;
gabrielap 0:293d4729cd57 53 int flag_Tx=0;
gabrielap 0:293d4729cd57 54 int flag_random_rango=0;
gabrielap 0:293d4729cd57 55
gabrielap 0:293d4729cd57 56 uint32_t Tiempo_reintento_join=10; // en seg
gabrielap 0:293d4729cd57 57 uint32_t Tiempo_reintento_join_2=15; // en seg
gabrielap 0:293d4729cd57 58 /////***************DECLARACION DE FUNCIONES**********************////
gabrielap 0:293d4729cd57 59
gabrielap 0:293d4729cd57 60 void func_timer_payload(void);
gabrielap 0:293d4729cd57 61 void func_timer_Reintento(void);
gabrielap 0:293d4729cd57 62 void func_timer_random_rango(void);
gabrielap 0:293d4729cd57 63 /////***************DEFINICION DE FUNCIONES**********************////
gabrielap 0:293d4729cd57 64
gabrielap 0:293d4729cd57 65 void func_timer_payload(void)
gabrielap 0:293d4729cd57 66 {
gabrielap 0:293d4729cd57 67 flag_payload=1;
gabrielap 0:293d4729cd57 68 }
gabrielap 0:293d4729cd57 69
gabrielap 0:293d4729cd57 70 void func_timer_Reintento(void)
gabrielap 0:293d4729cd57 71 {
gabrielap 0:293d4729cd57 72 flag_Tx=1;
gabrielap 0:293d4729cd57 73 }
gabrielap 0:293d4729cd57 74 void func_timer_random_rango(void)
gabrielap 0:293d4729cd57 75 {
gabrielap 0:293d4729cd57 76 flag_Tx=1;
gabrielap 0:293d4729cd57 77 }
gabrielap 0:293d4729cd57 78 //////////////////////////////////////////////////////////////////////
gabrielap 0:293d4729cd57 79
gabrielap 0:293d4729cd57 80 int main() {
gabrielap 0:293d4729cd57 81 // Custom event handler for automatically displaying RX data
gabrielap 0:293d4729cd57 82 RadioEvent events;
gabrielap 0:293d4729cd57 83 //mDotEvent events;
gabrielap 0:293d4729cd57 84
gabrielap 0:293d4729cd57 85 pc.baud(115200);
gabrielap 0:293d4729cd57 86
gabrielap 0:293d4729cd57 87 mts::MTSLog::setLogLevel(mts::MTSLog::TRACE_LEVEL);
gabrielap 0:293d4729cd57 88
gabrielap 0:293d4729cd57 89 plan = new lora::ChannelPlan_US915();
gabrielap 0:293d4729cd57 90 MBED_ASSERT(plan);
gabrielap 0:293d4729cd57 91
gabrielap 0:293d4729cd57 92 dot = mDot::getInstance(plan);
gabrielap 0:293d4729cd57 93 MBED_ASSERT(dot);
gabrielap 0:293d4729cd57 94
gabrielap 0:293d4729cd57 95 logInfo("mbed-os library version: %d.%d.%d", MBED_MAJOR_VERSION, MBED_MINOR_VERSION, MBED_PATCH_VERSION);
gabrielap 0:293d4729cd57 96
gabrielap 0:293d4729cd57 97 // start from a well-known state
gabrielap 0:293d4729cd57 98 logInfo("defaulting Dot configuration");
gabrielap 0:293d4729cd57 99 dot->resetConfig();
gabrielap 0:293d4729cd57 100 dot->resetNetworkSession();
gabrielap 0:293d4729cd57 101
gabrielap 0:293d4729cd57 102 // make sure library logging is turned on
gabrielap 0:293d4729cd57 103 dot->setLogLevel(mts::MTSLog::INFO_LEVEL);
gabrielap 0:293d4729cd57 104
gabrielap 0:293d4729cd57 105 // attach the custom events handler
gabrielap 0:293d4729cd57 106 dot->setEvents(&events);
gabrielap 0:293d4729cd57 107
gabrielap 0:293d4729cd57 108 // update configuration if necessary
gabrielap 0:293d4729cd57 109 if (dot->getJoinMode() != mDot::OTA) {
gabrielap 0:293d4729cd57 110 logInfo("changing network join mode to OTA");
gabrielap 0:293d4729cd57 111 if (dot->setJoinMode(mDot::OTA) != mDot::MDOT_OK) {
gabrielap 0:293d4729cd57 112 logError("failed to set network join mode to OTA");
gabrielap 0:293d4729cd57 113 }
gabrielap 0:293d4729cd57 114 }
gabrielap 0:293d4729cd57 115 // in OTA and AUTO_OTA join modes, the credentials can be passed to the library as a name and passphrase or an ID and KEY
gabrielap 0:293d4729cd57 116 // only one method or the other should be used!
gabrielap 0:293d4729cd57 117 // network ID = crc64(network name)
gabrielap 0:293d4729cd57 118 // network KEY = cmac(network passphrase)
gabrielap 0:293d4729cd57 119 //update_ota_config_name_phrase(network_name, network_passphrase, frequency_sub_band, network_type, ack);
gabrielap 0:293d4729cd57 120 update_ota_config_id_key(network_id, network_key, frequency_sub_band, network_type, ack);
gabrielap 0:293d4729cd57 121
gabrielap 0:293d4729cd57 122 dot->setLinkCheckThreshold(3);
gabrielap 0:293d4729cd57 123
gabrielap 0:293d4729cd57 124 // configure the Dot for class C operation
gabrielap 0:293d4729cd57 125 // the Dot must also be configured on the gateway for class C
gabrielap 0:293d4729cd57 126 // use the lora-query application to do this on a Conduit: http://www.multitech.net/developer/software/lora/lora-network-server/
gabrielap 0:293d4729cd57 127 // to provision your Dot for class C operation with a 3rd party gateway, see the gateway or network provider documentation
gabrielap 0:293d4729cd57 128 logInfo("changing network mode to class C");
gabrielap 0:293d4729cd57 129 if (dot->setClass("C") != mDot::MDOT_OK) {
gabrielap 0:293d4729cd57 130 logError("failed to set network mode to class C");
gabrielap 0:293d4729cd57 131 }
gabrielap 0:293d4729cd57 132
gabrielap 0:293d4729cd57 133 // enable or disable Adaptive Data Rate
gabrielap 0:293d4729cd57 134 dot->setAdr(adr);
gabrielap 0:293d4729cd57 135
gabrielap 0:293d4729cd57 136 // Configure the join delay
gabrielap 0:293d4729cd57 137 dot->setJoinDelay(join_delay);
gabrielap 0:293d4729cd57 138
gabrielap 0:293d4729cd57 139 // save changes to configuration
gabrielap 0:293d4729cd57 140 logInfo("saving configuration");
gabrielap 0:293d4729cd57 141 if (!dot->saveConfig()) {
gabrielap 0:293d4729cd57 142 logError("failed to save configuration");
gabrielap 0:293d4729cd57 143 }
gabrielap 0:293d4729cd57 144
gabrielap 0:293d4729cd57 145 // display configuration
gabrielap 0:293d4729cd57 146 display_config();
gabrielap 0:293d4729cd57 147
gabrielap 0:293d4729cd57 148 uint16_t valor=0;
gabrielap 0:293d4729cd57 149 uint32_t Downlink_Counter=0;
gabrielap 0:293d4729cd57 150 uint32_t Downlink_Counter_old=0;
gabrielap 0:293d4729cd57 151
gabrielap 0:293d4729cd57 152 std::vector<uint8_t> tx_data;
gabrielap 0:293d4729cd57 153 std::vector<uint8_t> rx_data;
gabrielap 0:293d4729cd57 154
gabrielap 0:293d4729cd57 155 timer_payload.attach(&func_timer_payload, TIEMPO_Tx);
gabrielap 0:293d4729cd57 156
gabrielap 0:293d4729cd57 157
gabrielap 0:293d4729cd57 158 int flag_error_envio=0;
gabrielap 0:293d4729cd57 159 int num_intentos=0;
gabrielap 0:293d4729cd57 160 int cantidad_envios= 1 + cantidad_REINTENTOS;
gabrielap 0:293d4729cd57 161 int tiempo_rand=0;
gabrielap 0:293d4729cd57 162
gabrielap 0:293d4729cd57 163
gabrielap 0:293d4729cd57 164 // join network if not joined
gabrielap 0:293d4729cd57 165 if (!dot->getNetworkJoinStatus()) {
gabrielap 0:293d4729cd57 166 //join_network(Tiempo_reintento_join);
gabrielap 0:293d4729cd57 167 join_network(Tiempo_reintento_join);
gabrielap 0:293d4729cd57 168 }
gabrielap 0:293d4729cd57 169
gabrielap 0:293d4729cd57 170 while (true) {
gabrielap 0:293d4729cd57 171
gabrielap 0:293d4729cd57 172 // join network if not joined
gabrielap 0:293d4729cd57 173 if (!dot->getNetworkJoinStatus()) {
gabrielap 0:293d4729cd57 174 // Elimito todos los timer para que no molesten, ademas no se van a usar hasta que no este enlazado otra vez
gabrielap 0:293d4729cd57 175 // pense que molestaban para reenlazar pero no
gabrielap 0:293d4729cd57 176 timer_payload.detach();
gabrielap 0:293d4729cd57 177
gabrielap 0:293d4729cd57 178 //join_network(Tiempo_reintento_join_2);
gabrielap 0:293d4729cd57 179 join_network(Tiempo_reintento_join_2);
gabrielap 0:293d4729cd57 180 // Una vez enlazado voy a medir y transmitir y ya activo el timer
gabrielap 0:293d4729cd57 181 flag_payload=1;
gabrielap 0:293d4729cd57 182 timer_payload.attach(&func_timer_payload, TIEMPO_Tx);
gabrielap 0:293d4729cd57 183 }
gabrielap 0:293d4729cd57 184
gabrielap 0:293d4729cd57 185 if(flag_payload) // mediciones y armado de payload
gabrielap 0:293d4729cd57 186 {
gabrielap 0:293d4729cd57 187
gabrielap 0:293d4729cd57 188 printf("\n\rarmando payload\n\r");
gabrielap 0:293d4729cd57 189
gabrielap 0:293d4729cd57 190 tx_data.clear();
gabrielap 0:293d4729cd57 191 //// El payload es un valor que se va incrementando
gabrielap 0:293d4729cd57 192 valor++;
gabrielap 0:293d4729cd57 193 tx_data.push_back((valor >> 8) & 0xFF);
gabrielap 0:293d4729cd57 194 tx_data.push_back(valor & 0xFF);
gabrielap 0:293d4729cd57 195
gabrielap 0:293d4729cd57 196 // Flags
gabrielap 0:293d4729cd57 197 flag_payload=0;
gabrielap 0:293d4729cd57 198 flag_Tx=1;
gabrielap 0:293d4729cd57 199 flag_error_envio=0;
gabrielap 0:293d4729cd57 200 num_intentos=0;
gabrielap 0:293d4729cd57 201 }
gabrielap 0:293d4729cd57 202
gabrielap 0:293d4729cd57 203 if(flag_Tx)
gabrielap 0:293d4729cd57 204 {
gabrielap 0:293d4729cd57 205 if(flag_error_envio==1)
gabrielap 0:293d4729cd57 206 {
gabrielap 0:293d4729cd57 207 timer_random_rango.detach();
gabrielap 0:293d4729cd57 208 printf("\n\rElimino timer timer_random_rango\n\r");
gabrielap 0:293d4729cd57 209
gabrielap 0:293d4729cd57 210 }
gabrielap 0:293d4729cd57 211
gabrielap 0:293d4729cd57 212
gabrielap 0:293d4729cd57 213 // Envio
gabrielap 0:293d4729cd57 214 logInfo("send valor: %d", valor);
gabrielap 0:293d4729cd57 215
gabrielap 0:293d4729cd57 216 if(((cantidad_envios - flag_error_envio - num_intentos) != 0) && send_data(tx_data)!=0) // si falla el envio y si todavia no se hicieron todos los reintentos
gabrielap 0:293d4729cd57 217 {
gabrielap 0:293d4729cd57 218 num_intentos++;
gabrielap 0:293d4729cd57 219 if(num_intentos==1 && ((cantidad_envios - flag_error_envio - num_intentos) != 0))// si es la primera vez que entra al if y si num de reintentos es mayor a cero
gabrielap 0:293d4729cd57 220 {
gabrielap 0:293d4729cd57 221 timer_Reintento.attach(&func_timer_Reintento, TIEMPO_REINTENTOS_CONSECUTIVOS);
gabrielap 0:293d4729cd57 222 printf("\n\rActivo timer timer_Tx\n\r");
gabrielap 0:293d4729cd57 223 }
gabrielap 0:293d4729cd57 224
gabrielap 0:293d4729cd57 225
gabrielap 0:293d4729cd57 226 }
gabrielap 0:293d4729cd57 227 else
gabrielap 0:293d4729cd57 228 {
gabrielap 0:293d4729cd57 229 if(flag_error_envio==0 && (cantidad_envios - flag_error_envio - num_intentos) == 0)
gabrielap 0:293d4729cd57 230 {
gabrielap 0:293d4729cd57 231 // Activo timer 3
gabrielap 0:293d4729cd57 232 // initialize random seed:
gabrielap 0:293d4729cd57 233 srand (time(NULL));
gabrielap 0:293d4729cd57 234
gabrielap 0:293d4729cd57 235 // Defino el intervalo del valor random y obtengo un valor
gabrielap 0:293d4729cd57 236 tiempo_rand = rand() % (TIEMPO_RANDOM_SUP-TIEMPO_RANDOM_INF) + TIEMPO_RANDOM_INF;
gabrielap 0:293d4729cd57 237 //logInfo("tiempo_rand = %d\n\r\n", tiempo_rand);
gabrielap 0:293d4729cd57 238 logInfo("tiempo_rand = %d\n\r\n",tiempo_rand);
gabrielap 0:293d4729cd57 239
gabrielap 0:293d4729cd57 240 timer_random_rango.attach(&func_timer_random_rango, tiempo_rand);
gabrielap 0:293d4729cd57 241 printf("\n\rActivo timer timer_random_rango\n\r");
gabrielap 0:293d4729cd57 242
gabrielap 0:293d4729cd57 243 flag_error_envio=1;
gabrielap 0:293d4729cd57 244 num_intentos=0;
gabrielap 0:293d4729cd57 245
gabrielap 0:293d4729cd57 246 }
gabrielap 0:293d4729cd57 247
gabrielap 0:293d4729cd57 248 // elimino timer
gabrielap 0:293d4729cd57 249 timer_Reintento.detach();
gabrielap 0:293d4729cd57 250 printf("\n\rElimino timer timer_Tx\n\r");
gabrielap 0:293d4729cd57 251
gabrielap 0:293d4729cd57 252
gabrielap 0:293d4729cd57 253 // Actualizo el contador
gabrielap 0:293d4729cd57 254 // en este caso siempre va a llegar un msj sea solo con el ACK o con un payload tambien
gabrielap 0:293d4729cd57 255 Downlink_Counter=dot->getDownLinkCounter();
gabrielap 0:293d4729cd57 256 Downlink_Counter_old=Downlink_Counter;
gabrielap 0:293d4729cd57 257
gabrielap 0:293d4729cd57 258 // Recepcion Rx1
gabrielap 0:293d4729cd57 259 // Leo el buffer si hay un payload
gabrielap 0:293d4729cd57 260 // Hasta Payload de 200 Bytes recibe, maximo desconocido
gabrielap 0:293d4729cd57 261 if(dot->recv(rx_data)==dot->MDOT_OK)
gabrielap 0:293d4729cd57 262 {
gabrielap 0:293d4729cd57 263 printf("longitud = %d", rx_data.size());
gabrielap 0:293d4729cd57 264
gabrielap 0:293d4729cd57 265 // Muestra todos los Bytes
gabrielap 0:293d4729cd57 266 printf("\n\rDato %d recibido:", Downlink_Counter);
gabrielap 0:293d4729cd57 267 for(int i=0;i<rx_data.size();i++)
gabrielap 0:293d4729cd57 268 {
gabrielap 0:293d4729cd57 269 printf("%02X ", rx_data[i]);
gabrielap 0:293d4729cd57 270 }
gabrielap 0:293d4729cd57 271 printf("\n\r");
gabrielap 0:293d4729cd57 272
gabrielap 0:293d4729cd57 273 rx_data.clear();
gabrielap 0:293d4729cd57 274
gabrielap 0:293d4729cd57 275 }
gabrielap 0:293d4729cd57 276 }
gabrielap 0:293d4729cd57 277
gabrielap 0:293d4729cd57 278 flag_Tx=0;
gabrielap 0:293d4729cd57 279 }
gabrielap 0:293d4729cd57 280
gabrielap 0:293d4729cd57 281 // Recepcion Rx2
gabrielap 0:293d4729cd57 282 // Si llego un msje
gabrielap 0:293d4729cd57 283 if (Downlink_Counter_old!=(Downlink_Counter=dot->getDownLinkCounter()))
gabrielap 0:293d4729cd57 284 {
gabrielap 0:293d4729cd57 285 Downlink_Counter_old=Downlink_Counter;
gabrielap 0:293d4729cd57 286 // Leo el buffer
gabrielap 0:293d4729cd57 287 // MAX Payload 52 Bytes (Rx total 65 Bytes)
gabrielap 0:293d4729cd57 288 if(dot->recv(rx_data)==dot->MDOT_OK)
gabrielap 0:293d4729cd57 289 {
gabrielap 0:293d4729cd57 290 printf("longitud = %d", rx_data.size());
gabrielap 0:293d4729cd57 291
gabrielap 0:293d4729cd57 292 // Muestra todos los Bytes
gabrielap 0:293d4729cd57 293 printf("\n\rDato %d recibido:", Downlink_Counter);
gabrielap 0:293d4729cd57 294 for(int i=0;i<rx_data.size();i++)
gabrielap 0:293d4729cd57 295 {
gabrielap 0:293d4729cd57 296 printf("%02X ", rx_data[i]);
gabrielap 0:293d4729cd57 297 }
gabrielap 0:293d4729cd57 298 printf("\n\r");
gabrielap 0:293d4729cd57 299
gabrielap 0:293d4729cd57 300 rx_data.clear();
gabrielap 0:293d4729cd57 301 }
gabrielap 0:293d4729cd57 302 }
gabrielap 0:293d4729cd57 303
gabrielap 0:293d4729cd57 304
gabrielap 0:293d4729cd57 305 }
gabrielap 0:293d4729cd57 306
gabrielap 0:293d4729cd57 307 return 0;
gabrielap 0:293d4729cd57 308 }
gabrielap 0:293d4729cd57 309
gabrielap 0:293d4729cd57 310
gabrielap 0:293d4729cd57 311
gabrielap 0:293d4729cd57 312