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:
Thu Sep 24 19:19:11 2015 +0000
Revision:
2:955a63247721
Parent:
1:a049d113e250
Child:
3:f6bceb9e5e1a
use D8 instead of A1 for button & clean up

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 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 2:955a63247721 59 static const std::string apn = "";
mfiore 0:a44e71488e1f 60
mfiore 0:a44e71488e1f 61 // Phone number to send SMS messages to
mfiore 2:955a63247721 62 static const std::string phone_number = "1xxxxxxxxxx";
mfiore 0:a44e71488e1f 63
mfiore 0:a44e71488e1f 64 // handle to MEMs board object
mfiore 0:a44e71488e1f 65 static X_NUCLEO_IKS01A1* mems = X_NUCLEO_IKS01A1::Instance();
mfiore 0:a44e71488e1f 66
mfiore 0:a44e71488e1f 67 // Moisture sensor
mfiore 0:a44e71488e1f 68 AnalogIn moisture_sensor(A0);
mfiore 0:a44e71488e1f 69
mfiore 0:a44e71488e1f 70 // Button
mfiore 2:955a63247721 71 InterruptIn button(D8);
mfiore 1:a049d113e250 72 bool button_pressed = false;
mfiore 0:a44e71488e1f 73
mfiore 0:a44e71488e1f 74 // variables for sensor data
mfiore 0:a44e71488e1f 75 float temp_celsius;
mfiore 0:a44e71488e1f 76 float temp_fahrenheit;
mfiore 0:a44e71488e1f 77 float humidity_percent;
mfiore 0:a44e71488e1f 78 float pressure_mbar;
mfiore 0:a44e71488e1f 79 float moisture_percent;
mfiore 0:a44e71488e1f 80 int32_t mag_mgauss[3];
mfiore 0:a44e71488e1f 81 int32_t acc_mg[3];
mfiore 0:a44e71488e1f 82 int32_t gyro_mdps[3];
mfiore 0:a44e71488e1f 83
mfiore 0:a44e71488e1f 84 // misc variables
mfiore 0:a44e71488e1f 85 static char wall_of_dash[] = "--------------------------------------------------";
mfiore 0:a44e71488e1f 86 bool radio_ok = false;
mfiore 0:a44e71488e1f 87 static int thp_interval_ms = 2000;
mfiore 0:a44e71488e1f 88 static int motion_interval_ms = 250;
mfiore 0:a44e71488e1f 89 static int print_interval_ms = 5000;
mfiore 0:a44e71488e1f 90 int debug_baud = 115200;
mfiore 0:a44e71488e1f 91
mfiore 0:a44e71488e1f 92 // function prototypes
mfiore 0:a44e71488e1f 93 bool init_mtsas();
mfiore 0:a44e71488e1f 94 void read_temperature();
mfiore 0:a44e71488e1f 95 void read_humidity();
mfiore 0:a44e71488e1f 96 void read_pressure();
mfiore 0:a44e71488e1f 97 void read_moisture();
mfiore 0:a44e71488e1f 98 void read_magnetometer();
mfiore 0:a44e71488e1f 99 void read_accelerometer();
mfiore 0:a44e71488e1f 100 void read_gyroscope();
mfiore 0:a44e71488e1f 101 void button_irq();
mfiore 0:a44e71488e1f 102
mfiore 0:a44e71488e1f 103 // main
mfiore 0:a44e71488e1f 104 int main() {
mfiore 0:a44e71488e1f 105 mts::MTSLog::setLogLevel(mts::MTSLog::TRACE_LEVEL);
mfiore 0:a44e71488e1f 106 debug.baud(debug_baud);
mfiore 0:a44e71488e1f 107 logInfo("starting...");
mfiore 0:a44e71488e1f 108
mfiore 0:a44e71488e1f 109 radio_ok = init_mtsas();
mfiore 0:a44e71488e1f 110 if (! radio_ok)
mfiore 0:a44e71488e1f 111 logError("MTSAS init failed");
mfiore 0:a44e71488e1f 112 else
mfiore 0:a44e71488e1f 113 logInfo("MTSAS is ok");
mfiore 0:a44e71488e1f 114
mfiore 1:a049d113e250 115 button.fall(&button_irq);
mfiore 0:a44e71488e1f 116
mfiore 0:a44e71488e1f 117 Timer thp_timer;
mfiore 0:a44e71488e1f 118 Timer motion_timer;
mfiore 1:a049d113e250 119 Timer print_timer;
mfiore 0:a44e71488e1f 120
mfiore 0:a44e71488e1f 121 thp_timer.start();
mfiore 0:a44e71488e1f 122 motion_timer.start();
mfiore 1:a049d113e250 123 print_timer.start();
mfiore 1:a049d113e250 124
mfiore 0:a44e71488e1f 125 while (true) {
mfiore 0:a44e71488e1f 126 if (motion_timer.read_ms() > motion_interval_ms) {
mfiore 0:a44e71488e1f 127 read_magnetometer();
mfiore 0:a44e71488e1f 128 read_accelerometer();
mfiore 0:a44e71488e1f 129 read_gyroscope();
mfiore 0:a44e71488e1f 130 motion_timer.reset();
mfiore 0:a44e71488e1f 131 }
mfiore 0:a44e71488e1f 132
mfiore 0:a44e71488e1f 133 if (thp_timer.read_ms() > thp_interval_ms) {
mfiore 0:a44e71488e1f 134 read_temperature();
mfiore 0:a44e71488e1f 135 read_humidity();
mfiore 0:a44e71488e1f 136 read_pressure();
mfiore 0:a44e71488e1f 137 thp_timer.reset();
mfiore 0:a44e71488e1f 138 }
mfiore 0:a44e71488e1f 139
mfiore 0:a44e71488e1f 140 if (print_timer.read_ms() > print_interval_ms) {
mfiore 0:a44e71488e1f 141 logDebug("%s", wall_of_dash);
mfiore 0:a44e71488e1f 142 logDebug("SENSOR DATA");
mfiore 0:a44e71488e1f 143 logDebug("temperature: %f C\t%f F", temp_celsius, temp_fahrenheit);
mfiore 0:a44e71488e1f 144 logDebug("humidity: %f%%", humidity_percent);
mfiore 0:a44e71488e1f 145 logDebug("pressure: %f mbar", pressure_mbar);
mfiore 0:a44e71488e1f 146 logDebug("moisture: %f%%", moisture_percent);
mfiore 0:a44e71488e1f 147 logDebug("magnetometer:\r\n\tx: %ld\ty: %ld\tz: %ld\tmgauss", mag_mgauss[0], mag_mgauss[1], mag_mgauss[2]);
mfiore 0:a44e71488e1f 148 logDebug("accelerometer:\r\n\tx: %ld\ty: %ld\tz: %ld\tmg", acc_mg[0], acc_mg[1], acc_mg[2]);
mfiore 0:a44e71488e1f 149 logDebug("gyroscope:\r\n\tx: %ld\ty: %ld\tz: %ld\tmdps", gyro_mdps[0], gyro_mdps[1], gyro_mdps[2]);
mfiore 0:a44e71488e1f 150 logDebug("%s", wall_of_dash);
mfiore 0:a44e71488e1f 151 print_timer.reset();
mfiore 0:a44e71488e1f 152 }
mfiore 0:a44e71488e1f 153
mfiore 1:a049d113e250 154 if (button_pressed) {
mfiore 0:a44e71488e1f 155 logInfo("Button was pressed");
mfiore 1:a049d113e250 156 button_pressed = false;
mfiore 0:a44e71488e1f 157 if (radio_ok) {
mfiore 0:a44e71488e1f 158 MbedJSONValue sms_json;
mfiore 0:a44e71488e1f 159 string sms_str;
mfiore 0:a44e71488e1f 160
mfiore 0:a44e71488e1f 161 sms_json["temp_C"] = temp_celsius;
mfiore 0:a44e71488e1f 162 sms_json["temp_F"] = temp_fahrenheit;
mfiore 0:a44e71488e1f 163 sms_json["humidity_percent"] = humidity_percent;
mfiore 0:a44e71488e1f 164 sms_json["pressure_mbar"] = pressure_mbar;
mfiore 0:a44e71488e1f 165 sms_json["moisture_percent"] = moisture_percent;
mfiore 0:a44e71488e1f 166 sms_json["mag_mgauss"]["x"] = mag_mgauss[0];
mfiore 0:a44e71488e1f 167 sms_json["mag_mgauss"]["y"] = mag_mgauss[1];
mfiore 0:a44e71488e1f 168 sms_json["mag_mgauss"]["z"] = mag_mgauss[2];
mfiore 0:a44e71488e1f 169 sms_json["acc_mg"]["x"] = acc_mg[0];
mfiore 0:a44e71488e1f 170 sms_json["acc_mg"]["y"] = acc_mg[1];
mfiore 0:a44e71488e1f 171 sms_json["acc_mg"]["z"] = acc_mg[2];
mfiore 0:a44e71488e1f 172 sms_json["gyro_mdps"]["x"] = gyro_mdps[0];
mfiore 0:a44e71488e1f 173 sms_json["gyro_mdps"]["y"] = gyro_mdps[1];
mfiore 0:a44e71488e1f 174 sms_json["gyro_mdps"]["z"] = gyro_mdps[2];
mfiore 0:a44e71488e1f 175
mfiore 2:955a63247721 176 sms_str = "SENSOR DATA:\n";
mfiore 0:a44e71488e1f 177 sms_str += sms_json.serialize();
mfiore 0:a44e71488e1f 178
mfiore 0:a44e71488e1f 179 logDebug("sending SMS to %s:\r\n%s", phone_number.c_str(), sms_str.c_str());
mfiore 0:a44e71488e1f 180 Code ret = radio->sendSMS(phone_number, sms_str);
mfiore 0:a44e71488e1f 181 if (ret != MTS_SUCCESS)
mfiore 0:a44e71488e1f 182 logError("sending SMS failed");
mfiore 0:a44e71488e1f 183 }
mfiore 0:a44e71488e1f 184 }
mfiore 0:a44e71488e1f 185
mfiore 1:a049d113e250 186 wait_ms(10);
mfiore 0:a44e71488e1f 187 }
mfiore 0:a44e71488e1f 188 }
mfiore 0:a44e71488e1f 189
mfiore 1:a049d113e250 190 // init functions
mfiore 1:a049d113e250 191 bool init_mtsas() {
mfiore 1:a049d113e250 192 io = new MTSSerialFlowControl(RADIO_TX, RADIO_RX, RADIO_RTS, RADIO_CTS);
mfiore 1:a049d113e250 193 if (! io)
mfiore 1:a049d113e250 194 return false;
mfiore 1:a049d113e250 195
mfiore 1:a049d113e250 196 io->baud(115200);
mfiore 1:a049d113e250 197 radio = CellularFactory::create(io);
mfiore 1:a049d113e250 198 if (! radio)
mfiore 1:a049d113e250 199 return false;
mfiore 1:a049d113e250 200
mfiore 1:a049d113e250 201 Code ret = radio->setApn(apn);
mfiore 1:a049d113e250 202 if (ret != MTS_SUCCESS)
mfiore 1:a049d113e250 203 return false;
mfiore 1:a049d113e250 204
mfiore 1:a049d113e250 205 return true;
mfiore 1:a049d113e250 206 }
mfiore 1:a049d113e250 207
mfiore 0:a44e71488e1f 208 // Sensor data acquisition functions
mfiore 0:a44e71488e1f 209 void read_temperature() {
mfiore 0:a44e71488e1f 210 int ret;
mfiore 0:a44e71488e1f 211
mfiore 0:a44e71488e1f 212 ret = mems->ht_sensor->GetTemperature(&temp_celsius);
mfiore 0:a44e71488e1f 213 if (ret)
mfiore 0:a44e71488e1f 214 logError("reading temp (C) failed");
mfiore 0:a44e71488e1f 215
mfiore 0:a44e71488e1f 216 ret = mems->ht_sensor->GetFahrenheit(&temp_fahrenheit);
mfiore 0:a44e71488e1f 217 if (ret)
mfiore 0:a44e71488e1f 218 logError("reading temp (F) failed");
mfiore 0:a44e71488e1f 219 }
mfiore 0:a44e71488e1f 220
mfiore 0:a44e71488e1f 221 void read_humidity() {
mfiore 0:a44e71488e1f 222 int ret;
mfiore 0:a44e71488e1f 223
mfiore 0:a44e71488e1f 224 ret = mems->ht_sensor->GetHumidity(&humidity_percent);
mfiore 0:a44e71488e1f 225 if (ret)
mfiore 0:a44e71488e1f 226 logError("reading humidity failed");
mfiore 0:a44e71488e1f 227 }
mfiore 0:a44e71488e1f 228
mfiore 0:a44e71488e1f 229 void read_pressure() {
mfiore 0:a44e71488e1f 230 int ret;
mfiore 0:a44e71488e1f 231
mfiore 0:a44e71488e1f 232 ret = mems->pt_sensor->GetPressure(&pressure_mbar);
mfiore 0:a44e71488e1f 233 if (ret)
mfiore 0:a44e71488e1f 234 logError("reading pressure failed");
mfiore 0:a44e71488e1f 235 }
mfiore 0:a44e71488e1f 236
mfiore 0:a44e71488e1f 237 void read_moisture() {
mfiore 0:a44e71488e1f 238 moisture_percent = moisture_sensor;
mfiore 0:a44e71488e1f 239 }
mfiore 0:a44e71488e1f 240
mfiore 0:a44e71488e1f 241 void read_magnetometer() {
mfiore 0:a44e71488e1f 242 int ret;
mfiore 0:a44e71488e1f 243
mfiore 0:a44e71488e1f 244 ret = mems->magnetometer->Get_M_Axes(mag_mgauss);
mfiore 0:a44e71488e1f 245 if (ret)
mfiore 0:a44e71488e1f 246 logError("reading magnetometer failed");
mfiore 0:a44e71488e1f 247 }
mfiore 0:a44e71488e1f 248
mfiore 0:a44e71488e1f 249 void read_accelerometer() {
mfiore 0:a44e71488e1f 250 int ret;
mfiore 0:a44e71488e1f 251
mfiore 0:a44e71488e1f 252 ret = mems->GetAccelerometer()->Get_X_Axes(acc_mg);
mfiore 0:a44e71488e1f 253 if (ret)
mfiore 0:a44e71488e1f 254 logError("reading accelerometer failed");
mfiore 0:a44e71488e1f 255 }
mfiore 0:a44e71488e1f 256
mfiore 0:a44e71488e1f 257 void read_gyroscope() {
mfiore 0:a44e71488e1f 258 int ret;
mfiore 0:a44e71488e1f 259
mfiore 0:a44e71488e1f 260 ret = mems->GetGyroscope()->Get_G_Axes(gyro_mdps);
mfiore 0:a44e71488e1f 261 if (ret)
mfiore 0:a44e71488e1f 262 logError("reading gyroscope failed");
mfiore 0:a44e71488e1f 263 }
mfiore 1:a049d113e250 264
mfiore 1:a049d113e250 265 void button_irq() {
mfiore 1:a049d113e250 266 button_pressed = true;
mfiore 1:a049d113e250 267 }