This program simply connects to a HTS221 I2C device to proximity sensor

Dependencies:   FXOS8700CQ mbed

Files at this revision

API Documentation at this revision

Comitter:
elmkom
Date:
Mon Oct 10 16:29:34 2016 +0000
Parent:
42:8500f0cb2ea5
Commit message:
Flush modem buffer before send

Changed in this revision

Wnc.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/Wnc.cpp	Fri Oct 07 17:01:22 2016 +0000
+++ b/Wnc.cpp	Mon Oct 10 16:29:34 2016 +0000
@@ -20,6 +20,7 @@
 SerialBuffered mdm(PTD3, PTD2, 512);
 
 bool powerSave = false;
+bool firstWake = false;
 
 Wnc::Wnc(void)
 {
@@ -61,6 +62,17 @@
                  mdm_wakeup_in = 1;
                  pc.printf("wake\r\n");
             }
+            else if(c == '}')
+            {
+                 mdm_uart2_rx_boot_mode_sel = 1;
+                 pc.printf("rx hi\r\n");
+            }
+            else if(c == '{')
+            {
+                 mdm_uart2_rx_boot_mode_sel = 0;
+                 pc.printf("rx low\r\n");
+            }
+
             else if(c == '^')
             {
                 pc.printf("reboot\r\n");
@@ -113,6 +125,10 @@
 char* Wnc::send(const char *cmd, int timeout_ms)
 {
     char* reply;
+    
+    while (mdm.readable()) {
+         mdm.getc();
+    }
         
     int tries = 4;
     while(tries > 0)
@@ -331,12 +347,14 @@
 {
     char *reply;
     mdm_wakeup_in = 1;
-    reply = send("AT+CFUN=1", WNC_WAIT_TIME_MS);
-    reply = send("AT%CMATT=1",  WNC_WAIT_TIME_MS);
-    wait(2); // wait to attach
-    reply = send("AT+CREG?",  WNC_WAIT_TIME_MS);
-    reply = send("AT@INTERNET=1",  WNC_WAIT_TIME_MS);  // Internet services enabled
-    reply = send("AT@SOCKDIAL=1",  WNC_WAIT_TIME_MS);
+    pc.printf("wake from power save\r\n");
+    firstWake = true;
+   // reply = send("AT+CFUN=1", WNC_WAIT_TIME_MS);
+   // reply = send("AT%CMATT=1",  WNC_WAIT_TIME_MS);
+   // wait(5); // wait to attach
+   // reply = send("AT+CREG?",  WNC_WAIT_TIME_MS);
+   // reply = send("AT@INTERNET=1",  WNC_WAIT_TIME_MS);  // Internet services enabled
+   // reply = send("AT@SOCKDIAL=1",  WNC_WAIT_TIME_MS);
     
 
 }
@@ -360,19 +378,49 @@
     sprintf(cmd,"AT@PINGREQ=\"%s\"",ip);
     return send(cmd,WNC_WAIT_TIME_MS);
 }
-// AT@SOCKCONN=1,"108.244.165.22",5005"
+// AT@SOCKCONN=1,"108.244.165.22",5005
 bool Wnc::connect(char* ip, int port)
 {
   char *reply;
 
-    reply = send("AT@SOCKCREAT=1", WNC_WAIT_TIME_MS);
-    if(strlen(reply) == 0 || strstr(reply,"OK") ==0)
-        return false;
+    if(isModemResponding())
+    {
+        reply = send("AT@SOCKCREAT=1", WNC_WAIT_TIME_MS);
+        if(strlen(reply) == 0 || strstr(reply,"OK") ==0)
+            return false;
+    }
+
+    
     char cmd[32];
-    sprintf(cmd,"AT@SOCKCONN=1,\"%s\",%d",ip,port);
-    reply = send(cmd,WNC_WAIT_TIME_MS);
-    if(strlen(reply) == 0 || strstr(reply,"OK") ==0)
-        return false;
+    if(isModemResponding())
+    {
+        sprintf(cmd,"AT@SOCKCONN=1,\"%s\",%d",ip,port);
+        reply = send(cmd,WNC_WAIT_TIME_MS);
+        if(firstWake)
+        {
+            for(int i = 0;i<10;i++)
+            {
+                pc.printf("%d",i);
+                send("AT", WNC_WAIT_TIME_MS);
+                reply = send("AT+CREG?",WNC_WAIT_TIME_MS);
+                if(strlen(reply) > 0 && strstr(reply,"2,1") != 0)
+                {
+                    pc.printf("connected %s",reply);
+                    break;
+                }
+                else
+                {
+                    pc.printf("Unconnected %s",reply);
+                }
+                wait(1);
+            }
+        }
+        firstWake = false;
+        reply = send(cmd,WNC_WAIT_TIME_MS);
+        
+        if(strlen(reply) == 0 || strstr(reply,"OK") ==0)
+            return false;
+    }
     return true;
 }