Backing up an unused program in case of future need

Dependencies:   mbed

Committer:
andrewboyson
Date:
Tue May 03 09:23:26 2016 +0000
Revision:
4:e076884ef8bd
Parent:
2:06fa34661f19
Child:
6:be97d38e0b01
Added 1-wire

Who changed what in which revision?

UserRevisionLine numberNew contents of line
andrewboyson 0:09f915e6f9f6 1 #include "mbed.h"
andrewboyson 0:09f915e6f9f6 2 #include "log.h"
andrewboyson 0:09f915e6f9f6 3 #include "esp.h"
andrewboyson 0:09f915e6f9f6 4 #include "io.h"
andrewboyson 0:09f915e6f9f6 5 #include "at.h"
andrewboyson 0:09f915e6f9f6 6 #include "wifi.h"
andrewboyson 0:09f915e6f9f6 7
andrewboyson 0:09f915e6f9f6 8 #define TIMEOUT 10
andrewboyson 0:09f915e6f9f6 9 #define TIMEOUT_OK 2
andrewboyson 0:09f915e6f9f6 10
andrewboyson 0:09f915e6f9f6 11 #define WAIT_FOR_COMMAND 0
andrewboyson 0:09f915e6f9f6 12 #define WAIT_FOR_ESP_READY 1
andrewboyson 0:09f915e6f9f6 13 #define WAIT_FOR_WIFI_CONNECTED 2
andrewboyson 0:09f915e6f9f6 14 #define WAIT_FOR_WIFI_GOT_IP 3
andrewboyson 2:06fa34661f19 15 #define WAIT_FOR_STATUS 4
andrewboyson 2:06fa34661f19 16 #define WAIT_FOR_VERSION 5
andrewboyson 2:06fa34661f19 17 #define WAIT_FOR_OK 6
andrewboyson 2:06fa34661f19 18 #define WAIT_FOR_SEND_OK 7
andrewboyson 0:09f915e6f9f6 19 static int wait_for;
andrewboyson 0:09f915e6f9f6 20
andrewboyson 4:e076884ef8bd 21 #define RESP_LENGTH 256
andrewboyson 4:e076884ef8bd 22 static char resp[RESP_LENGTH];
andrewboyson 4:e076884ef8bd 23
andrewboyson 0:09f915e6f9f6 24 static int * pFeedback;
andrewboyson 0:09f915e6f9f6 25 static void startCommand(int* pStatus, int waitfor)
andrewboyson 0:09f915e6f9f6 26 {
andrewboyson 0:09f915e6f9f6 27 wait_for = waitfor;
andrewboyson 0:09f915e6f9f6 28 pFeedback = pStatus;
andrewboyson 0:09f915e6f9f6 29 if (pFeedback) *pFeedback = AT_NONE;
andrewboyson 0:09f915e6f9f6 30 }
andrewboyson 0:09f915e6f9f6 31 static void finishAndReset(int feedback)
andrewboyson 0:09f915e6f9f6 32 {
andrewboyson 0:09f915e6f9f6 33 if (pFeedback)
andrewboyson 0:09f915e6f9f6 34 {
andrewboyson 0:09f915e6f9f6 35 *pFeedback = feedback;
andrewboyson 0:09f915e6f9f6 36 pFeedback = NULL;
andrewboyson 0:09f915e6f9f6 37 }
andrewboyson 0:09f915e6f9f6 38 wait_for = WAIT_FOR_COMMAND;
andrewboyson 0:09f915e6f9f6 39 }
andrewboyson 0:09f915e6f9f6 40 void AtReleaseResetAndStart(int *pStatus)
andrewboyson 0:09f915e6f9f6 41 {
andrewboyson 0:09f915e6f9f6 42 startCommand(pStatus, WAIT_FOR_ESP_READY);
andrewboyson 0:09f915e6f9f6 43 EspReleaseFromReset();
andrewboyson 0:09f915e6f9f6 44 }
andrewboyson 0:09f915e6f9f6 45 void AtConnect(const char *ssid, const char *password, int *pStatus)
andrewboyson 0:09f915e6f9f6 46 {
andrewboyson 0:09f915e6f9f6 47 startCommand(pStatus, WAIT_FOR_OK);
andrewboyson 4:e076884ef8bd 48 EspSendStringF(resp, RESP_LENGTH, "AT+CWJAP=\"%s\",\"%s\"\r\n", ssid, password);
andrewboyson 0:09f915e6f9f6 49 }
andrewboyson 0:09f915e6f9f6 50 void AtAt(int *pStatus)
andrewboyson 0:09f915e6f9f6 51 {
andrewboyson 0:09f915e6f9f6 52 startCommand(pStatus, WAIT_FOR_OK);
andrewboyson 4:e076884ef8bd 53 EspSendString(resp, RESP_LENGTH, "AT\r\n");
andrewboyson 0:09f915e6f9f6 54 }
andrewboyson 0:09f915e6f9f6 55
andrewboyson 0:09f915e6f9f6 56 void AtConnectId(int id, char * type, char * addr, int port, void * buffer, int bufferlength, int *pStatus)
andrewboyson 0:09f915e6f9f6 57 {
andrewboyson 0:09f915e6f9f6 58 startCommand(pStatus, WAIT_FOR_OK);
andrewboyson 0:09f915e6f9f6 59
andrewboyson 0:09f915e6f9f6 60 EspIpdBuffer[id] = buffer;
andrewboyson 0:09f915e6f9f6 61 EspIpdBufferLen[id] = bufferlength;
andrewboyson 0:09f915e6f9f6 62
andrewboyson 4:e076884ef8bd 63 EspSendStringF(resp, RESP_LENGTH, "AT+CIPSTART=%d,\"%s\",\"%s\",%d\r\n", id, type, addr, port);
andrewboyson 0:09f915e6f9f6 64 }
andrewboyson 0:09f915e6f9f6 65 void AtStartServer(int port, int *pStatus)
andrewboyson 0:09f915e6f9f6 66 {
andrewboyson 0:09f915e6f9f6 67 startCommand(pStatus, WAIT_FOR_OK);
andrewboyson 4:e076884ef8bd 68 EspSendStringF(resp, RESP_LENGTH, "AT+CIPSERVER=1,%d\r\n", port);
andrewboyson 0:09f915e6f9f6 69 }
andrewboyson 0:09f915e6f9f6 70 void AtClose(int id, int *pStatus)
andrewboyson 0:09f915e6f9f6 71 {
andrewboyson 0:09f915e6f9f6 72 startCommand(pStatus, WAIT_FOR_OK);
andrewboyson 4:e076884ef8bd 73 EspSendStringF(resp, RESP_LENGTH, "AT+CIPCLOSE=%d\r\n", id);
andrewboyson 0:09f915e6f9f6 74 }
andrewboyson 0:09f915e6f9f6 75 void AtMux(int *pStatus)
andrewboyson 0:09f915e6f9f6 76 {
andrewboyson 0:09f915e6f9f6 77 startCommand(pStatus, WAIT_FOR_OK);
andrewboyson 4:e076884ef8bd 78 EspSendString(resp, RESP_LENGTH, "AT+CIPMUX=1\r\n");
andrewboyson 0:09f915e6f9f6 79 }
andrewboyson 0:09f915e6f9f6 80 void AtBaud(int baud, int *pStatus)
andrewboyson 0:09f915e6f9f6 81 {
andrewboyson 0:09f915e6f9f6 82 startCommand(pStatus, WAIT_FOR_OK);
andrewboyson 4:e076884ef8bd 83 EspSendStringF(resp, RESP_LENGTH, "AT+CIOBAUD=%d\r\n", baud);
andrewboyson 0:09f915e6f9f6 84 }
andrewboyson 2:06fa34661f19 85 int AtEspStatus;
andrewboyson 2:06fa34661f19 86 void AtGetEspStatus(int *pStatus)
andrewboyson 2:06fa34661f19 87 {
andrewboyson 2:06fa34661f19 88 AtEspStatus = 0;
andrewboyson 2:06fa34661f19 89 startCommand(pStatus, WAIT_FOR_STATUS);
andrewboyson 4:e076884ef8bd 90 EspSendString(resp, RESP_LENGTH, "AT+CIPSTATUS\r\n");
andrewboyson 2:06fa34661f19 91 }
andrewboyson 2:06fa34661f19 92 char AtEspVersion[20];
andrewboyson 2:06fa34661f19 93 static int versionlinecount;
andrewboyson 2:06fa34661f19 94 void AtGetEspVersion(int *pStatus)
andrewboyson 2:06fa34661f19 95 {
andrewboyson 2:06fa34661f19 96 AtEspVersion[0] = '\0';
andrewboyson 2:06fa34661f19 97 startCommand(pStatus, WAIT_FOR_VERSION);
andrewboyson 4:e076884ef8bd 98 EspSendString(resp, RESP_LENGTH, "AT+GMR\r\n");
andrewboyson 2:06fa34661f19 99 versionlinecount = 0;
andrewboyson 2:06fa34661f19 100 }
andrewboyson 0:09f915e6f9f6 101 void AtSendData(int id, int length, const void * pdata, int *pStatus)
andrewboyson 0:09f915e6f9f6 102 {
andrewboyson 0:09f915e6f9f6 103 startCommand(pStatus, WAIT_FOR_SEND_OK);
andrewboyson 0:09f915e6f9f6 104 EspLengthToSend = length;
andrewboyson 0:09f915e6f9f6 105 EspDataToSend = pdata;
andrewboyson 4:e076884ef8bd 106 EspSendStringF(resp, RESP_LENGTH, "AT+CIPSEND=%d,%d\r\n", id, length);
andrewboyson 0:09f915e6f9f6 107 }
andrewboyson 0:09f915e6f9f6 108
andrewboyson 0:09f915e6f9f6 109 int AtBusy()
andrewboyson 0:09f915e6f9f6 110 {
andrewboyson 0:09f915e6f9f6 111 return wait_for;
andrewboyson 0:09f915e6f9f6 112 }
andrewboyson 0:09f915e6f9f6 113 void AtResetAndStop()
andrewboyson 0:09f915e6f9f6 114 {
andrewboyson 0:09f915e6f9f6 115 startCommand(NULL, WAIT_FOR_COMMAND);
andrewboyson 0:09f915e6f9f6 116 EspResetAndStop();
andrewboyson 0:09f915e6f9f6 117 WifiStatus = WIFI_STOPPED;
andrewboyson 0:09f915e6f9f6 118 }
andrewboyson 2:06fa34661f19 119 int AtInit()
andrewboyson 0:09f915e6f9f6 120 {
andrewboyson 0:09f915e6f9f6 121 startCommand(NULL, WAIT_FOR_COMMAND);
andrewboyson 0:09f915e6f9f6 122 WifiStatus = WIFI_STOPPED;
andrewboyson 2:06fa34661f19 123 return 0;
andrewboyson 0:09f915e6f9f6 124 }
andrewboyson 0:09f915e6f9f6 125 int handleLine()
andrewboyson 0:09f915e6f9f6 126 {
andrewboyson 0:09f915e6f9f6 127 switch (wait_for)
andrewboyson 0:09f915e6f9f6 128 {
andrewboyson 0:09f915e6f9f6 129 //No command
andrewboyson 0:09f915e6f9f6 130 case WAIT_FOR_COMMAND:
andrewboyson 0:09f915e6f9f6 131 return 0;
andrewboyson 0:09f915e6f9f6 132
andrewboyson 0:09f915e6f9f6 133 //Connect
andrewboyson 0:09f915e6f9f6 134 case WAIT_FOR_ESP_READY:
andrewboyson 0:09f915e6f9f6 135 if (strcmp(EspLine, "ready") == 0)
andrewboyson 0:09f915e6f9f6 136 {
andrewboyson 0:09f915e6f9f6 137 WifiStatus = WIFI_READY;
andrewboyson 0:09f915e6f9f6 138 wait_for = WAIT_FOR_WIFI_CONNECTED;
andrewboyson 0:09f915e6f9f6 139 }
andrewboyson 0:09f915e6f9f6 140 return 0;
andrewboyson 0:09f915e6f9f6 141 case WAIT_FOR_WIFI_CONNECTED:
andrewboyson 0:09f915e6f9f6 142 if (strcmp(EspLine, "WIFI CONNECTED") == 0)
andrewboyson 0:09f915e6f9f6 143 {
andrewboyson 0:09f915e6f9f6 144 WifiStatus = WIFI_CONNECTED;
andrewboyson 0:09f915e6f9f6 145 wait_for = WAIT_FOR_WIFI_GOT_IP;
andrewboyson 0:09f915e6f9f6 146 }
andrewboyson 0:09f915e6f9f6 147 return 0;
andrewboyson 0:09f915e6f9f6 148 case WAIT_FOR_WIFI_GOT_IP:
andrewboyson 0:09f915e6f9f6 149 if (strcmp(EspLine, "WIFI GOT IP") == 0)
andrewboyson 0:09f915e6f9f6 150 {
andrewboyson 0:09f915e6f9f6 151 WifiStatus = WIFI_GOT_IP;
andrewboyson 0:09f915e6f9f6 152 finishAndReset(AT_SUCCESS);
andrewboyson 0:09f915e6f9f6 153 }
andrewboyson 0:09f915e6f9f6 154 return 0;
andrewboyson 0:09f915e6f9f6 155
andrewboyson 0:09f915e6f9f6 156 //Send command
andrewboyson 0:09f915e6f9f6 157 case WAIT_FOR_SEND_OK:
andrewboyson 0:09f915e6f9f6 158 if (strcmp(EspLine, "SEND OK") == 0) finishAndReset(AT_SUCCESS);
andrewboyson 0:09f915e6f9f6 159 return 0;
andrewboyson 2:06fa34661f19 160
andrewboyson 2:06fa34661f19 161 //Status command
andrewboyson 2:06fa34661f19 162 case WAIT_FOR_STATUS:
andrewboyson 2:06fa34661f19 163 if (sscanf(EspLine, "STATUS:%d", &AtEspStatus) == 1) wait_for = WAIT_FOR_OK;
andrewboyson 2:06fa34661f19 164 return 0;
andrewboyson 2:06fa34661f19 165
andrewboyson 2:06fa34661f19 166 //Version command
andrewboyson 2:06fa34661f19 167 case WAIT_FOR_VERSION:
andrewboyson 2:06fa34661f19 168 if (++versionlinecount == 3)
andrewboyson 2:06fa34661f19 169 {
andrewboyson 2:06fa34661f19 170 strcpy(AtEspVersion, EspLine);
andrewboyson 2:06fa34661f19 171 wait_for = WAIT_FOR_OK;
andrewboyson 2:06fa34661f19 172 }
andrewboyson 2:06fa34661f19 173 return 0;
andrewboyson 0:09f915e6f9f6 174
andrewboyson 0:09f915e6f9f6 175 //Most commands
andrewboyson 0:09f915e6f9f6 176 case WAIT_FOR_OK:
andrewboyson 0:09f915e6f9f6 177 if (strcmp(EspLine, "OK" ) == 0) finishAndReset(AT_SUCCESS);
andrewboyson 0:09f915e6f9f6 178 if (strcmp(EspLine, "ERROR" ) == 0) finishAndReset(AT_ERROR);
andrewboyson 0:09f915e6f9f6 179 return 0;
andrewboyson 0:09f915e6f9f6 180
andrewboyson 0:09f915e6f9f6 181 default:
andrewboyson 0:09f915e6f9f6 182 LogF("Unknown wait_for %d\r\n", wait_for);
andrewboyson 0:09f915e6f9f6 183 return -1;
andrewboyson 0:09f915e6f9f6 184 }
andrewboyson 0:09f915e6f9f6 185 }
andrewboyson 0:09f915e6f9f6 186 int AtMain()
andrewboyson 0:09f915e6f9f6 187 {
andrewboyson 0:09f915e6f9f6 188
andrewboyson 0:09f915e6f9f6 189 //Monitor time taken
andrewboyson 0:09f915e6f9f6 190 static Timer receiveTimer;
andrewboyson 0:09f915e6f9f6 191 if (wait_for)
andrewboyson 0:09f915e6f9f6 192 {
andrewboyson 0:09f915e6f9f6 193 receiveTimer.start();
andrewboyson 0:09f915e6f9f6 194 }
andrewboyson 0:09f915e6f9f6 195 else
andrewboyson 0:09f915e6f9f6 196 {
andrewboyson 0:09f915e6f9f6 197 receiveTimer.stop();
andrewboyson 0:09f915e6f9f6 198 receiveTimer.reset();
andrewboyson 0:09f915e6f9f6 199 }
andrewboyson 0:09f915e6f9f6 200
andrewboyson 0:09f915e6f9f6 201 //Check for problems; feedback status and reset
andrewboyson 0:09f915e6f9f6 202 if (receiveTimer.read() > TIMEOUT || (wait_for == WAIT_FOR_OK && receiveTimer.read() > TIMEOUT_OK))
andrewboyson 0:09f915e6f9f6 203 {
andrewboyson 0:09f915e6f9f6 204 finishAndReset(AT_RESPONCE_TIMEOUT);
andrewboyson 0:09f915e6f9f6 205 LogCrLf("Command timed out");
andrewboyson 0:09f915e6f9f6 206 return 0;
andrewboyson 0:09f915e6f9f6 207 }
andrewboyson 0:09f915e6f9f6 208
andrewboyson 0:09f915e6f9f6 209
andrewboyson 0:09f915e6f9f6 210 //Depending on what the Esp has returned: do nothing; treat the line or finish and reset
andrewboyson 0:09f915e6f9f6 211 switch (EspLineAvailable)
andrewboyson 0:09f915e6f9f6 212 {
andrewboyson 0:09f915e6f9f6 213 case ESP_IDLE:
andrewboyson 0:09f915e6f9f6 214 break;
andrewboyson 0:09f915e6f9f6 215 case ESP_AVAILABLE:
andrewboyson 0:09f915e6f9f6 216 if (handleLine()) return -1;
andrewboyson 0:09f915e6f9f6 217 break;
andrewboyson 0:09f915e6f9f6 218 case ESP_TIMEOUT:
andrewboyson 0:09f915e6f9f6 219 finishAndReset(AT_LINE_TIMEOUT);
andrewboyson 0:09f915e6f9f6 220 LogCrLf("Esp timed out");
andrewboyson 0:09f915e6f9f6 221 break;
andrewboyson 0:09f915e6f9f6 222 case ESP_OVERFLOW:
andrewboyson 1:94282484baae 223 if (wait_for == WAIT_FOR_ESP_READY) break; //Ignore overflows from the esp's 450 byte boot message
andrewboyson 0:09f915e6f9f6 224 finishAndReset(AT_LINE_OVERFLOW);
andrewboyson 0:09f915e6f9f6 225 LogCrLf("Esp buffer overflow");
andrewboyson 0:09f915e6f9f6 226 break;
andrewboyson 0:09f915e6f9f6 227 default:
andrewboyson 0:09f915e6f9f6 228 LogF("Unknown EsplineAvailable %d\r\n");
andrewboyson 0:09f915e6f9f6 229 return -1;
andrewboyson 0:09f915e6f9f6 230 }
andrewboyson 0:09f915e6f9f6 231
andrewboyson 0:09f915e6f9f6 232 return 0;
andrewboyson 0:09f915e6f9f6 233 }