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

Committer:
AndersonIctus
Date:
Sun Jul 07 15:36:11 2019 +0000
Revision:
17:579f6353c6fb
Parent:
16:2b3715dd0f13
Child:
20:b25436619b59
Child:
22:b69940ec3452
- Melhorando inicializacao da comunicacao

Who changed what in which revision?

UserRevisionLine numberNew contents of line
AndersonIctus 12:2109a8bc8848 1 #include "mbed.h"
AndersonIctus 7:cabeef41022a 2 #include "sgam_mdw.h"
AndersonIctus 10:cb7a807b5225 3
AndersonIctus 7:cabeef41022a 4 #include "ControlImpl.h"
AndersonIctus 7:cabeef41022a 5 #include "Gyroscope.h"
AndersonIctus 14:8ec6bdff6f67 6 #include "LoRAWanComm.h"
AndersonIctus 16:2b3715dd0f13 7 #include "LoRaWANInterface.h"
AndersonIctus 16:2b3715dd0f13 8 #include "lora_radio_helper.h"
AndersonIctus 7:cabeef41022a 9
AndersonIctus 7:cabeef41022a 10 ControlImpl ctrl;
AndersonIctus 7:cabeef41022a 11
AndersonIctus 12:2109a8bc8848 12 #define D_LOG(args...) printf(args)
AndersonIctus 12:2109a8bc8848 13
AndersonIctus 14:8ec6bdff6f67 14 // LORA WAN CONFIGS
AndersonIctus 14:8ec6bdff6f67 15 #define TX_INTERVAL 10000
AndersonIctus 14:8ec6bdff6f67 16 #define MBED_CONF_LORA_APP_PORT 15 //15
AndersonIctus 14:8ec6bdff6f67 17
AndersonIctus 14:8ec6bdff6f67 18 static uint8_t LORAWAN_DEV_EUI[] = { 0x00, 0x1C, 0x73, 0x4A, 0x55, 0x89, 0xAE, 0xC6 };
AndersonIctus 14:8ec6bdff6f67 19 static uint8_t LORAWAN_APP_EUI[] = { 0x70, 0xB3, 0xD5, 0x7E, 0xD0, 0x01, 0xD0, 0x2F };
AndersonIctus 14:8ec6bdff6f67 20 static uint8_t LORAWAN_APP_KEY[] = { 0x75, 0xC6, 0xF2, 0xFB, 0xEE, 0xA9, 0x82, 0x6C, 0xA0, 0xBD, 0xB9, 0x0F, 0xC9, 0xEC, 0xF7, 0x10 };
AndersonIctus 14:8ec6bdff6f67 21
AndersonIctus 16:2b3715dd0f13 22 // static uint32_t DEVADDR_1 = 0x2601177B;
AndersonIctus 16:2b3715dd0f13 23 // static uint8_t NWKSKEY_1[] = { 0x2A, 0xE9, 0x3F, 0x5E, 0x4C, 0x25, 0xDC, 0x34, 0x16, 0x82, 0x1B, 0xD1, 0x1A, 0x7F, 0xD0, 0xF6 };
AndersonIctus 16:2b3715dd0f13 24 // static uint8_t APPSKEY_1[] = { 0x78, 0x59, 0x61, 0xBB, 0x6B, 0xB7, 0xDE, 0x57, 0x80, 0x74, 0xAF, 0xED, 0x10, 0xD7, 0x47, 0x18 };
AndersonIctus 16:2b3715dd0f13 25
AndersonIctus 16:2b3715dd0f13 26 static void lora_event_handler(lorawan_event_t event);
AndersonIctus 16:2b3715dd0f13 27 static int16_t count_message;
AndersonIctus 14:8ec6bdff6f67 28
AndersonIctus 17:579f6353c6fb 29 EventQueue ev_queue;
AndersonIctus 17:579f6353c6fb 30 static lorawan_app_callbacks_t callbacks;
AndersonIctus 17:579f6353c6fb 31
AndersonIctus 0:5d86284beacd 32 int main() {
AndersonIctus 12:2109a8bc8848 33 D_LOG("=============== INICIO ===============\r\n");
AndersonIctus 7:cabeef41022a 34
AndersonIctus 7:cabeef41022a 35 Control* control = ctrl.getControler();
AndersonIctus 12:2109a8bc8848 36 control->initialize();
AndersonIctus 7:cabeef41022a 37
AndersonIctus 14:8ec6bdff6f67 38 Communication<LoraData>* comm = (Communication<LoraData>*)control->getCommunication("LoRAWAN");
AndersonIctus 14:8ec6bdff6f67 39
AndersonIctus 16:2b3715dd0f13 40 ////////////////////////////////////////////////////////////////////////////////////////////
AndersonIctus 17:579f6353c6fb 41 // 1 - Configura os callbacks do loran WAN e inicializa os dados !!
AndersonIctus 17:579f6353c6fb 42 // Dados de Conexão
AndersonIctus 17:579f6353c6fb 43 lorawan_connect_t connect_params;
AndersonIctus 17:579f6353c6fb 44 connect_params.connect_type = LORAWAN_CONNECTION_OTAA;
AndersonIctus 10:cb7a807b5225 45
AndersonIctus 17:579f6353c6fb 46 connect_params.connection_u.otaa.dev_eui = LORAWAN_DEV_EUI;
AndersonIctus 17:579f6353c6fb 47 connect_params.connection_u.otaa.app_eui = LORAWAN_APP_EUI;
AndersonIctus 17:579f6353c6fb 48 connect_params.connection_u.otaa.app_key = LORAWAN_APP_KEY;
AndersonIctus 17:579f6353c6fb 49 connect_params.connection_u.otaa.nb_trials = 10;
AndersonIctus 14:8ec6bdff6f67 50
AndersonIctus 17:579f6353c6fb 51 LoraData* data = new LoraData(&connect_params, (LoRaRadio*)&radio, &ev_queue );
AndersonIctus 17:579f6353c6fb 52 callbacks.events = mbed::callback(lora_event_handler);
AndersonIctus 17:579f6353c6fb 53 data->prepareCallBack(callbacks);
AndersonIctus 17:579f6353c6fb 54
AndersonIctus 17:579f6353c6fb 55 /////////////////////////////////////////////////////////////////////////////////////////////
AndersonIctus 17:579f6353c6fb 56 // 2 - INICIALIZA A CONEXAO
AndersonIctus 17:579f6353c6fb 57 if( comm->initialize(data) != TRUE) {
AndersonIctus 16:2b3715dd0f13 58 D_LOG("Inicialização falhou !!\r\n");
AndersonIctus 16:2b3715dd0f13 59 return -1;
AndersonIctus 16:2b3715dd0f13 60 }
AndersonIctus 16:2b3715dd0f13 61
AndersonIctus 16:2b3715dd0f13 62 ////////////////////////////////////////////////////////////////////////////////////////////
AndersonIctus 17:579f6353c6fb 63 // 3 - Tentar conectar
AndersonIctus 17:579f6353c6fb 64 lorawan_status_t retcode = (lorawan_status_t) comm->connect();
AndersonIctus 17:579f6353c6fb 65 if (retcode == LORAWAN_STATUS_OK || retcode == LORAWAN_STATUS_CONNECT_IN_PROGRESS) {
AndersonIctus 17:579f6353c6fb 66 printf("Connection - In Progress ...\r\n");
AndersonIctus 17:579f6353c6fb 67 } else {
AndersonIctus 17:579f6353c6fb 68 printf("Connection error, code = %d \r\n", retcode);
AndersonIctus 14:8ec6bdff6f67 69 return -1;
AndersonIctus 14:8ec6bdff6f67 70 }
AndersonIctus 7:cabeef41022a 71
AndersonIctus 17:579f6353c6fb 72 // make your event queue dispatching events forever
AndersonIctus 17:579f6353c6fb 73 ev_queue.dispatch_forever();
AndersonIctus 7:cabeef41022a 74
AndersonIctus 10:cb7a807b5225 75 control->finalize();
AndersonIctus 12:2109a8bc8848 76 D_LOG("=============== FINAL ===============\r\n");
AndersonIctus 7:cabeef41022a 77 return 1;
AndersonIctus 0:5d86284beacd 78 }
AndersonIctus 16:2b3715dd0f13 79
AndersonIctus 16:2b3715dd0f13 80 /**
AndersonIctus 16:2b3715dd0f13 81 * Receive a message from the Network Server
AndersonIctus 16:2b3715dd0f13 82 */
AndersonIctus 16:2b3715dd0f13 83 static void receive_message()
AndersonIctus 16:2b3715dd0f13 84 {
AndersonIctus 16:2b3715dd0f13 85 printf("receive_message()\n");
AndersonIctus 16:2b3715dd0f13 86
AndersonIctus 16:2b3715dd0f13 87 // uint8_t rx_buffer[50] = { 0 };
AndersonIctus 16:2b3715dd0f13 88 // int16_t retcode = lorawan.receive(MBED_CONF_LORA_APP_PORT, rx_buffer,
AndersonIctus 16:2b3715dd0f13 89 // sizeof(rx_buffer),
AndersonIctus 16:2b3715dd0f13 90 // MSG_CONFIRMED_FLAG|MSG_UNCONFIRMED_FLAG);
AndersonIctus 16:2b3715dd0f13 91
AndersonIctus 16:2b3715dd0f13 92 // if (retcode < 0) {
AndersonIctus 16:2b3715dd0f13 93 // printf("receive() - Error code %d \r\n", retcode);
AndersonIctus 16:2b3715dd0f13 94 // return;
AndersonIctus 16:2b3715dd0f13 95 // }
AndersonIctus 16:2b3715dd0f13 96
AndersonIctus 16:2b3715dd0f13 97 // printf("RX Data (%d bytes): ", retcode);
AndersonIctus 16:2b3715dd0f13 98 // for (uint8_t i = 0; i < retcode; i++) {
AndersonIctus 16:2b3715dd0f13 99 // printf("%02x ", rx_buffer[i]);
AndersonIctus 16:2b3715dd0f13 100 // }
AndersonIctus 16:2b3715dd0f13 101 printf("\r\n");
AndersonIctus 16:2b3715dd0f13 102 }
AndersonIctus 16:2b3715dd0f13 103
AndersonIctus 16:2b3715dd0f13 104 /**
AndersonIctus 16:2b3715dd0f13 105 * Sends a message to the Network Server
AndersonIctus 16:2b3715dd0f13 106 */
AndersonIctus 16:2b3715dd0f13 107 static void send_message() {
AndersonIctus 16:2b3715dd0f13 108 //printf("send_message()\n");
AndersonIctus 16:2b3715dd0f13 109
AndersonIctus 16:2b3715dd0f13 110 // YOUR CODE HERE
AndersonIctus 16:2b3715dd0f13 111 // int16_t temperature = 10;
AndersonIctus 16:2b3715dd0f13 112 // //printf("temperature = (%d)\n", temperature);
AndersonIctus 16:2b3715dd0f13 113 // CayenneLPP payload(50);
AndersonIctus 16:2b3715dd0f13 114 // payload.addTemperature(1, temperature);
AndersonIctus 16:2b3715dd0f13 115 // int16_t retcode = lorawan.send(MBED_CONF_LORA_APP_PORT, payload.getBuffer(), payload.getSize(), MSG_UNCONFIRMED_FLAG);
AndersonIctus 16:2b3715dd0f13 116 // //printf("lorawan.send = retcode [%d]\n",retcode);
AndersonIctus 16:2b3715dd0f13 117
AndersonIctus 16:2b3715dd0f13 118 // if (retcode < 0) {
AndersonIctus 16:2b3715dd0f13 119 // retcode == LORAWAN_STATUS_WOULD_BLOCK ? printf("send - Duty cycle violation\r\n")
AndersonIctus 16:2b3715dd0f13 120 // : printf("send() - Error code %d \r\n", retcode);
AndersonIctus 16:2b3715dd0f13 121
AndersonIctus 16:2b3715dd0f13 122 // if (retcode == LORAWAN_STATUS_NO_ACTIVE_SESSIONS)
AndersonIctus 16:2b3715dd0f13 123 // printf("\r\n|-1017 - LORAWAN_STATUS_NO_ACTIVE_SESSIONS");
AndersonIctus 16:2b3715dd0f13 124
AndersonIctus 16:2b3715dd0f13 125 // if (retcode == LORAWAN_STATUS_WOULD_BLOCK) {
AndersonIctus 16:2b3715dd0f13 126 // //retry in 3 seconds
AndersonIctus 16:2b3715dd0f13 127 // ev_queue.call_in(3000, send_message);
AndersonIctus 16:2b3715dd0f13 128 // }
AndersonIctus 16:2b3715dd0f13 129 // else {
AndersonIctus 16:2b3715dd0f13 130 // ev_queue.call_in(TX_INTERVAL, send_message);
AndersonIctus 16:2b3715dd0f13 131 // }
AndersonIctus 16:2b3715dd0f13 132 // return;
AndersonIctus 16:2b3715dd0f13 133 // }
AndersonIctus 16:2b3715dd0f13 134
AndersonIctus 16:2b3715dd0f13 135 // ev_queue.call_in(TX_INTERVAL, send_message);
AndersonIctus 16:2b3715dd0f13 136
AndersonIctus 16:2b3715dd0f13 137 //receive_message();
AndersonIctus 16:2b3715dd0f13 138 //printf("%d bytes scheduled for transmission \r\n", retcode);
AndersonIctus 16:2b3715dd0f13 139 }
AndersonIctus 16:2b3715dd0f13 140
AndersonIctus 16:2b3715dd0f13 141 /**
AndersonIctus 16:2b3715dd0f13 142 * Event handler
AndersonIctus 16:2b3715dd0f13 143 */
AndersonIctus 16:2b3715dd0f13 144 static void lora_event_handler(lorawan_event_t event)
AndersonIctus 16:2b3715dd0f13 145 {
AndersonIctus 17:579f6353c6fb 146 // switch (event) {
AndersonIctus 17:579f6353c6fb 147 // case CONNECTED:
AndersonIctus 17:579f6353c6fb 148 // printf("# Connection - Successful \r\n");
AndersonIctus 17:579f6353c6fb 149 // if (MBED_CONF_LORA_DUTY_CYCLE_ON) {
AndersonIctus 17:579f6353c6fb 150 // send_message();
AndersonIctus 17:579f6353c6fb 151 // } else {
AndersonIctus 17:579f6353c6fb 152 // // ev_queue.call_in(TX_INTERVAL, send_message);
AndersonIctus 17:579f6353c6fb 153 // }
AndersonIctus 17:579f6353c6fb 154 // break;
AndersonIctus 16:2b3715dd0f13 155
AndersonIctus 17:579f6353c6fb 156 // case DISCONNECTED:
AndersonIctus 17:579f6353c6fb 157 // // ev_queue.break_dispatch();
AndersonIctus 17:579f6353c6fb 158 // printf("# Disconnected Successfully \r\n");
AndersonIctus 17:579f6353c6fb 159 // break;
AndersonIctus 17:579f6353c6fb 160 // case RX_DONE:
AndersonIctus 17:579f6353c6fb 161 // printf("# Received message from Network Server \r\n");
AndersonIctus 17:579f6353c6fb 162 // receive_message();
AndersonIctus 17:579f6353c6fb 163 // break;
AndersonIctus 17:579f6353c6fb 164 // case RX_TIMEOUT:
AndersonIctus 17:579f6353c6fb 165 // case RX_ERROR:
AndersonIctus 17:579f6353c6fb 166 // printf("# Error in reception - Code = %d \r\n", event);
AndersonIctus 17:579f6353c6fb 167 // break;
AndersonIctus 17:579f6353c6fb 168 // case TX_DONE:
AndersonIctus 17:579f6353c6fb 169 // count_message++;
AndersonIctus 17:579f6353c6fb 170 // printf("# Message Sent to Network Server - Count [%d] \r\n", count_message);
AndersonIctus 17:579f6353c6fb 171 // break;
AndersonIctus 17:579f6353c6fb 172 // case TX_TIMEOUT:
AndersonIctus 17:579f6353c6fb 173 // case TX_ERROR:
AndersonIctus 17:579f6353c6fb 174 // case TX_CRYPTO_ERROR:
AndersonIctus 17:579f6353c6fb 175 // case TX_SCHEDULING_ERROR:
AndersonIctus 17:579f6353c6fb 176 // printf("# Transmission Error - EventCode = %d \r\n", event);
AndersonIctus 17:579f6353c6fb 177 // break;
AndersonIctus 17:579f6353c6fb 178 // case JOIN_FAILURE:
AndersonIctus 17:579f6353c6fb 179 // printf("# OTAA Failed - Check Keys \r\n");
AndersonIctus 17:579f6353c6fb 180 // break;
AndersonIctus 17:579f6353c6fb 181 // case UPLINK_REQUIRED:
AndersonIctus 17:579f6353c6fb 182 // printf("# Uplink required by NS \r\n");
AndersonIctus 17:579f6353c6fb 183 // send_message();
AndersonIctus 17:579f6353c6fb 184 // break;
AndersonIctus 17:579f6353c6fb 185 // default:
AndersonIctus 17:579f6353c6fb 186 // printf("# Unknown Event \r\n");
AndersonIctus 17:579f6353c6fb 187 // MBED_ASSERT("# Unknown Event");
AndersonIctus 17:579f6353c6fb 188 // }
AndersonIctus 16:2b3715dd0f13 189 }