Initial commit

Dependencies:   ConfigFile FXOS8700CQ M2XStreamClient-JMF MODSERIAL SDFileSystem WNCInterface jsonlite mbed-rtos mbed

Fork of StarterKit_M2X_DevLab by Jan Korycan

Files at this revision

API Documentation at this revision

Comitter:
jk431j
Date:
Wed Apr 26 00:05:32 2017 +0000
Parent:
7:721eb6bb68d3
Child:
9:3f5dfac96ac1
Commit message:
* Added APN to WNCInterface initialization (more reliable initialization); * Added watchdog to M2X requests; * Added M2X configuration over SMS

Changed in this revision

config_me.h Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/config_me.h	Thu Apr 06 03:39:06 2017 +0000
+++ b/config_me.h	Wed Apr 26 00:05:32 2017 +0000
@@ -1,7 +1,7 @@
 // Configure all M2X related values here
 
-const char* deviceId = "beefbeefbeefbeefbeefbeefbeefbeef"; // Device you want to post to
-const char* m2xKey   = "cafecafecafecafecafecafecafecafe"; // Your M2X API Key or Master API Key
+char deviceId[33] = "beefbeefbeefbeefbeefbeefbeefbeef"; // Device you want to post to
+char m2xKey[33]   = "cafecafecafecafecafecafecafecafe"; // Your M2X API Key or Master API Key
 
 const char* hStreamName = "humidity";                      // Humidity stream ID
 const char* tStreamName = "temp";                          // Temperature stream ID
--- a/main.cpp	Thu Apr 06 03:39:06 2017 +0000
+++ b/main.cpp	Wed Apr 26 00:05:32 2017 +0000
@@ -35,6 +35,12 @@
 
 K64F_Sensors_t  SENSOR_DATA = {};
 bool bStop = false;
+bool bM2XConfigured;
+
+Ticker WatchdogTicker;
+int watchdogTicks = 0;
+bool bWatchdogOn = false;
+unsigned char lastLedColor = 0;
 
 //********************************************************************************************************************************************
 //* Set the RGB LED's Color
@@ -46,8 +52,48 @@
     led_red = !(ucColor & 0x1); //bit 0
     led_green = !(ucColor & 0x2); //bit 1
     led_blue = !(ucColor & 0x4); //bit 2
+    
+    lastLedColor = ucColor;
 } //SetLedColor()
 
+struct Watchdog {
+    Watchdog() {
+        bWatchdogOn = true;
+        };
+        
+    ~Watchdog() {
+        bWatchdogOn = false;
+        };
+};
+
+#define WATCHDOG struct Watchdog aWatchdog;
+
+void watchdog_check()
+{
+  // watchdog function is run every 1/4th of a second
+  if(bWatchdogOn) {
+     watchdogTicks++;
+     
+     // blink LED while watchdog is running
+    if  (watchdogTicks % 2 == 1) {
+        led_red = led_green = led_blue = 1;
+    } else {
+        SetLedColor(lastLedColor);
+    };
+  } else {
+     watchdogTicks = 0;
+     SetLedColor(lastLedColor);
+  }
+  
+  // reset if the watchod is on for more than one minute
+  if(watchdogTicks > 60 * 4)  NVIC_SystemReset();
+}
+
+
+bool power_on() {
+    return (RCM->SRS0 & RCM_SRS0_POR_MASK);
+}
+
 
 bool ExecuteCommand(const char* Command)
 {
@@ -80,8 +126,17 @@
             break;
         }
         case 'M':
-        { //Magenta
-            SetLedColor(5);
+        { //Magenta or M2X
+            char Key[33], Device[33];
+        
+            int count = sscanf(Command, "M2X:%32s:%32s", Key, Device);
+            if (count == 2) {
+                pc.printf("Got key %s and device %s" CRLF, Key, Device);
+                strncpy(deviceId, Device, 32);
+                strncpy(m2xKey, Key, 32);
+                bM2XConfigured = true;
+            } else
+                SetLedColor(5);
             break;
         }
         case 'T':
@@ -133,21 +188,22 @@
   ExecuteCommand(msg.msg.c_str());
 }
 
