GSMA version

Dependencies:   FXOS8700CQ mbed sfh7779

Fork of StarterKit by Rick McConney

Revision:
41:85a736a9b900
Parent:
40:45e4636d20cc
Child:
42:8500f0cb2ea5
--- a/main.cpp	Thu Sep 29 14:19:53 2016 +0000
+++ b/main.cpp	Tue Oct 04 20:29:31 2016 +0000
@@ -10,7 +10,12 @@
 
 #include "hardware.h"
 I2C i2c(PTC11, PTC10);    //SDA, SCL -- define the I2C pins being used
+
+#define PROXIMITYON 0
+
+#if PROXIMITYON == 1
 Proximity proximityStrip;
+#endif
 
 // comment out the following line if color is not supported on the terminal
 #define USE_COLOR
@@ -42,6 +47,8 @@
 #define MUXADDRESS 0x70
 #define PROXIMITYADDRESS  0x39
 
+
+
 int mdm_dbgmask = MDM_DBG_OFF;
 
 Serial         pc(USBTX, USBRX);
@@ -59,6 +66,18 @@
 DigitalOut  shield_3v3_1v8_sig_trans_ena(PTC4); // 0 = disabled (all signals high impedence, 1 = translation active
 DigitalOut  mdm_uart1_cts(PTD0);
 
+
+DigitalIn   slot1(PTB3,PullUp);
+DigitalIn   slot2(PTB10,PullUp);
+DigitalIn   slot3(PTB11,PullUp);
+//int slot1 = 0;
+//int slot2 = 1;
+//int slot3 = 1;
+int lastSlot1;
+int lastSlot2;
+int lastSlot3;
+string iccid;
+
 #define TOUPPER(a) (a) //toupper(a)
 
 const char ok_str[] = "OK";
@@ -71,7 +90,8 @@
 
 bool proximityChange = false;
 bool powerSave = false;
-
+bool toggleLed = false;
+int seqNum;
 
 
     
@@ -481,14 +501,26 @@
     .Humidity_Si7020    = "0"
 };
 
-void GenerateModemString(char * modem_string)
+void GenerateModemString(char * modem_string,int sensor)
 {
-    switch(iSensorsToReport)
+    switch(sensor)
     {
+#if PROXIMITYON == 1
         case PROXIMITY_ONLY:
         {
+
             char* data = proximityStrip.getDataStr();
-            sprintf(modem_string, "GET %s%s?serial=%s&data=%s %s%s\r\n\r\n", FLOW_BASE_URL, FLOW_INPUT_NAME, FLOW_DEVICE_NAME, data,  FLOW_URL_TYPE, MY_SERVER_URL);
+            seqNum++;
+            sprintf(modem_string, "GET %s%s?serial=%s&seq=%d&data=%s %s%s\r\n\r\n", FLOW_BASE_URL, "/shelf", iccid.c_str(), seqNum, data,  FLOW_URL_TYPE, MY_SERVER_URL);
+            break;
+        }
+#endif
+        case SWITCH_ONLY:
+        {
+            char data[32];
+            sprintf(data,"[{\"p\":%d},{\"p\":%d},{\"p\":%d}]",lastSlot1*26,lastSlot2*26,lastSlot3*26);
+            seqNum++;
+            sprintf(modem_string, "GET %s%s?serial=%s&seq=%d&data=%s %s%s\r\n\r\n", FLOW_BASE_URL, "/car", iccid.c_str(), seqNum, data,  FLOW_URL_TYPE, MY_SERVER_URL);
             break;
         }
         case TEMP_HUMIDITY_ONLY:
@@ -687,10 +719,68 @@
     }        
 }
 
