![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Example of using the mDot UDK with the X-NUCLEO-IKS01A2 board
Dependencies: MTS_X_NUCLEO_IKS01A2 libmDot-mbed5
Fork of mDot-IKS01A1 by
Diff: main.cpp
- Revision:
- 9:e642e8f9ea37
- Parent:
- 8:bfbc3dd47166
- Child:
- 10:1e3e3ab9c29c
--- a/main.cpp Fri Dec 16 04:24:54 2016 +0000 +++ b/main.cpp Tue Jan 17 22:53:01 2017 +0000 @@ -3,6 +3,7 @@ #include "x_nucleo_iks01a1.h" #include "dot_util.h" #include "RadioEvent.h" +#include <cmath> // mDot UDK board demo with X-NUCLEO-IKS01A1 sensor card // For more examples see the Dot-Examples project: @@ -10,20 +11,28 @@ // 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"; +#define SENET +#ifdef SENET // 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}; +// {0xD3,0x5A,0x30,0x60,0xA6,0x0D,0x9E,0xEA,0xD9,0xA1,0x19,0x61,0x4F,0x29,0x9E,0x5B} +static uint8_t network_key[] = {0xD3,0x5A,0x30,0x60,0xA6,0x0D,0x9E,0xEA,0xD9,0xA1,0x19,0x61,0x4F,0x29,0x9E,0x5B}; // 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; -static uint8_t tx_datarate = mDot::DR3; +#else +//Replace with settings on your Conduit +static std::string network_name = "TestTest"; +static std::string network_passphrase = "TestTest"; +// 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 = false; +#endif +static uint8_t ack = 1; +static uint8_t tx_datarate = mDot::DR4; // deepsleep consumes slightly less current than sleep // in sleep mode, IO state is maintained, RAM is retained, and application will resume after waking up @@ -86,9 +95,12 @@ // 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) +#ifdef SENET // 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); +#else + update_ota_config_name_phrase(network_name, network_passphrase, frequency_sub_band, public_network, ack); +#endif // 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 @@ -122,6 +134,53 @@ join_network(); } + // Payload structure for mydevices cayenne: + // 1 byte Data1 ID + // 1 Byte Data1 Type + // N Bytes Data1 + // 1 byte data 2 ID + // 1 byte data 2 type + // n Bytes data 2 + // ... + + // formats: + // Temperature sensor: + /* + * IPSO: 3303 + * LPP 103 + * HEX: 67 + * Data size: 2 + * Resolution: 0.1 degres C + + * Humidity sensor + * IPSO: 3304 + * LPP: 104 + * Hex: 68 + * Datasize: 1 + * Resolution: 0.5% unsigned + + * Barometer/pressure sensor + * IPSO: 3315 + * LPP: 115 + * Hex: 73 + * Datasize: 2 + * Resolution 0.1hPa unsigned MSB + + * Accelerometer + * IPSO: 3313 + * LPP: 113 + * Hex: 71 + * Data size: 6 + * Resolution: 0.001G signed MSB per axis + + * Gyrometer + * IPSO: 3334 + * LPP: 134 + * Hex: 86 + * Data size: 6 + * Resolution: 0.01 degrees/s signed msb per axis + */ + //temp floats float value1, value2; @@ -130,22 +189,60 @@ 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)))); + // Cayenne data: temperature; tag is 0x67, 2 bytes signed, 0.1 C/bit + tx_data.push_back(uint8_t(1)); // data id + tx_data.push_back(uint8_t(0x67)); // data type - temp + int16_t temp = floor(value1*10 + 0.5f); + logInfo("Temp payload: %d", temp); + tx_data.push_back(uint8_t( 0xFF & (temp >> 8))); + tx_data.push_back(uint8_t(0xFF & temp)); + + + tx_data.push_back(uint8_t(2)); // data id + tx_data.push_back(uint8_t(0x68)); // data type - humidity + temp = floor(value2 * 2.0f + 0.5f); + tx_data.push_back(uint8_t(0xFF & temp )); + logInfo("Temperature data %f", value1); + logInfo("Humidity data: %f", value2); - // Get accelerometer data, but we're only interested in the Z. + pressure_sensor->GetPressure(&value1); + logInfo("PRessure data: %f", value1); + // pressure is reported in mbar, cayenne wants it in 0.1 hPa + // 1mbar = 1 hPa + temp = floor(value1 * 100.0f + 0.5f); + tx_data.push_back(uint8_t(3)); // data id + tx_data.push_back(uint8_t(0x73)); // data type - pressure + temp = floor(value1 / 0.1f + 0.5f); + tx_data.push_back(uint8_t(0xFF & (temp >> 8))); + tx_data.push_back(uint8_t(0xFF & temp)); + + + // Get accelerometer data int32_t accel_vector[3]; + // returns in mG 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])); - + tx_data.push_back(uint8_t(4)); // data id + tx_data.push_back(uint8_t(0x71)); // data type - accelerometer + for(int i=0; i<3; i++){ + tx_data.push_back(uint8_t(0xFF & accel_vector[i]) >> 8); + tx_data.push_back(uint8_t(0xFF & accel_vector[i])); + } + + // Get gyro data + gyroscope->Get_G_Axes(accel_vector); + // gyro reports in milidegrees/sec, cayenne wants centidegrees/sec + tx_data.push_back(uint8_t(5)); //data id + tx_data.push_back(uint8_t(0x86)); // data type - gyrometer + for(int i=0; i<3; i++){ + accel_vector[i] /= 10; + tx_data.push_back(uint8_t(0xFF & (accel_vector[i] >> 8))); + tx_data.push_back(uint8_t(0xFF & accel_vector[i])); + } + + send_data(tx_data); // if going into deepsleep mode, save the session so we don't need to join again after waking up