Pubnub demo for AT&T IoT Starter Kit. Functionally similar to the Flow demo.

Dependencies:   FXOS8700CQ MODSERIAL mbed

http://pubnub.github.io/slides/workshop/pictures/broadcast.png

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"}
Revision:
46:da9d788f5d5a
Parent:
45:a836eecd5d12
Child:
53:dcccf2881fa0
diff -r a836eecd5d12 -r da9d788f5d5a wnc_control.cpp
--- a/wnc_control.cpp	Sun Jul 24 17:50:42 2016 +0000
+++ b/wnc_control.cpp	Sun Jul 24 19:33:23 2016 +0000
@@ -15,7 +15,8 @@
 enum WNC_ERR_e {
     WNC_OK =0,
     WNC_CMD_ERR = -1,
-    WNC_NO_RESPONSE = -2
+    WNC_NO_RESPONSE = -2,
+    WNC_CELL_LINK_DOWN = -3
 };
 
 // Contains result of last call to send_wnc_cmd(..)
@@ -64,7 +65,7 @@
 //     wait_ms(31000);
 //     at_send_wnc_cmd("AT+CFUN=1,0", &pRespStr, WNC_TIMEOUT_MS);
 //     wait_ms(31000);
-          WNC_MDM_ERR = WNC_NO_RESPONSE;
+          WNC_MDM_ERR = WNC_CELL_LINK_DOWN;
       }
   } while (WNC_MDM_ERR != WNC_OK);
 }
@@ -82,8 +83,6 @@
       else if (WNC_MDM_ERR == WNC_CMD_ERR)
       {
         pc.puts("Bad URL!!!!!!\r\n");
-        MyServerIpAddress = "192.168.0.1";
-        WNC_MDM_ERR = WNC_OK;
       }
     } while (WNC_MDM_ERR != WNC_OK);
     
@@ -224,13 +223,14 @@
     string * pRespStr;
     size_t pos;
     int regSts;
+    int cmdRes1, cmdRes2;
 
     pc.puts("<-------- Begin Cell Status ------------\r\n");
-  
-    at_send_wnc_cmd("AT+CSQ", &pRespStr, WNC_TIMEOUT_MS);       // Check RSSI,BER
-    at_send_wnc_cmd("AT+CPIN?", &pRespStr, WNC_TIMEOUT_MS);      // Check if SIM locked
-  
-    if (WNC_MDM_ERR == WNC_NO_RESPONSE)
+
+    cmdRes1 = at_send_wnc_cmd("AT+CSQ", &pRespStr, WNC_TIMEOUT_MS);       // Check RSSI,BER
+    cmdRes2 = at_send_wnc_cmd("AT+CPIN?", &pRespStr, WNC_TIMEOUT_MS);      // Check if SIM locked
+    
+    if ((cmdRes1 != 0) && (cmdRes2 != 0))
     {
         pc.puts("------------ WNC No Response! --------->\r\n");
         return (-2);      
@@ -244,7 +244,7 @@
     }
   
     // SIM card OK, now check for signal and cellular network registration
-    at_send_wnc_cmd("AT+CREG?", &pRespStr, WNC_TIMEOUT_MS);      // Check if registered on network
+    cmdRes1 = at_send_wnc_cmd("AT+CREG?", &pRespStr, WNC_TIMEOUT_MS);      // Check if registered on network
     pos = pRespStr->find("CREG: ");
     if (pos != string::npos)
     {
@@ -267,6 +267,8 @@
 // Sets a global with failure or success, assumes 1 thread all the time
 int send_wnc_cmd(const char * s, string ** r, int ms_timeout)
 {
+  int cmdRes;
+  
   if (check_wnc_ready() < 0)
   {
      static string noRespStr;
@@ -274,14 +276,25 @@
      pc.puts("FAIL send cmd: ");
      pc.puts(truncStr.c_str());
      pc.puts("\r\n");
-     WNC_MDM_ERR = WNC_NO_RESPONSE;
+     WNC_MDM_ERR = WNC_CELL_LINK_DOWN;
      noRespStr.erase();
      *r = &noRespStr;
-     return (-2);
+     return (-3);
   }
   
   // If WNC ready, send user command
-  return (at_send_wnc_cmd(s, r, ms_timeout));
+  cmdRes = at_send_wnc_cmd(s, r, ms_timeout);
+  
+  if (cmdRes == -1)
+     WNC_MDM_ERR = WNC_CMD_ERR;
+  
+  if (cmdRes == -2)
+     WNC_MDM_ERR = WNC_NO_RESPONSE;
+
+  if (cmdRes == 0)
+     WNC_MDM_ERR = WNC_OK;
+  
+  return (cmdRes);
 }
 
 int at_send_wnc_cmd(const char * s, string ** r, int ms_timeout)
@@ -313,17 +326,12 @@
       pc.puts("]\n\r");
 
       if (res > 0)
-      {
-          if (WNC_MDM_ERR != WNC_NO_RESPONSE)
-            WNC_MDM_ERR = WNC_CMD_ERR;
           return -1;
-      }
       else
           return 0;
   }
   else
   {
-      WNC_MDM_ERR = WNC_NO_RESPONSE;
       pc.puts("No response from WNC!\n\r");
       return -2;
   }
