Peter Ferland / Mbed 2 deprecated Dragonfly-m2x-IKS01A1

Dependencies:   MbedJSONValue X_NUCLEO_IKS01A1 mbed mtsas_lat3

Fork of VVV_MultiTech_Dragonfly_ATT_Dallas by Paul Jaeger

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /*************************************************************************
00002  * Dragonfly Example program of SMS on Freescale sensor board
00003  */
00004 
00005 #include "mbed.h"
00006 #include "mtsas.h"
00007 #include "MbedJSONValue.h"
00008 #include "HTTPJson.h"
00009 #include <string>
00010 #include <sstream>
00011 #include "x_nucleo_iks01a1.h"
00012 
00013 #define HTTP_RX_BUFFER_SIZE 1024
00014 char http_rx_buf[HTTP_RX_BUFFER_SIZE];
00015 
00016 // Debug serial port
00017 static Serial debug(USBTX, USBRX);
00018 
00019 // MTSSerialFlowControl - serial link between processor and radio
00020 static MTSSerialFlowControl* io;
00021 
00022 // Cellular - radio object for cellular operations (SMS, TCP, etc)
00023 Cellular* radio;
00024 
00025 // APN associated with SIM card
00026 // this APN should work for the AT&T SIM that came with your Dragonfly
00027 static const std::string apn = "broadband";
00028 
00029 // Phone number to send SMS messages to
00030 // just change the x digits - the 1 needs to stay!
00031 static const std::string phone_number = " NEED A NUMBER with a 1";
00032 
00033 // Keys needed by the m2x API
00034 // see more details here: https://m2x.att.com/developer/documentation/v2/overview
00035 // m2x device ID
00036 static const std::string m2x_device_id = "";
00037 
00038 // M2X primary API key
00039 static const std::string m2x_api_key = "";
00040 
00041 /* Instantiate the expansion board */
00042 static X_NUCLEO_IKS01A1 *mems_expansion_board = X_NUCLEO_IKS01A1::Instance(I2C_SDA, I2C_SCL);
00043 
00044 /* Retrieve the composing elements of the expansion board */
00045 static GyroSensor *gyroscope = mems_expansion_board->GetGyroscope();
00046 static MotionSensor *accelerometer = mems_expansion_board->GetAccelerometer();
00047 static MagneticSensor *magnetometer = mems_expansion_board->magnetometer;
00048 static HumiditySensor *humidity_sensor = mems_expansion_board->ht_sensor;
00049 static PressureSensor *pressure_sensor = mems_expansion_board->pt_sensor;
00050 static TempSensor *temp_sensor1 = mems_expansion_board->ht_sensor;
00051 static TempSensor *temp_sensor2 = mems_expansion_board->pt_sensor;
00052 
00053 // variables for sensor data
00054 char streamAcc[] = "acc_rms";
00055 char streamMag[] = "mag_rms";
00056 char streamGyr[] = "gyr_rms";
00057 char streamTemp[] = "temp_c";
00058 char streamPres[] = "pressure";
00059 char streamHum[] = "humidity";
00060 char streamAccel[] = "accel";
00061 
00062 // misc variables
00063 static int sms_interval_ms = 60000;
00064 static int read_interval_ms = 5000;
00065 static int print_interval_ms = 1500;
00066 int debug_baud = 115200;
00067 
00068 bool radio_ok = false;
00069 
00070 /****************************************************************************************************
00071 // function prototypes
00072  ****************************************************************************************************/
00073 bool init_mtsas();
00074 char* httpResToStr(HTTPResult res);
00075 
00076 /****************************************************************************************************
00077 // main
00078  ****************************************************************************************************/
00079 int main()
00080 {
00081     mts::MTSLog::setLogLevel(mts::MTSLog::TRACE_LEVEL);
00082     debug.baud(debug_baud);
00083     logInfo("starting...");
00084 
00085     /****************************************************************************************************
00086           Initialize I2C Devices ************
00087      ****************************************************************************************************/
00088 
00089     int32_t accel_data[3];
00090     float accel_sensitivity;
00091     //float mag_data[3];
00092     //float gyro_data[3];
00093     
00094     float dataX = 0.0f;
00095     float dataY = 0.0f;
00096     float dataZ = 1.0f;
00097     float humidity = 0.0f;
00098     float temperature = 0.0f;
00099     float pressure = 0.0f;
00100 
00101 // Initialization Radio Section **********************************************************
00102 
00103     radio_ok = init_mtsas();
00104     if (! radio_ok)
00105         logError("MTSAS init failed");
00106     else
00107         logInfo("MTSAS is ok");
00108 
00109 //End Radio Initialization Section **********************************************************
00110 
00111     Timer sms_timer;
00112     sms_timer.start();
00113     Timer read_timer;
00114     read_timer.start();         // Timer data is set in the Variable seciton see misc variables    Timer motion_timer;
00115     Timer print_timer;
00116     print_timer.start();
00117     Timer motion_timer;
00118     motion_timer.start();
00119     
00120 // http object initialization
00121     // HTTPClient object used for HTTP requests.
00122     HTTPClient http;
00123     
00124     // Enable strict certificate validation.
00125     // http.setPeerVerification(VERIFY_PEER);
00126     
00127     // Load certificates defined in ssl_certificates.h.
00128     // See comments in ssl_certificates.h for information on how to get and format root certificates.
00129 //    if (http.addRootCACertificate(ssl_certificates) != HTTP_OK)
00130 //        logError("loading SSL certificates failed");
00131 
00132     while (true) {
00133         if (read_timer.read_ms() > read_interval_ms) {
00134 
00135             accelerometer->get_x_axes(accel_data);
00136             accelerometer->get_x_sensitivity(&accel_sensitivity);
00137             dataX = accel_data[0] * accel_sensitivity;
00138             dataY = accel_data[1] * accel_sensitivity;
00139             dataZ = accel_data[2] * accel_sensitivity;
00140             humidity_sensor->get_humidity(&humidity);
00141             temp_sensor1->get_temperature(&temperature);
00142             pressure_sensor->get_pressure(&pressure);
00143             read_timer.reset();
00144         }
00145 // send to m2x on same interval as printing
00146         if (print_timer.read_ms() > print_interval_ms) {
00147 
00148             logDebug("SENSOR DATA");
00149             logDebug("DataZ  %0.3f", dataZ);
00150             logDebug("DataY  %0.3f", dataY);
00151             logDebug("DataX  %0.3f", dataX);
00152             logDebug("Humidity: %0.2f", humidity);
00153             logDebug("Temperature: %0.1f", temperature);
00154             logDebug("Presure: %f", pressure);
00155             
00156             HTTPResult res;
00157             // IHTTPDataIn object - will contain data received from server.
00158             HTTPText http_rx(http_rx_buf, HTTP_RX_BUFFER_SIZE);
00159             
00160             std::ostringstream json_packet;
00161             json_packet << "{\"values\": {\"value\" : " \
00162                     << temperature << "}}";
00163             
00164             // IHTTPDataOut object - contains data to be posted to server.
00165             // HTTPJson automatically adds the JSON content-type header to the request.
00166             char *tx_string = new char[json_packet.str().length()];
00167             sprintf(tx_string, json_packet.str().c_str());
00168             HTTPJson http_tx(tx_string, json_packet.str().length());
00169             std::string m2x_header = "X-M2X-KEY: " + m2x_api_key + "\r\n";
00170             std::string url = "https://api-m2x.att.com/v2/devices/" + \
00171                                 m2x_device_id + \
00172                                 "/streams/temperature/values";
00173             http.setHeader(m2x_header.c_str());
00174             
00175             http.post(url.c_str(), http_tx, &http_rx);
00176             delete tx_string;
00177             if (res != HTTP_OK)
00178                 logError("HTTPS POST failed [%d][%s]", res, httpResToStr(res));
00179             else
00180                 logInfo("HTTPS POST succeeded [%d]\r\n%s", http.getHTTPResponseCode(), http_rx_buf);
00181             
00182             print_timer.reset();
00183             
00184         }
00185 
00186 // SMS
00187         if (sms_timer.read_ms() > sms_interval_ms) {
00188             sms_timer.reset();
00189             logInfo("SMS Send Routine");
00190             printf("  In sms routine \r\n");
00191             if (radio_ok) {
00192                 MbedJSONValue sms_json;
00193                 string sms_str;
00194 
00195                 sms_json["  edge Gravity: "] = dataY;
00196                 sms_json[" level Gravity: "] = dataZ;
00197 
00198                 sms_str = "SENSOR DATA:\n";
00199                 sms_str += sms_json.serialize();
00200 
00201                 logDebug("sending SMS to %s:\r\n%s", phone_number.c_str(), sms_str.c_str());
00202                 if (dataZ<0.6f) {   //added
00203                 Code ret = radio->sendSMS(phone_number, sms_str);
00204                 if (ret != MTS_SUCCESS)
00205                     logError("sending SMS failed");
00206                     }     //added
00207             }
00208         }
00209     }
00210 }
00211 
00212 // init functions
00213 bool init_mtsas()
00214 {
00215     io = new MTSSerialFlowControl(RADIO_TX, RADIO_RX, RADIO_RTS, RADIO_CTS);
00216     if (! io)
00217         return false;
00218 
00219     io->baud(115200);
00220     radio = CellularFactory::create(io);
00221     if (! radio)
00222         return false;
00223 
00224     Code ret = radio->setApn(apn);
00225     if (ret != MTS_SUCCESS)
00226         return false;
00227 
00228     Transport::setTransport(radio);
00229 
00230     return true;
00231 }
00232 
00233 char* httpResToStr(HTTPResult res) {
00234     switch(res) {
00235         case HTTP_PROCESSING:
00236             return "HTTP_PROCESSING";
00237         case HTTP_PARSE:
00238             return "HTTP_PARSE";
00239         case HTTP_DNS:
00240             return "HTTP_DNS";
00241         case HTTP_PRTCL:
00242             return "HTTP_PRTCL";
00243         case HTTP_NOTFOUND:
00244             return "HTTP_NOTFOUND";
00245         case HTTP_REFUSED:
00246             return "HTTP_REFUSED";
00247         case HTTP_ERROR:
00248             return "HTTP_ERROR";
00249         case HTTP_TIMEOUT:
00250             return "HTTP_TIMEOUT";
00251         case HTTP_CONN:
00252             return "HTTP_CONN";
00253         case HTTP_CLOSED:
00254             return "HTTP_CLOSED";
00255         case HTTP_REDIRECT:
00256             return "HTTP_REDIRECT";
00257         case HTTP_OK:
00258             return "HTTP_OK";
00259         default:
00260             return "HTTP Result unknown";
00261     }
00262 }