add senet packet format

Dependencies:   Senet_Packet mDot_X_NUCLEO_IKS01A1 libmDot-dev-mbed5-deprecated

Fork of mDot-IKS01A1 by Peter Ferland

main.cpp

Committer:
pferland
Date:
2016-12-15
Revision:
7:d2052933ebcb
Parent:
6:9e6ac13de3e9
Child:
8:bfbc3dd47166

File content as of revision 7:d2052933ebcb:

#include "mbed.h"
#include "mDot.h"
#include "x_nucleo_iks01a1.h"
#include "dot_util.h"
#include "RadioEvent.h"

// mDot UDK board demo with X-NUCLEO-IKS01A1 sensor card
// For more examples see the Dot-Examples project:
// https://developer.mbed.org/teams/MultiTech/code/Dot-Examples/

// This triggers an I2C issue in mbed-os 5.1.5
// Use any other revision to compile. (Tested with libmDot-dev/mbed-os 5.2.2

//Replace with settings on your Conduit
static std::string network_name = "TestTest";
static std::string network_passphrase = "TestTest"; 
// Network Id for Senet public network
static uint8_t network_id[] = {0x00,0x25,0x0C,0x00,0x00,0x01,0x00,0x01};
// Register at or Sign in to http://portal.senetco.com/ and register your NodeId to receive your AppId
static uint8_t network_key[] = {0x9F,0x6B,0xD3,0xB2,0xD9,0x3A,0x3B,0x4D,0x7B,0x35,0x62,0xF2,0xB9,0x58,0x05,0x6C};
// 1 For Senet, configurable on your Conduit
static uint8_t frequency_sub_band = 1;
// True for Senet, false for your Conduit.
static bool public_network = true;
static uint8_t ack = 0;

// deepsleep consumes slightly less current than sleep
// in sleep mode, IO state is maintained, RAM is retained, and application will resume after waking up
// in deepsleep mode, IOs float, RAM is lost, and application will start from beginning after waking up
// if deep_sleep == true, device will enter deepsleep mode
static bool deep_sleep = false;

mDot *dot = NULL;

int main()
{
    Serial pc(USBTX, USBRX);
    
    /* Instantiate the expansion board */
    X_NUCLEO_IKS01A1 *mems_expansion_board = X_NUCLEO_IKS01A1::Instance(I2C_SDA, I2C_SCL, PC_1);
    
    /* Retrieve the composing elements of the expansion board */
    GyroSensor *gyroscope = mems_expansion_board->GetGyroscope();
    MotionSensor *accelerometer = mems_expansion_board->GetAccelerometer();
    MagneticSensor *magnetometer = mems_expansion_board->magnetometer;
    HumiditySensor *humidity_sensor = mems_expansion_board->ht_sensor;
    PressureSensor *pressure_sensor = mems_expansion_board->pt_sensor;
    TempSensor *temp_sensor1 = mems_expansion_board->ht_sensor;
    TempSensor *temp_sensor2 = mems_expansion_board->pt_sensor;
    // Custom event handler for automatically displaying RX data
    RadioEvent events;
    pc.baud(115200);

    /* Initialize mDot */
    dot = mDot::getInstance();
    dot->setAdr(true);
    mts::MTSLog::setLogLevel(mts::MTSLog::INFO_LEVEL);
    dot->setEvents(&events);
    

    if (!dot->getStandbyFlag()) {
        logInfo("mbed-os library version: %d", MBED_LIBRARY_VERSION);
        // start from a well-known state
        logInfo("defaulting Dot configuration");
        dot->resetConfig();
        dot->resetNetworkSession();
        
        // update configuration if necessary
        // in AUTO_OTA mode the session is automatically saved, so saveNetworkSession and restoreNetworkSession are not needed
        if (dot->getJoinMode() != mDot::AUTO_OTA) {
            logInfo("changing network join mode to AUTO_OTA");
            if (dot->setJoinMode(mDot::AUTO_OTA) != mDot::MDOT_OK) {
                logError("failed to set network join mode to AUTO_OTA");
            }
        }
        // in OTA and AUTO_OTA join modes, the credentials can be passed to the library as a name and passphrase or an ID and KEY
        // only one method or the other should be used!
        // network ID = crc64(network name)
        // network KEY = cmac(network passphrase)
        update_ota_config_id_key(network_id, network_key, frequency_sub_band, public_network, ack);
//        update_ota_config_name_phrase(network_name, network_passphrase, frequency_sub_band, public_network, ack);
        
        // configure network link checks
        // network link checks are a good alternative to requiring the gateway to ACK every packet and should allow a single gateway to handle more Dots
        // check the link every count packets
        // declare the Dot disconnected after threshold failed link checks
        // for count = 3 and threshold = 5, the Dot will be considered disconnected after 15 missed packets in a row
        update_network_link_check_config(3, 5);
        
        // save changes to configuration
        logInfo("saving configuration");
        if (!dot->saveConfig()) {
            logError("failed to save configuration");
        }
    
        // display configuration
        display_config();
    }  else {
        // restore the saved session if the dot woke from deepsleep mode
        // useful to use with deepsleep because session info is otherwise lost when the dot enters deepsleep
        logInfo("restoring network session from NVM");
        dot->restoreNetworkSession();
    }


    
    while (true) {
        std::vector<uint8_t> tx_data;

        // join network if not joined
        if (!dot->getNetworkJoinStatus()) {
            join_network();
        }

        //temp floats
        float value1, value2;
        
        // HTS221 Humidity sensor
        temp_sensor1->GetTemperature(&value1);
        humidity_sensor->GetHumidity(&value2);
        
        //serialize data and append to packet
        tx_data.push_back(uint8_t(0xFF & *((uint32_t*)(&value1))));
        tx_data.push_back(uint8_t((0xFF << 2 ) & *((uint32_t*)(&value1))));
        tx_data.push_back(uint8_t((0xFF << 4 ) & *((uint32_t*)(&value1))));
        tx_data.push_back(uint8_t((0xFF << 6 ) & *((uint32_t*)(&value1))));
        logInfo("Temperature data %f", value1);
        
        // Get accelerometer data, but we're only interested in the Z.
        int32_t accel_vector[3];
        accelerometer->Get_X_Axes(accel_vector);
        logInfo("Acclerometer Z axis: %d", accel_vector[2]);
        
        tx_data.push_back(uint8_t(0xFF & accel_vector[2]));
        tx_data.push_back(uint8_t((0xFF << 2 ) & accel_vector[2]));
        tx_data.push_back(uint8_t((0xFF << 4 ) & accel_vector[2]));
        tx_data.push_back(uint8_t((0xFF << 6 ) & accel_vector[2]));

        send_data(tx_data);
        
        // if going into deepsleep mode, save the session so we don't need to join again after waking up
        // not necessary if going into sleep mode since RAM is retained
        logInfo("saving network session to NVM");
        dot->saveNetworkSession();
        

        // ONLY ONE of the three functions below should be uncommented depending on the desired wakeup method
        sleep_wake_rtc_only(deep_sleep);
        //sleep_wake_interrupt_only(deep_sleep);
        //sleep_wake_rtc_or_interrupt(deep_sleep);
        
    }

    return 0;    
}