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

Committer:
AndersonIctus
Date:
2019-07-07
Revision:
17:579f6353c6fb
Parent:
16:2b3715dd0f13
Child:
20:b25436619b59
Child:
22:b69940ec3452

File content as of revision 17:579f6353c6fb:

#include "mbed.h"
#include "sgam_mdw.h"

#include "ControlImpl.h"
#include "Gyroscope.h"
#include "LoRAWanComm.h"
#include "LoRaWANInterface.h"
#include "lora_radio_helper.h"

ControlImpl ctrl;

#define D_LOG(args...)   printf(args) 

// LORA WAN CONFIGS 
#define TX_INTERVAL             10000
#define MBED_CONF_LORA_APP_PORT 15 //15

static uint8_t LORAWAN_DEV_EUI[] = { 0x00, 0x1C, 0x73, 0x4A, 0x55, 0x89, 0xAE, 0xC6 };
static uint8_t LORAWAN_APP_EUI[] = { 0x70, 0xB3, 0xD5, 0x7E, 0xD0, 0x01, 0xD0, 0x2F };
static uint8_t LORAWAN_APP_KEY[] = { 0x75, 0xC6, 0xF2, 0xFB, 0xEE, 0xA9, 0x82, 0x6C, 0xA0, 0xBD, 0xB9, 0x0F, 0xC9, 0xEC, 0xF7, 0x10 };

// static uint32_t DEVADDR_1 = 0x2601177B;
// static uint8_t NWKSKEY_1[] = { 0x2A, 0xE9, 0x3F, 0x5E, 0x4C, 0x25, 0xDC, 0x34, 0x16, 0x82, 0x1B, 0xD1, 0x1A, 0x7F, 0xD0, 0xF6 };
// static uint8_t APPSKEY_1[] = { 0x78, 0x59, 0x61, 0xBB, 0x6B, 0xB7, 0xDE, 0x57, 0x80, 0x74, 0xAF, 0xED, 0x10, 0xD7, 0x47, 0x18 };

static void lora_event_handler(lorawan_event_t event);
static int16_t count_message;

EventQueue ev_queue;
static lorawan_app_callbacks_t callbacks;

int main() {
    D_LOG("=============== INICIO ===============\r\n");

    Control* control = ctrl.getControler();
    control->initialize();

    Communication<LoraData>* comm = (Communication<LoraData>*)control->getCommunication("LoRAWAN");

    ////////////////////////////////////////////////////////////////////////////////////////////
    // 1 - Configura os callbacks do loran WAN e inicializa os dados !!
    // Dados de Conexão
    lorawan_connect_t connect_params;
    connect_params.connect_type = LORAWAN_CONNECTION_OTAA;

    connect_params.connection_u.otaa.dev_eui = LORAWAN_DEV_EUI;
    connect_params.connection_u.otaa.app_eui = LORAWAN_APP_EUI;
    connect_params.connection_u.otaa.app_key = LORAWAN_APP_KEY;
    connect_params.connection_u.otaa.nb_trials = 10;

    LoraData* data = new LoraData(&connect_params, (LoRaRadio*)&radio, &ev_queue );
    callbacks.events = mbed::callback(lora_event_handler);
    data->prepareCallBack(callbacks);

    /////////////////////////////////////////////////////////////////////////////////////////////
    // 2 - INICIALIZA A CONEXAO
    if( comm->initialize(data) != TRUE) {
        D_LOG("Inicialização falhou !!\r\n");
        return -1;
    }

    ////////////////////////////////////////////////////////////////////////////////////////////
    // 3 - Tentar conectar 
    lorawan_status_t retcode = (lorawan_status_t) comm->connect();
    if (retcode == LORAWAN_STATUS_OK || retcode == LORAWAN_STATUS_CONNECT_IN_PROGRESS) {
        printf("Connection - In Progress ...\r\n");
    } else {
        printf("Connection error, code = %d \r\n", retcode);
        return -1;
    }

    // make your event queue dispatching events forever
    ev_queue.dispatch_forever();

    control->finalize();
    D_LOG("=============== FINAL ===============\r\n");
    return 1;
}

