Fork from Sog Yang

Dependencies:   BNO055_AS7000 NNN50_WIFI_API

Fork of NNN50_BLEWIFISensor by Sog Yang

main.cpp

Committer:
sog_yang
Date:
2017-05-04
Revision:
8:ff7fdd152f14
Parent:
7:06495d54d107
Child:
9:d1861b00e0fd

File content as of revision 8:ff7fdd152f14:

#include "mbed.h"
#include "BNO055.h"
#include "AS7000.h"
#include "ble/BLE.h"
#include "ble/Gap.h"
#include "ble/services/BatteryService.h"
#include "DOORService.h"
#include "EthernetInterface.h"
#include "WIFIDevice.h"

#define DEBUG_LOG 0

union IP {
    unsigned int ip;
    struct {
      unsigned char d;
      unsigned char c;
      unsigned char b;
      unsigned char a;
    } ip2;
};

char  ips[20];
IP ip;

DOORService *doorServicePtr;

BNO055 imu(p0,p30);
AS7000 hrm(p0,p30);
Serial pc(USBTX, USBRX);
EthernetInterface eth;
WIFIDevice wifi;

TCPSocketConnection sock_tcp;
char* ECHO_SERVER_ADDRESS = "10.0.1.13";
int ECHO_SERVER_PORT = 1030;

uint8_t initialValueForDOORCharacteristic = 0xFF;
uint8_t BLE_RX_CMD = 0xFF;
const char DEVICE_NAME[] = "FITNCTL";
uint8_t ADV_manuf[] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
char     out_buffer[45];
static EventQueue eventQueue(
    /* event count */ 16 * /* event size */ 32
);

 bool isConnect = false;
 bool isWiFiEnable = false;
 bool isCloudFiling = false;


void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params)
{
    BLE::Instance().gap().startAdvertising();
}

void onDataWrittenCallback(const GattWriteCallbackParams *params) {
    
    
    if ((params->handle == doorServicePtr->getValueHandle()) && (params->len == 1)) {
        {
            BLE_RX_CMD = *(params->data);                   
        }
    }
}

/**
 * This function is called when the ble initialization process has failled
 */
void onBleInitError(BLE &ble, ble_error_t error)
{
    /* Initialization error handling should go here */
}

/**
 * Callback triggered when the ble initialization process has finished
 */
void bleInitComplete(BLE::InitializationCompleteCallbackContext *params)
{
    BLE&        ble   = params->ble;
    ble_error_t error = params->error;

    if (error != BLE_ERROR_NONE) {
        /* In case of error, forward the error handling to onBleInitError */
        onBleInitError(ble, error);
        return;
    }

    /* Ensure that it is the default instance of BLE */
    if(ble.getInstanceID() != BLE::DEFAULT_INSTANCE) {
        return;
    }

    ble.gap().onDisconnection(disconnectionCallback);
    ble.gattServer().onDataWritten(onDataWrittenCallback);
    
    /* Setup primary services */  
    doorServicePtr = new DOORService(ble, initialValueForDOORCharacteristic);

    /* Setup advertising */
    ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME));
    ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::MANUFACTURER_SPECIFIC_DATA, ADV_manuf, sizeof(ADV_manuf));
    ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
    ble.gap().setAdvertisingInterval(150); /* 1000ms */
    ble.gap().startAdvertising();
}

void scheduleBleEventsProcessing(BLE::OnEventsToProcessCallbackContext* context) {
    BLE &ble = BLE::Instance();
    eventQueue.call(Callback<void()>(&ble, &BLE::processEvents));
}


