The Hiking Pal tracking device firmware. See full description on the detail page: https://www.hackster.io/bowenfeng/hiking-pal-v1-07c02d

Dependencies:   FXOS8700CQ MODSERIAL mbed

Fork of Avnet_ATT_Cellular_IOT by Avnet

Revision:
85:43d791cd5967
Parent:
84:0bf8168b690a
Child:
86:5ff234988f53
--- a/main.cpp	Thu Dec 22 06:31:32 2016 +0000
+++ b/main.cpp	Thu Dec 22 06:52:58 2016 +0000
@@ -30,6 +30,9 @@
 DigitalOut led_red(LED_RED);
 DigitalOut led_blue(LED_BLUE);
 
+#define OK_COLOR 0x2
+#define ERROR_COLOR 0x1
+
 
 //********************************************************************************************************************************************
 //* Create string with sensor readings that can be sent to flow as an HTTP get
@@ -65,23 +68,14 @@
     .GPS_Course         = "0"
 };
 
+char hid[20] = {0};
+char sid[20] = {0};
+char sessionName[20] = {0};
+
 void display_app_firmware_version(void) {
-    PUTS("\r\n\r\nApp Firmware: Release 1.0 - built: "__DATE__" "__TIME__"\r\n\r\n");
+    PUTS("\r\n\r\nHiking Pal Firmware: Release 1.0 - built: "__DATE__" "__TIME__"\r\n\r\n");
 }
 
-void GenerateModemString(char * modem_string, long long hid, long long sid) {
-    sprintf(
-        modem_string,
-        "GET %s/hikings/%lld/sessions/%lld/moves?lat=%s&lng=%s HTTP/1.1\r\nHost: %s\r\n\r\n",
-        FLOW_BASE_URL,
-        hid,
-        sid,
-        SENSOR_DATA.GPS_Latitude,
-        SENSOR_DATA.GPS_Longitude,
-        MY_SERVER_URL);
-} //GenerateModemString        
-            
-            
 //Periodic timer
 Ticker OneMsTicker;
 volatile bool bTimerExpiredFlag = false;
@@ -108,72 +102,6 @@
     led_blue = !(ucColor & 0x4); //bit 2
 } //SetLedColor()
 
-//********************************************************************************************************************************************
-//* Process the JSON response.  In this example we are only extracting a LED color. 
-//********************************************************************************************************************************************
-bool parse_JSON(char* json_string) {
-    char* beginquote;
-    char token[] = "\"LED\":\"";
-    beginquote = strstr(json_string, token );
-    if ((beginquote != 0))
-    {
-        char cLedColor = beginquote[strlen(token)];
-        PRINTF(GRN "LED Found : %c" DEF "\r\n", cLedColor);
-        switch(cLedColor)
-        {
-            case 'O':
-            { //Off
-                SetLedColor(0);
-                break;
-            }
-            case 'R':
-            { //Red
-                SetLedColor(1);
-                break;
-            }
-            case 'G':
-            { //Green
-                SetLedColor(2);
-                break;
-            }
-            case 'Y':
-            { //Yellow
-                SetLedColor(3);
-                break;
-            }
-            case 'B':
-            { //Blue
-                SetLedColor(4);
-                break;
-            }
-            case 'M':
-            { //Magenta
-                SetLedColor(5);
-                break;
-            }
-            case 'T':
-            { //Turquoise
-                SetLedColor(6);
-                break;
-            }
-            case 'W':
-            { //White
-                SetLedColor(7);
-                break;
-            }
-            default:
-            {
-                break;
-            }
-        } //switch(cLedColor)
-        return true;
-    }
-    else
-    {
-        return false;
-    }
-} //parse_JSON
-
 void extract_longlong(const char* s, char v[]) {
     long long value = strtoll(s, NULL, 0);
     sprintf(v, "%lld", value);
@@ -186,11 +114,22 @@
     }
 }
 
+int send_receive(char* request, char* response) {
+    int result = cell_modem_Sendreceive(request, response);
+    SetLedColor(result ? OK_COLOR : ERROR_COLOR);
+    return result;
+}
+
+int send_only(char* request) {
+    char response[512];
+    return send_receive(request, response);
+}
+
 void find_first_hiking(char hikingId[]) {
     char request[512];
     char response[512];
     sprintf(request, "GET %s/hikings HTTP/1.1\r\nHost: %s\r\n\r\n", FLOW_BASE_URL, MY_SERVER_URL);
-    if (cell_modem_Sendreceive(&request[0], &response[0])) {
+    if (send_receive(&request[0], &response[0])) {
         find_longlong(response, "\"id\":", hikingId);
     }
 }
@@ -199,20 +138,36 @@
     char request[512];
     char response[512];
     sprintf(request, "GET %s/hikings/%s/sessions?name=%s HTTP/1.1\r\nHost: %s\r\n\r\n", FLOW_BASE_URL, hikingId, name, MY_SERVER_URL);
-    if (cell_modem_Sendreceive(&request[0], &response[0])) {
+    if (send_receive(&request[0], &response[0])) {
         find_longlong(response, "\"id\":", sessionId);
     }
 }
 
+void generate_move_request(char request[], const char* hid, const char* sid) {
+    sprintf(
+        request,
+        "GET %s/hikings/%s/sessions/%s/moves?lat=%s&lng=%s HTTP/1.1\r\nHost: %s\r\n\r\n",
+        FLOW_BASE_URL,
+        hid,
+        sid,
+        SENSOR_DATA.GPS_Latitude,
+        SENSOR_DATA.GPS_Longitude,
+        MY_SERVER_URL);
+}
+void report_move(const char* hid, const char* sid) {
+    char request[512];
+    generate_move_request(request, hid, sid);
+    send_only(&request[0]);
+}
+
 int main() {
-    static unsigned ledOnce = 0;
     //delay so that the debug terminal can open after power-on reset:
     wait (5.0);
     pc.baud(115200);
     
     display_app_firmware_version();
     
-    PRINTF(GRN "Hello World from the Cellular IoT Kit!\r\n\r\n");
+    PRINTF(GRN "Hiking Pal tracking device started!\r\n\r\n");
 
     //Initialize the I2C sensors that are present
     sensors_init();
@@ -232,10 +187,6 @@
     iTimer1Interval_ms = SENSOR_UPDATE_INTERVAL_MS;
     OneMsTicker.attach(OneMsFunction, 0.001f) ;
     
-    char hid[20] = {0};
-    char sid[20] = {0};
-    char sessionName[20] = {0};
-    
     sprintf(sessionName, "IoT-kit-%d", rand() % 1000);
     
     find_first_hiking(hid);
@@ -253,18 +204,6 @@
         if (bTimerExpiredFlag) {
             bTimerExpiredFlag = false;
             read_sensors(); //read available external sensors from a PMOD and the on-board motion sensor
-            char modem_string[512];
-            GenerateModemString(&modem_string[0], 1482292398681, 1482293421314);
-            char myJsonResponse[512];
-            if (cell_modem_Sendreceive(&modem_string[0], &myJsonResponse[0]))
-            {
-//                if (!ledOnce)
-//                {
-//                    ledOnce = 1;
-//                    SetLedColor(0x2); //Green
-//                }
-//                parse_JSON(&myJsonResponse[0]);
-            }
-        } //bTimerExpiredFlag
+            report_move(hid, sid);
     } //forever loop
 }