+bool getIccid(string * iccid)
+{
+    string * respStr;
+    
+    iccid->erase();
+    
+    send_wnc_cmd("AT%CCID", &respStr, WNC_TIMEOUT_MS); 
+
+    size_t pos = respStr->find("AT%CCID");
+    if (pos == string::npos)
+        return (false);
+    
+    size_t posOK = respStr->rfind("OK");
+    if (posOK == string::npos)
+        return (false);
+
+    pos += 7; // Advanced to the number
+    *iccid = respStr->substr(pos, posOK - pos);
+    
+    return (true);
+}
+
+void check_wnc(void)
+{
+  string * pRespStr;
+  send_wnc_cmd("AT", &pRespStr, WNC_TIMEOUT_MS);             // Heartbeat?
+  send_wnc_cmd("ATE1", &pRespStr, WNC_TIMEOUT_MS);           // Echo ON
+  string cmd_str("AT+CGDCONT=1,\"IP\",");
+  cmd_str += "\"";
+  cmd_str += MY_APN_STR;
+  cmd_str += "\"";
+  
+  send_wnc_cmd(cmd_str.c_str(), &pRespStr, 2*WNC_TIMEOUT_MS); // Set APN, cmd seems to take a little longer sometimes
+  send_wnc_cmd("AT+CGACT=1,1", &pRespStr, WNC_TIMEOUT_MS);
+  send_wnc_cmd("AT+CREG?", &pRespStr, WNC_TIMEOUT_MS);
+  send_wnc_cmd("AT@INTERNET=1", &pRespStr, WNC_TIMEOUT_MS);  // Internet services enabled
+  send_wnc_cmd("AT@SOCKDIAL=1", &pRespStr, WNC_TIMEOUT_MS);
+}
+
+bool checkSlots()
+{
+    bool changed = false;
+    int s1 = !slot1;
+    int s2 = !slot2;
+    int s3 = !slot3;
+    if(lastSlot1 != s1 || lastSlot2 != s2 ||lastSlot3 != s3)
+    {
+        pc.printf("slot 1 %d\r\n",s1);
+        pc.printf("slot 2 %d\r\n",s2);
+        pc.printf("slot 3 %d\r\n",s3);
+        changed = true;
+    }
+    lastSlot1 = s1;
+    lastSlot2 = s2;
+    lastSlot3 = s3;
+    return changed;
+}
+
 int main() {
 
     int i;
-    int sendAttemps = 0;
+
 
     HTS221 hts221;
     pc.baud(115200);
@@ -724,7 +814,10 @@
         }
     } while (!i);
     
-  
+    getIccid(&iccid);
+    printf(GRN "ICCID = %s" DEF "\r\n",iccid.c_str());
+    
+    //check_wnc();
          
     //Software init
     software_init_mdm();
@@ -741,10 +834,11 @@
     setTauTimer(12*60*60);
     setActivityTimer(20);
     setPowerSave(false);
-   
-    proximityStrip.init();
-   
+ 
+#if PROXIMITYON == 1  
+    proximityStrip.init();  
     proximityStrip.on();        
+#endif
 
     int count = 0;
     // Send and receive data perpetually
@@ -754,27 +848,46 @@
         //sprintf(SENSOR_DATA.Temperature, "%0.2f", CTOF(hts221.readTemperature()));
         //sprintf(SENSOR_DATA.Humidity, "%02d", hts221.readHumidity());
         // read_sensors(); //read available external sensors from a PMOD and the on-board motion sensor
-            
-        SetLedColor(0x2); //green       
-            
+        toggleLed = !toggleLed;
+        if(toggleLed)
+            SetLedColor(0x2); //green 
+        else    
+            SetLedColor(0); //off   
+
+#if PROXIMITYON == 1            
         proximityStrip.scan();
+#else
+        bool slotsChanged = checkSlots();
+#endif
                 
-        SetLedColor(0); //off 
-            
-        if(count >= 5*60 ||proximityStrip.changed(50))
+        
+        
+
+#if PROXIMITYON == 1            
+        if(count >= 5*60 || proximityStrip.changed(50))
+#else
+        if(count >= 5*60 || slotsChanged)
+#endif
         {
             count = 0;
             SetLedColor(0x04); //blue
                 
             char modem_string[512];
-            GenerateModemString(&modem_string[0]);
+            
+#if PROXIMITYON == 1
+            GenerateModemString(&modem_string[0],PROXIMITY_ONLY);
+#else
+            GenerateModemString(&modem_string[0],SWITCH_ONLY);
+#endif
+  
             printf(BLU "Sending to modem : %s" DEF "\r\n", modem_string);
             wakeModem(); 
+            sockopen_mdm();
             checkSocket();
             sockwrite_mdm(modem_string);
             wakeModem();
             sockread_mdm(&MySocketData, 1024, 20);
-            
+            sockclose_mdm();
             // If any non-zero response from server, make it GREEN one-time
             //  then the actual FLOW responses will set the color.
             if (MySocketData.length() > 0)
@@ -795,18 +908,9 @@
             else
             {
                 SetLedColor(0x1); //red
-                // reset socket if read fails
-                if(sendAttemps < 2)
-                {
-                    sendAttemps++;
-                    sockclose_mdm();
-                    sockopen_mdm();
-                }
-                else // give up and do full reset
-                {
-                    if(!rebootModem())
-                        system_reset();
-                }
+                // failed to read reply try reseting modem
+                if(!rebootModem()) // if reset fails do a full software reset
+                    system_reset();
                    
             }    
         }