+
+
 int main() {
     char timestamp[25];
     int length = 25;
     int response;
 
     ExecuteCommand("Red");
-        
+
     pc.baud(115200);    
     pc.printf("M2X StarterKit demo: initializing the network" CRLF);
-    response = eth.init();                     
+    response = eth.init("m2m.com.attz");                     
     pc.printf("WNC Module %s initialized (%02X)." CRLF, response?"IS":"IS NOT", response);
     if( !response ) {
-        pc.printf(" - - - - - - - SYSTEM RESET - - - - - - - " CRLF);
+        pc.printf(" - - - - - - - SYSTEM RESET - - - - - - - " CRLF CRLF);
         NVIC_SystemReset();
-        while(1);
     }
     
     response = sms.init(1, on_msg_rcvd);
@@ -164,15 +220,32 @@
     pc.printf("Initialize the sensors" CRLF);    
     sensors_init();
     read_sensors();
+    
+    bM2XConfigured = *deviceId && *m2xKey;
+    if (!bM2XConfigured) {
+        pc.printf(RED "Waiting for SMS configuration" CRLF);
+        
+        while (!bM2XConfigured) {
+            ExecuteCommand("Red");
+            delay(1000);
+            ExecuteCommand("Yellow");            
+            delay(1000);
+        }
+    };
+    
+    // set up watchdog ticker running every quarter of a second
+    WatchdogTicker.attach(watchdog_check, 0.25);
             
-    pc.printf(WHT "initialize the M2X time service" CRLF);
-    if (!m2x_status_is_success(timeService.init())) 
-        pc.printf("Cannot initialize time service!" CRLF);
-    else {
-        timeService.getTimestamp(timestamp, &length);
-        pc.printf("Current timestamp: %s" CRLF, timestamp);
-    }
-    
+    { WATCHDOG
+        pc.printf(WHT "initialize the M2X time service" CRLF);
+        if (!m2x_status_is_success(timeService.init())) 
+            pc.printf("Cannot initialize time service!" CRLF);
+        else {
+            timeService.getTimestamp(timestamp, &length);
+            pc.printf("Current timestamp: %s" CRLF, timestamp);
+        };
+    };
+                
     ExecuteCommand("Green");    
     
 #ifdef STARTUP_SMS    
@@ -180,31 +253,35 @@
     pc.printf("Startup SMS %s sent." CRLF, response ? "was" : "NOT");
 #endif
     
-    pc.printf("Query for pending commands ..." CRLF);
-    response = m2xClient.listCommands(deviceId, on_command_found, NULL, "status=pending");
-    pc.printf("listCommands response code: %d" CRLF, response);  
-
+    { WATCHDOG 
+        pc.printf("Query for pending commands ..." CRLF);
+        response = m2xClient.listCommands(deviceId, on_command_found, NULL, "status=pending");
+        pc.printf("listCommands response code: %d" CRLF, response);  
+    };
+    
     while (!bStop) {
         // read sensor values 
         read_sensors();
 
-        // post the humidity value
-        pc.printf("Post updateStreamValue (humidity = %.2f)..." CRLF, SENSOR_DATA.Humidity);
-        response = m2xClient.updateStreamValue(deviceId, hStreamName, SENSOR_DATA.Humidity);
-        pc.printf("Post response code: %d" CRLF, response);
-        
-        // post the temp value
-        pc.printf("Post updateStreamValue (temp = %.2f)..." CRLF, SENSOR_DATA.Temperature);
-        response = m2xClient.updateStreamValue(deviceId, tStreamName, SENSOR_DATA.Temperature);
-        pc.printf("Post response code: %d" CRLF, response);
-
-        // post accelerometer values
-        pc.printf("Post postDeviceUpdate (accelerometer)..." CRLF, SENSOR_DATA.Temperature);
-        response = m2xClient.postDeviceUpdate(deviceId, 3, accelStreamNames, (float []){SENSOR_DATA.AccelX, SENSOR_DATA.AccelY, SENSOR_DATA.AccelZ});
-        pc.printf("Post response code: %d" CRLF, response);
-                                   
-        timeService.getTimestamp(timestamp, &length);
-        pc.printf("%s waiting for %d seconds... " CRLF , timestamp, commandDelay * commandPolls);
+        { WATCHDOG 
+            // post the humidity value
+            pc.printf("Post updateStreamValue (humidity = %.2f)..." CRLF, SENSOR_DATA.Humidity);
+            response = m2xClient.updateStreamValue(deviceId, hStreamName, SENSOR_DATA.Humidity);
+            pc.printf("Post response code: %d" CRLF, response);
+            
+            // post the temp value
+            pc.printf("Post updateStreamValue (temp = %.2f)..." CRLF, SENSOR_DATA.Temperature);
+            response = m2xClient.updateStreamValue(deviceId, tStreamName, SENSOR_DATA.Temperature);
+            pc.printf("Post response code: %d" CRLF, response);
+    
+            // post accelerometer values
+            pc.printf("Post postDeviceUpdate (accelerometer)..." CRLF, SENSOR_DATA.Temperature);
+            response = m2xClient.postDeviceUpdate(deviceId, 3, accelStreamNames, (float []){SENSOR_DATA.AccelX, SENSOR_DATA.AccelY, SENSOR_DATA.AccelZ});
+            pc.printf("Post response code: %d" CRLF, response);
+                                       
+            timeService.getTimestamp(timestamp, &length);
+            pc.printf("%s waiting for %d seconds... " CRLF , timestamp, commandDelay * commandPolls);
+        };
         
         // now poll for pending commands
         for (short idx=0; idx < commandPolls; idx++) {
@@ -212,9 +289,11 @@
             delay(commandDelay * 1000);
 
             // and then query for commands
-            pc.printf("\tQuery for pending commands ..." CRLF);
-            response = m2xClient.listCommands(deviceId, on_command_found, NULL, "status=pending");
-            pc.printf("\tlistCommands response code: %d" CRLF, response);              
+            { WATCHDOG 
+                pc.printf("\tQuery for pending commands ..." CRLF);
+                response = m2xClient.listCommands(deviceId, on_command_found, NULL, "status=pending");
+                pc.printf("\tlistCommands response code: %d" CRLF, response);              
+            };
         }
     };
     
@@ -226,5 +305,4 @@
     
     pc.printf("- - - - - - - THE END - - - - - - - " CRLF);    
     NVIC_SystemReset();
-}
-
+}
\ No newline at end of file