int main (void) {
        
    hrm.enable(); 
    imu.reset();    
    imu.setmode(OPERATION_MODE_NDOF);
    
    
    BLE &ble = BLE::Instance();
    ble.onEventsToProcess(scheduleBleEventsProcessing);
    ble.init(bleInitComplete);
    
    while (true) {
        imu.get_angles(); //query the i2c device
        hrm.hr_only();
        pc.printf("*HR=%03d#", hrm.hrm.hreat_rate);  
        /* GATT Command 0xA1 Cloud Data Transfer*/
        if (BLE_RX_CMD == 0xA1){
            ADV_manuf[5] = 0xF1; 
            if (isWiFiEnable == true){                 
                if (isConnect == true) {
                    sprintf(out_buffer,"hrm:%03d yaw:%6.2f pitch:%6.2f roll:%6.2f\n", hrm.hrm.hreat_rate, imu.euler.yaw, imu.euler.pitch, imu.euler.roll);
                    sock_tcp.send_all(out_buffer, sizeof(out_buffer) - 1);  
                     isCloudFiling = true;
                } else {
                    if (sock_tcp.connect(ECHO_SERVER_ADDRESS, ECHO_SERVER_PORT) < 0) {
                        isConnect = false;
                        BLE_RX_CMD = 0xFF;
#if DEBUG_LOG                        
                        pc.printf("Unable to connect to (%s) on port (%d)\n", ECHO_SERVER_ADDRESS, ECHO_SERVER_PORT);
#endif                        
                        ADV_manuf[4] = 0xCF;
                        BLE_RX_CMD = 0xFF;
                    } else {
                         isConnect = true; 
                          ADV_manuf[4] = 0xF1;
#if DEBUG_LOG                           
                         pc.printf("Connected to Server at %s\n",ECHO_SERVER_ADDRESS);
#endif                         
                    }
                }
            }
        }
        /* GATT Command 0x02 WiFI & Cloud Connection Close*/
         if (BLE_RX_CMD == 0x02){
            ADV_manuf[5] = 0xF2;  
            if ( isConnect == true ) {
                sock_tcp.close(); 
                isConnect = false;                                
            }
            if ( isWiFiEnable == true ) {
                eth.disconnect();                              
                wifi.sleep();
                isWiFiEnable = false;
            } 
            if (isConnect == true) isConnect = false;   
            isConnect = false;
            isWiFiEnable = false;
            isCloudFiling = false;        
            
            BLE_RX_CMD = 0xFF;    
            ADV_manuf[0] = 0x00;
            ADV_manuf[1] = 0x00;
            ADV_manuf[2] = 0x00;
            ADV_manuf[3] = 0x00;
            ADV_manuf[4] = 0x00;         
        }
        /* GATT Command 0x0 init WiFI & Cloud Connection*/ 
       if (BLE_RX_CMD == 0x00){
        if(isCloudFiling == false) {
            BLE_RX_CMD = 0xFF;
            ADV_manuf[5] = 0xF0; 
            
            if (isWiFiEnable == false) {
                eth.init();  
                wifi.setNetwork(M2M_WIFI_SEC_WPA_PSK, "SOG", "0227322327"); 
                eth.connect(); 
                while(true) {
                    if (wifi.is_AP_connected()==1) break;
                }
#if DEBUG_LOG               
                pc.printf("Connect Success! \n");
                pc.printf("MAC: %s\n", eth.getMACAddress());            
                pc.printf("IP: %s\n", eth.getIPAddress());
                pc.printf("Gateway: %s\n", eth.getGateway());
                pc.printf("NetworkMask: %s\n", eth.getNetworkMask()); 
#endif            
                snprintf(ips, sizeof(ips), "%s",eth.getIPAddress());    
                unsigned short a, b, c, d;
                sscanf(ips, "%hu.%hu.%hu.%hu", &a, &b, &c, &d);    
                sprintf(ips, "%x.%x.%x.%x", a, b, c, d);
                ADV_manuf[0] = a;
                ADV_manuf[1] = b;
                ADV_manuf[2] = c;
                ADV_manuf[3] = d; 
                isWiFiEnable = true; 
            } 
            
            if (isConnect == false) {
                if (isWiFiEnable == true) {
                    if (sock_tcp.connect(ECHO_SERVER_ADDRESS, ECHO_SERVER_PORT) < 0) {
#if DEBUG_LOG                         
                            pc.printf("Unable to connect to (%s) on port (%d)\n", ECHO_SERVER_ADDRESS, ECHO_SERVER_PORT);
#endif                         
                            sock_tcp.close();
                            ADV_manuf[4] = 0xCF;
                        } else {
                             isConnect = true;
                             isCloudFiling = false;
                             ADV_manuf[4] = 0xF1;
#if DEBUG_LOG                          
                             pc.printf("Connected to Server at %s\n",ECHO_SERVER_ADDRESS);
#endif     
                        }
                    }  
                }
            }    
       }
       BLE::Instance(BLE::DEFAULT_INSTANCE).gap().updateAdvertisingPayload(GapAdvertisingData::MANUFACTURER_SPECIFIC_DATA, ADV_manuf, sizeof(ADV_manuf));
       ble.waitForEvent();    
    }
}