Backing up an unused program in case of future need

Dependencies:   mbed

Revision:
2:06fa34661f19
Parent:
1:94282484baae
Child:
3:accba7e07a0d
--- a/wifi.cpp	Fri Apr 15 09:03:54 2016 +0000
+++ b/wifi.cpp	Fri Apr 22 09:23:57 2016 +0000
@@ -4,21 +4,64 @@
 #include "wifi.h"
 #include  "log.h"
 #include   "io.h"
+#include  "cfg.h"
 
-#define SSID "Andrew and Kate"
-#define PASSWORD "Hopeless"
-
+#define IP_WAIT_TIME_MS 5000
 
 int WifiStatus;
 
-#define AM_STOPPING    0
-#define AM_STARTING    1
-#define AM_AUTOBAUDING 2
-#define AM_BAUDING     3
-#define AM_CONNECTING  4
-#define AM_MUXING      5
-#define AM_STARTED     6
-static int am = AM_STOPPING;
+/*
+START
+=====
+AT_SUCCESS + WIFI_GOT_IP    -> MUX         Under normal circumstances the module will start at the correct baud and already set up for the wifi.
+AT_TIMEOUT + WIFI_CONNECTED -> GET_STATUS  Connected to WiFi but no IP given - give the wifi a bit longer
+AT_TIMEOUT + WIFI_READY     -> GET_STATUS  Not connected to WiFi - give the wifi a bit longer
+AT_TIMEOUT + WIFI_NONE      -> AUTOBAUD    Not even the module ready message was received: probably the baud rate is wrong so check each baud.
+
+AUTOBAUD
+========
+Changes the baud rate; sends AT and waits for OK
+AT_SUCCESS                  -> CHANGE_BAUD Found a baud rate which works so now need to change to the correct baud rate
+AT_TIMEOUT                                 Try the next baud rate
+
+CHANGE_BAUD
+===========
+AT_SUCCESS                  -> GET_STATUS  Don't know the wifi status at this point so find it
+
+GET_STATUS
+==========
+wait until some time has passed since start
+AT_SUCCESS + WIFI_GOT_IP    -> MUX
+AT_SUCCESS + WIFI_CONNECTED -> BOMB_OUT    Connected to wifi but could not get an IP
+AT_SUCCESS + WIFI_READY     -> CONNECT     This happens when the module has never been, or has lost its connection to, the wifi.
+AT_SUCCESS + WIFI_NONE      -> BOMB_OUT    This should not happen
+
+CONNECT
+=======
+AT_SUCCESS                  -> GET_STATUS
+
+AT_MUX
+======
+
+AT_VERSION
+==========
+
+AT_STARTED
+==========
+
+*/
+
+#define AM_STOP        0
+#define AM_START       1
+#define AM_AUTOBAUD    2
+#define AM_CHANGE_BAUD 3
+#define AM_CONNECT     4
+#define AM_GET_STATUS  5
+#define AM_WAIT_WIFI   6
+#define AM_MUX         7
+#define AM_VERSION     8
+#define AM_STARTED     9
+static int am = AM_STOP;
 int WifiStarted() { return am == AM_STARTED; }
 
 int WifiMain()
@@ -28,26 +71,28 @@
     static int result = AT_NONE;
     static int autoBaud = 1;
     
