Pubnub demo for AT&T IoT Starter Kit. Functionally similar to the Flow demo.
Dependencies: FXOS8700CQ MODSERIAL mbed
Pubnub demo for AT&T IoT Starter Kit
This demo is functionally similar to the Flow demo, so you can find general information here: https://developer.mbed.org/users/JMF/code/Avnet_ATT_Cellular_IOT/.
The only difference is that we use Pubnub to publish the measurements and subscribe to receiving the instructions to set the LED.
Settings
Pubnub related settings are:
Pubnub settings in `config_me.h`
PUBNUB_SUBSCRIBE_KEY PUBNUB_PUBLISH_KEY PUBNUB_CHANNEL
All are documented in their respective comments.
Pubnub context class
Similar to Pubnub SDKs, we provide a Pubnub context class. It is defined in pubnub.h
header file and implemented in pubnub.cpp
.
It provides only the fundamental "publish" and "subscribe" methods. They are documented in the header file.
This class is reusable in other code (it is not specific to this demo), it has a very narrow interface to the AT&T IoT cellular modem code. For example of use, you can look at the main()
(in main.c
).
Sample of published data
Published message w/measurement data
{"serial":"vstarterkit001","temp":89.61,"humidity":35,"accelX":0.97,"accelY":0.013,"accelZ":-0.038}
Don't worry, nobody got burnt, the temperature is in degrees Fahrenheit. :)
Publish a message (from, say, the Pubnub console http://pubnub.com/console) of the form {"LED":<name-of-the-color>}
on the channel that this demo listens to (default is hello_world
) to turn the LED to that color on the Starter Kit:
Turn LED to red
{"LED":"Red"}
Turn LED to green
{"LED":"Green"}
Turn LED to blue
{"LED":"Blue"}
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