Example of using the mDot UDK with the X-NUCLEO-IKS01A1 shield

Dependencies:   mDot_X_NUCLEO_IKS01A1 libmDot-dev-mbed5-deprecated

Hardware

Hardware List

Setup

  • Install the mDot on the developer board.
  • Install the IKS01A1 on the developer board.
  • If using a MTUDK-ST-CELL (white board) plug in the AC power adapter
  • Connect the microusb power to your development PC
    • if using a MTUDK-ST-CELL there are 2 microusb ports. Use the one closest to the serial port.

Your developer board should look like the following:

/media/uploads/pferland/udk_iks01a1.jpg

Software

This example program uses LoRa utility functions from Dot-Examples and the IKS01A1 library from ST Micro.

LoRa Configuration

Senet

By default this program is configured to connect to the Senet network. To connect to Senet you will need to register your mDot's Node ID with the Senet developer portal and change the network_key array in main.cpp.

Others

To connect to a different LoRa gateway change the arrays network_id and network_key. If you are using passphrases, edit the strings network_name and network_key, uncomment the function "update_ota_config_name_phrase" and comment out the function "update_ota_config_id_key".

Committer:
pferland
Date:
Thu Dec 15 23:50:05 2016 +0000
Revision:
7:d2052933ebcb
Parent:
6:9e6ac13de3e9
Child:
8:bfbc3dd47166
Changed sleep mode from RTC+interrupt to RTC only

Who changed what in which revision?

