mDot project for Multitech and Explora worhshop

Dependencies:   mDot_X_NUCLEO_IKS01A1 libmDot-dev-mbed5-deprecated

Fork of mDot-IKS01A1 by Peter Ferland

main.cpp

Committer:
pferland
Date:
2016-12-09
Revision:
3:d34798ffcaf8
Parent:
2:d0873bb1255f
Child:
4:142c85980a6f

File content as of revision 3:d34798ffcaf8:

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

static std::string network_name = "TestTest";
static std::string network_passphrase = "TestTest";
static uint8_t frequency_sub_band = 1;
static bool public_network = false;
static uint8_t ack = 0;

mDot *dot = NULL;
Serial pc(USBTX, USBRX);

int main()
{
    // Custom event handler for automatically displaying RX data
    RadioEvent events;
    pc.baud(115200);

    /* Initialize mDot */

    mts::MTSLog::setLogLevel(mts::MTSLog::TRACE_LEVEL);
    dot = mDot::getInstance();
    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_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();
    }
            /* 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;

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

        // join network if not joined
        if (!dot->getNetworkJoinStatus()) {
            join_network();
        }
        // Retrieve sensor data and prepare the packet.
        //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 %d", value1);
        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(false);
        
    }

    return 0;    
}