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 Paul Jaeger

Committer:
mfiore
Date:
Fri Sep 25 21:16:02 2015 +0000
Revision:
4:730b61258422
Parent:
3:f6bceb9e5e1a
Child:
5:a946ef74a8c4
start cloud post

Who changed what in which revision?

UserRevisionLine numberNew 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 4:730b61258422 22 * - optionally sends sensor data to AT&T M2X cloud platform (user must
mfiore 4:730b61258422 23 * create own M2X account and configure a device)
mfiore 0:a44e71488e1f 24 *
mfiore 0:a44e71488e1f 25 * Setup:
mfiore 0:a44e71488e1f 26 * - Correctly insert SIM card into Dragonfly
mfiore 0:a44e71488e1f 27 * - Seat the Dragonfly on the UDK2 board
mfiore 0:a44e71488e1f 28 * - Connect an antenna to the connector on the Dragonfly labled "M"
mfiore 0:a44e71488e1f 29 * - Stack the Base Shield on the UDK2 Arduino headers
mfiore 0:a44e71488e1f 30 * - Connect the Grove button to the D8 socket on the Base Shield
mfiore 0:a44e71488e1f 31 * - Connect the Grove moisture sensor to the A0 socket on the Base
mfiore 0:a44e71488e1f 32 * Shield
mfiore 3:f6bceb9e5e1a 33 * - Make sure the reference voltage selector switch (next to the A0
mfiore 3:f6bceb9e5e1a 34 * socket) is switched to 5V so you get accurate analog readings
mfiore 0:a44e71488e1f 35 * - Stack the MEMs board on top of the Base Shield
mfiore 0:a44e71488e1f 36 * - Plug in the power cable
mfiore 0:a44e71488e1f 37 * - Plug a micro USB cable into the port below and slightly to the
mfiore 0:a44e71488e1f 38 * left of the Dragonfly (NOT the port on the Dragonfly)
mfiore 0:a44e71488e1f 39 *
mfiore 0:a44e71488e1f 40 * Go have fun and make something cool!
mfiore 0:a44e71488e1f 41 *
mfiore 0:a44e71488e1f 42 ************************************************************************/
mfiore 0:a44e71488e1f 43
mfiore 0:a44e71488e1f 44 #include "mbed.h"
mfiore 0:a44e71488e1f 45 #include "mtsas.h"
mfiore 0:a44e71488e1f 46 #include "x_nucleo_iks01a1.h"
mfiore 0:a44e71488e1f 47 #include "MbedJSONValue.h"
mfiore 0:a44e71488e1f 48 #include <string>
mfiore 0:a44e71488e1f 49
mfiore 0:a44e71488e1f 50 // Debug serial port
mfiore 0:a44e71488e1f 51 static Serial debug(USBTX, USBRX);
mfiore 0:a44e71488e1f 52
mfiore 0:a44e71488e1f 53 // MTSSerialFlowControl - serial link between processor and radio
mfiore 1:a049d113e250 54 static MTSSerialFlowControl* io;
mfiore 0:a44e71488e1f 55
mfiore 0:a44e71488e1f 56 // Cellular - radio object for cellular operations (SMS, TCP, etc)
mfiore 0:a44e71488e1f 57 Cellular* radio;
mfiore 0:a44e71488e1f 58
mfiore 0:a44e71488e1f 59 // APN associated with SIM card
mfiore 4:730b61258422 60 // this APN should work for the AT&T SIM that came with your Dragonfly
mfiore 4:730b61258422 61 //static const std::string apn = "";
mfiore 4:730b61258422 62 static const std::string apn = "broadband";
mfiore 0:a44e71488e1f 63
mfiore 0:a44e71488e1f 64 // Phone number to send SMS messages to
mfiore 2:955a63247721 65 static const std::string phone_number = "1xxxxxxxxxx";
mfiore 0:a44e71488e1f 66
mfiore 4:730b61258422 67 // M2X variables
mfiore 4:730b61258422 68 static const std::string m2x_device_id = "";
mfiore 4:730b61258422 69 static const std::string m2x_key = "";
mfiore 4:730b61258422 70 bool do_cloud_post = false;
mfiore 4:730b61258422 71
mfiore 0:a44e71488e1f 72 // handle to MEMs board object
mfiore 0:a44e71488e1f 73 static X_NUCLEO_IKS01A1* mems = X_NUCLEO_IKS01A1::Instance();
mfiore 0:a44e71488e1f 74
mfiore 0:a44e71488e1f 75 // Moisture sensor
mfiore 0:a44e71488e1f 76 AnalogIn moisture_sensor(A0);
mfiore 0:a44e71488e1f 77
mfiore 0:a44e71488e1f 78 // Button
mfiore 2:955a63247721 79 InterruptIn button(D8);
mfiore 1:a049d113e250 80 bool button_pressed = false;
mfiore 0:a44e71488e1f 81
mfiore 0:a44e71488e1f 82 // variables for sensor data
mfiore 0:a44e71488e1f 83 float temp_celsius;
mfiore 0:a44e71488e1f 84 float temp_fahrenheit;
mfiore 0:a44e71488e1f 85 float humidity_percent;
mfiore 0:a44e71488e1f 86 float pressure_mbar;
mfiore 0:a44e71488e1f 87 float moisture_percent;
mfiore 0:a44e71488e1f 88 int32_t mag_mgauss[3];
mfiore 0:a44e71488e1f 89 int32_t acc_mg[3];
mfiore 0:a44e71488e1f 90 int32_t gyro_mdps[3];
mfiore 0:a44e71488e1f 91
mfiore 4:730b61258422 92 // http variables
mfiore 4:730b61258422 93 HTTPClient* http;
mfiore 4:730b61258422 94 HTTPJson* http_json;
mfiore 4:730b61258422 95
mfiore 0:a44e71488e1f 96 // misc variables
mfiore 0:a44e71488e1f 97 static char wall_of_dash[] = "--------------------------------------------------";
mfiore 0:a44e71488e1f 98 bool radio_ok = false;
mfiore 3:f6bceb9e5e1a 99 static int thpm_interval_ms = 2000;
mfiore 0:a44e71488e1f 100 static int motion_interval_ms = 250;
mfiore 0:a44e71488e1f 101 static int print_interval_ms = 5000;
mfiore 0:a44e71488e1f 102 int debug_baud = 115200;
mfiore 0:a44e71488e1f 103
mfiore 0:a44e71488e1f 104 // function prototypes
mfiore 0:a44e71488e1f 105 bool init_mtsas();
mfiore 0:a44e71488e1f 106 void read_temperature();
mfiore 0:a44e71488e1f 107 void read_humidity();
mfiore 0:a44e71488e1f 108 void read_pressure();
mfiore 0:a44e71488e1f 109 void read_moisture();
mfiore 0:a44e71488e1f 110 void read_magnetometer();
mfiore 0:a44e71488e1f 111 void read_accelerometer();
mfiore 0:a44e71488e1f 112 void read_gyroscope();
mfiore 0:a44e71488e1f 113 void button_irq();
mfiore 0:a44e71488e1f 114
mfiore 0:a44e71488e1f 115 // main
mfiore 0:a44e71488e1f 116 int main() {
mfiore 0:a44e71488e1f 117 mts::MTSLog::setLogLevel(mts::MTSLog::TRACE_LEVEL);
mfiore 0:a44e71488e1f 118 debug.baud(debug_baud);
mfiore 0:a44e71488e1f 119 logInfo("starting...");
mfiore 0:a44e71488e1f 120
mfiore 0:a44e71488e1f 121 radio_ok = init_mtsas();
mfiore 0:a44e71488e1f 122 if (! radio_ok)
mfiore 0:a44e71488e1f 123 logError("MTSAS init failed");
mfiore 0:a44e71488e1f 124 else
mfiore 0:a44e71488e1f 125 logInfo("MTSAS is ok");
mfiore 0:a44e71488e1f 126
mfiore 1:a049d113e250 127 button.fall(&button_irq);
mfiore 0:a44e71488e1f 128
mfiore 3:f6bceb9e5e1a 129 Timer thpm_timer;
mfiore 0:a44e71488e1f 130 Timer motion_timer;
mfiore 1:a049d113e250 131 Timer print_timer;
mfiore 4:730b61258422 132 Timer post_timer;
mfiore 0:a44e71488e1f 133
mfiore 3:f6bceb9e5e1a 134 thpm_timer.start();
mfiore 0:a44e71488e1f 135 motion_timer.start();
mfiore 1:a049d113e250 136 print_timer.start();
mfiore 4:730b61258422 137 post_timer.start();
mfiore 1:a049d113e250 138
mfiore 0:a44e71488e1f 139 while (true) {
mfiore 0:a44e71488e1f 140 if (motion_timer.read_ms() > motion_interval_ms) {
mfiore 0:a44e71488e1f 141 read_magnetometer();
mfiore 0:a44e71488e1f 142 read_accelerometer();
mfiore 0:a44e71488e1f 143 read_gyroscope();
mfiore 0:a44e71488e1f 144 motion_timer.reset();
mfiore 0:a44e71488e1f 145 }
mfiore 0:a44e71488e1f 146
mfiore 3:f6bceb9e5e1a 147 if (thpm_timer.read_ms() > thpm_interval_ms) {
mfiore 0:a44e71488e1f 148 read_temperature();
mfiore 0:a44e71488e1f 149 read_humidity();
mfiore 0:a44e71488e1f 150 read_pressure();
mfiore 3:f6bceb9e5e1a 151 read_moisture();
mfiore 3:f6bceb9e5e1a 152 thpm_timer.reset();
mfiore 0:a44e71488e1f 153 }
mfiore 0:a44e71488e1f 154
mfiore 0:a44e71488e1f 155 if (print_timer.read_ms() > print_interval_ms) {
mfiore 0:a44e71488e1f 156 logDebug("%s", wall_of_dash);
mfiore 0:a44e71488e1f 157 logDebug("SENSOR DATA");
mfiore 0:a44e71488e1f 158 logDebug("temperature: %f C\t%f F", temp_celsius, temp_fahrenheit);
mfiore 0:a44e71488e1f 159 logDebug("humidity: %f%%", humidity_percent);
mfiore 0:a44e71488e1f 160 logDebug("pressure: %f mbar", pressure_mbar);
mfiore 0:a44e71488e1f 161 logDebug("moisture: %f%%", moisture_percent);
mfiore 0:a44e71488e1f 162 logDebug("magnetometer:\r\n\tx: %ld\ty: %ld\tz: %ld\tmgauss", mag_mgauss[0], mag_mgauss[1], mag_mgauss[2]);
mfiore 0:a44e71488e1f 163 logDebug("accelerometer:\r\n\tx: %ld\ty: %ld\tz: %ld\tmg", acc_mg[0], acc_mg[1], acc_mg[2]);
mfiore 0:a44e71488e1f 164 logDebug("gyroscope:\r\n\tx: %ld\ty: %ld\tz: %ld\tmdps", gyro_mdps[0], gyro_mdps[1], gyro_mdps[2]);
mfiore 0:a44e71488e1f 165 logDebug("%s", wall_of_dash);
mfiore 0:a44e71488e1f 166 print_timer.reset();
mfiore 0:a44e71488e1f 167 }
mfiore 0:a44e71488e1f 168
mfiore 1:a049d113e250 169 if (button_pressed) {
mfiore 0:a44e71488e1f 170 logInfo("Button was pressed");
mfiore 1:a049d113e250 171 button_pressed = false;
mfiore 0:a44e71488e1f 172 if (radio_ok) {
mfiore 0:a44e71488e1f 173 MbedJSONValue sms_json;
mfiore 0:a44e71488e1f 174 string sms_str;
mfiore 0:a44e71488e1f 175
mfiore 0:a44e71488e1f 176 sms_json["temp_C"] = temp_celsius;
mfiore 0:a44e71488e1f 177 sms_json["temp_F"] = temp_fahrenheit;
mfiore 0:a44e71488e1f 178 sms_json["humidity_percent"] = humidity_percent;
mfiore 0:a44e71488e1f 179 sms_json["pressure_mbar"] = pressure_mbar;
mfiore 0:a44e71488e1f 180 sms_json["moisture_percent"] = moisture_percent;
mfiore 0:a44e71488e1f 181 sms_json["mag_mgauss"]["x"] = mag_mgauss[0];
mfiore 0:a44e71488e1f 182 sms_json["mag_mgauss"]["y"] = mag_mgauss[1];
mfiore 0:a44e71488e1f 183 sms_json["mag_mgauss"]["z"] = mag_mgauss[2];
mfiore 0:a44e71488e1f 184 sms_json["acc_mg"]["x"] = acc_mg[0];
mfiore 0:a44e71488e1f 185 sms_json["acc_mg"]["y"] = acc_mg[1];
mfiore 0:a44e71488e1f 186 sms_json["acc_mg"]["z"] = acc_mg[2];
mfiore 0:a44e71488e1f 187 sms_json["gyro_mdps"]["x"] = gyro_mdps[0];
mfiore 0:a44e71488e1f 188 sms_json["gyro_mdps"]["y"] = gyro_mdps[1];
mfiore 0:a44e71488e1f 189 sms_json["gyro_mdps"]["z"] = gyro_mdps[2];
mfiore 0:a44e71488e1f 190
mfiore 2:955a63247721 191 sms_str = "SENSOR DATA:\n";
mfiore 0:a44e71488e1f 192 sms_str += sms_json.serialize();
mfiore 0:a44e71488e1f 193
mfiore 0:a44e71488e1f 194 logDebug("sending SMS to %s:\r\n%s", phone_number.c_str(), sms_str.c_str());
mfiore 0:a44e71488e1f 195 Code ret = radio->sendSMS(phone_number, sms_str);
mfiore 0:a44e71488e1f 196 if (ret != MTS_SUCCESS)
mfiore 0:a44e71488e1f 197 logError("sending SMS failed");
mfiore 0:a44e71488e1f 198 }
mfiore 0:a44e71488e1f 199 }
mfiore 0:a44e71488e1f 200
mfiore 4:730b61258422 201 if (post_timer.read_ms() > post_interval_ms && do_cloud_post) {
mfiore 4:730b61258422 202 if (radio->connect()) {
mfiore 4:730b61258422 203 logDebug("posting sensor data");
mfiore 4:730b61258422 204 radio->disconnect();
mfiore 4:730b61258422 205 } else {
mfiore 4:730b61258422 206 logError("establishing PPP link failed");
mfiore 4:730b61258422 207 }
mfiore 4:730b61258422 208
mfiore 4:730b61258422 209 post_timer.reset();
mfiore 4:730b61258422 210 }
mfiore 4:730b61258422 211
mfiore 1:a049d113e250 212 wait_ms(10);
mfiore 0:a44e71488e1f 213 }
mfiore 0:a44e71488e1f 214 }
mfiore 0:a44e71488e1f 215
mfiore 1:a049d113e250 216 // init functions
mfiore 1:a049d113e250 217 bool init_mtsas() {
mfiore 1:a049d113e250 218 io = new MTSSerialFlowControl(RADIO_TX, RADIO_RX, RADIO_RTS, RADIO_CTS);
mfiore 1:a049d113e250 219 if (! io)
mfiore 1:a049d113e250 220 return false;
mfiore 1:a049d113e250 221
mfiore 1:a049d113e250 222 io->baud(115200);
mfiore 1:a049d113e250 223 radio = CellularFactory::create(io);
mfiore 1:a049d113e250 224 if (! radio)
mfiore 1:a049d113e250 225 return false;
mfiore 1:a049d113e250 226
mfiore 1:a049d113e250 227 Code ret = radio->setApn(apn);
mfiore 1:a049d113e250 228 if (ret != MTS_SUCCESS)
mfiore 1:a049d113e250 229 return false;
mfiore 1:a049d113e250 230
mfiore 1:a049d113e250 231 return true;
mfiore 1:a049d113e250 232 }
mfiore 1:a049d113e250 233
mfiore 0:a44e71488e1f 234 // Sensor data acquisition functions
mfiore 0:a44e71488e1f 235 void read_temperature() {
mfiore 0:a44e71488e1f 236 int ret;
mfiore 0:a44e71488e1f 237
mfiore 0:a44e71488e1f 238 ret = mems->ht_sensor->GetTemperature(&temp_celsius);
mfiore 0:a44e71488e1f 239 if (ret)
mfiore 0:a44e71488e1f 240 logError("reading temp (C) failed");
mfiore 0:a44e71488e1f 241
mfiore 0:a44e71488e1f 242 ret = mems->ht_sensor->GetFahrenheit(&temp_fahrenheit);
mfiore 0:a44e71488e1f 243 if (ret)
mfiore 0:a44e71488e1f 244 logError("reading temp (F) failed");
mfiore 0:a44e71488e1f 245 }
mfiore 0:a44e71488e1f 246
mfiore 0:a44e71488e1f 247 void read_humidity() {
mfiore 0:a44e71488e1f 248 int ret;
mfiore 0:a44e71488e1f 249
mfiore 0:a44e71488e1f 250 ret = mems->ht_sensor->GetHumidity(&humidity_percent);
mfiore 0:a44e71488e1f 251 if (ret)
mfiore 0:a44e71488e1f 252 logError("reading humidity failed");
mfiore 0:a44e71488e1f 253 }
mfiore 0:a44e71488e1f 254
mfiore 0:a44e71488e1f 255 void read_pressure() {
mfiore 0:a44e71488e1f 256 int ret;
mfiore 0:a44e71488e1f 257
mfiore 0:a44e71488e1f 258 ret = mems->pt_sensor->GetPressure(&pressure_mbar);
mfiore 0:a44e71488e1f 259 if (ret)
mfiore 0:a44e71488e1f 260 logError("reading pressure failed");
mfiore 0:a44e71488e1f 261 }
mfiore 0:a44e71488e1f 262
mfiore 0:a44e71488e1f 263 void read_moisture() {
mfiore 3:f6bceb9e5e1a 264 moisture_percent = moisture_sensor * 100.0;
mfiore 0:a44e71488e1f 265 }
mfiore 0:a44e71488e1f 266
mfiore 0:a44e71488e1f 267 void read_magnetometer() {
mfiore 0:a44e71488e1f 268 int ret;
mfiore 0:a44e71488e1f 269
mfiore 0:a44e71488e1f 270 ret = mems->magnetometer->Get_M_Axes(mag_mgauss);
mfiore 0:a44e71488e1f 271 if (ret)
mfiore 0:a44e71488e1f 272 logError("reading magnetometer failed");
mfiore 0:a44e71488e1f 273 }
mfiore 0:a44e71488e1f 274
mfiore 0:a44e71488e1f 275 void read_accelerometer() {
mfiore 0:a44e71488e1f 276 int ret;
mfiore 0:a44e71488e1f 277
mfiore 0:a44e71488e1f 278 ret = mems->GetAccelerometer()->Get_X_Axes(acc_mg);
mfiore 0:a44e71488e1f 279 if (ret)
mfiore 0:a44e71488e1f 280 logError("reading accelerometer failed");
mfiore 0:a44e71488e1f 281 }
mfiore 0:a44e71488e1f 282
mfiore 0:a44e71488e1f 283 void read_gyroscope() {
mfiore 0:a44e71488e1f 284 int ret;
mfiore 0:a44e71488e1f 285
mfiore 0:a44e71488e1f 286 ret = mems->GetGyroscope()->Get_G_Axes(gyro_mdps);
mfiore 0:a44e71488e1f 287 if (ret)
mfiore 0:a44e71488e1f 288 logError("reading gyroscope failed");
mfiore 0:a44e71488e1f 289 }
mfiore 1:a049d113e250 290
mfiore 1:a049d113e250 291 void button_irq() {
mfiore 1:a049d113e250 292 button_pressed = true;
mfiore 1:a049d113e250 293 }