/**
 * Receive a message from the Network Server
 */
static void receive_message()
{
    printf("receive_message()\n");
    
    // uint8_t rx_buffer[50] = { 0 };
    // int16_t retcode = lorawan.receive(MBED_CONF_LORA_APP_PORT, rx_buffer,
    //                                   sizeof(rx_buffer),
    //                                   MSG_CONFIRMED_FLAG|MSG_UNCONFIRMED_FLAG);

    // if (retcode < 0) {
    //     printf("receive() - Error code %d \r\n", retcode);
    //     return;
    // }

    // printf("RX Data (%d bytes): ", retcode);
    // for (uint8_t i = 0; i < retcode; i++) {
    //     printf("%02x ", rx_buffer[i]);
    // }
    printf("\r\n");
}

/**
 * Sends a message to the Network Server
 */
static void send_message() {
    //printf("send_message()\n");
        
    // YOUR CODE HERE
    // int16_t temperature = 10;
    // //printf("temperature = (%d)\n", temperature);
    // CayenneLPP payload(50);
    // payload.addTemperature(1, temperature);
    // int16_t retcode = lorawan.send(MBED_CONF_LORA_APP_PORT, payload.getBuffer(), payload.getSize(), MSG_UNCONFIRMED_FLAG);
    // //printf("lorawan.send = retcode [%d]\n",retcode);

    // if (retcode < 0) {
    //     retcode == LORAWAN_STATUS_WOULD_BLOCK ? printf("send - Duty cycle violation\r\n")
    //             : printf("send() - Error code %d \r\n", retcode);
                
    //     if (retcode == LORAWAN_STATUS_NO_ACTIVE_SESSIONS)
    //         printf("\r\n|-1017 - LORAWAN_STATUS_NO_ACTIVE_SESSIONS"); 

    //     if (retcode == LORAWAN_STATUS_WOULD_BLOCK) {
    //         //retry in 3 seconds
    //         ev_queue.call_in(3000, send_message);
    //     }
    //     else {
    //         ev_queue.call_in(TX_INTERVAL, send_message);
    //     }
    //     return;
    // }

    // ev_queue.call_in(TX_INTERVAL, send_message);

    //receive_message();
    //printf("%d bytes scheduled for transmission \r\n", retcode);
}

/**
 * Event handler
 */
static void lora_event_handler(lorawan_event_t event)
{
    // switch (event) {
    //     case CONNECTED:
    //         printf("# Connection - Successful \r\n");            
    //         if (MBED_CONF_LORA_DUTY_CYCLE_ON) {
    //             send_message();
    //         } else {
    //             // ev_queue.call_in(TX_INTERVAL, send_message);
    //         }
    //         break;

    //     case DISCONNECTED:
    //         // ev_queue.break_dispatch();
    //         printf("# Disconnected Successfully \r\n");
    //         break;
    //     case RX_DONE:
    //         printf("# Received message from Network Server \r\n");
    //         receive_message();
    //         break;
    //     case RX_TIMEOUT:
    //     case RX_ERROR:
    //         printf("# Error in reception - Code = %d \r\n", event);
    //         break;
    //     case TX_DONE:    
    //         count_message++;        
    //         printf("# Message Sent to Network Server - Count [%d] \r\n", count_message);
    //         break;
    //     case TX_TIMEOUT:
    //     case TX_ERROR:
    //     case TX_CRYPTO_ERROR:
    //     case TX_SCHEDULING_ERROR:
    //         printf("# Transmission Error - EventCode = %d \r\n", event);
    //         break;        
    //     case JOIN_FAILURE:
    //         printf("# OTAA Failed - Check Keys \r\n");
    //         break;
    //     case UPLINK_REQUIRED:
    //         printf("# Uplink required by NS \r\n");
    //         send_message();
    //         break;
    //     default:
    //         printf("# Unknown Event \r\n");
    //         MBED_ASSERT("# Unknown Event");
    // }
}