add senet packet format

Dependencies:   Senet_Packet mDot_X_NUCLEO_IKS01A1 libmDot-dev-mbed5-deprecated

Fork of mDot-IKS01A1 by Peter Ferland

Committer:
mfiore
Date:
Fri Apr 07 23:17:16 2017 +0000
Revision:
13:3a0a00664054
Parent:
12:922bb95e4c2e
multiply temp by 10 to preserve the decimal

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"
mfiore 12:922bb95e4c2e 6 #include "senet_packet.h"
pferland 9:e642e8f9ea37 7 #include <cmath>
pferland 0:9e88a9018fc0 8
pferland 4:142c85980a6f 9 // mDot UDK board demo with X-NUCLEO-IKS01A1 sensor card
pferland 4:142c85980a6f 10 // For more examples see the Dot-Examples project:
pferland 4:142c85980a6f 11 // https://developer.mbed.org/teams/MultiTech/code/Dot-Examples/
pferland 4:142c85980a6f 12
pferland 4:142c85980a6f 13 // This triggers an I2C issue in mbed-os 5.1.5
pferland 4:142c85980a6f 14 // Use any other revision to compile. (Tested with libmDot-dev/mbed-os 5.2.2
pferland 9:e642e8f9ea37 15 #define SENET
pferland 10:1e3e3ab9c29c 16 #ifdef ACTILITY
pferland 10:1e3e3ab9c29c 17 // Network Id for Senet public network
pferland 10:1e3e3ab9c29c 18 static uint8_t network_id[] = {0xF0, 0x3D, 0x29,0xAC,0x71,0x00,0x00, 0x00};
pferland 10:1e3e3ab9c29c 19 // Register at or Sign in to http://portal.senetco.com/ and register your NodeId to receive your AppId
pferland 10:1e3e3ab9c29c 20 // {0xD3,0x5A,0x30,0x60,0xA6,0x0D,0x9E,0xEA,0xD9,0xA1,0x19,0x61,0x4F,0x29,0x9E,0x5B}
pferland 10:1e3e3ab9c29c 21 static uint8_t network_key[] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; //{0xD3,0x5A,0x30,0x60,0xA6,0x0D,0x9E,0xEA,0xD9,0xA1,0x19,0x61,0x4F,0x29,0x9E,0x5B};
pferland 10:1e3e3ab9c29c 22 static uint8_t frequency_sub_band = 0;
pferland 10:1e3e3ab9c29c 23 static bool public_network = true;
pferland 10:1e3e3ab9c29c 24 #elif defined(SENET)
pferland 4:142c85980a6f 25 // Network Id for Senet public network
mfiore 12:922bb95e4c2e 26 static uint8_t network_id[] = { 0x00, 0x25, 0x0C, 0x00, 0x00, 0x01, 0x00, 0x01 };
pferland 4:142c85980a6f 27 // Register at or Sign in to http://portal.senetco.com/ and register your NodeId to receive your AppId
pferland 9:e642e8f9ea37 28 // {0xD3,0x5A,0x30,0x60,0xA6,0x0D,0x9E,0xEA,0xD9,0xA1,0x19,0x61,0x4F,0x29,0x9E,0x5B}
mfiore 12:922bb95e4c2e 29 static uint8_t network_key[] = { 0xFD, 0x68, 0xE9, 0xB5, 0x7C, 0xCA, 0x5E, 0xD6, 0xAD, 0xF6, 0x23, 0xCB, 0x03, 0x26, 0x68, 0xF4 };
pferland 4:142c85980a6f 30 // 1 For Senet, configurable on your Conduit
mfiore 12:922bb95e4c2e 31 static uint8_t frequency_sub_band = 0;
pferland 4:142c85980a6f 32 // True for Senet, false for your Conduit.
pferland 4:142c85980a6f 33 static bool public_network = true;
pferland 9:e642e8f9ea37 34 #else
pferland 9:e642e8f9ea37 35 //Replace with settings on your Conduit
pferland 10:1e3e3ab9c29c 36 static std::string network_name = "EcoLabTest";
pferland 10:1e3e3ab9c29c 37 static std::string network_passphrase = "EcoLabTest";
pferland 9:e642e8f9ea37 38 // 1 For Senet, configurable on your Conduit
pferland 9:e642e8f9ea37 39 static uint8_t frequency_sub_band = 1;
pferland 9:e642e8f9ea37 40 // True for Senet, false for your Conduit.
pferland 9:e642e8f9ea37 41 static bool public_network = false;
pferland 9:e642e8f9ea37 42 #endif
pferland 10:1e3e3ab9c29c 43 static uint8_t ack = 0;
pferland 10:1e3e3ab9c29c 44 static uint8_t tx_datarate = mDot::SF_7;
pferland 0:9e88a9018fc0 45
pferland 4:142c85980a6f 46 // deepsleep consumes slightly less current than sleep
pferland 4:142c85980a6f 47 // in sleep mode, IO state is maintained, RAM is retained, and application will resume after waking up
pferland 4:142c85980a6f 48 // in deepsleep mode, IOs float, RAM is lost, and application will start from beginning after waking up
pferland 4:142c85980a6f 49 // if deep_sleep == true, device will enter deepsleep mode
pferland 4:142c85980a6f 50 static bool deep_sleep = false;
pferland 4:142c85980a6f 51
pferland 0:9e88a9018fc0 52 mDot *dot = NULL;
pferland 0:9e88a9018fc0 53
pferland 0:9e88a9018fc0 54 int main()
pferland 0:9e88a9018fc0 55 {
pferland 4:142c85980a6f 56 Serial pc(USBTX, USBRX);
pferland 4:142c85980a6f 57
pferland 4:142c85980a6f 58 /* Instantiate the expansion board */
pferland 4:142c85980a6f 59 X_NUCLEO_IKS01A1 *mems_expansion_board = X_NUCLEO_IKS01A1::Instance(I2C_SDA, I2C_SCL, PC_1);
pferland 4:142c85980a6f 60
pferland 4:142c85980a6f 61 /* Retrieve the composing elements of the expansion board */
pferland 4:142c85980a6f 62 GyroSensor *gyroscope = mems_expansion_board->GetGyroscope();
pferland 4:142c85980a6f 63 MotionSensor *accelerometer = mems_expansion_board->GetAccelerometer();
pferland 4:142c85980a6f 64 MagneticSensor *magnetometer = mems_expansion_board->magnetometer;
pferland 4:142c85980a6f 65 HumiditySensor *humidity_sensor = mems_expansion_board->ht_sensor;
pferland 4:142c85980a6f 66 PressureSensor *pressure_sensor = mems_expansion_board->pt_sensor;
pferland 4:142c85980a6f 67 TempSensor *temp_sensor1 = mems_expansion_board->ht_sensor;
pferland 4:142c85980a6f 68 TempSensor *temp_sensor2 = mems_expansion_board->pt_sensor;
pferland 0:9e88a9018fc0 69 // Custom event handler for automatically displaying RX data
pferland 0:9e88a9018fc0 70 RadioEvent events;
pferland 0:9e88a9018fc0 71 pc.baud(115200);
pferland 0:9e88a9018fc0 72
pferland 0:9e88a9018fc0 73 /* Initialize mDot */
pferland 0:9e88a9018fc0 74 dot = mDot::getInstance();
pferland 8:bfbc3dd47166 75
pferland 8:bfbc3dd47166 76 //dot->setAdr(true);
pferland 4:142c85980a6f 77 mts::MTSLog::setLogLevel(mts::MTSLog::INFO_LEVEL);
pferland 0:9e88a9018fc0 78 dot->setEvents(&events);
pferland 0:9e88a9018fc0 79
pferland 0:9e88a9018fc0 80
pferland 0:9e88a9018fc0 81 if (!dot->getStandbyFlag()) {
pferland 0:9e88a9018fc0 82 logInfo("mbed-os library version: %d", MBED_LIBRARY_VERSION);
pferland 0:9e88a9018fc0 83 // start from a well-known state
pferland 0:9e88a9018fc0 84 logInfo("defaulting Dot configuration");
pferland 0:9e88a9018fc0 85 dot->resetConfig();
pferland 0:9e88a9018fc0 86 dot->resetNetworkSession();
pferland 0:9e88a9018fc0 87
pferland 0:9e88a9018fc0 88 // update configuration if necessary
pferland 0:9e88a9018fc0 89 // in AUTO_OTA mode the session is automatically saved, so saveNetworkSession and restoreNetworkSession are not needed
pferland 0:9e88a9018fc0 90 if (dot->getJoinMode() != mDot::AUTO_OTA) {
pferland 0:9e88a9018fc0 91 logInfo("changing network join mode to AUTO_OTA");
pferland 0:9e88a9018fc0 92 if (dot->setJoinMode(mDot::AUTO_OTA) != mDot::MDOT_OK) {
pferland 0:9e88a9018fc0 93 logError("failed to set network join mode to AUTO_OTA");
pferland 0:9e88a9018fc0 94 }
pferland 0:9e88a9018fc0 95 }
pferland 8:bfbc3dd47166 96
pferland 8:bfbc3dd47166 97 uint32_t current_tx_datarate = dot->getTxDataRate();
pferland 8:bfbc3dd47166 98 if (current_tx_datarate != tx_datarate) {
pferland 8:bfbc3dd47166 99 logInfo("changing TX datarate from %u to %u", current_tx_datarate, tx_datarate);
pferland 8:bfbc3dd47166 100 if (dot->setTxDataRate(tx_datarate) != mDot::MDOT_OK) {
pferland 8:bfbc3dd47166 101 logError("failed to set TX datarate to %u", tx_datarate);
pferland 8:bfbc3dd47166 102 }
pferland 8:bfbc3dd47166 103 }
pferland 0:9e88a9018fc0 104 // 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 105 // only one method or the other should be used!
pferland 0:9e88a9018fc0 106 // network ID = crc64(network name)
pferland 10:1e3e3ab9c29c 107 #if defined(SENET) || defined(ACTILITY)
pferland 0:9e88a9018fc0 108 // network KEY = cmac(network passphrase)
pferland 4:142c85980a6f 109 update_ota_config_id_key(network_id, network_key, frequency_sub_band, public_network, ack);
pferland 9:e642e8f9ea37 110 #else
pferland 9:e642e8f9ea37 111 update_ota_config_name_phrase(network_name, network_passphrase, frequency_sub_band, public_network, ack);
pferland 9:e642e8f9ea37 112 #endif
pferland 0:9e88a9018fc0 113
pferland 0:9e88a9018fc0 114 // configure network link checks
pferland 0:9e88a9018fc0 115 // 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 116 // check the link every count packets
pferland 0:9e88a9018fc0 117 // declare the Dot disconnected after threshold failed link checks
pferland 0:9e88a9018fc0 118 // for count = 3 and threshold = 5, the Dot will be considered disconnected after 15 missed packets in a row
pferland 0:9e88a9018fc0 119 update_network_link_check_config(3, 5);
pferland 0:9e88a9018fc0 120
pferland 0:9e88a9018fc0 121 // save changes to configuration
pferland 0:9e88a9018fc0 122 logInfo("saving configuration");
pferland 0:9e88a9018fc0 123 if (!dot->saveConfig()) {
pferland 0:9e88a9018fc0 124 logError("failed to save configuration");
pferland 0:9e88a9018fc0 125 }
pferland 0:9e88a9018fc0 126
pferland 0:9e88a9018fc0 127 // display configuration
pferland 0:9e88a9018fc0 128 display_config();
pferland 0:9e88a9018fc0 129 } else {
pferland 0:9e88a9018fc0 130 // restore the saved session if the dot woke from deepsleep mode
pferland 0:9e88a9018fc0 131 // useful to use with deepsleep because session info is otherwise lost when the dot enters deepsleep
pferland 0:9e88a9018fc0 132 logInfo("restoring network session from NVM");
pferland 0:9e88a9018fc0 133 dot->restoreNetworkSession();
pferland 0:9e88a9018fc0 134 }
pferland 3:d34798ffcaf8 135
pferland 3:d34798ffcaf8 136
pferland 0:9e88a9018fc0 137
pferland 0:9e88a9018fc0 138 while (true) {
pferland 0:9e88a9018fc0 139 // join network if not joined
pferland 0:9e88a9018fc0 140 if (!dot->getNetworkJoinStatus()) {
pferland 0:9e88a9018fc0 141 join_network();
pferland 0:9e88a9018fc0 142 }
pferland 7:d2052933ebcb 143
pferland 9:e642e8f9ea37 144 // Payload structure for mydevices cayenne:
pferland 9:e642e8f9ea37 145 // 1 byte Data1 ID
pferland 9:e642e8f9ea37 146 // 1 Byte Data1 Type
pferland 9:e642e8f9ea37 147 // N Bytes Data1
pferland 9:e642e8f9ea37 148 // 1 byte data 2 ID
pferland 9:e642e8f9ea37 149 // 1 byte data 2 type
pferland 9:e642e8f9ea37 150 // n Bytes data 2
pferland 9:e642e8f9ea37 151 // ...
pferland 9:e642e8f9ea37 152
pferland 9:e642e8f9ea37 153 // formats:
pferland 9:e642e8f9ea37 154 // Temperature sensor:
pferland 9:e642e8f9ea37 155 /*
pferland 9:e642e8f9ea37 156 * IPSO: 3303
pferland 9:e642e8f9ea37 157 * LPP 103
pferland 9:e642e8f9ea37 158 * HEX: 67
pferland 9:e642e8f9ea37 159 * Data size: 2
pferland 9:e642e8f9ea37 160 * Resolution: 0.1 degres C
pferland 9:e642e8f9ea37 161
pferland 9:e642e8f9ea37 162 * Humidity sensor
pferland 9:e642e8f9ea37 163 * IPSO: 3304
pferland 9:e642e8f9ea37 164 * LPP: 104
pferland 9:e642e8f9ea37 165 * Hex: 68
pferland 9:e642e8f9ea37 166 * Datasize: 1
pferland 9:e642e8f9ea37 167 * Resolution: 0.5% unsigned
pferland 9:e642e8f9ea37 168
pferland 9:e642e8f9ea37 169 * Barometer/pressure sensor
pferland 9:e642e8f9ea37 170 * IPSO: 3315
pferland 9:e642e8f9ea37 171 * LPP: 115
pferland 9:e642e8f9ea37 172 * Hex: 73
pferland 9:e642e8f9ea37 173 * Datasize: 2
pferland 9:e642e8f9ea37 174 * Resolution 0.1hPa unsigned MSB
pferland 9:e642e8f9ea37 175
pferland 9:e642e8f9ea37 176 * Accelerometer
pferland 9:e642e8f9ea37 177 * IPSO: 3313
pferland 9:e642e8f9ea37 178 * LPP: 113
pferland 9:e642e8f9ea37 179 * Hex: 71
pferland 9:e642e8f9ea37 180 * Data size: 6
pferland 9:e642e8f9ea37 181 * Resolution: 0.001G signed MSB per axis
pferland 9:e642e8f9ea37 182
pferland 9:e642e8f9ea37 183 * Gyrometer
pferland 9:e642e8f9ea37 184 * IPSO: 3334
pferland 9:e642e8f9ea37 185 * LPP: 134
pferland 9:e642e8f9ea37 186 * Hex: 86
pferland 9:e642e8f9ea37 187 * Data size: 6
pferland 9:e642e8f9ea37 188 * Resolution: 0.01 degrees/s signed msb per axis
pferland 9:e642e8f9ea37 189 */
pferland 9:e642e8f9ea37 190
mfiore 12:922bb95e4c2e 191 /*
pferland 0:9e88a9018fc0 192 //temp floats
pferland 0:9e88a9018fc0 193 float value1, value2;
pferland 4:142c85980a6f 194
pferland 0:9e88a9018fc0 195 // HTS221 Humidity sensor
pferland 0:9e88a9018fc0 196 temp_sensor1->GetTemperature(&value1);
pferland 0:9e88a9018fc0 197 humidity_sensor->GetHumidity(&value2);
pferland 4:142c85980a6f 198
pferland 0:9e88a9018fc0 199 //serialize data and append to packet
pferland 9:e642e8f9ea37 200 // Cayenne data: temperature; tag is 0x67, 2 bytes signed, 0.1 C/bit
pferland 9:e642e8f9ea37 201 tx_data.push_back(uint8_t(1)); // data id
pferland 9:e642e8f9ea37 202 tx_data.push_back(uint8_t(0x67)); // data type - temp
pferland 9:e642e8f9ea37 203 int16_t temp = floor(value1*10 + 0.5f);
pferland 9:e642e8f9ea37 204 logInfo("Temp payload: %d", temp);
pferland 9:e642e8f9ea37 205 tx_data.push_back(uint8_t( 0xFF & (temp >> 8)));
pferland 9:e642e8f9ea37 206 tx_data.push_back(uint8_t(0xFF & temp));
pferland 9:e642e8f9ea37 207
pferland 9:e642e8f9ea37 208
pferland 9:e642e8f9ea37 209 tx_data.push_back(uint8_t(2)); // data id
pferland 9:e642e8f9ea37 210 tx_data.push_back(uint8_t(0x68)); // data type - humidity
pferland 9:e642e8f9ea37 211 temp = floor(value2 * 2.0f + 0.5f);
pferland 9:e642e8f9ea37 212 tx_data.push_back(uint8_t(0xFF & temp ));
pferland 9:e642e8f9ea37 213
pferland 4:142c85980a6f 214 logInfo("Temperature data %f", value1);
pferland 9:e642e8f9ea37 215 logInfo("Humidity data: %f", value2);
pferland 4:142c85980a6f 216
pferland 9:e642e8f9ea37 217 pressure_sensor->GetPressure(&value1);
pferland 9:e642e8f9ea37 218 logInfo("PRessure data: %f", value1);
pferland 9:e642e8f9ea37 219 // pressure is reported in mbar, cayenne wants it in 0.1 hPa
pferland 9:e642e8f9ea37 220 // 1mbar = 1 hPa
pferland 9:e642e8f9ea37 221 temp = floor(value1 * 100.0f + 0.5f);
pferland 9:e642e8f9ea37 222 tx_data.push_back(uint8_t(3)); // data id
pferland 9:e642e8f9ea37 223 tx_data.push_back(uint8_t(0x73)); // data type - pressure
pferland 9:e642e8f9ea37 224 temp = floor(value1 / 0.1f + 0.5f);
pferland 9:e642e8f9ea37 225 tx_data.push_back(uint8_t(0xFF & (temp >> 8)));
pferland 9:e642e8f9ea37 226 tx_data.push_back(uint8_t(0xFF & temp));
pferland 9:e642e8f9ea37 227
pferland 9:e642e8f9ea37 228
pferland 9:e642e8f9ea37 229 // Get accelerometer data
pferland 4:142c85980a6f 230 int32_t accel_vector[3];
pferland 9:e642e8f9ea37 231 // returns in mG
pferland 4:142c85980a6f 232 accelerometer->Get_X_Axes(accel_vector);
pferland 4:142c85980a6f 233 logInfo("Acclerometer Z axis: %d", accel_vector[2]);
pferland 4:142c85980a6f 234
pferland 9:e642e8f9ea37 235 tx_data.push_back(uint8_t(4)); // data id
pferland 9:e642e8f9ea37 236 tx_data.push_back(uint8_t(0x71)); // data type - accelerometer
pferland 9:e642e8f9ea37 237 for(int i=0; i<3; i++){
pferland 9:e642e8f9ea37 238 tx_data.push_back(uint8_t(0xFF & accel_vector[i]) >> 8);
pferland 9:e642e8f9ea37 239 tx_data.push_back(uint8_t(0xFF & accel_vector[i]));
pferland 9:e642e8f9ea37 240 }
pferland 9:e642e8f9ea37 241
pferland 9:e642e8f9ea37 242 // Get gyro data
pferland 9:e642e8f9ea37 243 gyroscope->Get_G_Axes(accel_vector);
pferland 9:e642e8f9ea37 244 // gyro reports in milidegrees/sec, cayenne wants centidegrees/sec
pferland 9:e642e8f9ea37 245 tx_data.push_back(uint8_t(5)); //data id
pferland 9:e642e8f9ea37 246 tx_data.push_back(uint8_t(0x86)); // data type - gyrometer
pferland 9:e642e8f9ea37 247 for(int i=0; i<3; i++){
pferland 9:e642e8f9ea37 248 accel_vector[i] /= 10;
pferland 9:e642e8f9ea37 249 tx_data.push_back(uint8_t(0xFF & (accel_vector[i] >> 8)));
pferland 9:e642e8f9ea37 250 tx_data.push_back(uint8_t(0xFF & accel_vector[i]));
pferland 9:e642e8f9ea37 251 }
mfiore 12:922bb95e4c2e 252 */
pferland 9:e642e8f9ea37 253
mfiore 12:922bb95e4c2e 254 std::vector<uint8_t> tx_data;
mfiore 12:922bb95e4c2e 255 uint8_t buffer[64];
mfiore 12:922bb95e4c2e 256 float value;
pferland 9:e642e8f9ea37 257
mfiore 12:922bb95e4c2e 258 SensorPacket packet(buffer, sizeof(buffer));
mfiore 12:922bb95e4c2e 259
mfiore 12:922bb95e4c2e 260 // get temperature
mfiore 12:922bb95e4c2e 261 temp_sensor1->GetTemperature(&value);
mfiore 12:922bb95e4c2e 262 logInfo("temperature: %2.1f C", value);
mfiore 13:3a0a00664054 263 packet.addSensorValue(0, 2, (int16_t)(value*10));
mfiore 12:922bb95e4c2e 264 packet.serialize();
mfiore 12:922bb95e4c2e 265
mfiore 12:922bb95e4c2e 266 tx_data.assign(packet.payload(), packet.payload() + packet.length());
pferland 0:9e88a9018fc0 267 send_data(tx_data);
pferland 0:9e88a9018fc0 268
pferland 10:1e3e3ab9c29c 269 if(deep_sleep){
pferland 0:9e88a9018fc0 270 // if going into deepsleep mode, save the session so we don't need to join again after waking up
pferland 0:9e88a9018fc0 271 // not necessary if going into sleep mode since RAM is retained
pferland 10:1e3e3ab9c29c 272 logInfo("saving network session to NVM");
pferland 10:1e3e3ab9c29c 273 dot->saveNetworkSession();
pferland 10:1e3e3ab9c29c 274 }
pferland 0:9e88a9018fc0 275
pferland 0:9e88a9018fc0 276
pferland 0:9e88a9018fc0 277 // ONLY ONE of the three functions below should be uncommented depending on the desired wakeup method
mfiore 12:922bb95e4c2e 278 //sleep_wake_rtc_only(deep_sleep);
pferland 0:9e88a9018fc0 279 //sleep_wake_interrupt_only(deep_sleep);
pferland 6:9e6ac13de3e9 280 //sleep_wake_rtc_or_interrupt(deep_sleep);
pferland 0:9e88a9018fc0 281
mfiore 12:922bb95e4c2e 282 wait(5);
mfiore 12:922bb95e4c2e 283
pferland 0:9e88a9018fc0 284 }
pferland 0:9e88a9018fc0 285
pferland 0:9e88a9018fc0 286 return 0;
pferland 0:9e88a9018fc0 287 }