Example of using the mDot UDK with the X-NUCLEO-IKS01A2 board

Dependencies:   MTS_X_NUCLEO_IKS01A2 libmDot-mbed5

Fork of mDot-IKS01A1 by Peter Ferland

Revision:
4:142c85980a6f
Parent:
3:d34798ffcaf8
Child:
6:9e6ac13de3e9
--- a/main.cpp	Fri Dec 09 22:06:09 2016 +0000
+++ b/main.cpp	Fri Dec 09 23:31:44 2016 +0000
@@ -4,25 +4,56 @@
 #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";
+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;
-static bool public_network = false;
+// 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;
-Serial pc(USBTX, USBRX);
 
 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 */
-
-    mts::MTSLog::setLogLevel(mts::MTSLog::TRACE_LEVEL);
     dot = mDot::getInstance();
+    mts::MTSLog::setLogLevel(mts::MTSLog::INFO_LEVEL);
     dot->setEvents(&events);
 
 
@@ -48,7 +79,8 @@
         // 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);
+        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
@@ -71,17 +103,7 @@
         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) {
@@ -94,15 +116,28 @@
         // 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);
+        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
@@ -114,7 +149,7 @@
         // 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);
+        sleep_wake_rtc_or_interrupt(deep_sleep);
         
     }