example using LOF sensor board and MEMS sensor board
Dependencies: ST_INTERFACES X_NUCLEO_53L0A1 X_NUCLEO_COMMON X_NUCLEO_IKS01A1 Senet_Packet libmDot-mbed5
main.cpp@5:1a52bf3f0fb9, 2017-04-08 (annotated)
- Committer:
- Mike Fiore
- Date:
- Sat Apr 08 11:06:17 2017 -0400
- Revision:
- 5:1a52bf3f0fb9
- Parent:
- 2:a0fb0b785bd6
add lora communication. sending temperature, distance, and pressure in senet packet format so it can be viewed on exosite
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mfiore | 0:06fc5a68ead2 | 1 | #include "mbed.h" |
mfiore | 0:06fc5a68ead2 | 2 | #include "x_nucleo_iks01a1.h" |
mfiore | 0:06fc5a68ead2 | 3 | #include "x_nucleo_53l0a1.h" |
Mike Fiore |
5:1a52bf3f0fb9 | 4 | #include "senet_packet.h" |
Mike Fiore |
5:1a52bf3f0fb9 | 5 | #include "mDot.h" |
Mike Fiore |
5:1a52bf3f0fb9 | 6 | #include "dot_util.h" |
Mike Fiore |
5:1a52bf3f0fb9 | 7 | |
Mike Fiore |
5:1a52bf3f0fb9 | 8 | ////////////////////////////////////////////////////////////////////////// |
Mike Fiore |
5:1a52bf3f0fb9 | 9 | // * these options must match the the Senet credentials for your device // |
Mike Fiore |
5:1a52bf3f0fb9 | 10 | // * edit their values to match your configuration // |
Mike Fiore |
5:1a52bf3f0fb9 | 11 | ///////////////////////////////////////////////////////////////////////// |
Mike Fiore |
5:1a52bf3f0fb9 | 12 | // Network Id for Senet public network |
Mike Fiore |
5:1a52bf3f0fb9 | 13 | static uint8_t app_eui[] = { 0x00, 0x25, 0x0C, 0x00, 0x00, 0x01, 0x00, 0x01 }; |
Mike Fiore |
5:1a52bf3f0fb9 | 14 | // Register at or Sign in to http://portal.senetco.com/ and register your NodeId to receive your ApKey |
Mike Fiore |
5:1a52bf3f0fb9 | 15 | // replace this default key with yours |
Mike Fiore |
5:1a52bf3f0fb9 | 16 | static uint8_t app_key[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; |
Mike Fiore |
5:1a52bf3f0fb9 | 17 | |
Mike Fiore |
5:1a52bf3f0fb9 | 18 | mDot* dot = NULL; |
mfiore | 0:06fc5a68ead2 | 19 | |
mfiore | 0:06fc5a68ead2 | 20 | DevI2C i2c(D14, D15); |
mfiore | 0:06fc5a68ead2 | 21 | |
mfiore | 0:06fc5a68ead2 | 22 | /* Instantiate the expansion board */ |
mfiore | 0:06fc5a68ead2 | 23 | static X_NUCLEO_IKS01A1 *mems_expansion_board = X_NUCLEO_IKS01A1::Instance(&i2c); |
mfiore | 0:06fc5a68ead2 | 24 | // MAKE SURE YOU JUMPER PIN D7 TO PIN D8! |
Mike Fiore |
1:a2c5a8a74527 | 25 | static X_NUCLEO_53L0A1 *range_board = X_NUCLEO_53L0A1::Instance(&i2c, A2, D7, D2); |
mfiore | 0:06fc5a68ead2 | 26 | |
mfiore | 0:06fc5a68ead2 | 27 | /* Retrieve the composing elements of the expansion board */ |
mfiore | 0:06fc5a68ead2 | 28 | static GyroSensor *gyroscope = mems_expansion_board->GetGyroscope(); |
mfiore | 0:06fc5a68ead2 | 29 | static MotionSensor *accelerometer = mems_expansion_board->GetAccelerometer(); |
mfiore | 0:06fc5a68ead2 | 30 | static MagneticSensor *magnetometer = mems_expansion_board->magnetometer; |
mfiore | 0:06fc5a68ead2 | 31 | static HumiditySensor *humidity_sensor = mems_expansion_board->ht_sensor; |
mfiore | 0:06fc5a68ead2 | 32 | static PressureSensor *pressure_sensor = mems_expansion_board->pt_sensor; |
mfiore | 0:06fc5a68ead2 | 33 | static TempSensor *temp_sensor1 = mems_expansion_board->ht_sensor; |
mfiore | 0:06fc5a68ead2 | 34 | static TempSensor *temp_sensor2 = mems_expansion_board->pt_sensor; |
mfiore | 0:06fc5a68ead2 | 35 | |
mfiore | 0:06fc5a68ead2 | 36 | /* Helper function for printing floats & doubles */ |
mfiore | 0:06fc5a68ead2 | 37 | static char *printDouble(char* str, double v, int decimalDigits=2) { |
mfiore | 0:06fc5a68ead2 | 38 | int i = 1; |
mfiore | 0:06fc5a68ead2 | 39 | int intPart, fractPart; |
mfiore | 0:06fc5a68ead2 | 40 | int len; |
mfiore | 0:06fc5a68ead2 | 41 | char *ptr; |
mfiore | 0:06fc5a68ead2 | 42 | |
mfiore | 0:06fc5a68ead2 | 43 | /* prepare decimal digits multiplicator */ |
mfiore | 0:06fc5a68ead2 | 44 | for (;decimalDigits!=0; i*=10, decimalDigits--); |
mfiore | 0:06fc5a68ead2 | 45 | |
mfiore | 0:06fc5a68ead2 | 46 | /* calculate integer & fractinal parts */ |
mfiore | 0:06fc5a68ead2 | 47 | intPart = (int)v; |
mfiore | 0:06fc5a68ead2 | 48 | fractPart = (int)((v-(double)(int)v)*i); |
mfiore | 0:06fc5a68ead2 | 49 | |
mfiore | 0:06fc5a68ead2 | 50 | /* fill in integer part */ |
mfiore | 0:06fc5a68ead2 | 51 | sprintf(str, "%i.", intPart); |
mfiore | 0:06fc5a68ead2 | 52 | |
mfiore | 0:06fc5a68ead2 | 53 | /* prepare fill in of fractional part */ |
mfiore | 0:06fc5a68ead2 | 54 | len = strlen(str); |
mfiore | 0:06fc5a68ead2 | 55 | ptr = &str[len]; |
mfiore | 0:06fc5a68ead2 | 56 | |
mfiore | 0:06fc5a68ead2 | 57 | /* fill in leading fractional zeros */ |
mfiore | 0:06fc5a68ead2 | 58 | for (i/=10;i>1; i/=10, ptr++) { |
mfiore | 0:06fc5a68ead2 | 59 | if(fractPart >= i) break; |
mfiore | 0:06fc5a68ead2 | 60 | *ptr = '0'; |
mfiore | 0:06fc5a68ead2 | 61 | } |
mfiore | 0:06fc5a68ead2 | 62 | |
mfiore | 0:06fc5a68ead2 | 63 | /* fill in (rest of) fractional part */ |
mfiore | 0:06fc5a68ead2 | 64 | sprintf(ptr, "%i", fractPart); |
mfiore | 0:06fc5a68ead2 | 65 | |
mfiore | 0:06fc5a68ead2 | 66 | return str; |
mfiore | 0:06fc5a68ead2 | 67 | } |
mfiore | 0:06fc5a68ead2 | 68 | |
Mike Fiore |
2:a0fb0b785bd6 | 69 | void start_mems_board() { |
mfiore | 0:06fc5a68ead2 | 70 | uint8_t id; |
Mike Fiore |
2:a0fb0b785bd6 | 71 | |
Mike Fiore |
2:a0fb0b785bd6 | 72 | humidity_sensor->read_id(&id); |
Mike Fiore |
2:a0fb0b785bd6 | 73 | printf("HTS221 humidity & temperature = 0x%X\r\n", id); |
Mike Fiore |
2:a0fb0b785bd6 | 74 | |
Mike Fiore |
2:a0fb0b785bd6 | 75 | pressure_sensor->read_id(&id); |
Mike Fiore |
2:a0fb0b785bd6 | 76 | printf("LPS25H pressure & temperature = 0x%X\r\n", id); |
Mike Fiore |
2:a0fb0b785bd6 | 77 | |
Mike Fiore |
2:a0fb0b785bd6 | 78 | magnetometer->read_id(&id); |
Mike Fiore |
2:a0fb0b785bd6 | 79 | printf("LIS3MDL magnetometer = 0x%X\r\n", id); |
Mike Fiore |
2:a0fb0b785bd6 | 80 | |
Mike Fiore |
2:a0fb0b785bd6 | 81 | gyroscope->read_id(&id); |
Mike Fiore |
2:a0fb0b785bd6 | 82 | printf("LSM6DS0 accelerometer & gyroscope = 0x%X\r\n", id); |
Mike Fiore |
2:a0fb0b785bd6 | 83 | |
Mike Fiore |
2:a0fb0b785bd6 | 84 | wait(3); |
Mike Fiore |
2:a0fb0b785bd6 | 85 | } |
Mike Fiore |
2:a0fb0b785bd6 | 86 | |
Mike Fiore |
2:a0fb0b785bd6 | 87 | void start_range_board() { |
Mike Fiore |
2:a0fb0b785bd6 | 88 | range_board->InitBoard(); |
Mike Fiore |
2:a0fb0b785bd6 | 89 | |
Mike Fiore |
2:a0fb0b785bd6 | 90 | wait(1); |
Mike Fiore |
2:a0fb0b785bd6 | 91 | } |
Mike Fiore |
2:a0fb0b785bd6 | 92 | |
Mike Fiore |
2:a0fb0b785bd6 | 93 | int main() { |
mfiore | 0:06fc5a68ead2 | 94 | float value1, value2; |
mfiore | 0:06fc5a68ead2 | 95 | char buffer1[32], buffer2[32]; |
mfiore | 0:06fc5a68ead2 | 96 | int32_t axes[3]; |
Mike Fiore |
1:a2c5a8a74527 | 97 | int ret; |
mfiore | 0:06fc5a68ead2 | 98 | int status; |
Mike Fiore |
5:1a52bf3f0fb9 | 99 | uint32_t distance; |
Mike Fiore |
5:1a52bf3f0fb9 | 100 | std::vector<uint8_t> tx_data; |
Mike Fiore |
5:1a52bf3f0fb9 | 101 | uint8_t buffer[64]; |
Mike Fiore |
5:1a52bf3f0fb9 | 102 | float value; |
Mike Fiore |
5:1a52bf3f0fb9 | 103 | |
Mike Fiore |
5:1a52bf3f0fb9 | 104 | SensorPacket packet(buffer, sizeof(buffer)); |
mfiore | 0:06fc5a68ead2 | 105 | |
Mike Fiore |
2:a0fb0b785bd6 | 106 | printf("\r\n--- Application starting up ---\r\n"); |
mfiore | 0:06fc5a68ead2 | 107 | |
Mike Fiore |
2:a0fb0b785bd6 | 108 | printf("\r\n--- Initializing mems board ---\r\n"); |
Mike Fiore |
2:a0fb0b785bd6 | 109 | start_mems_board(); |
Mike Fiore |
2:a0fb0b785bd6 | 110 | |
Mike Fiore |
2:a0fb0b785bd6 | 111 | printf("\r\n--- Initializing range board ---\r\n"); |
Mike Fiore |
2:a0fb0b785bd6 | 112 | start_range_board(); |
mfiore | 0:06fc5a68ead2 | 113 | |
Mike Fiore |
5:1a52bf3f0fb9 | 114 | printf("\r\n--- Initializing LoRa stack ---\r\n"); |
Mike Fiore |
5:1a52bf3f0fb9 | 115 | // get a handle to the stack |
Mike Fiore |
5:1a52bf3f0fb9 | 116 | dot = mDot::getInstance(); |
Mike Fiore |
5:1a52bf3f0fb9 | 117 | // reset config to start from a known state |
Mike Fiore |
5:1a52bf3f0fb9 | 118 | dot->resetConfig(); |
Mike Fiore |
5:1a52bf3f0fb9 | 119 | dot->resetNetworkSession(); |
Mike Fiore |
5:1a52bf3f0fb9 | 120 | dot->setLogLevel(mts::MTSLog::INFO_LEVEL); |
Mike Fiore |
5:1a52bf3f0fb9 | 121 | if (dot->getJoinMode() != mDot::OTA) { |
Mike Fiore |
5:1a52bf3f0fb9 | 122 | logInfo("changing network join mode to OTA"); |
Mike Fiore |
5:1a52bf3f0fb9 | 123 | if (dot->setJoinMode(mDot::OTA) != mDot::MDOT_OK) { |
Mike Fiore |
5:1a52bf3f0fb9 | 124 | logError("failed to set network join mode to OTA"); |
Mike Fiore |
5:1a52bf3f0fb9 | 125 | } |
Mike Fiore |
5:1a52bf3f0fb9 | 126 | } |
Mike Fiore |
5:1a52bf3f0fb9 | 127 | update_ota_config_id_key(app_eui, app_key, 0, true, 0); |
Mike Fiore |
5:1a52bf3f0fb9 | 128 | // save changes to configuration |
Mike Fiore |
5:1a52bf3f0fb9 | 129 | logInfo("saving configuration"); |
Mike Fiore |
5:1a52bf3f0fb9 | 130 | if (!dot->saveConfig()) { |
Mike Fiore |
5:1a52bf3f0fb9 | 131 | logError("failed to save configuration"); |
Mike Fiore |
5:1a52bf3f0fb9 | 132 | } |
Mike Fiore |
5:1a52bf3f0fb9 | 133 | |
Mike Fiore |
5:1a52bf3f0fb9 | 134 | // display configuration |
Mike Fiore |
5:1a52bf3f0fb9 | 135 | display_config(); |
Mike Fiore |
5:1a52bf3f0fb9 | 136 | |
Mike Fiore |
5:1a52bf3f0fb9 | 137 | // join network |
mfiore | 0:06fc5a68ead2 | 138 | while (true) { |
Mike Fiore |
5:1a52bf3f0fb9 | 139 | if (dot->joinNetwork() == mDot::MDOT_OK) { |
Mike Fiore |
5:1a52bf3f0fb9 | 140 | break; |
Mike Fiore |
5:1a52bf3f0fb9 | 141 | } |
Mike Fiore |
2:a0fb0b785bd6 | 142 | |
Mike Fiore |
5:1a52bf3f0fb9 | 143 | wait(1); |
Mike Fiore |
5:1a52bf3f0fb9 | 144 | } |
Mike Fiore |
5:1a52bf3f0fb9 | 145 | |
Mike Fiore |
5:1a52bf3f0fb9 | 146 | while (true) { |
Mike Fiore |
5:1a52bf3f0fb9 | 147 | // display sensor data on debug port |
Mike Fiore |
2:a0fb0b785bd6 | 148 | ret = temp_sensor1->get_temperature(&value1); |
Mike Fiore |
2:a0fb0b785bd6 | 149 | if (ret) { |
Mike Fiore |
2:a0fb0b785bd6 | 150 | printf("failed to get temp C\r\n"); |
Mike Fiore |
2:a0fb0b785bd6 | 151 | } |
Mike Fiore |
2:a0fb0b785bd6 | 152 | ret = humidity_sensor->get_humidity(&value2); |
Mike Fiore |
2:a0fb0b785bd6 | 153 | if (ret) { |
Mike Fiore |
2:a0fb0b785bd6 | 154 | printf("failed to get humidity\r\n"); |
Mike Fiore |
2:a0fb0b785bd6 | 155 | } |
Mike Fiore |
2:a0fb0b785bd6 | 156 | printf("HTS221: [temp] %7s°C, [hum] %s%%\r\n", printDouble(buffer1, value1), printDouble(buffer2, value2)); |
Mike Fiore |
2:a0fb0b785bd6 | 157 | |
Mike Fiore |
2:a0fb0b785bd6 | 158 | ret = temp_sensor2->get_fahrenheit(&value1); |
Mike Fiore |
2:a0fb0b785bd6 | 159 | if (ret) { |
Mike Fiore |
2:a0fb0b785bd6 | 160 | printf("failed to get temp F\r\n"); |
Mike Fiore |
2:a0fb0b785bd6 | 161 | } |
Mike Fiore |
2:a0fb0b785bd6 | 162 | ret = pressure_sensor->get_pressure(&value2); |
Mike Fiore |
2:a0fb0b785bd6 | 163 | if (ret) { |
Mike Fiore |
2:a0fb0b785bd6 | 164 | printf("failed to get pressure F\r\n"); |
Mike Fiore |
2:a0fb0b785bd6 | 165 | } |
Mike Fiore |
2:a0fb0b785bd6 | 166 | printf("LPS25H: [temp] %7s°F, [press] %smbar\r\n", printDouble(buffer1, value1), printDouble(buffer2, value2)); |
Mike Fiore |
2:a0fb0b785bd6 | 167 | |
Mike Fiore |
2:a0fb0b785bd6 | 168 | ret = magnetometer->get_m_axes(axes); |
Mike Fiore |
2:a0fb0b785bd6 | 169 | if (ret) { |
Mike Fiore |
2:a0fb0b785bd6 | 170 | printf("failed to get magnetometer\r\n"); |
Mike Fiore |
2:a0fb0b785bd6 | 171 | } |
Mike Fiore |
2:a0fb0b785bd6 | 172 | printf("LIS3MDL [mag/mgauss]: %7ld, %7ld, %7ld\r\n", axes[0], axes[1], axes[2]); |
Mike Fiore |
2:a0fb0b785bd6 | 173 | |
Mike Fiore |
2:a0fb0b785bd6 | 174 | ret = accelerometer->get_x_axes(axes); |
Mike Fiore |
2:a0fb0b785bd6 | 175 | if (ret) { |
Mike Fiore |
2:a0fb0b785bd6 | 176 | printf("failed to get accelerometer\r\n"); |
Mike Fiore |
2:a0fb0b785bd6 | 177 | } |
Mike Fiore |
2:a0fb0b785bd6 | 178 | printf("LSM6DS0 [acc/mg]: %7ld, %7ld, %7ld\r\n", axes[0], axes[1], axes[2]); |
Mike Fiore |
2:a0fb0b785bd6 | 179 | |
Mike Fiore |
2:a0fb0b785bd6 | 180 | ret = gyroscope->get_g_axes(axes); |
Mike Fiore |
2:a0fb0b785bd6 | 181 | if (ret) { |
Mike Fiore |
2:a0fb0b785bd6 | 182 | printf("failed to get gyroscope\r\n"); |
Mike Fiore |
2:a0fb0b785bd6 | 183 | } |
Mike Fiore |
2:a0fb0b785bd6 | 184 | printf("LSM6DS0 [gyro/mdps]: %7ld, %7ld, %7ld\r\n", axes[0], axes[1], axes[2]); |
Mike Fiore |
2:a0fb0b785bd6 | 185 | |
Mike Fiore |
1:a2c5a8a74527 | 186 | status = range_board->sensor_centre->GetDistance(&distance); |
mfiore | 0:06fc5a68ead2 | 187 | if (status == VL53L0X_ERROR_NONE) { |
Mike Fiore |
2:a0fb0b785bd6 | 188 | printf("Distance : %ld\n", distance); |
Mike Fiore |
2:a0fb0b785bd6 | 189 | } else { |
Mike Fiore |
2:a0fb0b785bd6 | 190 | printf("failed to get distance - possibly out of range!\r\n"); |
mfiore | 0:06fc5a68ead2 | 191 | } |
mfiore | 0:06fc5a68ead2 | 192 | |
Mike Fiore |
5:1a52bf3f0fb9 | 193 | // add temperature to packet - slot 0, sensor type 2 |
Mike Fiore |
5:1a52bf3f0fb9 | 194 | packet.addSensorValue(0, 2, (int16_t)value1); |
Mike Fiore |
5:1a52bf3f0fb9 | 195 | |
Mike Fiore |
5:1a52bf3f0fb9 | 196 | // add distance to packet - slot 1, sensor type 3 |
Mike Fiore |
5:1a52bf3f0fb9 | 197 | packet.addSensorValue(1, 3, (int16_t)distance); |
Mike Fiore |
5:1a52bf3f0fb9 | 198 | |
Mike Fiore |
5:1a52bf3f0fb9 | 199 | // add pressure to packet - slot 2, sensor type 4 |
Mike Fiore |
5:1a52bf3f0fb9 | 200 | packet.addSensorValue(2, 4, (int16_t)value2); |
Mike Fiore |
5:1a52bf3f0fb9 | 201 | |
Mike Fiore |
5:1a52bf3f0fb9 | 202 | //serialize and send the data |
Mike Fiore |
5:1a52bf3f0fb9 | 203 | packet.serialize(); |
Mike Fiore |
5:1a52bf3f0fb9 | 204 | tx_data.assign(packet.payload(), packet.payload() + packet.length()); |
Mike Fiore |
5:1a52bf3f0fb9 | 205 | |
Mike Fiore |
5:1a52bf3f0fb9 | 206 | if (dot->send(tx_data) != mDot::MDOT_OK) { |
Mike Fiore |
5:1a52bf3f0fb9 | 207 | printf("failed to send data!\r\n"); |
Mike Fiore |
5:1a52bf3f0fb9 | 208 | } |
Mike Fiore |
5:1a52bf3f0fb9 | 209 | |
Mike Fiore |
2:a0fb0b785bd6 | 210 | wait(1); |
mfiore | 0:06fc5a68ead2 | 211 | } |
mfiore | 0:06fc5a68ead2 | 212 | } |