Public Repository for IoT demo, leveraging the FRDM-K64F, WNC Cellular Modem, and AT&T M2X platform. Expects GPIO input from a sensor to send a discrete value to M2X.

Dependencies:   ConfigFile M2XStreamClient-JMF PinDetect SDFileSystem WNCInterface jsonlite mbed-rtos mbed

Fork of SVP_IoT_M2X_Cellular by Casey Bleeker

Revision:
7:5f9a23deaca1
Parent:
6:67203388294b
Child:
8:b18770740a4d
--- a/main.cpp	Sun Feb 19 07:17:45 2017 +0000
+++ b/main.cpp	Sun Feb 19 20:30:23 2017 +0000
@@ -18,61 +18,19 @@
 char deviceId[] = "3263056a467bc4eacd787bb0af9c1ad6"; // Device you want to post to
 char m2xKey[]   = "0aad274b67c7faa8110769ae9d739033"; // Your M2X API Key or Master API Key
 
-const char *hstreamName = "humidity";
-const char *tstreamName = "temperature";
-const char *streamNames[] = { tstreamName, hstreamName };
-char name[] = "Wake Forest"; // Name of current location of datasource
-
-int counts[] = { 2, 1 };
-const char *ats[] = { "2016-09-09T02:05:14.692Z", 
-                      "2016-09-09T02:05:14.700Z", 
-                      "2016-09-09T02:05:14.692Z" };
-double values[] = { 10.9, 11.2, 6.1 };
-
 char fromTime[]= "1969-12-31T19:00:01.000Z"; // yyyy-mm-ddTHH:MM:SS.SSSZ
 char endTime[25];
 
-double latitude = 33.007872;   // You could read these values from a GPS but
-double longitude = -96.751614; // for now, will just hardcode them
-double elevation = 697.00;
-
 DigitalOut led_green(LED_GREEN);
 DigitalOut led_red(LED_RED);
 DigitalOut led_blue(LED_BLUE);
 
-
 WNCInterface eth;
 Client client;
 M2XStreamClient m2xClient(&client, m2xKey);
 TimeService timeService(&m2xClient);
 MODSERIAL pc(USBTX,USBRX,256,256);
 
-void on_data_point_found(const char* at, const char* value, int index, void* context, int type) {
-  pc.printf(">>Found a data point, index: %d type: %d" CRLF, index, type);
-  pc.printf(">>At: %s" CRLF " Value: %s" CRLF, at, value);
-}
-
-
-void on_command_found(const char* id, const char* name, int index, void *context) {
-  pc.printf(">>Found a command, index: %d" CRLF, index);
-  pc.printf(">>ID: %s\n Name: %s" CRLF, id, name);
-}
-
-void on_location_found(const char* name,
-                       double latitude,
-                       double longitude,
-                       double elevation,
-                       const char* timestamp,
-                       int index,
-                       void* context) {
-  pc.printf(">>Found a location, index: %d" CRLF, index);
-  pc.printf(">>Name: %s" CRLF ">>Latitude: %lf" CRLF ">>Longitude: %lf" CRLF, name, latitude, longitude);
-  pc.printf(">>Elevation: %lf" CRLF ">>Timestamp: %s" CRLF, elevation, timestamp);
-}
-
-
-
-
 //********************************************************************************************************************************************
 //* Set the RGB LED's Color
 //* LED Color 0=Off to 7=White.  3 bits represent BGR (bit0=Red, bit1=Green, bit2=Blue) 
@@ -125,6 +83,45 @@
     //led4 = 1;
     //pc.printf("Key Released Held\n");
 }
+
+void initModem() {
+    int response;
+    pc.printf("Start m2x-demo-all by initializing the network" CRLF);
+    eth.doDebug(2);
+    response = eth.init(NULL, &pc);                     
+    pc.printf("Response is: %d" CRLF, response);
+    pc.printf("WNC Module %s initialized (%02X)." CRLF, response?"IS":"IS NOT", response);
+    while( !response ) {
+        SetLedColor(0);
+        pc.printf(" - - - - - - - WAITING FOR CELL MODEM TO COME ONLINE - - - - - - - " CRLF);
+        wait(30);
+        response = eth.init(NULL, &pc);
+        pc.printf("WNC Module %s initialized (%02X)." CRLF, response?"IS":"IS NOT", response);
+    };
+    
+    if(response){
+       eth.doDebug(0);
+       SetLedColor(2);
+    };
+    
+    response = eth.connect();                 
+    pc.printf("IP Address: %s " CRLF CRLF, eth.getIPAddress());
+    
+}
+
+void initM2X() {
+    pc.printf("starting M2X data push..." CRLF);
+
+    pc.printf("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);
+        strcpy(endTime,timestamp);
+        SetLedColor(4);
+    }
+}
  
 // The main program.
  
