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:
Mon Jul 15 21:41:33 2019 -0300
Revision:
29:8251bb11d117
Parent:
28:18a54c786ba9
Child:
31:8b734e4fb89b
- revisao dos sensores

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