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:
42:be4b9ee3a615
Parent:
40:aec4d09fde23
Child:
43:6821a9c78c4b
--- a/wnc_control.cpp	Sat Jul 23 20:13:01 2016 +0000
+++ b/wnc_control.cpp	Sun Jul 24 03:10:21 2016 +0000
@@ -27,13 +27,46 @@
 
 void software_init_mdm(void)
 {
+  static bool reportStatus = true;
   do
   {
-    WNC_MDM_ERR = WNC_OK;
-    at_init_wnc();
-    if (WNC_MDM_ERR == WNC_NO_RESPONSE)
-      reinitialize_mdm();
-  } while (WNC_MDM_ERR != WNC_OK); 
+      if (check_wnc_ready() == 0)
+      {
+          if (reportStatus == false)
+          {
+              puts("Re-connected to cellular network!\n\r");
+              reportStatus = true;
+          }
+    
+          // WNC has SIM and registered on network
+          do
+          {
+              WNC_MDM_ERR = WNC_OK;
+              at_init_wnc();
+              if (WNC_MDM_ERR == WNC_NO_RESPONSE)
+              {
+                  reinitialize_mdm();
+                  at_init_wnc(true);  // Hard reset occurred so make it go through the software init();
+              }
+          } while (WNC_MDM_ERR != WNC_OK);
+      }
+      else
+      {
+          if (reportStatus == true)
+          {
+               puts("Not connected to cellular network!\n\r");
+               reportStatus = false;
+          }
+     // Atempt to re-register
+//     string * pRespStr;
+//     pc.puts("Force re-register!\r\n");
+//     at_send_wnc_cmd("AT+CFUN=0,0", &pRespStr, WNC_TIMEOUT_MS);
+//     wait_ms(31000);
+//     at_send_wnc_cmd("AT+CFUN=1,0", &pRespStr, WNC_TIMEOUT_MS);
+//     wait_ms(31000);
+          WNC_MDM_ERR = WNC_NO_RESPONSE;
+      }
+  } while (WNC_MDM_ERR != WNC_OK);
 }
 
 void resolve_mdm(void)
@@ -44,7 +77,6 @@
       at_dnsresolve_wnc(MY_SERVER_URL, &MyServerIpAddress);
       if (WNC_MDM_ERR == WNC_NO_RESPONSE)
       {
-        reinitialize_mdm();
         software_init_mdm();
       }
       else if (WNC_MDM_ERR == WNC_CMD_ERR)