UserRevisionLine numberNew contents of line
pferland 0:9e88a9018fc0 1 #include "mbed.h"
pferland 0:9e88a9018fc0 2 #include "mDot.h"
pferland 0:9e88a9018fc0 3 #include "x_nucleo_iks01a1.h"
pferland 0:9e88a9018fc0 4 #include "dot_util.h"
pferland 0:9e88a9018fc0 5 #include "RadioEvent.h"
pferland 0:9e88a9018fc0 6
pferland 4:142c85980a6f 7 // mDot UDK board demo with X-NUCLEO-IKS01A1 sensor card
pferland 4:142c85980a6f 8 // For more examples see the Dot-Examples project:
pferland 4:142c85980a6f 9 // https://developer.mbed.org/teams/MultiTech/code/Dot-Examples/
pferland 4:142c85980a6f 10
pferland 4:142c85980a6f 11 // This triggers an I2C issue in mbed-os 5.1.5
pferland 4:142c85980a6f 12 // Use any other revision to compile. (Tested with libmDot-dev/mbed-os 5.2.2
pferland 4:142c85980a6f 13
pferland 4:142c85980a6f 14 //Replace with settings on your Conduit
pferland 0:9e88a9018fc0 15 static std::string network_name = "TestTest";
pferland 4:142c85980a6f 16 static std::string network_passphrase = "TestTest";
pferland 4:142c85980a6f 17 // Network Id for Senet public network
pferland 4:142c85980a6f 18 static uint8_t network_id[] = {0x00,0x25,0x0C,0x00,0x00,0x01,0x00,0x01};
pferland 4:142c85980a6f 19 // Register at or Sign in to http://portal.senetco.com/ and register your NodeId to receive your AppId
pferland 4:142c85980a6f 20 static uint8_t network_key[] = {0x9F,0x6B,0xD3,0xB2,0xD9,0x3A,0x3B,0x4D,0x7B,0x35,0x62,0xF2,0xB9,0x58,0x05,0x6C};
pferland 4:142c85980a6f 21 // 1 For Senet, configurable on your Conduit
pferland 0:9e88a9018fc0 22 static uint8_t frequency_sub_band = 1;
pferland 4:142c85980a6f 23 // True for Senet, false for your Conduit.
pferland 4:142c85980a6f 24 static bool public_network = true;
pferland 0:9e88a9018fc0 25 static uint8_t ack = 0;
pferland 0:9e88a9018fc0 26
pferland 4:142c85980a6f 27 // deepsleep consumes slightly less current than sleep
pferland 4:142c85980a6f 28 // in sleep mode, IO state is maintained, RAM is retained, and application will resume after waking up
pferland 4:142c85980a6f 29 // in deepsleep mode, IOs float, RAM is lost, and application will start from beginning after waking up
pferland 4:142c85980a6f 30 // if deep_sleep == true, device will enter deepsleep mode
pferland 4:142c85980a6f 31 static bool deep_sleep = false;
pferland 4:142c85980a6f 32
pferland 0:9e88a9018fc0 33 mDot *dot = NULL;
pferland 0:9e88a9018fc0 34
pferland 0:9e88a9018fc0 35 int main()
pferland 0:9e88a9018fc0 36 {
pferland 4:142c85980a6f 37 Serial pc(USBTX, USBRX);
pferland 4:142c85980a6f 38
pferland 4:142c85980a6f 39 /* Instantiate the expansion board */
pferland 4:142c85980a6f 40 X_NUCLEO_IKS01A1 *mems_expansion_board = X_NUCLEO_IKS01A1::Instance(I2C_SDA, I2C_SCL, PC_1);
pferland 4:142c85980a6f 41
pferland 4:142c85980a6f 42 /* Retrieve the composing elements of the expansion board */
pferland 4:142c85980a6f 43 GyroSensor *gyroscope = mems_expansion_board->GetGyroscope();
pferland 4:142c85980a6f 44 MotionSensor *accelerometer = mems_expansion_board->GetAccelerometer();
pferland 4:142c85980a6f 45 MagneticSensor *magnetometer = mems_expansion_board->magnetometer;
pferland 4:142c85980a6f 46 HumiditySensor *humidity_sensor = mems_expansion_board->ht_sensor;
pferland 4:142c85980a6f 47 PressureSensor *pressure_sensor = mems_expansion_board->pt_sensor;
pferland 4:142c85980a6f 48 TempSensor *temp_sensor1 = mems_expansion_board->ht_sensor;
pferland 4:142c85980a6f 49 TempSensor *temp_sensor2 = mems_expansion_board->pt_sensor;
pferland 0:9e88a9018fc0 50 // Custom event handler for automatically displaying RX data
pferland 0:9e88a9018fc0 51 RadioEvent events;
pferland 0:9e88a9018fc0 52 pc.baud(115200);
pferland 0:9e88a9018fc0 53
pferland 0:9e88a9018fc0 54 /* Initialize mDot */
pferland 0:9e88a9018fc0 55 dot = mDot::getInstance();
pferland 7:d2052933ebcb 56 dot->setAdr(true);
pferland 4:142c85980a6f 57 mts::MTSLog::setLogLevel(mts::MTSLog::INFO_LEVEL);
pferland 0:9e88a9018fc0 58 dot->setEvents(&events);
pferland 0:9e88a9018fc0 59
pferland 0:9e88a9018fc0 60
pferland 0:9e88a9018fc0 61 if (!dot->getStandbyFlag()) {
pferland 0:9e88a9018fc0 62 logInfo("mbed-os library version: %d", MBED_LIBRARY_VERSION);
pferland 0:9e88a9018fc0 63 // start from a well-known state
pferland 0:9e88a9018fc0 64 logInfo("defaulting Dot configuration");
pferland 0:9e88a9018fc0 65 dot->resetConfig();
pferland 0:9e88a9018fc0 66 dot->resetNetworkSession();
pferland 0:9e88a9018fc0 67
pferland 0:9e88a9018fc0 68 // update configuration if necessary
pferland 0:9e88a9018fc0 69 // in AUTO_OTA mode the session is automatically saved, so saveNetworkSession and restoreNetworkSession are not needed
pferland 0:9e88a9018fc0 70 if (dot->getJoinMode() != mDot::AUTO_OTA) {
pferland 0:9e88a9018fc0 71 logInfo("changing network join mode to AUTO_OTA");
pferland 0:9e88a9018fc0 72 if (dot->setJoinMode(mDot::AUTO_OTA) != mDot::MDOT_OK) {
pferland 0:9e88a9018fc0 73 logError("failed to set network join mode to AUTO_OTA");
pferland 0:9e88a9018fc0 74 }
pferland 0:9e88a9018fc0 75 }
pferland 0:9e88a9018fc0 76 // 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
pferland 0:9e88a9018fc0 77 // only one method or the other should be used!
pferland 0:9e88a9018fc0 78 // network ID = crc64(network name)
pferland 0:9e88a9018fc0 79 // network KEY = cmac(network passphrase)
pferland 4:142c85980a6f 80 update_ota_config_id_key(network_id, network_key, frequency_sub_band, public_network, ack);
pferland 4:142c85980a6f 81 // update_ota_config_name_phrase(network_name, network_passphrase, frequency_sub_band, public_network, ack);
pferland 0:9e88a9018fc0 82
pferland 0:9e88a9018fc0 83 // configure network link checks
pferland 0:9e88a9018fc0 84 // 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
pferland 0:9e88a9018fc0 85 // check the link every count packets
pferland 0:9e88a9018fc0 86 // declare the Dot disconnected after threshold failed link checks
pferland 0:9e88a9018fc0 87 // for count = 3 and threshold = 5, the Dot will be considered disconnected after 15 missed packets in a row
pferland 0:9e88a9018fc0 88 update_network_link_check_config(3, 5);
pferland 0:9e88a9018fc0 89
pferland 0:9e88a9018fc0 90 // save changes to configuration
pferland 0:9e88a9018fc0 91 logInfo("saving configuration");
pferland 0:9e88a9018fc0 92 if (!dot->saveConfig()) {
pferland 0:9e88a9018fc0 93 logError("failed to save configuration");
pferland 0:9e88a9018fc0 94 }
pferland 0:9e88a9018fc0 95
pferland 0:9e88a9018fc0 96 // display configuration
pferland 0:9e88a9018fc0 97 display_config();
pferland 0:9e88a9018fc0 98 } else {
pferland 0:9e88a9018fc0 99 // restore the saved session if the dot woke from deepsleep mode
pferland 0:9e88a9018fc0 100 // useful to use with deepsleep because session info is otherwise lost when the dot enters deepsleep
pferland 0:9e88a9018fc0 101 logInfo("restoring network session from NVM");
pferland 0:9e88a9018fc0 102 dot->restoreNetworkSession();
pferland 0:9e88a9018fc0 103 }
pferland 3:d34798ffcaf8 104
pferland 3:d34798ffcaf8 105
pferland 0:9e88a9018fc0 106
pferland 0:9e88a9018fc0 107 while (true) {
pferland 0:9e88a9018fc0 108 std::vector<uint8_t> tx_data;
pferland 0:9e88a9018fc0 109
pferland 0:9e88a9018fc0 110 // join network if not joined
pferland 0:9e88a9018fc0 111 if (!dot->getNetworkJoinStatus()) {
pferland 0:9e88a9018fc0 112 join_network();
pferland 0:9e88a9018fc0 113 }
pferland 7:d2052933ebcb 114
pferland 0:9e88a9018fc0 115 //temp floats
pferland 0:9e88a9018fc0 116 float value1, value2;
pferland 4:142c85980a6f 117
pferland 0:9e88a9018fc0 118 // HTS221 Humidity sensor
pferland 0:9e88a9018fc0 119 temp_sensor1->GetTemperature(&value1);
pferland 0:9e88a9018fc0 120 humidity_sensor->GetHumidity(&value2);
pferland 4:142c85980a6f 121
pferland 0:9e88a9018fc0 122 //serialize data and append to packet
pferland 0:9e88a9018fc0 123 tx_data.push_back(uint8_t(0xFF & *((uint32_t*)(&value1))));
pferland 0:9e88a9018fc0 124 tx_data.push_back(uint8_t((0xFF << 2 ) & *((uint32_t*)(&value1))));
pferland 0:9e88a9018fc0 125 tx_data.push_back(uint8_t((0xFF << 4 ) & *((uint32_t*)(&value1))));
pferland 0:9e88a9018fc0 126 tx_data.push_back(uint8_t((0xFF << 6 ) & *((uint32_t*)(&value1))));
pferland 4:142c85980a6f 127 logInfo("Temperature data %f", value1);
pferland 4:142c85980a6f 128
pferland 4:142c85980a6f 129 // Get accelerometer data, but we're only interested in the Z.
pferland 4:142c85980a6f 130 int32_t accel_vector[3];
pferland 4:142c85980a6f 131 accelerometer->Get_X_Axes(accel_vector);
pferland 4:142c85980a6f 132 logInfo("Acclerometer Z axis: %d", accel_vector[2]);
pferland 4:142c85980a6f 133
pferland 4:142c85980a6f 134 tx_data.push_back(uint8_t(0xFF & accel_vector[2]));
pferland 4:142c85980a6f 135 tx_data.push_back(uint8_t((0xFF << 2 ) & accel_vector[2]));
pferland 4:142c85980a6f 136 tx_data.push_back(uint8_t((0xFF << 4 ) & accel_vector[2]));
pferland 4:142c85980a6f 137 tx_data.push_back(uint8_t((0xFF << 6 ) & accel_vector[2]));
pferland 4:142c85980a6f 138
pferland 0:9e88a9018fc0 139 send_data(tx_data);
pferland 0:9e88a9018fc0 140
pferland 0:9e88a9018fc0 141 // if going into deepsleep mode, save the session so we don't need to join again after waking up
pferland 0:9e88a9018fc0 142 // not necessary if going into sleep mode since RAM is retained
pferland 0:9e88a9018fc0 143 logInfo("saving network session to NVM");
pferland 0:9e88a9018fc0 144 dot->saveNetworkSession();
pferland 0:9e88a9018fc0 145
pferland 0:9e88a9018fc0 146
pferland 0:9e88a9018fc0 147 // ONLY ONE of the three functions below should be uncommented depending on the desired wakeup method
pferland 6:9e6ac13de3e9 148 sleep_wake_rtc_only(deep_sleep);
pferland 0:9e88a9018fc0 149 //sleep_wake_interrupt_only(deep_sleep);
pferland 6:9e6ac13de3e9 150 //sleep_wake_rtc_or_interrupt(deep_sleep);
pferland 0:9e88a9018fc0 151
pferland 0:9e88a9018fc0 152 }
pferland 0:9e88a9018fc0 153
pferland 0:9e88a9018fc0 154 return 0;
pferland 0:9e88a9018fc0 155 }