A simple project for sending button input up to AT&T Flow

Dependencies:   FXOS8700CQ MODSERIAL mbed

For instructions on using this program, see the AT&T Starter Kit tutorial on it.

Revision:
68:d38834307b17
Parent:
64:09004cd610df
Child:
70:e25fe7e5488a
--- a/main.cpp	Thu Aug 04 16:24:13 2016 +0000
+++ b/main.cpp	Tue Aug 09 19:23:11 2016 +0000
@@ -1,8 +1,7 @@
-#include "mbed.h" 
+#include "mbed.h"
 #include <cctype>
 #include <string>
 #include "config_me.h"
-#include "sensors.h"
 #include "cell_modem.h"
 #include "hardware.h"
 
@@ -13,89 +12,48 @@
 DigitalOut led_red(LED_RED);
 DigitalOut led_blue(LED_BLUE);
 
+// interrupts for buttons
+InterruptIn sw3(SW3);
+InterruptIn sw2(SW2);
 
-//********************************************************************************************************************************************
-//* Create string with sensor readings that can be sent to flow as an HTTP get
-//********************************************************************************************************************************************
-K64F_Sensors_t  SENSOR_DATA =
+// flag - must be volatile as changes within ISR
+// g_ prefix makes it easier to distinguish it as global
+volatile int g_sw3_flag = 0;
+volatile int g_sw2_flag = 0;
+
+// SW2 event-triggered interrupt
+void sw2_isr_down()
 {
-    .Temperature        = "0",
-    .Humidity           = "0",
-    .AccelX             = "0",
-    .AccelY             = "0",
-    .AccelZ             = "0",
-    .MagnetometerX      = "0",
-    .MagnetometerY      = "0",
-    .MagnetometerZ      = "0",
-    .AmbientLightVis    = "0",
-    .AmbientLightIr     = "0",
-    .UVindex            = "0",
-    .Proximity          = "0",
-    .Temperature_Si7020 = "0",
-    .Humidity_Si7020    = "0",
-    .Virtual_Sensor1    = "0",
-    .Virtual_Sensor2    = "0",
-    .Virtual_Sensor3    = "0",
-    .Virtual_Sensor4    = "0",
-    .Virtual_Sensor5    = "0",
-    .Virtual_Sensor6    = "0",
-    .Virtual_Sensor7    = "0",
-    .Virtual_Sensor8    = "0"
-};
+    g_sw2_flag = 1;   // set flag in ISR
+}
 