@@ -62,13 +94,10 @@
 {
     do
     {
-      at_at_wnc();
-      at_at_wnc();
       WNC_MDM_ERR = WNC_OK;
       at_sockopen_wnc(MyServerIpAddress, MY_PORT_STR);
       if (WNC_MDM_ERR == WNC_NO_RESPONSE)
       {
-        reinitialize_mdm();
         software_init_mdm();
       }
       else if (WNC_MDM_ERR == WNC_CMD_ERR)
@@ -88,14 +117,12 @@
       at_sockwrite_wnc(s);
       if (WNC_MDM_ERR == WNC_NO_RESPONSE)
       {
-        reinitialize_mdm();
+        pc.puts("Sock write no response!\r\n");
         software_init_mdm();
       }
       else if (WNC_MDM_ERR == WNC_CMD_ERR)
       {
         pc.puts("Socket Write fail!!!\r\n");
-        // Have seen when write fails modem gets stuck in bad state, try to recover
-        reinitialize_mdm();
         software_init_mdm();
       }
     } while (WNC_MDM_ERR != WNC_OK);
@@ -120,10 +147,7 @@
       if (WNC_MDM_ERR == WNC_NO_RESPONSE)
       {
         if (n == 0)
-        {
-            reinitialize_mdm();
             software_init_mdm();
-        }
         else
             puts("Sock read partial data!!!\r\n");
       }
@@ -149,7 +173,6 @@
       socketOpen = 0;
       if (WNC_MDM_ERR == WNC_NO_RESPONSE)
       {
-        reinitialize_mdm();
         software_init_mdm();
       }
       else if (WNC_MDM_ERR == WNC_CMD_ERR)
@@ -196,21 +219,92 @@
 
 extern int mdm_sendAtCmdRsp(const char *cmd, const char **rsp_list, int timeout_ms, string * rsp, int * len);
 
+int check_wnc_ready(void)
+{
+  string * pRespStr;
+  size_t pos;
+  int regSts;
+
+  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 SIM Card not ready don't bother with commands!
+  if (pRespStr->find("CPIN: READY") == string::npos)
+  {
+      pc.puts("------------ WNC SIM Problem! --------->\r\n");
+      return (-1);
+  }
+  
+  // SIM card OK, now check for signal and cellular network registration
+//  do
+//  {
+    at_send_wnc_cmd("AT+CREG?", &pRespStr, WNC_TIMEOUT_MS);      // Check if registered on network
+    pos = pRespStr->find("CREG: ");
+    if (pos != string::npos)
+    {
+      // The registration is the 2nd arg in the comma separated list
+      *pRespStr = pRespStr->substr(pos+8, 1);
+      regSts = atoi(pRespStr->c_str());
+      // 1 - registered home, 5 - registered roaming
+      if ((regSts != 1) && (regSts != 5))
+      {
+          pc.puts("------------ WNC Cell Link Down! ------>\r\n");
+          return (-2);
+      }
+    }
+//  } while ((regSts != 1) && (regSts !=5));
+  pc.puts("------------ WNC Ready ---------------->\r\n");
+  
+  return (0);
+}
+
 // 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)
 {
+  if (check_wnc_ready() < 0)
+  {
+     string truncStr(s, 50);
+     pc.puts("FAIL send cmd: ");
+     pc.puts(truncStr.c_str());
+     pc.puts("\r\n");
+     WNC_MDM_ERR = WNC_NO_RESPONSE;
+     return (-2);
+  }
+  
+  // If WNC ready, send user command
+  return (at_send_wnc_cmd(s, r, ms_timeout));
+}
+
+int at_send_wnc_cmd(const char * s, string ** r, int ms_timeout)
+{
   static const char * rsp_lst[] = { "OK", "ERROR", NULL };
   int len;
-  
-  pc.printf("Send: %s\r\n",s);
+  if (strlen(s) > 60)
+  {
+      string truncStr(s,57);
+      truncStr += "...";
+      pc.printf("Send: <<%s>>\r\n",truncStr.c_str());
+  }
+  else
+      pc.printf("Send: <<%s>>\r\n",s);
+    
   int res = mdm_sendAtCmdRsp(s, rsp_lst, ms_timeout, &wncStr, &len);
   *r = &wncStr;   // Return a pointer to the static string
       
   if (res >= 0)
-  {
+  {   
       pc.puts("[");
-      pc.puts(wncStr.c_str());
+      if (wncStr.size() < 51)
+          pc.puts(wncStr.c_str());
+      else
+      {
+          string truncStr = wncStr.substr(0,50) + "...";
+          pc.puts(truncStr.c_str());
+      }
       pc.puts("]\n\r");
+
       if (res > 0)
       {
           if (WNC_MDM_ERR != WNC_NO_RESPONSE)
@@ -228,50 +322,62 @@
   }
 }
 
+
 void at_at_wnc(void)
 {
     string * pRespStr;
     send_wnc_cmd("AT", &pRespStr, WNC_TIMEOUT_MS); // Heartbeat?
 }
 
-void at_init_wnc(void)
+void at_init_wnc(bool hardReset)
 {
   static bool pdnSet = false;
+  static bool intSet = false;
+  static bool sockDialSet = false;  
+  string * pRespStr;
   
-  string * pRespStr;
+  if (hardReset == true)
+  {
+      pdnSet = false;
+      intSet = false;
+      sockDialSet = false;
+  }
+  
   pc.puts("Start AT init of WNC:\r\n");
   send_wnc_cmd("AT", &pRespStr, WNC_TIMEOUT_MS);             // Heartbeat?
-  send_wnc_cmd("ATE0", &pRespStr, WNC_TIMEOUT_MS);           // Echo ON
-  send_wnc_cmd("AT", &pRespStr, WNC_TIMEOUT_MS);             // Heartbeat?
+  send_wnc_cmd("ATE0", &pRespStr, WNC_TIMEOUT_MS);           // Echo Off
   send_wnc_cmd("AT+CMEE=2", &pRespStr, WNC_TIMEOUT_MS);      // 2 - verbose error, 1 - numeric error, 0 - just ERROR
-  do {
+  
+  if ((WNC_MDM_ERR == WNC_OK) && (intSet == false))
+    send_wnc_cmd("AT@INTERNET=1", &pRespStr, WNC_TIMEOUT_MS);
+  
+  if (WNC_MDM_ERR == WNC_OK)
+    intSet = true;
+  else
+    return ;
+  
   if (pdnSet == false)
   {
-    send_wnc_cmd("AT", &pRespStr, WNC_TIMEOUT_MS);             // Heartbeat?
-    send_wnc_cmd("AT@INTERNET=1", &pRespStr, WNC_TIMEOUT_MS);
     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
-    send_wnc_cmd("AT", &pRespStr, WNC_TIMEOUT_MS);             // Heartbeat?
+  }
+  
+  if (WNC_MDM_ERR == WNC_OK)
+    pdnSet = true;
+  else
+    return ;
+  
+  if (sockDialSet == false)
     send_wnc_cmd("AT@SOCKDIAL=1", &pRespStr, WNC_TIMEOUT_MS);
-  }
-  }
-  while (WNC_MDM_ERR != WNC_OK);
-  // One time during power up to set the APN
-  pdnSet = true;
-
-//  send_wnc_cmd("AT", &pRespStr, WNC_TIMEOUT_MS);             // Heartbeat?
-//  send_wnc_cmd("AT+CFUN=0", &pRespStr, WNC_TIMEOUT_MS);     // Try to force on network pt1
-//  send_wnc_cmd("AT", &pRespStr, WNC_TIMEOUT_MS);             // Heartbeat?
-//  send_wnc_cmd("AT+CFUN=1", &pRespStr, WNC_TIMEOUT_MS);     // Try to force on network pt2
-//  send_wnc_cmd("AT", &pRespStr, WNC_TIMEOUT_MS);             // Heartbeat?
-  pc.puts("WNC Status:\r\n");
-  send_wnc_cmd("AT@SOCKDIAL?", &pRespStr, WNC_TIMEOUT_MS);
-  send_wnc_cmd("AT+CPIN?", &pRespStr, WNC_TIMEOUT_MS);      // Check if SIM locked
-  send_wnc_cmd("AT+CREG?", &pRespStr, WNC_TIMEOUT_MS);      // Check if registered on network
-  send_wnc_cmd("AT+CSQ", &pRespStr, WNC_TIMEOUT_MS);        // Check RSSI
-  pc.puts("End AT init of WNC\r\n");
+  
+  if (WNC_MDM_ERR == WNC_OK)
+    sockDialSet = true;
+  else
+    return ;
+  
+  pc.puts("SUCCESS: AT init of WNC!\r\n");
 }
 
 void at_sockopen_wnc(const string & ipStr, const char * port )
@@ -284,8 +390,6 @@
   cmd_str += port;
   cmd_str += ",30";
   send_wnc_cmd(cmd_str.c_str(), &pRespStr, 31000);
-  send_wnc_cmd("AT@SOCKCREAT?", &pRespStr, WNC_TIMEOUT_MS);
-  send_wnc_cmd("AT@SOCKCONN?", &pRespStr, WNC_TIMEOUT_MS);
 }
 
 void at_sockclose_wnc(void)
@@ -353,7 +457,7 @@
       cmd_str += num2str;
     }
     cmd_str += "\"";
-    send_wnc_cmd(cmd_str.c_str(), &pRespStr, 31000);
+    send_wnc_cmd(cmd_str.c_str(), &pRespStr, 120000);
   }
   else
     pc.puts("sockwrite Err, string to long\r\n");