-    static int autoBaudSucceeded = false;
+    static Timer wifiWaitTimer;
     
     switch (am)
     {
-        case AM_STOPPING:
+        case AM_STOP:
             AtResetAndStop();
-            am = AM_STARTING;
+            am = AM_START;
             result = AT_NONE;
             break;
-        case AM_STARTING:
+        case AM_START:
             switch (result)
             {
                 case AT_NONE:
                     AtReleaseResetAndStart(&result);
+                    wifiWaitTimer.reset();
+                    wifiWaitTimer.start();
                     break;
                 case AT_SUCCESS:
                     switch (WifiStatus)
                     {
                         case WIFI_GOT_IP:
-                            am = AM_MUXING;
+                            am = AM_MUX;
                             result = AT_NONE;
                             break;
                         default:
@@ -59,11 +104,11 @@
                     switch (WifiStatus)
                     {
                         case WIFI_READY:
-                            am = AM_CONNECTING;
+                            am = AM_CONNECT;
                             result = AT_NONE;
                             break;
                         default:
-                            am = AM_AUTOBAUDING;
+                            am = AM_AUTOBAUD;
                             result = AT_NONE;
                             autoBaud = 1;
                             break;
@@ -71,23 +116,21 @@
                     break;
             }
             break;
-        case AM_AUTOBAUDING:
-            if (autoBaudSucceeded) return -1;
-            EspBaud(9600 * autoBaud);
+        case AM_AUTOBAUD:
             switch (result)
             {
                 case AT_NONE:
+                    EspBaud(9600 * autoBaud);
                     AtAt(&result);
                     break;
                 case AT_SUCCESS:
-                    autoBaudSucceeded = true;
-                    am = AM_BAUDING;
+                    am = AM_CHANGE_BAUD;
                     result = AT_NONE;
                     break;
                 default:
-                    if (autoBaud == 16)
+                    if (autoBaud == 48)
                     {
-                        LogCrLf("Could not connect to ESP");
+                        LogCrLf("Could not find a baud rate to connect to ESP");
                         return -1;
                     }
                     autoBaud++;
@@ -95,15 +138,16 @@
                     break;
             }
             break;
-        case AM_BAUDING:
+        case AM_CHANGE_BAUD:
             switch (result)
             {
                 case AT_NONE:
-                    AtBaud(BAUD, &result);
+                    LogF("Changing baud to %d\r\n", CfgBaud);
+                    AtBaud(CfgBaud, &result);
                     break;
                 case AT_SUCCESS:
-                    EspBaud(BAUD);
-                    am = AM_STOPPING;
+                    EspBaud(CfgBaud);
+                    am = AM_GET_STATUS;
                     result = AT_NONE;
                     break;
                 default:
@@ -111,14 +155,16 @@
                     return -1;
             }
             break;
-        case AM_CONNECTING:
+        case AM_CONNECT:
             switch (result)
             {
                 case AT_NONE:
-                    AtConnect(SSID, PASSWORD, &result);
+                    AtConnect(CfgSsid, CfgPassword, &result);
                     break;
                 case AT_SUCCESS:
-                    am = AM_MUXING;
+                    wifiWaitTimer.reset();
+                    wifiWaitTimer.start();
+                    am = AM_GET_STATUS;
                     result = AT_NONE;
                     break;
                 default:
@@ -126,13 +172,63 @@
                     return -1;
             }
             break;
-        case AM_MUXING:
+        case AM_GET_STATUS:
+            if (wifiWaitTimer.read_ms() < IP_WAIT_TIME_MS) break; //Do nothing until enough time has passed
+            wifiWaitTimer.stop();
+            switch (result)
+            {
+                case AT_NONE:
+                    AtGetEspStatus(&result);
+                    break;
+                case AT_SUCCESS:
+                    switch(AtEspStatus)
+                    {
+                        case 2:
+                            WifiStatus = WIFI_GOT_IP;
+                            am = AM_MUX;
+                            break;
+                        case 3:
+                            WifiStatus = WIFI_CONNECTED;
+                            LogCrLf("Could connect but not get an IP address");
+                            return -1;
+                        case 4:
+                            WifiStatus = WIFI_READY;
+                            am = AM_CONNECT;
+                            break;
+                        default:
+                            LogF("Unknown CIPSTATUS --> STATUS:%d", AtEspStatus);
+                            return -1;
+                    }
+                    result = AT_NONE;
+                    break;
+                default:
+                    LogCrLf("Could not connect to WiFi");
+                    return -1;
+            }
+            break;
+        
+        case AM_MUX:
             switch (result)
             {
                 case AT_NONE:
                     AtMux(&result);
                     break;
                 case AT_SUCCESS:
+                    am = AM_VERSION;
+                    result = AT_NONE;
+                    break;
+                default:
+                    LogCrLf("Could not set up multiple ids");
+                    return -1;
+            }
+            break;
+        case AM_VERSION:
+            switch (result)
+            {
+                case AT_NONE:
+                    AtGetEspVersion(&result);
+                    break;
+                case AT_SUCCESS:
                     am = AM_STARTED;
                     result = AT_NONE;
                     break;
@@ -140,8 +236,9 @@
                     LogCrLf("Could not set up multiple ids");
                     return -1;
             }
-            break;
+            break;        
         case AM_STARTED:
+            wifiWaitTimer.stop();
             return 0;
         default:
             LogF("Unknown \'am\' %d", am);