-void GenerateModemString(char * modem_string)
+void sw2_isr_up()
 {
-    switch(iSensorsToReport)
-    {
-        case TEMP_HUMIDITY_ONLY:
-        {
-            sprintf(modem_string, "GET %s%s?serial=%s&temp=%s&humidity=%s %s%s\r\n\r\n", FLOW_BASE_URL, FLOW_INPUT_NAME, FLOW_DEVICE_NAME, SENSOR_DATA.Temperature, SENSOR_DATA.Humidity, FLOW_URL_TYPE, MY_SERVER_URL);
-            break;
-        }
-        case TEMP_HUMIDITY_ACCELEROMETER:
-        {
-            sprintf(modem_string, "GET %s%s?serial=%s&temp=%s&humidity=%s&accelX=%s&accelY=%s&accelZ=%s %s%s\r\n\r\n", FLOW_BASE_URL, FLOW_INPUT_NAME, FLOW_DEVICE_NAME, SENSOR_DATA.Temperature, SENSOR_DATA.Humidity, SENSOR_DATA.AccelX,SENSOR_DATA.AccelY,SENSOR_DATA.AccelZ, FLOW_URL_TYPE, MY_SERVER_URL);
-            break;
-        }
-        case TEMP_HUMIDITY_ACCELEROMETER_PMODSENSORS:
-        {
-            sprintf(modem_string, "GET %s%s?serial=%s&temp=%s&humidity=%s&accelX=%s&accelY=%s&accelZ=%s&proximity=%s&light_uv=%s&light_vis=%s&light_ir=%s %s%s\r\n\r\n", FLOW_BASE_URL, FLOW_INPUT_NAME, FLOW_DEVICE_NAME, SENSOR_DATA.Temperature, SENSOR_DATA.Humidity, SENSOR_DATA.AccelX,SENSOR_DATA.AccelY,SENSOR_DATA.AccelZ, SENSOR_DATA.Proximity, SENSOR_DATA.UVindex, SENSOR_DATA.AmbientLightVis, SENSOR_DATA.AmbientLightIr, FLOW_URL_TYPE, MY_SERVER_URL);
-            break;
-        }
-        case TEMP_HUMIDITY_ACCELEROMETER_PMODSENSORS_VIRTUALSENSORS:
-        {
-            sprintf(modem_string, "GET %s%s?serial=%s&temp=%s&humidity=%s&accelX=%s&accelY=%s&accelZ=%s&proximity=%s&light_uv=%s&light_vis=%s&light_ir=%s&virt_sens1=%s&virt_sens2=%s&virt_sens3=%s&virt_sens4=%s&virt_sens5=%s&virt_sens6=%s&virt_sens7=%s&virt_sens8=%s %s%s\r\n\r\n", FLOW_BASE_URL, FLOW_INPUT_NAME, FLOW_DEVICE_NAME, SENSOR_DATA.Temperature, SENSOR_DATA.Humidity, SENSOR_DATA.AccelX,SENSOR_DATA.AccelY,SENSOR_DATA.AccelZ, SENSOR_DATA.Proximity, SENSOR_DATA.UVindex, SENSOR_DATA.AmbientLightVis, SENSOR_DATA.AmbientLightIr, SENSOR_DATA.Virtual_Sensor1, SENSOR_DATA.Virtual_Sensor2, SENSOR_DATA.Virtual_Sensor3, SENSOR_DATA.Virtual_Sensor4, SENSOR_DATA.Virtual_Sensor5, SENSOR_DATA.Virtual_Sensor6, SENSOR_DATA.Virtual_Sensor7, SENSOR_DATA.Virtual_Sensor8, FLOW_URL_TYPE, MY_SERVER_URL);
-            break;
-        }
-        default:
-        {
-            sprintf(modem_string, "Invalid sensor selected\r\n\r\n");
-            break;
-        }
-    } //switch(iSensorsToReport)
-} //GenerateModemString        
-            
-            
-//Periodic timer
-Ticker OneMsTicker;
-volatile bool bTimerExpiredFlag = false;
-int OneMsTicks = 0;
-int iTimer1Interval_ms = 1000;
-//********************************************************************************************************************************************
-//* Periodic 1ms timer tick
-//********************************************************************************************************************************************
-void OneMsFunction() 
+    g_sw2_flag = 0;
+}
+
+// SW3 event-triggered interrupt
+void sw3_isr_down()
+{
+    g_sw3_flag = 1;   // set flag in ISR
+}
+
+void sw3_isr_up()
 {
-    OneMsTicks++;
-    if ((OneMsTicks % iTimer1Interval_ms) == 0)
-    {
-        bTimerExpiredFlag = true;
-    }            
-} //OneMsFunction()
+    g_sw3_flag = 0;
+}
+
+void GenerateModemString(char * modem_string, char btn1, char btn2)
+{
+    sprintf(modem_string, "serial:%s button1:%c button2:%c\r\n",
+            M2X_DEVICE_SERIAL, btn1, btn2
+           );
+} //GenerateModemString
+
 
 //********************************************************************************************************************************************
 //* Set the RGB LED's Color
-//* LED Color 0=Off to 7=White.  3 bits represent BGR (bit0=Red, bit1=Green, bit2=Blue) 
+//* LED Color 0=Off to 7=White.  3 bits represent BGR (bit0=Red, bit1=Green, bit2=Blue)
 //********************************************************************************************************************************************
 void SetLedColor(unsigned char ucColor)
 {
@@ -106,81 +64,100 @@
 } //SetLedColor()
 
 //********************************************************************************************************************************************
