Rev 1.0 4/26/2016 Paul Jaeger - Multitech, Brian Huey - Sprint Changed post interval to 2000ms added temp, analoguv and pressure to http post added alias: TEMP ANALOG-UV PRESSURE concatenated http post, to post all within the same routine and check for error after the post confirmed that data is published to Exosite
Dependencies: MbedJSONValue mbed mtsas
Fork of UUU_MultiTech_Dragonfly_Sprint by
main.cpp@3:f6bceb9e5e1a, 2015-09-24 (annotated)
- Committer:
- mfiore
- Date:
- Thu Sep 24 19:35:46 2015 +0000
- Revision:
- 3:f6bceb9e5e1a
- Parent:
- 2:955a63247721
- Child:
- 4:730b61258422
forgot to read moisture sensor & fix moisture reading & add note about analog reference voltage
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mfiore | 0:a44e71488e1f | 1 | /************************************************************************* |
mfiore | 0:a44e71488e1f | 2 | * Dragonfly Example program for 2015 AT&T Government Solutions Hackathon |
mfiore | 0:a44e71488e1f | 3 | * |
mfiore | 0:a44e71488e1f | 4 | * The following hardware is required to successfully run this program: |
mfiore | 0:a44e71488e1f | 5 | * - MultiTech UDK2 (4" square white PCB with Arduino headers, antenna |
mfiore | 0:a44e71488e1f | 6 | * connector, micro USB ports, and 40-pin connector for Dragonfly) |
mfiore | 0:a44e71488e1f | 7 | * - MultiTech Dragonfly (1"x2" green PCB with Telit radio) |
mfiore | 0:a44e71488e1f | 8 | * - Seeed Studio Base Shield |
mfiore | 0:a44e71488e1f | 9 | * - Grove moisture sensor (to connect to Base Shield) |
mfiore | 0:a44e71488e1f | 10 | * - Grove button (to connect to Base Shield) |
mfiore | 0:a44e71488e1f | 11 | * - MEMs Inertial and Environmental Nucleo Expansion board (LSM6DS0 |
mfiore | 0:a44e71488e1f | 12 | * 3-axis accelerometer + 3-axis gyroscope, LIS3MDL 3-axis |
mfiore | 0:a44e71488e1f | 13 | * magnetometer, HTS221 humidity and temperature sensor and LPS25HB |
mfiore | 0:a44e71488e1f | 14 | * pressure sensor) |
mfiore | 0:a44e71488e1f | 15 | * |
mfiore | 0:a44e71488e1f | 16 | * What this program does: |
mfiore | 0:a44e71488e1f | 17 | * - reads data from all sensors on MEMs board and moisture sensor on a |
mfiore | 0:a44e71488e1f | 18 | * periodic basis |
mfiore | 0:a44e71488e1f | 19 | * - prints all sensor data to debug port on a periodic basis |
mfiore | 0:a44e71488e1f | 20 | * - optionally send a SMS containing sensor data when the Grove Button |
mfiore | 0:a44e71488e1f | 21 | * is pushed (phone number must be configured) |
mfiore | 0:a44e71488e1f | 22 | * - optionally send sensor data to AT&T M2X cloud platform (user must |
mfiore | 0:a44e71488e1f | 23 | * create own M2X account and configure a device) if sensor data |
mfiore | 0:a44e71488e1f | 24 | * crosses established thresholds |
mfiore | 0:a44e71488e1f | 25 | * |
mfiore | 0:a44e71488e1f | 26 | * Setup: |
mfiore | 0:a44e71488e1f | 27 | * - Correctly insert SIM card into Dragonfly |
mfiore | 0:a44e71488e1f | 28 | * - Seat the Dragonfly on the UDK2 board |
mfiore | 0:a44e71488e1f | 29 | * - Connect an antenna to the connector on the Dragonfly labled "M" |
mfiore | 0:a44e71488e1f | 30 | * - Stack the Base Shield on the UDK2 Arduino headers |
mfiore | 0:a44e71488e1f | 31 | * - Connect the Grove button to the D8 socket on the Base Shield |
mfiore | 0:a44e71488e1f | 32 | * - Connect the Grove moisture sensor to the A0 socket on the Base |
mfiore | 0:a44e71488e1f | 33 | * Shield |
mfiore | 3:f6bceb9e5e1a | 34 | * - Make sure the reference voltage selector switch (next to the A0 |
mfiore | 3:f6bceb9e5e1a | 35 | * socket) is switched to 5V so you get accurate analog readings |
mfiore | 0:a44e71488e1f | 36 | * - Stack the MEMs board on top of the Base Shield |
mfiore | 0:a44e71488e1f | 37 | * - Plug in the power cable |
mfiore | 0:a44e71488e1f | 38 | * - Plug a micro USB cable into the port below and slightly to the |
mfiore | 0:a44e71488e1f | 39 | * left of the Dragonfly (NOT the port on the Dragonfly) |
mfiore | 0:a44e71488e1f | 40 | * |
mfiore | 0:a44e71488e1f | 41 | * Go have fun and make something cool! |
mfiore | 0:a44e71488e1f | 42 | * |
mfiore | 0:a44e71488e1f | 43 | ************************************************************************/ |
mfiore | 0:a44e71488e1f | 44 | |
mfiore | 0:a44e71488e1f | 45 | #include "mbed.h" |
mfiore | 0:a44e71488e1f | 46 | #include "mtsas.h" |
mfiore | 0:a44e71488e1f | 47 | #include "x_nucleo_iks01a1.h" |
mfiore | 0:a44e71488e1f | 48 | #include "MbedJSONValue.h" |
mfiore | 0:a44e71488e1f | 49 | #include <string> |
mfiore | 0:a44e71488e1f | 50 | |
mfiore | 0:a44e71488e1f | 51 | // Debug serial port |
mfiore | 0:a44e71488e1f | 52 | static Serial debug(USBTX, USBRX); |
mfiore | 0:a44e71488e1f | 53 | |
mfiore | 0:a44e71488e1f | 54 | // MTSSerialFlowControl - serial link between processor and radio |
mfiore | 1:a049d113e250 | 55 | static MTSSerialFlowControl* io; |
mfiore | 0:a44e71488e1f | 56 | |
mfiore | 0:a44e71488e1f | 57 | // Cellular - radio object for cellular operations (SMS, TCP, etc) |
mfiore | 0:a44e71488e1f | 58 | Cellular* radio; |
mfiore | 0:a44e71488e1f | 59 | |
mfiore | 0:a44e71488e1f | 60 | // APN associated with SIM card |
mfiore | 2:955a63247721 | 61 | static const std::string apn = ""; |
mfiore | 0:a44e71488e1f | 62 | |
mfiore | 0:a44e71488e1f | 63 | // Phone number to send SMS messages to |
mfiore | 2:955a63247721 | 64 | static const std::string phone_number = "1xxxxxxxxxx"; |
mfiore | 0:a44e71488e1f | 65 | |
mfiore | 0:a44e71488e1f | 66 | // handle to MEMs board object |
mfiore | 0:a44e71488e1f | 67 | static X_NUCLEO_IKS01A1* mems = X_NUCLEO_IKS01A1::Instance(); |
mfiore | 0:a44e71488e1f | 68 | |
mfiore | 0:a44e71488e1f | 69 | // Moisture sensor |
mfiore | 0:a44e71488e1f | 70 | AnalogIn moisture_sensor(A0); |
mfiore | 0:a44e71488e1f | 71 | |
mfiore | 0:a44e71488e1f | 72 | // Button |
mfiore | 2:955a63247721 | 73 | InterruptIn button(D8); |
mfiore | 1:a049d113e250 | 74 | bool button_pressed = false; |
mfiore | 0:a44e71488e1f | 75 | |
mfiore | 0:a44e71488e1f | 76 | // variables for sensor data |
mfiore | 0:a44e71488e1f | 77 | float temp_celsius; |
mfiore | 0:a44e71488e1f | 78 | float temp_fahrenheit; |
mfiore | 0:a44e71488e1f | 79 | float humidity_percent; |
mfiore | 0:a44e71488e1f | 80 | float pressure_mbar; |
mfiore | 0:a44e71488e1f | 81 | float moisture_percent; |
mfiore | 0:a44e71488e1f | 82 | int32_t mag_mgauss[3]; |
mfiore | 0:a44e71488e1f | 83 | int32_t acc_mg[3]; |
mfiore | 0:a44e71488e1f | 84 | int32_t gyro_mdps[3]; |
mfiore | 0:a44e71488e1f | 85 | |
mfiore | 0:a44e71488e1f | 86 | // misc variables |
mfiore | 0:a44e71488e1f | 87 | static char wall_of_dash[] = "--------------------------------------------------"; |
mfiore | 0:a44e71488e1f | 88 | bool radio_ok = false; |
mfiore | 3:f6bceb9e5e1a | 89 | static int thpm_interval_ms = 2000; |
mfiore | 0:a44e71488e1f | 90 | static int motion_interval_ms = 250; |
mfiore | 0:a44e71488e1f | 91 | static int print_interval_ms = 5000; |
mfiore | 0:a44e71488e1f | 92 | int debug_baud = 115200; |
mfiore | 0:a44e71488e1f | 93 | |
mfiore | 0:a44e71488e1f | 94 | // function prototypes |
mfiore | 0:a44e71488e1f | 95 | bool init_mtsas(); |
mfiore | 0:a44e71488e1f | 96 | void read_temperature(); |
mfiore | 0:a44e71488e1f | 97 | void read_humidity(); |
mfiore | 0:a44e71488e1f | 98 | void read_pressure(); |
mfiore | 0:a44e71488e1f | 99 | void read_moisture(); |
mfiore | 0:a44e71488e1f | 100 | void read_magnetometer(); |
mfiore | 0:a44e71488e1f | 101 | void read_accelerometer(); |
mfiore | 0:a44e71488e1f | 102 | void read_gyroscope(); |
mfiore | 0:a44e71488e1f | 103 | void button_irq(); |
mfiore | 0:a44e71488e1f | 104 | |
mfiore | 0:a44e71488e1f | 105 | // main |
mfiore | 0:a44e71488e1f | 106 | int main() { |
mfiore | 0:a44e71488e1f | 107 | mts::MTSLog::setLogLevel(mts::MTSLog::TRACE_LEVEL); |
mfiore | 0:a44e71488e1f | 108 | debug.baud(debug_baud); |
mfiore | 0:a44e71488e1f | 109 | logInfo("starting..."); |
mfiore | 0:a44e71488e1f | 110 | |
mfiore | 0:a44e71488e1f | 111 | radio_ok = init_mtsas(); |
mfiore | 0:a44e71488e1f | 112 | if (! radio_ok) |
mfiore | 0:a44e71488e1f | 113 | logError("MTSAS init failed"); |
mfiore | 0:a44e71488e1f | 114 | else |
mfiore | 0:a44e71488e1f | 115 | logInfo("MTSAS is ok"); |
mfiore | 0:a44e71488e1f | 116 | |
mfiore | 1:a049d113e250 | 117 | button.fall(&button_irq); |
mfiore | 0:a44e71488e1f | 118 | |
mfiore | 3:f6bceb9e5e1a | 119 | Timer thpm_timer; |
mfiore | 0:a44e71488e1f | 120 | Timer motion_timer; |
mfiore | 1:a049d113e250 | 121 | Timer print_timer; |
mfiore | 0:a44e71488e1f | 122 | |
mfiore | 3:f6bceb9e5e1a | 123 | thpm_timer.start(); |
mfiore | 0:a44e71488e1f | 124 | motion_timer.start(); |
mfiore | 1:a049d113e250 | 125 | print_timer.start(); |
mfiore | 1:a049d113e250 | 126 | |
mfiore | 0:a44e71488e1f | 127 | while (true) { |
mfiore | 0:a44e71488e1f | 128 | if (motion_timer.read_ms() > motion_interval_ms) { |
mfiore | 0:a44e71488e1f | 129 | read_magnetometer(); |
mfiore | 0:a44e71488e1f | 130 | read_accelerometer(); |
mfiore | 0:a44e71488e1f | 131 | read_gyroscope(); |
mfiore | 0:a44e71488e1f | 132 | motion_timer.reset(); |
mfiore | 0:a44e71488e1f | 133 | } |
mfiore | 0:a44e71488e1f | 134 | |
mfiore | 3:f6bceb9e5e1a | 135 | if (thpm_timer.read_ms() > thpm_interval_ms) { |
mfiore | 0:a44e71488e1f | 136 | read_temperature(); |
mfiore | 0:a44e71488e1f | 137 | read_humidity(); |
mfiore | 0:a44e71488e1f | 138 | read_pressure(); |
mfiore | 3:f6bceb9e5e1a | 139 | read_moisture(); |
mfiore | 3:f6bceb9e5e1a | 140 | thpm_timer.reset(); |
mfiore | 0:a44e71488e1f | 141 | } |
mfiore | 0:a44e71488e1f | 142 | |
mfiore | 0:a44e71488e1f | 143 | if (print_timer.read_ms() > print_interval_ms) { |
mfiore | 0:a44e71488e1f | 144 | logDebug("%s", wall_of_dash); |
mfiore | 0:a44e71488e1f | 145 | logDebug("SENSOR DATA"); |
mfiore | 0:a44e71488e1f | 146 | logDebug("temperature: %f C\t%f F", temp_celsius, temp_fahrenheit); |
mfiore | 0:a44e71488e1f | 147 | logDebug("humidity: %f%%", humidity_percent); |
mfiore | 0:a44e71488e1f | 148 | logDebug("pressure: %f mbar", pressure_mbar); |
mfiore | 0:a44e71488e1f | 149 | logDebug("moisture: %f%%", moisture_percent); |
mfiore | 0:a44e71488e1f | 150 | logDebug("magnetometer:\r\n\tx: %ld\ty: %ld\tz: %ld\tmgauss", mag_mgauss[0], mag_mgauss[1], mag_mgauss[2]); |
mfiore | 0:a44e71488e1f | 151 | logDebug("accelerometer:\r\n\tx: %ld\ty: %ld\tz: %ld\tmg", acc_mg[0], acc_mg[1], acc_mg[2]); |
mfiore | 0:a44e71488e1f | 152 | logDebug("gyroscope:\r\n\tx: %ld\ty: %ld\tz: %ld\tmdps", gyro_mdps[0], gyro_mdps[1], gyro_mdps[2]); |
mfiore | 0:a44e71488e1f | 153 | logDebug("%s", wall_of_dash); |
mfiore | 0:a44e71488e1f | 154 | print_timer.reset(); |
mfiore | 0:a44e71488e1f | 155 | } |
mfiore | 0:a44e71488e1f | 156 | |
mfiore | 1:a049d113e250 | 157 | if (button_pressed) { |
mfiore | 0:a44e71488e1f | 158 | logInfo("Button was pressed"); |
mfiore | 1:a049d113e250 | 159 | button_pressed = false; |
mfiore | 0:a44e71488e1f | 160 | if (radio_ok) { |
mfiore | 0:a44e71488e1f | 161 | MbedJSONValue sms_json; |
mfiore | 0:a44e71488e1f | 162 | string sms_str; |
mfiore | 0:a44e71488e1f | 163 | |
mfiore | 0:a44e71488e1f | 164 | sms_json["temp_C"] = temp_celsius; |
mfiore | 0:a44e71488e1f | 165 | sms_json["temp_F"] = temp_fahrenheit; |
mfiore | 0:a44e71488e1f | 166 | sms_json["humidity_percent"] = humidity_percent; |
mfiore | 0:a44e71488e1f | 167 | sms_json["pressure_mbar"] = pressure_mbar; |
mfiore | 0:a44e71488e1f | 168 | sms_json["moisture_percent"] = moisture_percent; |
mfiore | 0:a44e71488e1f | 169 | sms_json["mag_mgauss"]["x"] = mag_mgauss[0]; |
mfiore | 0:a44e71488e1f | 170 | sms_json["mag_mgauss"]["y"] = mag_mgauss[1]; |
mfiore | 0:a44e71488e1f | 171 | sms_json["mag_mgauss"]["z"] = mag_mgauss[2]; |
mfiore | 0:a44e71488e1f | 172 | sms_json["acc_mg"]["x"] = acc_mg[0]; |
mfiore | 0:a44e71488e1f | 173 | sms_json["acc_mg"]["y"] = acc_mg[1]; |
mfiore | 0:a44e71488e1f | 174 | sms_json["acc_mg"]["z"] = acc_mg[2]; |
mfiore | 0:a44e71488e1f | 175 | sms_json["gyro_mdps"]["x"] = gyro_mdps[0]; |
mfiore | 0:a44e71488e1f | 176 | sms_json["gyro_mdps"]["y"] = gyro_mdps[1]; |
mfiore | 0:a44e71488e1f | 177 | sms_json["gyro_mdps"]["z"] = gyro_mdps[2]; |
mfiore | 0:a44e71488e1f | 178 | |
mfiore | 2:955a63247721 | 179 | sms_str = "SENSOR DATA:\n"; |
mfiore | 0:a44e71488e1f | 180 | sms_str += sms_json.serialize(); |
mfiore | 0:a44e71488e1f | 181 | |
mfiore | 0:a44e71488e1f | 182 | logDebug("sending SMS to %s:\r\n%s", phone_number.c_str(), sms_str.c_str()); |
mfiore | 0:a44e71488e1f | 183 | Code ret = radio->sendSMS(phone_number, sms_str); |
mfiore | 0:a44e71488e1f | 184 | if (ret != MTS_SUCCESS) |
mfiore | 0:a44e71488e1f | 185 | logError("sending SMS failed"); |
mfiore | 0:a44e71488e1f | 186 | } |
mfiore | 0:a44e71488e1f | 187 | } |
mfiore | 0:a44e71488e1f | 188 | |
mfiore | 1:a049d113e250 | 189 | wait_ms(10); |
mfiore | 0:a44e71488e1f | 190 | } |
mfiore | 0:a44e71488e1f | 191 | } |
mfiore | 0:a44e71488e1f | 192 | |
mfiore | 1:a049d113e250 | 193 | // init functions |
mfiore | 1:a049d113e250 | 194 | bool init_mtsas() { |
mfiore | 1:a049d113e250 | 195 | io = new MTSSerialFlowControl(RADIO_TX, RADIO_RX, RADIO_RTS, RADIO_CTS); |
mfiore | 1:a049d113e250 | 196 | if (! io) |
mfiore | 1:a049d113e250 | 197 | return false; |
mfiore | 1:a049d113e250 | 198 | |
mfiore | 1:a049d113e250 | 199 | io->baud(115200); |
mfiore | 1:a049d113e250 | 200 | radio = CellularFactory::create(io); |
mfiore | 1:a049d113e250 | 201 | if (! radio) |
mfiore | 1:a049d113e250 | 202 | return false; |
mfiore | 1:a049d113e250 | 203 | |
mfiore | 1:a049d113e250 | 204 | Code ret = radio->setApn(apn); |
mfiore | 1:a049d113e250 | 205 | if (ret != MTS_SUCCESS) |
mfiore | 1:a049d113e250 | 206 | return false; |
mfiore | 1:a049d113e250 | 207 | |
mfiore | 1:a049d113e250 | 208 | return true; |
mfiore | 1:a049d113e250 | 209 | } |
mfiore | 1:a049d113e250 | 210 | |
mfiore | 0:a44e71488e1f | 211 | // Sensor data acquisition functions |
mfiore | 0:a44e71488e1f | 212 | void read_temperature() { |
mfiore | 0:a44e71488e1f | 213 | int ret; |
mfiore | 0:a44e71488e1f | 214 | |
mfiore | 0:a44e71488e1f | 215 | ret = mems->ht_sensor->GetTemperature(&temp_celsius); |
mfiore | 0:a44e71488e1f | 216 | if (ret) |
mfiore | 0:a44e71488e1f | 217 | logError("reading temp (C) failed"); |
mfiore | 0:a44e71488e1f | 218 | |
mfiore | 0:a44e71488e1f | 219 | ret = mems->ht_sensor->GetFahrenheit(&temp_fahrenheit); |
mfiore | 0:a44e71488e1f | 220 | if (ret) |
mfiore | 0:a44e71488e1f | 221 | logError("reading temp (F) failed"); |
mfiore | 0:a44e71488e1f | 222 | } |
mfiore | 0:a44e71488e1f | 223 | |
mfiore | 0:a44e71488e1f | 224 | void read_humidity() { |
mfiore | 0:a44e71488e1f | 225 | int ret; |
mfiore | 0:a44e71488e1f | 226 | |
mfiore | 0:a44e71488e1f | 227 | ret = mems->ht_sensor->GetHumidity(&humidity_percent); |
mfiore | 0:a44e71488e1f | 228 | if (ret) |
mfiore | 0:a44e71488e1f | 229 | logError("reading humidity failed"); |
mfiore | 0:a44e71488e1f | 230 | } |
mfiore | 0:a44e71488e1f | 231 | |
mfiore | 0:a44e71488e1f | 232 | void read_pressure() { |
mfiore | 0:a44e71488e1f | 233 | int ret; |
mfiore | 0:a44e71488e1f | 234 | |
mfiore | 0:a44e71488e1f | 235 | ret = mems->pt_sensor->GetPressure(&pressure_mbar); |
mfiore | 0:a44e71488e1f | 236 | if (ret) |
mfiore | 0:a44e71488e1f | 237 | logError("reading pressure failed"); |
mfiore | 0:a44e71488e1f | 238 | } |
mfiore | 0:a44e71488e1f | 239 | |
mfiore | 0:a44e71488e1f | 240 | void read_moisture() { |
mfiore | 3:f6bceb9e5e1a | 241 | moisture_percent = moisture_sensor * 100.0; |
mfiore | 0:a44e71488e1f | 242 | } |
mfiore | 0:a44e71488e1f | 243 | |
mfiore | 0:a44e71488e1f | 244 | void read_magnetometer() { |
mfiore | 0:a44e71488e1f | 245 | int ret; |
mfiore | 0:a44e71488e1f | 246 | |
mfiore | 0:a44e71488e1f | 247 | ret = mems->magnetometer->Get_M_Axes(mag_mgauss); |
mfiore | 0:a44e71488e1f | 248 | if (ret) |
mfiore | 0:a44e71488e1f | 249 | logError("reading magnetometer failed"); |
mfiore | 0:a44e71488e1f | 250 | } |
mfiore | 0:a44e71488e1f | 251 | |
mfiore | 0:a44e71488e1f | 252 | void read_accelerometer() { |
mfiore | 0:a44e71488e1f | 253 | int ret; |
mfiore | 0:a44e71488e1f | 254 | |
mfiore | 0:a44e71488e1f | 255 | ret = mems->GetAccelerometer()->Get_X_Axes(acc_mg); |
mfiore | 0:a44e71488e1f | 256 | if (ret) |
mfiore | 0:a44e71488e1f | 257 | logError("reading accelerometer failed"); |
mfiore | 0:a44e71488e1f | 258 | } |
mfiore | 0:a44e71488e1f | 259 | |
mfiore | 0:a44e71488e1f | 260 | void read_gyroscope() { |
mfiore | 0:a44e71488e1f | 261 | int ret; |
mfiore | 0:a44e71488e1f | 262 | |
mfiore | 0:a44e71488e1f | 263 | ret = mems->GetGyroscope()->Get_G_Axes(gyro_mdps); |
mfiore | 0:a44e71488e1f | 264 | if (ret) |
mfiore | 0:a44e71488e1f | 265 | logError("reading gyroscope failed"); |
mfiore | 0:a44e71488e1f | 266 | } |
mfiore | 1:a049d113e250 | 267 | |
mfiore | 1:a049d113e250 | 268 | void button_irq() { |
mfiore | 1:a049d113e250 | 269 | button_pressed = true; |
mfiore | 1:a049d113e250 | 270 | } |