add senet packet format

Dependencies:   Senet_Packet mDot_X_NUCLEO_IKS01A1 libmDot-dev-mbed5-deprecated

Fork of mDot-IKS01A1 by Peter Ferland

Committer:
pferland
Date:
Fri Dec 09 22:06:09 2016 +0000
Revision:
3:d34798ffcaf8
Parent:
2:d0873bb1255f
Child:
4:142c85980a6f
Updated to version of mbed-os and libmdot that doesn't have i2c issues

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 0:9e88a9018fc0 7 static std::string network_name = "TestTest";
pferland 0:9e88a9018fc0 8 static std::string network_passphrase = "TestTest";
pferland 0:9e88a9018fc0 9 static uint8_t frequency_sub_band = 1;
pferland 0:9e88a9018fc0 10 static bool public_network = false;
pferland 0:9e88a9018fc0 11 static uint8_t ack = 0;
pferland 0:9e88a9018fc0 12
pferland 0:9e88a9018fc0 13 mDot *dot = NULL;
pferland 0:9e88a9018fc0 14 Serial pc(USBTX, USBRX);
pferland 0:9e88a9018fc0 15
pferland 0:9e88a9018fc0 16 int main()
pferland 0:9e88a9018fc0 17 {
pferland 0:9e88a9018fc0 18 // Custom event handler for automatically displaying RX data
pferland 0:9e88a9018fc0 19 RadioEvent events;
pferland 0:9e88a9018fc0 20 pc.baud(115200);
pferland 0:9e88a9018fc0 21
pferland 0:9e88a9018fc0 22 /* Initialize mDot */
pferland 0:9e88a9018fc0 23
pferland 0:9e88a9018fc0 24 mts::MTSLog::setLogLevel(mts::MTSLog::TRACE_LEVEL);
pferland 0:9e88a9018fc0 25 dot = mDot::getInstance();
pferland 0:9e88a9018fc0 26 dot->setEvents(&events);
pferland 0:9e88a9018fc0 27
pferland 0:9e88a9018fc0 28
pferland 0:9e88a9018fc0 29
pferland 0:9e88a9018fc0 30
pferland 0:9e88a9018fc0 31
pferland 0:9e88a9018fc0 32 if (!dot->getStandbyFlag()) {
pferland 0:9e88a9018fc0 33 logInfo("mbed-os library version: %d", MBED_LIBRARY_VERSION);
pferland 0:9e88a9018fc0 34 // start from a well-known state
pferland 0:9e88a9018fc0 35 logInfo("defaulting Dot configuration");
pferland 0:9e88a9018fc0 36 dot->resetConfig();
pferland 0:9e88a9018fc0 37 dot->resetNetworkSession();
pferland 0:9e88a9018fc0 38
pferland 0:9e88a9018fc0 39 // update configuration if necessary
pferland 0:9e88a9018fc0 40 // in AUTO_OTA mode the session is automatically saved, so saveNetworkSession and restoreNetworkSession are not needed
pferland 0:9e88a9018fc0 41 if (dot->getJoinMode() != mDot::AUTO_OTA) {
pferland 0:9e88a9018fc0 42 logInfo("changing network join mode to AUTO_OTA");
pferland 0:9e88a9018fc0 43 if (dot->setJoinMode(mDot::AUTO_OTA) != mDot::MDOT_OK) {
pferland 0:9e88a9018fc0 44 logError("failed to set network join mode to AUTO_OTA");
pferland 0:9e88a9018fc0 45 }
pferland 0:9e88a9018fc0 46 }
pferland 0:9e88a9018fc0 47 // 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 48 // only one method or the other should be used!
pferland 0:9e88a9018fc0 49 // network ID = crc64(network name)
pferland 0:9e88a9018fc0 50 // network KEY = cmac(network passphrase)
pferland 0:9e88a9018fc0 51 update_ota_config_name_phrase(network_name, network_passphrase, frequency_sub_band, public_network, ack);
pferland 0:9e88a9018fc0 52
pferland 0:9e88a9018fc0 53 // configure network link checks
pferland 0:9e88a9018fc0 54 // 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 55 // check the link every count packets
pferland 0:9e88a9018fc0 56 // declare the Dot disconnected after threshold failed link checks
pferland 0:9e88a9018fc0 57 // for count = 3 and threshold = 5, the Dot will be considered disconnected after 15 missed packets in a row
pferland 0:9e88a9018fc0 58 update_network_link_check_config(3, 5);
pferland 0:9e88a9018fc0 59
pferland 0:9e88a9018fc0 60 // save changes to configuration
pferland 0:9e88a9018fc0 61 logInfo("saving configuration");
pferland 0:9e88a9018fc0 62 if (!dot->saveConfig()) {
pferland 0:9e88a9018fc0 63 logError("failed to save configuration");
pferland 0:9e88a9018fc0 64 }
pferland 0:9e88a9018fc0 65
pferland 0:9e88a9018fc0 66 // display configuration
pferland 0:9e88a9018fc0 67 display_config();
pferland 0:9e88a9018fc0 68 } else {
pferland 0:9e88a9018fc0 69 // restore the saved session if the dot woke from deepsleep mode
pferland 0:9e88a9018fc0 70 // useful to use with deepsleep because session info is otherwise lost when the dot enters deepsleep
pferland 0:9e88a9018fc0 71 logInfo("restoring network session from NVM");
pferland 0:9e88a9018fc0 72 dot->restoreNetworkSession();
pferland 0:9e88a9018fc0 73 }
pferland 3:d34798ffcaf8 74 /* Instantiate the expansion board */
pferland 3:d34798ffcaf8 75 X_NUCLEO_IKS01A1 *mems_expansion_board = X_NUCLEO_IKS01A1::Instance(I2C_SDA, I2C_SCL, PC_1);
pferland 3:d34798ffcaf8 76
pferland 3:d34798ffcaf8 77 /* Retrieve the composing elements of the expansion board */
pferland 3:d34798ffcaf8 78 GyroSensor *gyroscope = mems_expansion_board->GetGyroscope();
pferland 3:d34798ffcaf8 79 MotionSensor *accelerometer = mems_expansion_board->GetAccelerometer();
pferland 3:d34798ffcaf8 80 MagneticSensor *magnetometer = mems_expansion_board->magnetometer;
pferland 3:d34798ffcaf8 81 HumiditySensor *humidity_sensor = mems_expansion_board->ht_sensor;
pferland 3:d34798ffcaf8 82 PressureSensor *pressure_sensor = mems_expansion_board->pt_sensor;
pferland 3:d34798ffcaf8 83 TempSensor *temp_sensor1 = mems_expansion_board->ht_sensor;
pferland 3:d34798ffcaf8 84 TempSensor *temp_sensor2 = mems_expansion_board->pt_sensor;
pferland 3:d34798ffcaf8 85
pferland 0:9e88a9018fc0 86
pferland 0:9e88a9018fc0 87 while (true) {
pferland 0:9e88a9018fc0 88 std::vector<uint8_t> tx_data;
pferland 0:9e88a9018fc0 89
pferland 0:9e88a9018fc0 90 // join network if not joined
pferland 0:9e88a9018fc0 91 if (!dot->getNetworkJoinStatus()) {
pferland 0:9e88a9018fc0 92 join_network();
pferland 0:9e88a9018fc0 93 }
pferland 0:9e88a9018fc0 94 // Retrieve sensor data and prepare the packet.
pferland 0:9e88a9018fc0 95 //temp floats
pferland 0:9e88a9018fc0 96 float value1, value2;
pferland 0:9e88a9018fc0 97 // HTS221 Humidity sensor
pferland 0:9e88a9018fc0 98 temp_sensor1->GetTemperature(&value1);
pferland 0:9e88a9018fc0 99 humidity_sensor->GetHumidity(&value2);
pferland 0:9e88a9018fc0 100 //serialize data and append to packet
pferland 0:9e88a9018fc0 101 tx_data.push_back(uint8_t(0xFF & *((uint32_t*)(&value1))));
pferland 0:9e88a9018fc0 102 tx_data.push_back(uint8_t((0xFF << 2 ) & *((uint32_t*)(&value1))));
pferland 0:9e88a9018fc0 103 tx_data.push_back(uint8_t((0xFF << 4 ) & *((uint32_t*)(&value1))));
pferland 0:9e88a9018fc0 104 tx_data.push_back(uint8_t((0xFF << 6 ) & *((uint32_t*)(&value1))));
pferland 0:9e88a9018fc0 105 logInfo("Temperature data %d", value1);
pferland 0:9e88a9018fc0 106 send_data(tx_data);
pferland 0:9e88a9018fc0 107
pferland 0:9e88a9018fc0 108 // if going into deepsleep mode, save the session so we don't need to join again after waking up
pferland 0:9e88a9018fc0 109 // not necessary if going into sleep mode since RAM is retained
pferland 0:9e88a9018fc0 110 logInfo("saving network session to NVM");
pferland 0:9e88a9018fc0 111 dot->saveNetworkSession();
pferland 0:9e88a9018fc0 112
pferland 0:9e88a9018fc0 113
pferland 0:9e88a9018fc0 114 // ONLY ONE of the three functions below should be uncommented depending on the desired wakeup method
pferland 0:9e88a9018fc0 115 //sleep_wake_rtc_only(deep_sleep);
pferland 0:9e88a9018fc0 116 //sleep_wake_interrupt_only(deep_sleep);
pferland 0:9e88a9018fc0 117 sleep_wake_rtc_or_interrupt(false);
pferland 0:9e88a9018fc0 118
pferland 0:9e88a9018fc0 119 }
pferland 0:9e88a9018fc0 120
pferland 0:9e88a9018fc0 121 return 0;
pferland 0:9e88a9018fc0 122 }