@@ -342,6 +350,7 @@
   static bool intSet = false;
   static bool sockDialSet = false;  
   string * pRespStr;
+  int cmdRes;
   
   if (hardReset == true)
   {
@@ -351,22 +360,27 @@
       sockDialSet = false;
   }
   
-  WNC_MDM_ERR = WNC_OK;  // Below commands will re-establish error state
   pc.puts("Start AT init of WNC:\r\n");
   // Quick commands below do not need to check cellular connectivity
-  at_send_wnc_cmd("AT", &pRespStr, WNC_TIMEOUT_MS);             // Heartbeat?
-  at_send_wnc_cmd("ATE0", &pRespStr, WNC_TIMEOUT_MS);           // Echo Off
-  at_send_wnc_cmd("AT+CMEE=2", &pRespStr, WNC_TIMEOUT_MS);      // 2 - verbose error, 1 - numeric error, 0 - just ERROR
+  cmdRes = at_send_wnc_cmd("AT", &pRespStr, WNC_TIMEOUT_MS);             // Heartbeat?
+  cmdRes += at_send_wnc_cmd("ATE0", &pRespStr, WNC_TIMEOUT_MS);           // Echo Off
+  cmdRes += at_send_wnc_cmd("AT+CMEE=2", &pRespStr, WNC_TIMEOUT_MS);      // 2 - verbose error, 1 - numeric error, 0 - just ERROR
   
   // If the simple commands are not working no chance of more complex.
   //  I have seen re-trying commands make it worse.
-  if (WNC_MDM_ERR != WNC_OK)
+  if (cmdRes < 0)
+  {
+      // Since I used the at_send_wnc_cmd I am setting the error state based upon
+      //  the responses.  And since these are simple commands, even if the WNC
+      //  is saying ERROR, treat it like a no response.
+      WNC_MDM_ERR = WNC_NO_RESPONSE;
       return ;
+  }
   
-  if ((WNC_MDM_ERR == WNC_OK) && (intSet == false))
-    send_wnc_cmd("AT@INTERNET=1", &pRespStr, WNC_TIMEOUT_MS);
+  if (intSet == false)
+    cmdRes = send_wnc_cmd("AT@INTERNET=1", &pRespStr, WNC_TIMEOUT_MS);
   
-  if (WNC_MDM_ERR == WNC_OK)
+  if (cmdRes == 0)
     intSet = true;
   else
     return ;
@@ -376,18 +390,18 @@
     string cmd_str("AT%PDNSET=1,");
     cmd_str += MY_APN_STR;
     cmd_str += ",IP";
-    send_wnc_cmd(cmd_str.c_str(), &pRespStr, 4*WNC_TIMEOUT_MS); // Set APN, cmd seems to take a little longer sometimes
+    cmdRes = send_wnc_cmd(cmd_str.c_str(), &pRespStr, 4*WNC_TIMEOUT_MS); // Set APN, cmd seems to take a little longer sometimes
   }
   
-  if (WNC_MDM_ERR == WNC_OK)
+  if (cmdRes == 0)
     pdnSet = true;
   else
     return ;
   
   if (sockDialSet == false)
-    send_wnc_cmd("AT@SOCKDIAL=1", &pRespStr, WNC_TIMEOUT_MS);
+    cmdRes = send_wnc_cmd("AT@SOCKDIAL=1", &pRespStr, WNC_TIMEOUT_MS);
   
-  if (WNC_MDM_ERR == WNC_OK)
+  if (cmdRes == 0)
     sockDialSet = true;
   else
     return ;
@@ -418,7 +432,7 @@
   string * pRespStr;
   string str(s);
   str = "AT@DNSRESVDON=\"" + str + "\"";
-  if (send_wnc_cmd(str.c_str(), &pRespStr, WNC_TIMEOUT_MS) == 0)
+  if (send_wnc_cmd(str.c_str(), &pRespStr, 15000) == 0)
   {
     size_t pos_start = pRespStr->find(":\"") + 2;
     size_t pos_end = pRespStr->rfind("\"") - 1;