![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
MultiTech Dragonfly sending SMS sensor data from Freescale (NXP) MEMs board to cell phone
Dependencies: FXAS21002 FXOS8700 MbedJSONValue mbed mtsas
Fork of VVV_MultiTech_Dragonfly_ATT_Dallas by
Diff: main.cpp
- Revision:
- 1:a049d113e250
- Parent:
- 0:a44e71488e1f
- Child:
- 2:955a63247721
diff -r a44e71488e1f -r a049d113e250 main.cpp --- a/main.cpp Thu Sep 24 17:56:49 2015 +0000 +++ b/main.cpp Thu Sep 24 18:33:02 2015 +0000 @@ -41,7 +41,6 @@ ************************************************************************/ #include "mbed.h" -#include "rtos.h" #include "mtsas.h" #include "x_nucleo_iks01a1.h" #include "MbedJSONValue.h" @@ -51,7 +50,7 @@ static Serial debug(USBTX, USBRX); // MTSSerialFlowControl - serial link between processor and radio -static MTSSerialFlowControl io(SERIAL_TX, SERIAL_RX, SERIAL_RTS, SERIAL_CTS); +static MTSSerialFlowControl* io; // Cellular - radio object for cellular operations (SMS, TCP, etc) Cellular* radio; @@ -71,7 +70,8 @@ AnalogIn moisture_sensor(A0); // Button -DigitalIn button(D8); +InterruptIn button(A1); +bool button_pressed = false; // variables for sensor data float temp_celsius; @@ -83,10 +83,6 @@ int32_t acc_mg[3]; int32_t gyro_mdps[3]; -// mutexes -Mutex data_mutex; -Mutex mtsas_mutex; - // misc variables static char wall_of_dash[] = "--------------------------------------------------"; bool radio_ok = false; @@ -97,9 +93,6 @@ // function prototypes bool init_mtsas(); -void sensor_thread(void const* args); -void print_thread(void const* args); -void sms_thread(void const* args); void read_temperature(); void read_humidity(); void read_pressure(); @@ -120,41 +113,17 @@ logError("MTSAS init failed"); else logInfo("MTSAS is ok"); - - Thread sensors(sensor_thread); - Thread prints(print_thread); - Thread sms(sms_thread); - - while (true) { - } -} - -// init functions -bool init_mtsas() { - io.baud(115200); - - mtsas_mutex.lock(); - radio = CellularFactory::create(&io); - mtsas_mutex.unlock(); - if (! radio) - return false; - mtsas_mutex.lock(); - Code ret = radio->setApn(apn); - mtsas_mutex.unlock(); - if (ret != MTS_SUCCESS) - return false; + button.fall(&button_irq); - return true; -} - -// thread "main" functions -void sensor_thread(void const* args) { Timer thp_timer; Timer motion_timer; + Timer print_timer; thp_timer.start(); motion_timer.start(); + print_timer.start(); + while (true) { if (motion_timer.read_ms() > motion_interval_ms) { read_magnetometer(); @@ -170,17 +139,7 @@ thp_timer.reset(); } - Thread::wait(100); - } -} - -void print_thread(void const* args) { - Timer print_timer; - - print_timer.start(); - while (true) { if (print_timer.read_ms() > print_interval_ms) { - data_mutex.lock(); logDebug("%s", wall_of_dash); logDebug("SENSOR DATA"); logDebug("temperature: %f C\t%f F", temp_celsius, temp_fahrenheit); @@ -191,18 +150,12 @@ logDebug("accelerometer:\r\n\tx: %ld\ty: %ld\tz: %ld\tmg", acc_mg[0], acc_mg[1], acc_mg[2]); logDebug("gyroscope:\r\n\tx: %ld\ty: %ld\tz: %ld\tmdps", gyro_mdps[0], gyro_mdps[1], gyro_mdps[2]); logDebug("%s", wall_of_dash); - data_mutex.unlock(); print_timer.reset(); } - Thread::wait(1000); - } -} - -void sms_thread(void const* args) { - while (true) { - if (button) { + if (button_pressed) { logInfo("Button was pressed"); + button_pressed = false; if (radio_ok) { MbedJSONValue sms_json; string sms_str; @@ -226,31 +179,43 @@ sms_str += sms_json.serialize(); logDebug("sending SMS to %s:\r\n%s", phone_number.c_str(), sms_str.c_str()); - mtsas_mutex.lock(); Code ret = radio->sendSMS(phone_number, sms_str); - mtsas_mutex.unlock(); if (ret != MTS_SUCCESS) logError("sending SMS failed"); } } - Thread::wait(200); + wait_ms(10); } } +// init functions +bool init_mtsas() { + io = new MTSSerialFlowControl(RADIO_TX, RADIO_RX, RADIO_RTS, RADIO_CTS); + if (! io) + return false; + + io->baud(115200); + radio = CellularFactory::create(io); + if (! radio) + return false; + + Code ret = radio->setApn(apn); + if (ret != MTS_SUCCESS) + return false; + + return true; +} + // Sensor data acquisition functions void read_temperature() { int ret; - data_mutex.lock(); ret = mems->ht_sensor->GetTemperature(&temp_celsius); - data_mutex.unlock(); if (ret) logError("reading temp (C) failed"); - data_mutex.lock(); ret = mems->ht_sensor->GetFahrenheit(&temp_fahrenheit); - data_mutex.unlock(); if (ret) logError("reading temp (F) failed"); } @@ -258,9 +223,7 @@ void read_humidity() { int ret; - data_mutex.lock(); ret = mems->ht_sensor->GetHumidity(&humidity_percent); - data_mutex.unlock(); if (ret) logError("reading humidity failed"); } @@ -268,25 +231,19 @@ void read_pressure() { int ret; - data_mutex.lock(); ret = mems->pt_sensor->GetPressure(&pressure_mbar); - data_mutex.unlock(); if (ret) logError("reading pressure failed"); } void read_moisture() { - data_mutex.lock(); moisture_percent = moisture_sensor; - data_mutex.unlock(); } void read_magnetometer() { int ret; - data_mutex.lock(); ret = mems->magnetometer->Get_M_Axes(mag_mgauss); - data_mutex.unlock(); if (ret) logError("reading magnetometer failed"); } @@ -294,9 +251,7 @@ void read_accelerometer() { int ret; - data_mutex.lock(); ret = mems->GetAccelerometer()->Get_X_Axes(acc_mg); - data_mutex.unlock(); if (ret) logError("reading accelerometer failed"); } @@ -304,9 +259,11 @@ void read_gyroscope() { int ret; - data_mutex.lock(); ret = mems->GetGyroscope()->Get_G_Axes(gyro_mdps); - data_mutex.unlock(); if (ret) logError("reading gyroscope failed"); } + +void button_irq() { + button_pressed = true; +}