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 17:05:34 2019 +0000
Revision:
34:fea430612423
Parent:
31:8b734e4fb89b
- Exemplo de Monitoramento de ATIVOS !!

Who changed what in which revision?

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