Projeto teste do Programa de integracao com biblioteca SGAM_MDW !! Aqui visa um usuario que usa o projeto implementado para a placa NUCLEO F429ZI
Dependencies: sgam_mdw sgam_mdw_NUCLEOF429ZI_impl Cayenne-LPP
main.cpp
00001 #include "mbed.h" 00002 #include "sgam_mdw.h" 00003 #include "sgam_mdw_impl.h" 00004 00005 #include "CayenneLPP.h" 00006 00007 Serial pc(USBTX, USBRX); // SAIDA SERIAL PADRÃO 00008 // #define D_LOG(args...) pc.printf(args) 00009 00010 #define ONE_SECOND 1 00011 00012 // LORA WAN CONFIGS 00013 #define TX_INTERVAL 10000 00014 #define MBED_CONF_LORA_APP_PORT 15 //15 00015 00016 static uint8_t LORAWAN_DEV_EUI[] = { 0x00, 0x1C, 0x73, 0x4A, 0x55, 0x89, 0xAE, 0xC6 }; 00017 static uint8_t LORAWAN_APP_EUI[] = { 0x70, 0xB3, 0xD5, 0x7E, 0xD0, 0x01, 0xD0, 0x2F }; 00018 static uint8_t LORAWAN_APP_KEY[] = { 0x75, 0xC6, 0xF2, 0xFB, 0xEE, 0xA9, 0x82, 0x6C, 0xA0, 0xBD, 0xB9, 0x0F, 0xC9, 0xEC, 0xF7, 0x10 }; 00019 00020 //////////////////////////////////////////////////////////////// 00021 static void lora_event_handler(lorawan_event_t event); 00022 static int16_t count_message; 00023 00024 static lorawan_app_callbacks_t callbacks; 00025 00026 ControlImpl ctrl; 00027 Communication<LoraData>* comm = NULL; 00028 00029 //////////////////////////////////////////////////////////////// 00030 int inicializa_lorawan(Control* control); 00031 00032 void read_send_gyroscope(void const *self_context); 00033 void read_send_temperature(void const *self_context); 00034 void read_send_GPS(void const *self_context); 00035 void inicializa_thread_GPS(Control* control); 00036 00037 void finalize_sending(Communication<void*>* COMM, CayenneLPP* payload); 00038 static void receive_message(); 00039 00040 // FILAS PARA EVENTOS 00041 EventQueue ev_queue; // Usado no callback do lora wan 00042 00043 int main() { 00044 D_LOG("=============== INICIO ===============\r\n"); 00045 00046 Control* control = ctrl.getControler(); 00047 control->initialize(); 00048 00049 ////////////////////////////////////////////////////////////////////////// 00050 // 1 - Inicializa o LoRaWAN 00051 inicializa_lorawan(control); 00052 00053 // 2 - Prepara as threads de Temperatura e Gyroscopio 00054 // 2.1 -> A cada 20 Segundos, faz uma leitura de Temperatura e Envia pro LoRaWan 00055 Thread eventTemperature(osPriorityNormal); 00056 eventTemperature.start(callback(read_send_temperature, control)); 00057 00058 // 2.2 -> Se houver 10 Mudanças no Gyroscopio, ele Aciona o GPS, que fica enviando ao LoRaWan sua localização a cada 10 segundos !! 00059 Thread eventTGyroscope(osPriorityNormal); 00060 eventTGyroscope.start(callback(read_send_gyroscope, control)); 00061 00062 ////////////////////////////////////////////////////////////////////////// 00063 00064 // make your event queue dispatching events forever 00065 wait(osWaitForever); // Para a thread principal aqui ! 00066 00067 control->finalize(); 00068 D_LOG("=============== FINAL =============== \r\n"); 00069 return 1; 00070 } 00071 00072 int inicializa_lorawan(Control* control) { 00073 D_LOG("=============== LoRaWAN Comunication ===============\r\n"); 00074 00075 comm = (Communication<LoraData>*)control->getCommunication("LoRAWAN"); 00076 00077 //////////////////////////////////////////////////////////////////////////////////////////// 00078 // 1 - Configura os callbacks do loran WAN e inicializa os dados !! 00079 // Dados de Conexão 00080 lorawan_connect_t connect_params; 00081 connect_params.connect_type = LORAWAN_CONNECTION_OTAA; 00082 00083 connect_params.connection_u.otaa.dev_eui = LORAWAN_DEV_EUI; 00084 connect_params.connection_u.otaa.app_eui = LORAWAN_APP_EUI; 00085 connect_params.connection_u.otaa.app_key = LORAWAN_APP_KEY; 00086 connect_params.connection_u.otaa.nb_trials = 10; 00087 00088 LoraData* data = new LoraData(&connect_params, (LoRaRadio*)&radio, &ev_queue); 00089 callbacks.events = mbed::callback(lora_event_handler); 00090 data->prepareCallBack(callbacks); 00091 00092 // Flags de leitura !! 00093 data->lora_port = MBED_CONF_LORA_APP_PORT; 00094 00095 ///////////////////////////////////////////////////////////////////////////////////////////// 00096 // 2 - INICIALIZA A CONEXAO 00097 if( comm->initialize(data) != TRUE) { 00098 D_LOG("Inicialização falhou !!\r\n"); 00099 return -1; 00100 } 00101 00102 //////////////////////////////////////////////////////////////////////////////////////////// 00103 // 3 - Tentar conectar 00104 lorawan_status_t retcode = (lorawan_status_t) comm->connect(); 00105 if (retcode == LORAWAN_STATUS_OK || retcode == LORAWAN_STATUS_CONNECT_IN_PROGRESS) { 00106 printf("Connection - In Progress ...\r\n"); 00107 } else { 00108 printf("Connection error, code = %d \r\n", retcode); 00109 return -1; 00110 } 00111 00112 return 1; 00113 } 00114 00115 // --------------------------------------------------------------------------------- // 00116 void read_send_temperature(void const *self_context) { 00117 D_LOG("=============== TEMPERATURE ===============\r\n"); 00118 Control* control = (Control*)self_context; 00119 00120 Sensor<void*>* temp = control->getSensor("Temperature"); 00121 Communication<void*>* COMM = control->getCommunication("LoRAWAN"); 00122 00123 CayenneLPP payload(50); 00124 00125 while(1) { 00126 float* valor = (float*)temp->getValue(); 00127 D_LOG("TEMPERATURA -> %f\r\n", *valor); 00128 00129 payload.addTemperature(1, *valor); 00130 00131 // FINALIZE SENDING ... 00132 finalize_sending(COMM, &payload); 00133 00134 ThisThread::sleep_for(20* ONE_SECOND); // Espera 20 Segundos !! 00135 } 00136 } 00137 00138 void read_send_gyroscope(void const *self_context) { 00139 D_LOG("=============== GYROSCOPE ===============\r\n"); 00140 Control* control = (Control*)self_context; 00141 00142 Sensor<void*>* gyro = control->getSensor("Gyroscope"); 00143 Communication<void*>* COMM = control->getCommunication("LoRAWAN"); 00144 00145 CayenneLPP payload(100); 00146 00147 GyroscopeData* dataAnterior = NULL; 00148 int repeticoes = 0; 00149 bool iniciaGPS = false; 00150 00151 while(1) { 00152 GyroscopeData* data = (GyroscopeData*) gyro->getValue(); 00153 D_LOG("Gyro (X) => %d \r\n", data->gx); 00154 D_LOG("Gyro (Y) => %d \r\n", data->gy); 00155 D_LOG("Gyro (Z) => %d \r\n", data->gz); 00156 D_LOG("***********************************\r\n\r\n"); 00157 00158 payload.addGyrometer(1, data->gx, data->gy, data->gz); 00159 00160 // FINALIZE SENDING ... 00161 finalize_sending(COMM, &payload); 00162 00163 if(dataAnterior != NULL && iniciaGPS == false) { 00164 if( dataAnterior->gx != data->gx || 00165 dataAnterior->gy != data->gy || 00166 dataAnterior->gz != data->gz ){ 00167 if(++repeticoes > 10) { 00168 iniciaGPS = true; 00169 inicializa_thread_GPS(control); 00170 } 00171 } 00172 } 00173 dataAnterior = data; 00174 00175 ThisThread::sleep_for(10* ONE_SECOND); // Espera 10 Segundos para proxima leitura !! 00176 } 00177 } 00178 00179 void read_send_GPS(void const *self_context) { 00180 D_LOG("=================== GPS ==================\r\n"); 00181 Control* control = (Control*)self_context; 00182 00183 Sensor<void*>* gps = control->getSensor("GPS"); 00184 Communication<void*>* COMM = control->getCommunication("LoRAWAN"); 00185 00186 CayenneLPP payload(100); 00187 00188 while(1) { 00189 GPSData* data = (GPSData*) gps->getValue(); 00190 D_LOG("GPS (Lat) => %d \r\n", data->latitude); 00191 D_LOG("GPS (Long) => %d \r\n", data->longitude); 00192 D_LOG("GPS (Meters) => %d \r\n", data->meters); 00193 D_LOG("***********************************\r\n\r\n"); 00194 00195 payload.addGPS(1, data->latitude, data->longitude, data->meters); 00196 00197 // FINALIZE SENDING ... 00198 finalize_sending(COMM, &payload); 00199 00200 ThisThread::sleep_for(5* ONE_SECOND); // Espera 5 Segundos para proxima leitura !! 00201 } 00202 } 00203 00204 void inicializa_thread_GPS(Control* control) { 00205 // 2.2 -> Se houver 10 Mudanças no Gyroscopio, ele Aciona o GPS, que fica enviando ao LoRaWan sua localização a cada 10 segundos !! 00206 Thread eventTGPS(osPriorityNormal); 00207 eventTGPS.start(callback(read_send_GPS, control)); 00208 } 00209 00210 // --------------------------------------------------------------------------------- // 00211 void finalize_sending(Communication<void*>* COMM, CayenneLPP* payload) { 00212 LoraData* data = (LoraData*) COMM->getData(); 00213 data->read_write_flags = MSG_UNCONFIRMED_FLAG; 00214 00215 int16_t retcode = COMM->write(payload->getBuffer(), payload->getSize()); 00216 D_LOG("lorawan.send = retcode [%d]\n", retcode); 00217 00218 if (retcode < 0) { 00219 retcode == LORAWAN_STATUS_WOULD_BLOCK 00220 ? D_LOG("send - Duty cycle violation\r\n") 00221 : D_LOG("send() - Error code %d \r\n", retcode); 00222 00223 if (retcode == LORAWAN_STATUS_NO_ACTIVE_SESSIONS) 00224 D_LOG("\r\n|-1017 - LORAWAN_STATUS_NO_ACTIVE_SESSIONS"); 00225 00226 if (retcode == LORAWAN_STATUS_WOULD_BLOCK) { // Retry in 3 seconds 00227 // queue->call_in(3000, send_message); 00228 D_LOG("RETRY FOI REMOVIDO DO LORAWAN !!\r\n"); 00229 } else { 00230 // queue->call_in(TX_INTERVAL, send_message); 00231 D_LOG("Nao re-envia a mensagem !!"); 00232 } 00233 00234 return; 00235 } 00236 00237 // TODO: precisa re-enviar ?? queue->call_in(TX_INTERVAL, send_message); [Passar comunicacao/payload denovo ??] 00238 00239 receive_message(); 00240 D_LOG("%d bytes scheduled for transmission \r\n", retcode); 00241 } 00242 00243 /** 00244 * Receive a message from the Network Server 00245 */ 00246 static void receive_message() 00247 { 00248 printf("receive_message()\n"); 00249 00250 LoraData* data = (LoraData*) comm->getData(); 00251 data->read_write_flags = MSG_CONFIRMED_FLAG|MSG_UNCONFIRMED_FLAG; 00252 00253 uint8_t rx_buffer[50] = { 0 }; 00254 int16_t retcode = comm->read( rx_buffer, sizeof(rx_buffer) ); 00255 if (retcode < 0) { 00256 printf("receive() - Error code %d \r\n", retcode); 00257 return; 00258 } 00259 00260 printf("RX Data (%d bytes): ", retcode); 00261 for (uint8_t i = 0; i < retcode; i++) { 00262 printf("%02x ", rx_buffer[i]); 00263 } 00264 00265 printf("\r\n"); 00266 } 00267 00268 /** 00269 * Sends a message to the Network Server 00270 */ 00271 static void send_message() { 00272 printf("send_message()\n"); 00273 00274 // YOUR CODE HERE 00275 int16_t temperature = 10; 00276 printf("temperature = (%d)\n", temperature); 00277 00278 CayenneLPP payload(50); 00279 payload.addTemperature(1, temperature); 00280 00281 LoraData* data = (LoraData*) comm->getData(); 00282 data->read_write_flags = MSG_UNCONFIRMED_FLAG; 00283 00284 int16_t retcode = comm->write(payload.getBuffer(), payload.getSize()); 00285 printf("lorawan.send = retcode [%d]\n",retcode); 00286 00287 if (retcode < 0) { 00288 retcode == LORAWAN_STATUS_WOULD_BLOCK 00289 ? printf("send - Duty cycle violation\r\n") 00290 : printf("send() - Error code %d \r\n", retcode); 00291 00292 if (retcode == LORAWAN_STATUS_NO_ACTIVE_SESSIONS) 00293 printf("\r\n|-1017 - LORAWAN_STATUS_NO_ACTIVE_SESSIONS"); 00294 00295 if (retcode == LORAWAN_STATUS_WOULD_BLOCK) { //retry in 3 seconds 00296 ev_queue.call_in(3000, send_message); 00297 } else { 00298 ev_queue.call_in(TX_INTERVAL, send_message); 00299 } 00300 00301 return; 00302 } 00303 00304 ev_queue.call_in(TX_INTERVAL, send_message); 00305 00306 receive_message(); 00307 printf("%d bytes scheduled for transmission \r\n", retcode); 00308 } 00309 00310 /** 00311 * Event handler 00312 */ 00313 static void lora_event_handler(lorawan_event_t event) { 00314 switch (event) { 00315 case CONNECTED: 00316 printf("# Connection - Successful \r\n"); 00317 if (MBED_CONF_LORA_DUTY_CYCLE_ON) { 00318 send_message(); 00319 } else { 00320 ev_queue.call_in(TX_INTERVAL, send_message); 00321 } 00322 break; 00323 00324 case DISCONNECTED: 00325 ev_queue.break_dispatch(); 00326 printf("# Disconnected Successfully \r\n"); 00327 break; 00328 case RX_DONE: 00329 printf("# Received message from Network Server \r\n"); 00330 receive_message(); 00331 break; 00332 case RX_TIMEOUT: 00333 case RX_ERROR: 00334 printf("# Error in reception - Code = %d \r\n", event); 00335 break; 00336 case TX_DONE: 00337 count_message++; 00338 printf("# Message Sent to Network Server - Count [%d] \r\n", count_message); 00339 break; 00340 case TX_TIMEOUT: 00341 case TX_ERROR: 00342 case TX_CRYPTO_ERROR: 00343 case TX_SCHEDULING_ERROR: 00344 printf("# Transmission Error - EventCode = %d \r\n", event); 00345 break; 00346 case JOIN_FAILURE: 00347 printf("# OTAA Failed - Check Keys \r\n"); 00348 break; 00349 case UPLINK_REQUIRED: 00350 printf("# Uplink required by NS \r\n"); 00351 send_message(); 00352 break; 00353 default: 00354 printf("# Unknown Event \r\n"); 00355 // MBED_ASSERT("# Unknown Event"); 00356 } 00357 }
Generated on Sun Jul 17 2022 12:42:28 by 1.7.2