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:
43:6821a9c78c4b
Parent:
42:be4b9ee3a615
Child:
44:c95a85b5cf92
--- a/wnc_control.cpp	Sun Jul 24 03:10:21 2016 +0000
+++ b/wnc_control.cpp	Sun Jul 24 16:58:17 2016 +0000
@@ -221,43 +221,47 @@
 
 int check_wnc_ready(void)
 {
-  string * pRespStr;
-  size_t pos;
-  int regSts;
+    string * pRespStr;
+    size_t pos;
+    int regSts;
 
-  pc.puts("<-------- Begin Cell Status ------------\r\n");
+    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
+    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);
-  }
+    if (WNC_MDM_ERR == WNC_NO_RESPONSE)
+    {
+        pc.puts("------------ WNC No Response! --------->\r\n");
+        return (-2);      
+    }
   
-  // SIM card OK, now check for signal and cellular network registration
-//  do
-//  {
+    // 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
     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);
-      }
+        // 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");
+
+    pc.puts("------------ WNC Ready ---------------->\r\n");
   
-  return (0);
+    return (0);
 }
 
 // Sets a global with failure or success, assumes 1 thread all the time
@@ -338,15 +342,23 @@
   
   if (hardReset == true)
   {
+      pc.puts("Hard Reset!\r\n");
       pdnSet = false;
       intSet = false;
       sockDialSet = false;
   }
   
+  WNC_MDM_ERR = WNC_OK;  // Below commands will re-establish error state
   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 Off
-  send_wnc_cmd("AT+CMEE=2", &pRespStr, WNC_TIMEOUT_MS);      // 2 - verbose error, 1 - numeric error, 0 - just ERROR
+  // 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
+  
+  // 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)
+      return ;
   
   if ((WNC_MDM_ERR == WNC_OK) && (intSet == false))
     send_wnc_cmd("AT@INTERNET=1", &pRespStr, WNC_TIMEOUT_MS);
@@ -402,16 +414,13 @@
 {
   string * pRespStr;
   string str(s);
-  str = "AT@DNSRESVDON=\"" + str;
-  str += "\"\r\n";
+  str = "AT@DNSRESVDON=\"" + str + "\"";
   if (send_wnc_cmd(str.c_str(), &pRespStr, WNC_TIMEOUT_MS) == 0)
   {
     size_t pos_start = pRespStr->find(":\"") + 2;
-    if (pos_start !=  string::npos)
+    size_t pos_end = pRespStr->rfind("\"") - 1;
+    if ((pos_start !=  string::npos) && (pos_end != string::npos))
     {
-      size_t pos_end = pRespStr->rfind("\"") - 1;
-      if (pos_end != string::npos)
-      {
         if (pos_end > pos_start)
         {
           // Make a copy for use later (the source string is re-used)
@@ -420,9 +429,6 @@
         }
         else
           pc.puts("URL Resolve fail, substr Err\r\n");
-      }
-      else
-        pc.puts("URL Resolve fail, no 2nd quote\r\n");
     }
     else
       pc.puts("URL Resolve fail, no quotes\r\n");
@@ -430,6 +436,8 @@
   else
     pc.puts("URL Resolve fail, WNC cmd fail\r\n");
   
+  *ipStr = "192.168.0.1";
+  
   return -1;
 }
 
@@ -484,9 +492,16 @@
       wait_ms(10);
       
       send_wnc_cmd(cmd_str.c_str(), &pRespStr, WNC_TIMEOUT_MS);
+      
       size_t pos_start = pRespStr->find("\"")  + 1;
       size_t pos_end   = pRespStr->rfind("\"") - 1;
-      i = (pos_end - pos_start + 1);  // Num hex chars, 2 per byte
+      
+      // Make sure search finds what it's looking for!
+      if (pos_start != string::npos && pos_end != string::npos)
+          i = (pos_end - pos_start + 1);  // Num hex chars, 2 per byte
+      else
+          i = 0;
+          
       if (i > 0)
       {
         retries = 1;  // If any data found retry 1 more time to catch data that might be in another
@@ -501,10 +516,9 @@
         numBytes += i/2;
       }
     }
-    return numBytes;
   }
   else
     pc.puts("sockread Err, to many to read\r\n");
   
-  return 0;
+  return (numBytes);
 }