@@ -162,6 +159,36 @@
     pin.setAssertValue( 0 );
     button.setAssertValue( 0 );
  
+
+    
+    
+    char timestamp[25];
+    int length = 25;
+    int m2xresponse, cnt=1;
+    int pinValue;
+    
+    wait(20);
+    initModem(); //call function to init modem, wait till it comes online, and get IP address
+    initM2X{};
+
+//    //debug
+//    wait(10);   // wait for terminal connected
+//    pc.printf("STARTING WNCInterface" CRLF);
+//    eth.doDebug(0); // if you want a LOT of AT commands logged (1)
+//    
+//    // init and connect the WNCInterface cell modem
+//    pc.printf("init() returned 0x%04X" CRLF, eth.init());
+//    response = eth.connect();
+//
+//    if(response){
+//        SetLedColor(2);
+//    };
+
+ //   pc.printf("IP Address: %s " CRLF, eth.getIPAddress());
+ //   pc.printf("-------------------------------------" CRLF);
+
+    
+    // Start Pin Samping
     // Sampling does NOT begin until you set the frequency. So, until
     // you call this function NO callbacks will be made. With no arguments
     // passed the default is 20000 microseconds (20ms). Specifiy the sampling
@@ -174,45 +201,31 @@
     pin.setSampleFrequency(); // Defaults to 20ms.
     button.setSampleFrequency();
     
-    
-    char timestamp[25];
-    int length = 25;
-    int response, cnt=1;
-    
-    pc.printf("Start m2x-demo-all by initializing the network" CRLF);
-    response = eth.init();                     
-    pc.printf("WNC Module %s initialized (%02X)." CRLF, response?"IS":"IS NOT", response);
-    while( !response ) {
-     response = eth.init();
-     pc.printf(" - - - - - - - WAITING FOR CELL MODEM TO COME ONLINE - - - - - - - " CRLF);
-     wait(15);
-    };
-    
-    if(response){
-       SetLedColor(2);
-    };
-    
-    response = eth.connect();                 
-    pc.printf("IP Address: %s " CRLF CRLF, eth.getIPAddress());
-    
-    pc.printf("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);
-        strcpy(endTime,timestamp);
-        SetLedColor(4);
-    }
-    
     while( 1 ) {
         //pc.printf("Hello World! I am still here!" CRLF);
         pc.printf("Pin state is: %d" CRLF, pinState);
+        if(pinState >= 1){
+            pinValue = 1;
+        }
+        else {
+            pinValue = 0;
+        }
+        pc.printf("Pin value is: %d" CRLF, pinValue);
         pc.printf("cnt=%d\r\n",cnt++);
         // post the temp value
-        pc.printf("Post updateStreamValue (pinState = $d)..." CRLF, pinState);
-        response = m2xClient.updateStreamValue(deviceId, "water-sensor", pinState);
-        pc.printf("Post response code: %d" CRLF, response);
+        pc.printf("Post updateStreamValue (pinValue = %d)..." CRLF, pinState);
+        m2xresponse = m2xClient.updateStreamValue(deviceId, "water-sensor", pinValue);
+        pc.printf("Post response code: %d" CRLF, m2xresponse);
+        if (m2xresponse != 202){
+            SetLedColor(2);
+            pc.printf("Error connecting to M2X" CRLF);
+            //pc.printf("IP Address: %s " CRLF CRLF, eth.getIPAddress());
+            if (m2xresponse == -1){
+                pc.printf("Modem Error - attempting to reconnect" CRLF);
+                initModem();
+                initM2X();
+            }
+        }
         timeService.getTimestamp(timestamp, &length);
         pc.printf("Thats all folks, got to wait 5 seconds... (%s)" CRLF CRLF CRLF, timestamp);