Proximity strip reader

Dependencies:   mbed sfh7779

Fork of StarterKit by Rick McConney

Revision:
45:fe90f1fcb4e0
Parent:
44:60008ebffdd4
Child:
46:69966d555de7
diff -r 60008ebffdd4 -r fe90f1fcb4e0 main.cpp
--- a/main.cpp	Wed Oct 26 18:40:40 2016 +0000
+++ b/main.cpp	Wed Nov 30 14:50:16 2016 +0000
@@ -1,6 +1,5 @@
 #include "mbed.h"
 #include <cctype>
-#include <string>
 #include "SerialBuffered.h"
 #include "HTS221.h"
 #include "config_me.h"
@@ -76,6 +75,8 @@
 DigitalIn   slot2(PTB10,PullUp);
 DigitalIn   slot3(PTB11,PullUp);
 
+DigitalIn   sw2(SW2);
+
 int lastSlot1;
 int lastSlot2;
 int lastSlot3;
@@ -116,17 +117,11 @@
 
 void system_reset()
 {
+    //wnc.passthrough();
     printf(RED "\n\rSystem resetting..." DEF "\n");
     NVIC_SystemReset();
 }
 
-// These are built on the fly
-string MyServerIpAddress;
-string MySocketData;
-
-// These are to be built on the fly
-string my_temp;
-string my_humidity;
 
 #define CTOF(x)  ((x)*1.8+32)
 
@@ -378,25 +373,29 @@
 
     // Initialize the modem
     printf(GRN "Modem initializing... will take up to 60 seconds" DEF "\r\n");
-    do {
-        i=wnc.init();
-        if (!i) {
-            pc.printf(RED "Modem initialization failed!" DEF "\n");
-        }
-    } while (!i);
-    
-   // wnc.setIn();
-   // wnc.send("AT+CPSMS=0",WNC_WAIT_TIME_MS);
-
-   // wnc.toggleWake();   
-        
+    if(!wnc.init()) 
+    {
+        pc.printf(RED "Modem initialization failed!" DEF "\n");
+        system_reset();
+    }
+       
     
     iccid = wnc.getIccid();
     printf(GRN "ICCID = %s" DEF "\r\n",iccid);
+    
+
            
     //Software init
-    wnc.startInternet();
-    //wnc.ping("108.244.165.22");
+    if(!wnc.startInternet())
+        system_reset();
+    
+    char* ip = wnc.resolveDn(MY_SERVER_URL);
+    if(strlen(ip) == 0)
+    {
+       printf(RED "Failed to resolve IP for %s" DEF "\r\n",MY_SERVER_URL); 
+    }
+    else
+        printf(GRN "IP = %s" DEF "\r\n",ip);
 
     // Set LED BLUE for partial init
     SetLedColor(0x4);
@@ -406,6 +405,7 @@
 #if PROXIMITYON == 1 
     bool stripChanged[2];
     myi2c = new I2C(PTE25, PTE24); 
+    //myi2c = new I2C(PTC11, PTC10); 
     myi2c->frequency(100000);
     ATT160826 strip0 (myi2c, 0);
     ATT160826 strip1 (myi2c, 3);
@@ -415,6 +415,7 @@
 #elif PROXIMITYON == 2
 
     myi2c = new I2C(PTE25, PTE24); 
+    
     myi2c->frequency(100000);
     Proximity strip0;
     Proximity strip1;
@@ -427,7 +428,7 @@
      
 #endif
 
-    int count = 0;
+    int count = 5*120;
     // Send and receive data perpetually
     while(1) {
        
@@ -467,13 +468,19 @@
 
      char modem_string[512];                
         
-#if PROXIMITYON == 1            
+#if PROXIMITYON == 1 
+/*           
         if(count >= 5*120)
         {
             stripChanged[0] = true;
             stripChanged[1] = true;
         }
-        
+ */     
+        if(sw2 == 0)
+        {
+            stripChanged[0] = true;
+            stripChanged[1] = true;
+        }             
         if(stripChanged[0] || stripChanged[1])
 
         
@@ -515,43 +522,47 @@
             printf(BLU "Sending to modem : %s" DEF "\r\n", modem_string);
 
             //if(wnc.connect("108.244.165.22",5005)) // node-red
-            if(wnc.connect("52.21.224.249",80)) // shelf
+            
+            
+            if(wnc.connect(ip,MY_PORT)) // shelf
             //if(wnc.connect("52.33.231.251",80)) // car
             {
-                char * reply = wnc.writeSocket(modem_string);
-           
-                char* mydata;
-                int tries = 4;
-                while(tries > 0) // wait for reply
+                if(wnc.writeSocket(modem_string))
                 {
-                    tries--;
-                    mydata = wnc.readSocket();
-                    if (strlen(mydata) > 0)
-                        break;
+           
+                    char* mydata;
+                    int tries = 8;
+                    while(tries > 0) // wait for reply
+                    {
+                        tries--;
+                        mydata = wnc.readSocket();
+                        if (strlen(mydata) > 0)
+                            break;
                  
-                    wait(0.5);
-                }
+                        wait(0.5);
+                    }
             
-                if (strlen(mydata) > 0)
-                {   
+                    if (strlen(mydata) > 0)
+                    {   
                                 
-                    SetLedColor(0x2); // green
-                    //only copy on sucessful send
+                        SetLedColor(0x2); // green
+                        //only copy on sucessful send
 
-                    printf(BLU "Read back : %s" DEF "\r\n", mydata);
-                    char datareply[512];
-                    if (extract_reply(mydata, datareply))
+                        printf(BLU "Read back : [%s]" DEF "\r\n", mydata);
+                        char datareply[512];
+                        if (extract_reply(mydata, datareply))
+                        {
+                            printf(GRN "JSON : %s" DEF "\r\n", datareply);
+                            parse_reply(datareply);
+                        }
+                        SetLedColor(0); // off             
+                    }
+                    else // no reply reset
                     {
-                        printf(GRN "JSON : %s" DEF "\r\n", datareply);
-                        parse_reply(datareply);
-                    }
-                    SetLedColor(0); // off             
+                        SetLedColor(0x1); //red
+                        system_reset();                 
+                    } 
                 }
-                else // no reply reset
-                {
-                    SetLedColor(0x1); //red
-                    system_reset();                 
-                } 
                 wnc.disconnect();
             }
             else // failed to connect reset