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:
Sat Jul 20 11:02:15 2019 -0300
Revision:
31:8b734e4fb89b
Parent:
29:8251bb11d117
Child:
33:31e2feb7bf78
- update de inclusao do helper do controler implementation.

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 31:8b734e4fb89b 3 #include "sgam_mdw_impl.h"
AndersonIctus 10:cb7a807b5225 4
AndersonIctus 28:18a54c786ba9 5 #include "CayenneLPP.h"
AndersonIctus 28:18a54c786ba9 6
AndersonIctus 12:2109a8bc8848 7 #define D_LOG(args...) printf(args)
AndersonIctus 12:2109a8bc8848 8
AndersonIctus 14:8ec6bdff6f67 9 // LORA WAN CONFIGS
AndersonIctus 14:8ec6bdff6f67 10 #define TX_INTERVAL 10000
AndersonIctus 14:8ec6bdff6f67 11 #define MBED_CONF_LORA_APP_PORT 15 //15
AndersonIctus 14:8ec6bdff6f67 12
AndersonIctus 14:8ec6bdff6f67 13 static uint8_t LORAWAN_DEV_EUI[] = { 0x00, 0x1C, 0x73, 0x4A, 0x55, 0x89, 0xAE, 0xC6 };
AndersonIctus 14:8ec6bdff6f67 14 static uint8_t LORAWAN_APP_EUI[] = { 0x70, 0xB3, 0xD5, 0x7E, 0xD0, 0x01, 0xD0, 0x2F };
AndersonIctus 14:8ec6bdff6f67 15 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 16
AndersonIctus 16:2b3715dd0f13 17 // static uint32_t DEVADDR_1 = 0x2601177B;
AndersonIctus 16:2b3715dd0f13 18 // static uint8_t NWKSKEY_1[] = { 0x2A, 0xE9, 0x3F, 0x5E, 0x4C, 0x25, 0xDC, 0x34, 0x16, 0x82, 0x1B, 0xD1, 0x1A, 0x7F, 0xD0, 0xF6 };
AndersonIctus 16:2b3715dd0f13 19 // static uint8_t APPSKEY_1[] = { 0x78, 0x59, 0x61, 0xBB, 0x6B, 0xB7, 0xDE, 0x57, 0x80, 0x74, 0xAF, 0xED, 0x10, 0xD7, 0x47, 0x18 };
AndersonIctus 16:2b3715dd0f13 20
AndersonIctus 16:2b3715dd0f13 21 static void lora_event_handler(lorawan_event_t event);
AndersonIctus 16:2b3715dd0f13 22 static int16_t count_message;
AndersonIctus 14:8ec6bdff6f67 23
AndersonIctus 17:579f6353c6fb 24 EventQueue ev_queue;
AndersonIctus 17:579f6353c6fb 25 static lorawan_app_callbacks_t callbacks;
AndersonIctus 17:579f6353c6fb 26
AndersonIctus 29:8251bb11d117 27 ControlImpl ctrl;
AndersonIctus 20:b25436619b59 28 Communication<LoraData>* comm = NULL;
AndersonIctus 20:b25436619b59 29
AndersonIctus 29:8251bb11d117 30 int test_temperature(Control* control);
AndersonIctus 29:8251bb11d117 31 int test_gyroscope(Control* control);
AndersonIctus 29:8251bb11d117 32 int test_lorawan(Control* control);
AndersonIctus 29:8251bb11d117 33
AndersonIctus 0:5d86284beacd 34 int main() {
AndersonIctus 12:2109a8bc8848 35 D_LOG("=============== INICIO ===============\r\n");
AndersonIctus 7:cabeef41022a 36
AndersonIctus 29:8251bb11d117 37 int ret = 0;
AndersonIctus 7:cabeef41022a 38 Control* control = ctrl.getControler();
AndersonIctus 12:2109a8bc8848 39 control->initialize();
AndersonIctus 7:cabeef41022a 40
AndersonIctus 29:8251bb11d117 41
AndersonIctus 29:8251bb11d117 42 //////////////////////////////////////////////////////////////////////////
AndersonIctus 29:8251bb11d117 43 // 1 - Test Temnperature ...
AndersonIctus 29:8251bb11d117 44 // ret = test_temperature(control);
AndersonIctus 29:8251bb11d117 45
AndersonIctus 29:8251bb11d117 46 // 2 - Test Gyroscope ...
AndersonIctus 29:8251bb11d117 47 // ret = test_gyroscope(control);
AndersonIctus 29:8251bb11d117 48
AndersonIctus 29:8251bb11d117 49 // 3 - Test LoRaWAN
AndersonIctus 29:8251bb11d117 50 ret = test_lorawan(control);
AndersonIctus 29:8251bb11d117 51 //////////////////////////////////////////////////////////////////////////
AndersonIctus 29:8251bb11d117 52
AndersonIctus 29:8251bb11d117 53 control->finalize();
AndersonIctus 29:8251bb11d117 54 D_LOG("=============== FINAL ===============\r\n");
AndersonIctus 29:8251bb11d117 55 return ret;
AndersonIctus 29:8251bb11d117 56 }
AndersonIctus 29:8251bb11d117 57
AndersonIctus 29:8251bb11d117 58 int test_temperature(Control* control) {
AndersonIctus 29:8251bb11d117 59 D_LOG("=============== TEMPERATURE ===============\r\n");
AndersonIctus 31:8b734e4fb89b 60 Sensor<void*>* temp = control->getSensor("Temperature");
AndersonIctus 29:8251bb11d117 61
AndersonIctus 29:8251bb11d117 62 int count = 0;
AndersonIctus 29:8251bb11d117 63
AndersonIctus 29:8251bb11d117 64 D_LOG("## GET VALUES ....\r\n");
AndersonIctus 29:8251bb11d117 65 while(count++ < 10) {
AndersonIctus 31:8b734e4fb89b 66 float* valor = (float*)temp->getValue();
AndersonIctus 31:8b734e4fb89b 67 D_LOG("## Temp -> %f \r\n", *valor);
AndersonIctus 29:8251bb11d117 68 wait(1.0); // espera 1 segundo !
AndersonIctus 29:8251bb11d117 69 }
AndersonIctus 29:8251bb11d117 70
AndersonIctus 29:8251bb11d117 71 return 1;
AndersonIctus 29:8251bb11d117 72 }
AndersonIctus 29:8251bb11d117 73
AndersonIctus 29:8251bb11d117 74 int test_gyroscope(Control* control) {
AndersonIctus 29:8251bb11d117 75 D_LOG("=============== GYROSCOPE ===============\r\n");
AndersonIctus 29:8251bb11d117 76
AndersonIctus 31:8b734e4fb89b 77 // Sensor<GyroscopeData>* gyro = (Sensor<GyroscopeData>*) control->getSensor("Gyroscope");
AndersonIctus 31:8b734e4fb89b 78 Sensor<void*>* gyro = control->getSensor("Gyroscope");
AndersonIctus 29:8251bb11d117 79
AndersonIctus 29:8251bb11d117 80 int count = 0;
AndersonIctus 29:8251bb11d117 81
AndersonIctus 29:8251bb11d117 82 D_LOG("## GET VALUES ....\r\n");
AndersonIctus 29:8251bb11d117 83 while(count++ < 10) {
AndersonIctus 31:8b734e4fb89b 84 GyroscopeData* data = (GyroscopeData*) gyro->getValue();
AndersonIctus 29:8251bb11d117 85
AndersonIctus 29:8251bb11d117 86 printf("Gyro (X) => %d \r\n", data->gx);
AndersonIctus 29:8251bb11d117 87 printf("Gyro (Y) => %d \r\n", data->gy);
AndersonIctus 29:8251bb11d117 88 printf("Gyro (Z) => %d \r\n", data->gz);
AndersonIctus 29:8251bb11d117 89 printf("***********************************\r\n\r\n");
AndersonIctus 29:8251bb11d117 90
AndersonIctus 29:8251bb11d117 91 wait(1); // espera 1 segundo !
AndersonIctus 29:8251bb11d117 92 }
AndersonIctus 29:8251bb11d117 93
AndersonIctus 29:8251bb11d117 94 return 1;
AndersonIctus 29:8251bb11d117 95 }
AndersonIctus 29:8251bb11d117 96
AndersonIctus 29:8251bb11d117 97 int test_lorawan(Control* control) {
AndersonIctus 29:8251bb11d117 98 D_LOG("=============== LoRaWAN Comunication ===============\r\n");
AndersonIctus 29:8251bb11d117 99
AndersonIctus 20:b25436619b59 100 comm = (Communication<LoraData>*)control->getCommunication("LoRAWAN");
AndersonIctus 14:8ec6bdff6f67 101
AndersonIctus 16:2b3715dd0f13 102 ////////////////////////////////////////////////////////////////////////////////////////////
AndersonIctus 17:579f6353c6fb 103 // 1 - Configura os callbacks do loran WAN e inicializa os dados !!
AndersonIctus 17:579f6353c6fb 104 // Dados de Conexão
AndersonIctus 17:579f6353c6fb 105 lorawan_connect_t connect_params;
AndersonIctus 17:579f6353c6fb 106 connect_params.connect_type = LORAWAN_CONNECTION_OTAA;
AndersonIctus 10:cb7a807b5225 107
AndersonIctus 17:579f6353c6fb 108 connect_params.connection_u.otaa.dev_eui = LORAWAN_DEV_EUI;
AndersonIctus 17:579f6353c6fb 109 connect_params.connection_u.otaa.app_eui = LORAWAN_APP_EUI;
AndersonIctus 17:579f6353c6fb 110 connect_params.connection_u.otaa.app_key = LORAWAN_APP_KEY;
AndersonIctus 17:579f6353c6fb 111 connect_params.connection_u.otaa.nb_trials = 10;
AndersonIctus 14:8ec6bdff6f67 112
AndersonIctus 17:579f6353c6fb 113 LoraData* data = new LoraData(&connect_params, (LoRaRadio*)&radio, &ev_queue );
AndersonIctus 17:579f6353c6fb 114 callbacks.events = mbed::callback(lora_event_handler);
AndersonIctus 17:579f6353c6fb 115 data->prepareCallBack(callbacks);
AndersonIctus 16:2b3715dd0f13 116
AndersonIctus 20:b25436619b59 117 // Flags de leitura !!
AndersonIctus 26:8c55aac7887f 118 data->lora_port = MBED_CONF_LORA_APP_PORT;
AndersonIctus 20:b25436619b59 119
AndersonIctus 17:579f6353c6fb 120 /////////////////////////////////////////////////////////////////////////////////////////////
AndersonIctus 17:579f6353c6fb 121 // 2 - INICIALIZA A CONEXAO
AndersonIctus 17:579f6353c6fb 122 if( comm->initialize(data) != TRUE) {
AndersonIctus 16:2b3715dd0f13 123 D_LOG("Inicialização falhou !!\r\n");
AndersonIctus 16:2b3715dd0f13 124 return -1;
AndersonIctus 16:2b3715dd0f13 125 }
AndersonIctus 7:cabeef41022a 126
AndersonIctus 16:2b3715dd0f13 127 ////////////////////////////////////////////////////////////////////////////////////////////
AndersonIctus 17:579f6353c6fb 128 // 3 - Tentar conectar
AndersonIctus 17:579f6353c6fb 129 lorawan_status_t retcode = (lorawan_status_t) comm->connect();
AndersonIctus 17:579f6353c6fb 130 if (retcode == LORAWAN_STATUS_OK || retcode == LORAWAN_STATUS_CONNECT_IN_PROGRESS) {
AndersonIctus 17:579f6353c6fb 131 printf("Connection - In Progress ...\r\n");
AndersonIctus 17:579f6353c6fb 132 } else {
AndersonIctus 17:579f6353c6fb 133 printf("Connection error, code = %d \r\n", retcode);
AndersonIctus 14:8ec6bdff6f67 134 return -1;
AndersonIctus 21:1a2ecd140da8 135 }
AndersonIctus 7:cabeef41022a 136
AndersonIctus 17:579f6353c6fb 137 // make your event queue dispatching events forever
AndersonIctus 17:579f6353c6fb 138 ev_queue.dispatch_forever();
AndersonIctus 7:cabeef41022a 139 return 1;
AndersonIctus 0:5d86284beacd 140 }
AndersonIctus 16:2b3715dd0f13 141
AndersonIctus 16:2b3715dd0f13 142 /**
AndersonIctus 16:2b3715dd0f13 143 * Receive a message from the Network Server
AndersonIctus 16:2b3715dd0f13 144 */
AndersonIctus 16:2b3715dd0f13 145 static void receive_message()
AndersonIctus 16:2b3715dd0f13 146 {
AndersonIctus 16:2b3715dd0f13 147 printf("receive_message()\n");
AndersonIctus 16:2b3715dd0f13 148
AndersonIctus 26:8c55aac7887f 149 LoraData* data = (LoraData*) comm->getData();
AndersonIctus 26:8c55aac7887f 150 data->read_write_flags = MSG_CONFIRMED_FLAG|MSG_UNCONFIRMED_FLAG;
AndersonIctus 26:8c55aac7887f 151
AndersonIctus 20:b25436619b59 152 uint8_t rx_buffer[50] = { 0 };
AndersonIctus 20:b25436619b59 153 int16_t retcode = comm->read( rx_buffer, sizeof(rx_buffer) );
AndersonIctus 20:b25436619b59 154 if (retcode < 0) {
AndersonIctus 20:b25436619b59 155 printf("receive() - Error code %d \r\n", retcode);
AndersonIctus 20:b25436619b59 156 return;
AndersonIctus 20:b25436619b59 157 }
AndersonIctus 16:2b3715dd0f13 158
AndersonIctus 20:b25436619b59 159 printf("RX Data (%d bytes): ", retcode);
AndersonIctus 20:b25436619b59 160 for (uint8_t i = 0; i < retcode; i++) {
AndersonIctus 20:b25436619b59 161 printf("%02x ", rx_buffer[i]);
AndersonIctus 20:b25436619b59 162 }
AndersonIctus 16:2b3715dd0f13 163
AndersonIctus 16:2b3715dd0f13 164 printf("\r\n");
AndersonIctus 16:2b3715dd0f13 165 }
AndersonIctus 16:2b3715dd0f13 166
AndersonIctus 16:2b3715dd0f13 167 /**
AndersonIctus 16:2b3715dd0f13 168 * Sends a message to the Network Server
AndersonIctus 16:2b3715dd0f13 169 */
AndersonIctus 16:2b3715dd0f13 170 static void send_message() {
AndersonIctus 20:b25436619b59 171 printf("send_message()\n");
AndersonIctus 16:2b3715dd0f13 172
AndersonIctus 16:2b3715dd0f13 173 // YOUR CODE HERE
AndersonIctus 26:8c55aac7887f 174 int16_t temperature = 10;
AndersonIctus 26:8c55aac7887f 175 printf("temperature = (%d)\n", temperature);
AndersonIctus 20:b25436619b59 176
AndersonIctus 26:8c55aac7887f 177 CayenneLPP payload(50);
AndersonIctus 26:8c55aac7887f 178 payload.addTemperature(1, temperature);
AndersonIctus 20:b25436619b59 179
AndersonIctus 26:8c55aac7887f 180 LoraData* data = (LoraData*) comm->getData();
AndersonIctus 26:8c55aac7887f 181 data->read_write_flags = MSG_UNCONFIRMED_FLAG;
AndersonIctus 16:2b3715dd0f13 182
AndersonIctus 26:8c55aac7887f 183 int16_t retcode = comm->write(payload.getBuffer(), payload.getSize());
AndersonIctus 26:8c55aac7887f 184 printf("lorawan.send = retcode [%d]\n",retcode);
AndersonIctus 26:8c55aac7887f 185
AndersonIctus 26:8c55aac7887f 186 if (retcode < 0) {
AndersonIctus 26:8c55aac7887f 187 retcode == LORAWAN_STATUS_WOULD_BLOCK
AndersonIctus 26:8c55aac7887f 188 ? printf("send - Duty cycle violation\r\n")
AndersonIctus 26:8c55aac7887f 189 : printf("send() - Error code %d \r\n", retcode);
AndersonIctus 16:2b3715dd0f13 190
AndersonIctus 26:8c55aac7887f 191 if (retcode == LORAWAN_STATUS_NO_ACTIVE_SESSIONS)
AndersonIctus 26:8c55aac7887f 192 printf("\r\n|-1017 - LORAWAN_STATUS_NO_ACTIVE_SESSIONS");
AndersonIctus 16:2b3715dd0f13 193
AndersonIctus 26:8c55aac7887f 194 if (retcode == LORAWAN_STATUS_WOULD_BLOCK) { //retry in 3 seconds
AndersonIctus 26:8c55aac7887f 195 ev_queue.call_in(3000, send_message);
AndersonIctus 26:8c55aac7887f 196 } else {
AndersonIctus 26:8c55aac7887f 197 ev_queue.call_in(TX_INTERVAL, send_message);
AndersonIctus 26:8c55aac7887f 198 }
AndersonIctus 20:b25436619b59 199
AndersonIctus 26:8c55aac7887f 200 return;
AndersonIctus 26:8c55aac7887f 201 }
AndersonIctus 16:2b3715dd0f13 202
AndersonIctus 26:8c55aac7887f 203 ev_queue.call_in(TX_INTERVAL, send_message);
AndersonIctus 16:2b3715dd0f13 204
AndersonIctus 26:8c55aac7887f 205 receive_message();
AndersonIctus 26:8c55aac7887f 206 printf("%d bytes scheduled for transmission \r\n", retcode);
AndersonIctus 16:2b3715dd0f13 207 }
AndersonIctus 16:2b3715dd0f13 208
AndersonIctus 16:2b3715dd0f13 209 /**
AndersonIctus 16:2b3715dd0f13 210 * Event handler
AndersonIctus 16:2b3715dd0f13 211 */
AndersonIctus 20:b25436619b59 212 static void lora_event_handler(lorawan_event_t event) {
AndersonIctus 20:b25436619b59 213 switch (event) {
AndersonIctus 20:b25436619b59 214 case CONNECTED:
AndersonIctus 20:b25436619b59 215 printf("# Connection - Successful \r\n");
AndersonIctus 20:b25436619b59 216 if (MBED_CONF_LORA_DUTY_CYCLE_ON) {
AndersonIctus 20:b25436619b59 217 send_message();
AndersonIctus 20:b25436619b59 218 } else {
AndersonIctus 20:b25436619b59 219 ev_queue.call_in(TX_INTERVAL, send_message);
AndersonIctus 20:b25436619b59 220 }
AndersonIctus 20:b25436619b59 221 break;
AndersonIctus 16:2b3715dd0f13 222
AndersonIctus 20:b25436619b59 223 case DISCONNECTED:
AndersonIctus 20:b25436619b59 224 ev_queue.break_dispatch();
AndersonIctus 20:b25436619b59 225 printf("# Disconnected Successfully \r\n");
AndersonIctus 20:b25436619b59 226 break;
AndersonIctus 20:b25436619b59 227 case RX_DONE:
AndersonIctus 20:b25436619b59 228 printf("# Received message from Network Server \r\n");
AndersonIctus 20:b25436619b59 229 receive_message();
AndersonIctus 20:b25436619b59 230 break;
AndersonIctus 20:b25436619b59 231 case RX_TIMEOUT:
AndersonIctus 20:b25436619b59 232 case RX_ERROR:
AndersonIctus 20:b25436619b59 233 printf("# Error in reception - Code = %d \r\n", event);
AndersonIctus 20:b25436619b59 234 break;
AndersonIctus 20:b25436619b59 235 case TX_DONE:
AndersonIctus 20:b25436619b59 236 count_message++;
AndersonIctus 20:b25436619b59 237 printf("# Message Sent to Network Server - Count [%d] \r\n", count_message);
AndersonIctus 20:b25436619b59 238 break;
AndersonIctus 20:b25436619b59 239 case TX_TIMEOUT:
AndersonIctus 20:b25436619b59 240 case TX_ERROR:
AndersonIctus 20:b25436619b59 241 case TX_CRYPTO_ERROR:
AndersonIctus 20:b25436619b59 242 case TX_SCHEDULING_ERROR:
AndersonIctus 20:b25436619b59 243 printf("# Transmission Error - EventCode = %d \r\n", event);
AndersonIctus 20:b25436619b59 244 break;
AndersonIctus 20:b25436619b59 245 case JOIN_FAILURE:
AndersonIctus 20:b25436619b59 246 printf("# OTAA Failed - Check Keys \r\n");
AndersonIctus 20:b25436619b59 247 break;
AndersonIctus 20:b25436619b59 248 case UPLINK_REQUIRED:
AndersonIctus 20:b25436619b59 249 printf("# Uplink required by NS \r\n");
AndersonIctus 20:b25436619b59 250 send_message();
AndersonIctus 20:b25436619b59 251 break;
AndersonIctus 20:b25436619b59 252 default:
AndersonIctus 20:b25436619b59 253 printf("# Unknown Event \r\n");
AndersonIctus 20:b25436619b59 254 // MBED_ASSERT("# Unknown Event");
AndersonIctus 20:b25436619b59 255 }
AndersonIctus 16:2b3715dd0f13 256 }