![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Dragonfly Class in Dallas 12/11/2015. Rohm Sensor board AT&T SIM
Dependencies: MbedJSONValue mbed mtsas FXAS21002 FXOS8700
Fork of VVV_MultiTech_Dragonfly_ATT_Dallas by
main.cpp@1:a049d113e250, 2015-09-24 (annotated)
- Committer:
- mfiore
- Date:
- Thu Sep 24 18:33:02 2015 +0000
- Revision:
- 1:a049d113e250
- Parent:
- 0:a44e71488e1f
- Child:
- 2:955a63247721
single threaded implementation with SMS works
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 | 0:a44e71488e1f | 34 | * - Stack the MEMs board on top of the Base Shield |
mfiore | 0:a44e71488e1f | 35 | * - Plug in the power cable |
mfiore | 0:a44e71488e1f | 36 | * - Plug a micro USB cable into the port below and slightly to the |
mfiore | 0:a44e71488e1f | 37 | * left of the Dragonfly (NOT the port on the Dragonfly) |
mfiore | 0:a44e71488e1f | 38 | * |
mfiore | 0:a44e71488e1f | 39 | * Go have fun and make something cool! |
mfiore | 0:a44e71488e1f | 40 | * |
mfiore | 0:a44e71488e1f | 41 | ************************************************************************/ |
mfiore | 0:a44e71488e1f | 42 | |
mfiore | 0:a44e71488e1f | 43 | #include "mbed.h" |
mfiore | 0:a44e71488e1f | 44 | #include "mtsas.h" |
mfiore | 0:a44e71488e1f | 45 | #include "x_nucleo_iks01a1.h" |
mfiore | 0:a44e71488e1f | 46 | #include "MbedJSONValue.h" |
mfiore | 0:a44e71488e1f | 47 | #include <string> |
mfiore | 0:a44e71488e1f | 48 | |
mfiore | 0:a44e71488e1f | 49 | // Debug serial port |
mfiore | 0:a44e71488e1f | 50 | static Serial debug(USBTX, USBRX); |
mfiore | 0:a44e71488e1f | 51 | |
mfiore | 0:a44e71488e1f | 52 | // MTSSerialFlowControl - serial link between processor and radio |
mfiore | 1:a049d113e250 | 53 | static MTSSerialFlowControl* io; |
mfiore | 0:a44e71488e1f | 54 | |
mfiore | 0:a44e71488e1f | 55 | // Cellular - radio object for cellular operations (SMS, TCP, etc) |
mfiore | 0:a44e71488e1f | 56 | Cellular* radio; |
mfiore | 0:a44e71488e1f | 57 | |
mfiore | 0:a44e71488e1f | 58 | // APN associated with SIM card |
mfiore | 0:a44e71488e1f | 59 | //static const std::string apn = ""; |
mfiore | 0:a44e71488e1f | 60 | static const std::string apn = "b2b.tmobile.com"; |
mfiore | 0:a44e71488e1f | 61 | |
mfiore | 0:a44e71488e1f | 62 | // Phone number to send SMS messages to |
mfiore | 0:a44e71488e1f | 63 | //static const std::string phone_number = "1xxxxxxxxxx"; |
mfiore | 0:a44e71488e1f | 64 | static const std::string phone_number = "19524062053"; |
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 | 1:a049d113e250 | 73 | InterruptIn button(A1); |
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 | 0:a44e71488e1f | 89 | static int thp_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 | 0:a44e71488e1f | 119 | Timer thp_timer; |
mfiore | 0:a44e71488e1f | 120 | Timer motion_timer; |
mfiore | 1:a049d113e250 | 121 | Timer print_timer; |
mfiore | 0:a44e71488e1f | 122 | |
mfiore | 0:a44e71488e1f | 123 | thp_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 | 0:a44e71488e1f | 135 | if (thp_timer.read_ms() > thp_interval_ms) { |
mfiore | 0:a44e71488e1f | 136 | read_temperature(); |
mfiore | 0:a44e71488e1f | 137 | read_humidity(); |
mfiore | 0:a44e71488e1f | 138 | read_pressure(); |
mfiore | 0:a44e71488e1f | 139 | thp_timer.reset(); |
mfiore | 0:a44e71488e1f | 140 | } |
mfiore | 0:a44e71488e1f | 141 | |
mfiore | 0:a44e71488e1f | 142 | if (print_timer.read_ms() > print_interval_ms) { |
mfiore | 0:a44e71488e1f | 143 | logDebug("%s", wall_of_dash); |
mfiore | 0:a44e71488e1f | 144 | logDebug("SENSOR DATA"); |
mfiore | 0:a44e71488e1f | 145 | logDebug("temperature: %f C\t%f F", temp_celsius, temp_fahrenheit); |
mfiore | 0:a44e71488e1f | 146 | logDebug("humidity: %f%%", humidity_percent); |
mfiore | 0:a44e71488e1f | 147 | logDebug("pressure: %f mbar", pressure_mbar); |
mfiore | 0:a44e71488e1f | 148 | logDebug("moisture: %f%%", moisture_percent); |
mfiore | 0:a44e71488e1f | 149 | logDebug("magnetometer:\r\n\tx: %ld\ty: %ld\tz: %ld\tmgauss", mag_mgauss[0], mag_mgauss[1], mag_mgauss[2]); |
mfiore | 0:a44e71488e1f | 150 | logDebug("accelerometer:\r\n\tx: %ld\ty: %ld\tz: %ld\tmg", acc_mg[0], acc_mg[1], acc_mg[2]); |
mfiore | 0:a44e71488e1f | 151 | logDebug("gyroscope:\r\n\tx: %ld\ty: %ld\tz: %ld\tmdps", gyro_mdps[0], gyro_mdps[1], gyro_mdps[2]); |
mfiore | 0:a44e71488e1f | 152 | logDebug("%s", wall_of_dash); |
mfiore | 0:a44e71488e1f | 153 | print_timer.reset(); |
mfiore | 0:a44e71488e1f | 154 | } |
mfiore | 0:a44e71488e1f | 155 | |
mfiore | 1:a049d113e250 | 156 | if (button_pressed) { |
mfiore | 0:a44e71488e1f | 157 | logInfo("Button was pressed"); |
mfiore | 1:a049d113e250 | 158 | button_pressed = false; |
mfiore | 0:a44e71488e1f | 159 | if (radio_ok) { |
mfiore | 0:a44e71488e1f | 160 | MbedJSONValue sms_json; |
mfiore | 0:a44e71488e1f | 161 | string sms_str; |
mfiore | 0:a44e71488e1f | 162 | |
mfiore | 0:a44e71488e1f | 163 | sms_json["temp_C"] = temp_celsius; |
mfiore | 0:a44e71488e1f | 164 | sms_json["temp_F"] = temp_fahrenheit; |
mfiore | 0:a44e71488e1f | 165 | sms_json["humidity_percent"] = humidity_percent; |
mfiore | 0:a44e71488e1f | 166 | sms_json["pressure_mbar"] = pressure_mbar; |
mfiore | 0:a44e71488e1f | 167 | sms_json["moisture_percent"] = moisture_percent; |
mfiore | 0:a44e71488e1f | 168 | sms_json["mag_mgauss"]["x"] = mag_mgauss[0]; |
mfiore | 0:a44e71488e1f | 169 | sms_json["mag_mgauss"]["y"] = mag_mgauss[1]; |
mfiore | 0:a44e71488e1f | 170 | sms_json["mag_mgauss"]["z"] = mag_mgauss[2]; |
mfiore | 0:a44e71488e1f | 171 | sms_json["acc_mg"]["x"] = acc_mg[0]; |
mfiore | 0:a44e71488e1f | 172 | sms_json["acc_mg"]["y"] = acc_mg[1]; |
mfiore | 0:a44e71488e1f | 173 | sms_json["acc_mg"]["z"] = acc_mg[2]; |
mfiore | 0:a44e71488e1f | 174 | sms_json["gyro_mdps"]["x"] = gyro_mdps[0]; |
mfiore | 0:a44e71488e1f | 175 | sms_json["gyro_mdps"]["y"] = gyro_mdps[1]; |
mfiore | 0:a44e71488e1f | 176 | sms_json["gyro_mdps"]["z"] = gyro_mdps[2]; |
mfiore | 0:a44e71488e1f | 177 | |
mfiore | 0:a44e71488e1f | 178 | sms_str = "SENSOR DATA: "; |
mfiore | 0:a44e71488e1f | 179 | sms_str += sms_json.serialize(); |
mfiore | 0:a44e71488e1f | 180 | |
mfiore | 0:a44e71488e1f | 181 | logDebug("sending SMS to %s:\r\n%s", phone_number.c_str(), sms_str.c_str()); |
mfiore | 0:a44e71488e1f | 182 | Code ret = radio->sendSMS(phone_number, sms_str); |
mfiore | 0:a44e71488e1f | 183 | if (ret != MTS_SUCCESS) |
mfiore | 0:a44e71488e1f | 184 | logError("sending SMS failed"); |
mfiore | 0:a44e71488e1f | 185 | } |
mfiore | 0:a44e71488e1f | 186 | } |
mfiore | 0:a44e71488e1f | 187 | |
mfiore | 1:a049d113e250 | 188 | wait_ms(10); |
mfiore | 0:a44e71488e1f | 189 | } |
mfiore | 0:a44e71488e1f | 190 | } |
mfiore | 0:a44e71488e1f | 191 | |
mfiore | 1:a049d113e250 | 192 | // init functions |
mfiore | 1:a049d113e250 | 193 | bool init_mtsas() { |
mfiore | 1:a049d113e250 | 194 | io = new MTSSerialFlowControl(RADIO_TX, RADIO_RX, RADIO_RTS, RADIO_CTS); |
mfiore | 1:a049d113e250 | 195 | if (! io) |
mfiore | 1:a049d113e250 | 196 | return false; |
mfiore | 1:a049d113e250 | 197 | |
mfiore | 1:a049d113e250 | 198 | io->baud(115200); |
mfiore | 1:a049d113e250 | 199 | radio = CellularFactory::create(io); |
mfiore | 1:a049d113e250 | 200 | if (! radio) |
mfiore | 1:a049d113e250 | 201 | return false; |
mfiore | 1:a049d113e250 | 202 | |
mfiore | 1:a049d113e250 | 203 | Code ret = radio->setApn(apn); |
mfiore | 1:a049d113e250 | 204 | if (ret != MTS_SUCCESS) |
mfiore | 1:a049d113e250 | 205 | return false; |
mfiore | 1:a049d113e250 | 206 | |
mfiore | 1:a049d113e250 | 207 | return true; |
mfiore | 1:a049d113e250 | 208 | } |
mfiore | 1:a049d113e250 | 209 | |
mfiore | 0:a44e71488e1f | 210 | // Sensor data acquisition functions |
mfiore | 0:a44e71488e1f | 211 | void read_temperature() { |
mfiore | 0:a44e71488e1f | 212 | int ret; |
mfiore | 0:a44e71488e1f | 213 | |
mfiore | 0:a44e71488e1f | 214 | ret = mems->ht_sensor->GetTemperature(&temp_celsius); |
mfiore | 0:a44e71488e1f | 215 | if (ret) |
mfiore | 0:a44e71488e1f | 216 | logError("reading temp (C) failed"); |
mfiore | 0:a44e71488e1f | 217 | |
mfiore | 0:a44e71488e1f | 218 | ret = mems->ht_sensor->GetFahrenheit(&temp_fahrenheit); |
mfiore | 0:a44e71488e1f | 219 | if (ret) |
mfiore | 0:a44e71488e1f | 220 | logError("reading temp (F) failed"); |
mfiore | 0:a44e71488e1f | 221 | } |
mfiore | 0:a44e71488e1f | 222 | |
mfiore | 0:a44e71488e1f | 223 | void read_humidity() { |
mfiore | 0:a44e71488e1f | 224 | int ret; |
mfiore | 0:a44e71488e1f | 225 | |
mfiore | 0:a44e71488e1f | 226 | ret = mems->ht_sensor->GetHumidity(&humidity_percent); |
mfiore | 0:a44e71488e1f | 227 | if (ret) |
mfiore | 0:a44e71488e1f | 228 | logError("reading humidity failed"); |
mfiore | 0:a44e71488e1f | 229 | } |
mfiore | 0:a44e71488e1f | 230 | |
mfiore | 0:a44e71488e1f | 231 | void read_pressure() { |
mfiore | 0:a44e71488e1f | 232 | int ret; |
mfiore | 0:a44e71488e1f | 233 | |
mfiore | 0:a44e71488e1f | 234 | ret = mems->pt_sensor->GetPressure(&pressure_mbar); |
mfiore | 0:a44e71488e1f | 235 | if (ret) |
mfiore | 0:a44e71488e1f | 236 | logError("reading pressure failed"); |
mfiore | 0:a44e71488e1f | 237 | } |
mfiore | 0:a44e71488e1f | 238 | |
mfiore | 0:a44e71488e1f | 239 | void read_moisture() { |
mfiore | 0:a44e71488e1f | 240 | moisture_percent = moisture_sensor; |
mfiore | 0:a44e71488e1f | 241 | } |
mfiore | 0:a44e71488e1f | 242 | |
mfiore | 0:a44e71488e1f | 243 | void read_magnetometer() { |
mfiore | 0:a44e71488e1f | 244 | int ret; |
mfiore | 0:a44e71488e1f | 245 | |
mfiore | 0:a44e71488e1f | 246 | ret = mems->magnetometer->Get_M_Axes(mag_mgauss); |
mfiore | 0:a44e71488e1f | 247 | if (ret) |
mfiore | 0:a44e71488e1f | 248 | logError("reading magnetometer failed"); |
mfiore | 0:a44e71488e1f | 249 | } |
mfiore | 0:a44e71488e1f | 250 | |
mfiore | 0:a44e71488e1f | 251 | void read_accelerometer() { |
mfiore | 0:a44e71488e1f | 252 | int ret; |
mfiore | 0:a44e71488e1f | 253 | |
mfiore | 0:a44e71488e1f | 254 | ret = mems->GetAccelerometer()->Get_X_Axes(acc_mg); |
mfiore | 0:a44e71488e1f | 255 | if (ret) |
mfiore | 0:a44e71488e1f | 256 | logError("reading accelerometer failed"); |
mfiore | 0:a44e71488e1f | 257 | } |
mfiore | 0:a44e71488e1f | 258 | |
mfiore | 0:a44e71488e1f | 259 | void read_gyroscope() { |
mfiore | 0:a44e71488e1f | 260 | int ret; |
mfiore | 0:a44e71488e1f | 261 | |
mfiore | 0:a44e71488e1f | 262 | ret = mems->GetGyroscope()->Get_G_Axes(gyro_mdps); |
mfiore | 0:a44e71488e1f | 263 | if (ret) |
mfiore | 0:a44e71488e1f | 264 | logError("reading gyroscope failed"); |
mfiore | 0:a44e71488e1f | 265 | } |
mfiore | 1:a049d113e250 | 266 | |
mfiore | 1:a049d113e250 | 267 | void button_irq() { |
mfiore | 1:a049d113e250 | 268 | button_pressed = true; |
mfiore | 1:a049d113e250 | 269 | } |