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.
Fork of StarterKit by
Diff: main.cpp
- Revision:
- 16:17c5916f2d12
- Parent:
- 14:0c353e212296
- Child:
- 17:38a8cc0c6ba5
--- a/main.cpp Wed Jul 13 00:39:08 2016 +0000 +++ b/main.cpp Wed Jul 13 06:10:58 2016 +0000 @@ -40,8 +40,9 @@ Serial pc(USBTX, USBRX); SerialBuffered mdm(PTD3, PTD2, 128); -DigitalOut led_red(LED_RED); -DigitalOut led_green(LED_GREEN); +DigitalOut led_green(LED_GREEN); +DigitalOut led_red(LED_RED); +DigitalOut led_blue(LED_BLUE); DigitalOut mdm_uart2_rx_boot_mode_sel(PTC17); // on powerup, 0 = boot mode, 1 = normal boot DigitalOut mdm_power_on(PTB9); // 0 = turn modem on, 1 = turn modem off (should be held high for >5 seconds to cycle modem) @@ -310,7 +311,7 @@ sprintf(modem_string, "Invalid sensor selected\r\n\r\n"); break; } - } //switch(*ucCommandIndex) + } //switch(iSensorsToReport) } //GenerateModemString @@ -331,6 +332,116 @@ } } //OneMsFunction() +//******************************************************************************************************************************************** +//* Set the RGB LED's Color +//* LED Color 0=Off to 7=White. 3 bits represent BGR (bit0=Red, bit1=Green, bit2=Blue) +//******************************************************************************************************************************************** +void SetLedColor(unsigned char ucColor) +{ + //Note that when an LED is on, you write a 0 to it: + led_red = !(ucColor & 0x1); //bit 0 + led_green = !(ucColor & 0x2); //bit 1 + led_blue = !(ucColor & 0x4); //bit 2 +} //SetLedColor() + +//******************************************************************************************************************************************** +//* Process JSON response messages +//******************************************************************************************************************************************** +bool extract_JSON(char* search_field, char* found_string) +{ + char* beginquote; + char* endquote; + beginquote = strchr(search_field, '{'); //start of JSON + endquote = strchr(search_field, '}'); //end of JSON + if (beginquote != 0) + { + uint16_t ifoundlen; + if (endquote != 0) + { + ifoundlen = (uint16_t) (endquote - beginquote) + 1; + strncpy(found_string, beginquote, ifoundlen ); + found_string[ifoundlen] = 0; //null terminate + return true; + } + else + { + endquote = strchr(search_field, '\0'); //end of string... sometimes the end bracket is missing + ifoundlen = (uint16_t) (endquote - beginquote) + 1; + strncpy(found_string, beginquote, ifoundlen ); + found_string[ifoundlen] = 0; //null terminate + return false; + } + } + else + { + return false; + } +} //extract_JSON + +bool parse_JSON(char* json_string) +{ + char* beginquote; + char token[] = "\"LED\":\""; + beginquote = strstr(json_string, token ); + if ((beginquote != 0)) + { + char cLedColor = beginquote[strlen(token)]; + printf(GRN "LED Found : %c" DEF "\r\n", cLedColor); + switch(cLedColor) + { + case 'O': + { //Off + SetLedColor(0); + break; + } + case 'R': + { //Red + SetLedColor(1); + break; + } + case 'G': + { //Green + SetLedColor(2); + break; + } + case 'Y': + { //Yellow + SetLedColor(3); + break; + } + case 'B': + { //Blue + SetLedColor(4); + break; + } + case 'M': + { //Magenta + SetLedColor(5); + break; + } + case 'T': + { //Turquoise + SetLedColor(6); + break; + } + case 'W': + { //White + SetLedColor(7); + break; + } + default: + { + break; + } + } //switch(cLedColor) + return true; + } + else + { + return false; + } +} //parse_JSON + int main() { int i; HTS221 hts221; @@ -350,6 +461,7 @@ printf("Temp is: %0.2f F \n\r",CTOF(hts221.readTemperature())); printf("Humid is: %02d %%\n\r",hts221.readHumidity()); + SetLedColor(0); //Off sensors_init(); read_sensors(); @@ -383,9 +495,21 @@ sockopen_mdm(); char modem_string[512]; GenerateModemString(&modem_string[0]); - printf(DEF "Sending to modem : %s\n", modem_string); + printf(BLU "Sending to modem : %s" DEF "\n", modem_string); sockwrite_mdm(modem_string); sockread_mdm(&MySocketData, 1024, 20); + printf(BLU "Read back : %s" DEF "\n", &MySocketData[0]); + char * myJsonResponse; + if (extract_JSON(&MySocketData[0], &myJsonResponse[0])) + { + printf(GRN "JSON : %s" DEF "\n", &myJsonResponse[0]); + parse_JSON(&myJsonResponse[0]); + } + else + { + printf(RED "JSON : %s" DEF "\n", &myJsonResponse[0]); //most likely an incomplete JSON string + parse_JSON(&myJsonResponse[0]); //This is risky, as the string may be corrupted + } sockclose_mdm(); } //bTimerExpiredFlag } //forever loop