-//* Process the JSON response.  In this example we are only extracting a LED color. 
+//* Process the JSON response.  In this example we are only extracting a LED color.
 //********************************************************************************************************************************************
 bool parse_JSON(char* json_string)
 {
     char* beginquote;
     char token[] = "\"LED\":\"";
     beginquote = strstr(json_string, token );
-    if ((beginquote != 0))
-    {
+    if ((beginquote != 0)) {
         char cLedColor = beginquote[strlen(token)];
         PRINTF(GRN "LED Found : %c" DEF "\r\n", cLedColor);
-        switch(cLedColor)
-        {
-            case 'O':
-            { //Off
+        switch(cLedColor) {
+            case 'O': {
+                //Off
                 SetLedColor(0);
                 break;
             }
-            case 'R':
-            { //Red
+            case 'R': {
+                //Red
                 SetLedColor(1);
                 break;
             }
-            case 'G':
-            { //Green
+            case 'G': {
+                //Green
                 SetLedColor(2);
                 break;
             }
-            case 'Y':
-            { //Yellow
+            case 'Y': {
+                //Yellow
                 SetLedColor(3);
                 break;
             }
-            case 'B':
-            { //Blue
+            case 'B': {
+                //Blue
                 SetLedColor(4);
                 break;
             }
-            case 'M':
-            { //Magenta
+            case 'M': {
+                //Magenta
                 SetLedColor(5);
                 break;
             }
-            case 'T':
-            { //Turquoise
+            case 'T': {
+                //Turquoise
                 SetLedColor(6);
                 break;
             }
-            case 'W':
-            { //White
+            case 'W': {
+                //White
                 SetLedColor(7);
                 break;
             }
-            default:
-            {
+            default: {
                 break;
             }
         } //switch(cLedColor)
         return true;
-    }
-    else
-    {
+    } else {
         return false;
     }
 } //parse_JSON
 
-int main() {
-    static unsigned ledOnce = 0;
+void send_button_data(char btn1, char btn2)
+{
+    char modem_string[512];
+    GenerateModemString(&modem_string[0], btn1, btn2);
+    char myJsonResponse[512];
+    if (cell_modem_Sendreceive(&modem_string[0], &myJsonResponse[0])) {
+        if (true) {
+            //ledOnce = 1;
+            SetLedColor(0x2); //Green
+        }
+        parse_JSON(&myJsonResponse[0]);
+    }
+}
+
+int main()
+{
+    //static unsigned ledOnce = 0;
 
     pc.baud(115200);
-    PRINTF(GRN "Hello World from the Cellular IoT Kit!\r\n\r\n");
+    PRINTF(GRN "Hello World from the Cellular IoT Kit!\r\n");
+    PRINTF(GRN "Cellular IoT Buttons!\r\n\r\n");
 
-    //Initialize the I2C sensors that are present
-    sensors_init();
-    read_sensors();
+    // SW have a pull-up resistor, so the pin will be at 3.3 V by default
+    // and fall to 0 V when pressed. We therefore need to look for a falling edge
+    // on the pin to fire the interrupt
+    sw3.fall(&sw3_isr_down);
+    sw3.rise(&sw3_isr_up);
+    sw2.fall(&sw2_isr_down);
+    sw2.rise(&sw2_isr_up);
+    // since SW have an external pull-up, we should disable to internal pull-down
+    // resistor that is enabled by default using InterruptIn
+    sw3.mode(PullNone);
+    sw2.mode(PullNone);
 
     // Set LED to RED until init finishes
     SetLedColor(0x1); //Red
@@ -190,31 +167,25 @@
     // Set LED BLUE for partial init
     SetLedColor(0x4); //Blue
 
-    //Create a 1ms timer tick function:
-    iTimer1Interval_ms = SENSOR_UPDATE_INTERVAL_MS;
-    OneMsTicker.attach(OneMsFunction, 0.001f) ;
-
     // Send and receive data perpetually
     while(1) {
-        #ifdef USE_VIRTUAL_SENSORS
-        ProcessUsbInterface();
-        #endif
-        if  (bTimerExpiredFlag)
-        {
-            bTimerExpiredFlag = false;
-            read_sensors(); //read available external sensors from a PMOD and the on-board motion sensor
-            char modem_string[512];
-            GenerateModemString(&modem_string[0]);
-            char myJsonResponse[512];
-            if (cell_modem_Sendreceive(&modem_string[0], &myJsonResponse[0]))
-            {
-                if (!ledOnce)
-                {
-                    ledOnce = 1;
-                    SetLedColor(0x2); //Green
-                }
-                parse_JSON(&myJsonResponse[0]);
-            }
-        } //bTimerExpiredFlag
+        if (g_sw3_flag) {
+            printf("%s\n","SW3 Button");
+            led_green = 0;
+            send_button_data('0','1');
+        } else {
+            led_green = 1;
+        }
+
+        if (g_sw2_flag) {
+            printf("%s\n","SW2 Button");
+            led_blue = 0;
+            send_button_data('1','0');
+        } else {
+            led_blue = 1;
+        }
+
+        // put the MCU to sleep until an interrupt wakes it up
+        sleep();
     } //forever loop
 }