Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: FXOS8700CQ MODSERIAL mbed
main.cpp
00001 #include "mbed.h" 00002 #include <cctype> 00003 #include <string> 00004 #include "config_me.h" 00005 #include "cell_modem.h" 00006 #include "hardware.h" 00007 00008 I2C i2c(PTC11, PTC10); //SDA, SCL -- define the I2C pins being used 00009 MODSERIAL pc(USBTX, USBRX, 256, 256); // tx, rx with default tx, rx buffer sizes 00010 MODSERIAL mdm(PTD3, PTD2, 4096, 4096); 00011 DigitalOut led_green(LED_GREEN); 00012 DigitalOut led_red(LED_RED); 00013 DigitalOut led_blue(LED_BLUE); 00014 00015 // interrupts for buttons 00016 InterruptIn sw3(SW3); 00017 InterruptIn sw2(SW2); 00018 00019 // flag - must be volatile as changes within ISR 00020 // g_ prefix makes it easier to distinguish it as global 00021 volatile int g_sw3_flag = 0; 00022 volatile int g_sw2_flag = 0; 00023 00024 // SW2 event-triggered interrupt 00025 // ISR = interrupt service routine 00026 // down = button was pushed down/falling 00027 // up = button was released/rising 00028 void sw2_isr_down() 00029 { 00030 g_sw2_flag = 1; // set flag in ISR 00031 } 00032 00033 void sw2_isr_up() 00034 { 00035 g_sw2_flag = 0; 00036 } 00037 00038 // SW3 event-triggered interrupt 00039 void sw3_isr_down() 00040 { 00041 g_sw3_flag = 1; // set flag in ISR 00042 } 00043 00044 void sw3_isr_up() 00045 { 00046 g_sw3_flag = 0; 00047 } 00048 00049 void GenerateModemString(char * modem_string, char btn1, char btn2) 00050 { 00051 sprintf(modem_string, "serial:%s button1:%c button2:%c\r\n", 00052 M2X_DEVICE_SERIAL, btn1, btn2 00053 ); 00054 00055 // HTTP method 00056 /*sprintf(modem_string, "GET %s%s?serial=%s&button1=%c&button2=%c %s%s\r\n\r\n", 00057 FLOW_BASE_PATH, FLOW_INPUT_NAME, M2X_DEVICE_NAME, 00058 btn1, btn2, 00059 FLOW_URL_TYPE, MY_SERVER_NAME); */ 00060 00061 } //GenerateModemString 00062 00063 00064 //******************************************************************************************************************************************** 00065 //* Set the RGB LED's Color 00066 //* LED Color 0=Off to 7=White. 3 bits represent BGR (bit0=Red, bit1=Green, bit2=Blue) 00067 //******************************************************************************************************************************************** 00068 void SetLedColor(unsigned char ucColor) 00069 { 00070 //Note that when an LED is on, you write a 0 to it: 00071 led_red = !(ucColor & 0x1); //bit 0 00072 led_green = !(ucColor & 0x2); //bit 1 00073 led_blue = !(ucColor & 0x4); //bit 2 00074 } //SetLedColor() 00075 00076 //******************************************************************************************************************************************** 00077 //* Process the JSON response. In this example we are only extracting a LED color. 00078 //******************************************************************************************************************************************** 00079 bool parse_JSON(char* json_string) 00080 { 00081 char* beginquote; 00082 char token[] = "\"LED\":\""; 00083 beginquote = strstr(json_string, token ); 00084 if ((beginquote != 0)) { 00085 char cLedColor = beginquote[strlen(token)]; 00086 PRINTF(GRN "LED Found : %c" DEF "\r\n", cLedColor); 00087 switch(cLedColor) { 00088 case 'O': { 00089 //Off 00090 SetLedColor(0); 00091 break; 00092 } 00093 case 'R': { 00094 //Red 00095 SetLedColor(1); 00096 break; 00097 } 00098 case 'G': { 00099 //Green 00100 SetLedColor(2); 00101 break; 00102 } 00103 case 'Y': { 00104 //Yellow 00105 SetLedColor(3); 00106 break; 00107 } 00108 case 'B': { 00109 //Blue 00110 SetLedColor(4); 00111 break; 00112 } 00113 case 'M': { 00114 //Magenta 00115 SetLedColor(5); 00116 break; 00117 } 00118 case 'T': { 00119 //Turquoise 00120 SetLedColor(6); 00121 break; 00122 } 00123 case 'W': { 00124 //White 00125 SetLedColor(7); 00126 break; 00127 } 00128 default: { 00129 break; 00130 } 00131 } //switch(cLedColor) 00132 return true; 00133 } else { 00134 return false; 00135 } 00136 } //parse_JSON 00137 00138 void send_button_data(char btn1, char btn2) 00139 { 00140 char modem_string[512]; 00141 GenerateModemString(&modem_string[0], btn1, btn2); 00142 char myJsonResponse[512]; 00143 if (cell_modem_Sendreceive(&modem_string[0], &myJsonResponse[0])) { 00144 if (true) { 00145 //ledOnce = 1; 00146 SetLedColor(0x2); //Green 00147 } 00148 parse_JSON(&myJsonResponse[0]); 00149 } 00150 } 00151 00152 int main() 00153 { 00154 //static unsigned ledOnce = 0; 00155 00156 pc.baud(115200); 00157 PRINTF(GRN "Hello World from the Cellular IoT Kit!\r\n"); 00158 PRINTF(GRN "Cellular IoT Buttons!\r\n\r\n"); 00159 00160 // SW have a pull-up resistor, so the pin will be at 3.3 V by default 00161 // and fall to 0 V when pressed. We therefore need to look for a falling edge 00162 // on the pin to fire the interrupt 00163 sw3.fall(&sw3_isr_down); 00164 sw3.rise(&sw3_isr_up); 00165 sw2.fall(&sw2_isr_down); 00166 sw2.rise(&sw2_isr_up); 00167 // since SW have an external pull-up, we should disable to internal pull-down 00168 // resistor that is enabled by default using InterruptIn 00169 sw3.mode(PullNone); 00170 sw2.mode(PullNone); 00171 00172 // Set LED to RED until init finishes 00173 SetLedColor(0x1); //Red 00174 // Initialize the modem 00175 PRINTF("\r\n"); 00176 cell_modem_init(); 00177 // Set LED BLUE for partial init 00178 SetLedColor(0x4); //Blue 00179 00180 // Send and receive data perpetually 00181 while(1) { 00182 if (g_sw3_flag) { 00183 printf("%s\n","SW3 Button"); 00184 led_green = 0; 00185 send_button_data('0','1'); 00186 } else { 00187 led_green = 1; 00188 } 00189 00190 if (g_sw2_flag) { 00191 printf("%s\n","SW2 Button"); 00192 led_blue = 0; 00193 send_button_data('1','0'); 00194 } else { 00195 led_blue = 1; 00196 } 00197 00198 // put the MCU to sleep until an interrupt wakes it up 00199 sleep(); 00200 } //forever loop 00201 }
Generated on Wed Jul 13 2022 09:36:47 by
1.7.2