![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Evan Davies ECE 4180 Lab4: Wifi equipped Robot
Dependencies: Motordriver mbed
main.cpp@0:47657ab4100b, 2015-10-21 (annotated)
- Committer:
- edavies
- Date:
- Wed Oct 21 16:11:38 2015 +0000
- Revision:
- 0:47657ab4100b
Evan Davies: ECE 4180 Lab4: Wifi equipped robot.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
edavies | 0:47657ab4100b | 1 | // Evan Davies ECE 4180 Lab 4 Wireless Robot |
edavies | 0:47657ab4100b | 2 | |
edavies | 0:47657ab4100b | 3 | #include "mbed.h" |
edavies | 0:47657ab4100b | 4 | #include "motordriver.h" |
edavies | 0:47657ab4100b | 5 | |
edavies | 0:47657ab4100b | 6 | |
edavies | 0:47657ab4100b | 7 | Serial pc(USBTX, USBRX); |
edavies | 0:47657ab4100b | 8 | |
edavies | 0:47657ab4100b | 9 | // setup for wifi module |
edavies | 0:47657ab4100b | 10 | Serial esp(p28, p27); // tx, rx |
edavies | 0:47657ab4100b | 11 | DigitalOut reset(p26); |
edavies | 0:47657ab4100b | 12 | |
edavies | 0:47657ab4100b | 13 | //Setup motors |
edavies | 0:47657ab4100b | 14 | Motor A(p22, p6, p5, 1); // pwm, fwd, rev, can brake |
edavies | 0:47657ab4100b | 15 | Motor B(p21, p7, p8, 1); // pwm, fwd, rev, can brake |
edavies | 0:47657ab4100b | 16 | |
edavies | 0:47657ab4100b | 17 | // Standard Mbed LED definitions |
edavies | 0:47657ab4100b | 18 | DigitalOut led1(LED1); // (PTB18) |
edavies | 0:47657ab4100b | 19 | DigitalOut led2(LED2); // (PTB19) |
edavies | 0:47657ab4100b | 20 | DigitalOut led3(LED3); // (PTD1) |
edavies | 0:47657ab4100b | 21 | DigitalOut led4(LED4); |
edavies | 0:47657ab4100b | 22 | int LeftWheelDir; |
edavies | 0:47657ab4100b | 23 | int LeftWheelSpeed; |
edavies | 0:47657ab4100b | 24 | int RightWheelSpeed; |
edavies | 0:47657ab4100b | 25 | int RightWheelDir; |
edavies | 0:47657ab4100b | 26 | |
edavies | 0:47657ab4100b | 27 | |
edavies | 0:47657ab4100b | 28 | Timer t1; |
edavies | 0:47657ab4100b | 29 | Timer t2; |
edavies | 0:47657ab4100b | 30 | |
edavies | 0:47657ab4100b | 31 | struct tm t; |
edavies | 0:47657ab4100b | 32 | |
edavies | 0:47657ab4100b | 33 | int bufflen, DataRX, count, getcount, replycount, servreq, timeout; |
edavies | 0:47657ab4100b | 34 | int bufl, ipdLen, linkID, weberror, webcounter; |
edavies | 0:47657ab4100b | 35 | float temperature, AdcIn, Ht; |
edavies | 0:47657ab4100b | 36 | float R1=100000, R2=10000; // resistor values to give a 10:1 reduction of measured AnalogIn voltage |
edavies | 0:47657ab4100b | 37 | char Vcc[10]; |
edavies | 0:47657ab4100b | 38 | char Temp[10]; |
edavies | 0:47657ab4100b | 39 | char temp[10]; |
edavies | 0:47657ab4100b | 40 | char webcount[8]; |
edavies | 0:47657ab4100b | 41 | char lasthit[30]; |
edavies | 0:47657ab4100b | 42 | char timebuf[30]; |
edavies | 0:47657ab4100b | 43 | char type[16]; |
edavies | 0:47657ab4100b | 44 | char type1[16]; |
edavies | 0:47657ab4100b | 45 | char channel[2]; |
edavies | 0:47657ab4100b | 46 | char cmdbuff[32]; |
edavies | 0:47657ab4100b | 47 | char replybuff[1024]; |
edavies | 0:47657ab4100b | 48 | char webdata[1024]; // This may need to be bigger depending on WEB browser used |
edavies | 0:47657ab4100b | 49 | char webbuff[4096]; // Currently using 1986 characters, Increase this if more web page data added |
edavies | 0:47657ab4100b | 50 | |
edavies | 0:47657ab4100b | 51 | void SendCMD(),getreply(),ReadWebData(),startserver(),sendpage(),SendWEB(),sendcheck(); |
edavies | 0:47657ab4100b | 52 | void gettime(),setRTC(); |
edavies | 0:47657ab4100b | 53 | |
edavies | 0:47657ab4100b | 54 | // manual set RTC values |
edavies | 0:47657ab4100b | 55 | int minute =00; // 0-59 |
edavies | 0:47657ab4100b | 56 | int hour =12; // 2-23 |
edavies | 0:47657ab4100b | 57 | int dayofmonth =26; // 1-31 |
edavies | 0:47657ab4100b | 58 | int month =8; // 1-12 |
edavies | 0:47657ab4100b | 59 | int year =15; // last 2 digits |
edavies | 0:47657ab4100b | 60 | |
edavies | 0:47657ab4100b | 61 | int port =80; // set server port |
edavies | 0:47657ab4100b | 62 | int SERVtimeout =5; // set server timeout in seconds in case link breaks. |
edavies | 0:47657ab4100b | 63 | |
edavies | 0:47657ab4100b | 64 | // Serial Interrupt read ESP data |
edavies | 0:47657ab4100b | 65 | void callback() |
edavies | 0:47657ab4100b | 66 | { |
edavies | 0:47657ab4100b | 67 | //RightWheelSpeed=1; |
edavies | 0:47657ab4100b | 68 | while (esp.readable()) { |
edavies | 0:47657ab4100b | 69 | webbuff[count] = esp.getc(); |
edavies | 0:47657ab4100b | 70 | count++; |
edavies | 0:47657ab4100b | 71 | } |
edavies | 0:47657ab4100b | 72 | if(strlen(webbuff)>bufflen) { |
edavies | 0:47657ab4100b | 73 | DataRX=1; |
edavies | 0:47657ab4100b | 74 | //RightWheelSpeed=0; |
edavies | 0:47657ab4100b | 75 | } |
edavies | 0:47657ab4100b | 76 | } |
edavies | 0:47657ab4100b | 77 | |
edavies | 0:47657ab4100b | 78 | int main() |
edavies | 0:47657ab4100b | 79 | { |
edavies | 0:47657ab4100b | 80 | A.speed(0); |
edavies | 0:47657ab4100b | 81 | B.speed(0); |
edavies | 0:47657ab4100b | 82 | reset=0; |
edavies | 0:47657ab4100b | 83 | pc.baud(115200); |
edavies | 0:47657ab4100b | 84 | |
edavies | 0:47657ab4100b | 85 | pc.printf("\f\n\r------------ ESP8266 Hardware Reset --------------\n\r"); |
edavies | 0:47657ab4100b | 86 | wait(0.5); |
edavies | 0:47657ab4100b | 87 | reset=1; |
edavies | 0:47657ab4100b | 88 | LeftWheelSpeed=0,LeftWheelDir=0,RightWheelSpeed=0, RightWheelDir=0; |
edavies | 0:47657ab4100b | 89 | timeout=6000; |
edavies | 0:47657ab4100b | 90 | getcount=500; |
edavies | 0:47657ab4100b | 91 | getreply(); |
edavies | 0:47657ab4100b | 92 | esp.baud(115200); // ESP8266 baudrate. Maximum on KLxx' is 115200, 230400 works on K20 and K22F |
edavies | 0:47657ab4100b | 93 | if (time(NULL) < 1420070400) { |
edavies | 0:47657ab4100b | 94 | setRTC(); |
edavies | 0:47657ab4100b | 95 | } |
edavies | 0:47657ab4100b | 96 | |
edavies | 0:47657ab4100b | 97 | startserver(); |
edavies | 0:47657ab4100b | 98 | |
edavies | 0:47657ab4100b | 99 | while(1) { |
edavies | 0:47657ab4100b | 100 | if(DataRX==1) { |
edavies | 0:47657ab4100b | 101 | ReadWebData(); |
edavies | 0:47657ab4100b | 102 | if (servreq == 1 && weberror == 0) { |
edavies | 0:47657ab4100b | 103 | sendpage(); |
edavies | 0:47657ab4100b | 104 | } |
edavies | 0:47657ab4100b | 105 | esp.attach(&callback); |
edavies | 0:47657ab4100b | 106 | pc.printf(" IPD Data:\r\n\n Link ID = %d,\r\n IPD Header Length = %d \r\n IPD Type = %s\r\n", linkID, ipdLen, type); |
edavies | 0:47657ab4100b | 107 | pc.printf("\n\n HTTP Packet: \n\n%s\n", webdata); |
edavies | 0:47657ab4100b | 108 | pc.printf(" Web Characters sent : %d\n\n", bufl); |
edavies | 0:47657ab4100b | 109 | pc.printf(" -------------------------------------\n\n"); |
edavies | 0:47657ab4100b | 110 | strcpy(lasthit, timebuf); |
edavies | 0:47657ab4100b | 111 | servreq=0; |
edavies | 0:47657ab4100b | 112 | } |
edavies | 0:47657ab4100b | 113 | } |
edavies | 0:47657ab4100b | 114 | } |
edavies | 0:47657ab4100b | 115 | // Static WEB page |
edavies | 0:47657ab4100b | 116 | void sendpage() |
edavies | 0:47657ab4100b | 117 | { |
edavies | 0:47657ab4100b | 118 | |
edavies | 0:47657ab4100b | 119 | gettime(); |
edavies | 0:47657ab4100b | 120 | |
edavies | 0:47657ab4100b | 121 | // WEB page data |
edavies | 0:47657ab4100b | 122 | strcpy(webbuff, "<!DOCTYPE html>"); |
edavies | 0:47657ab4100b | 123 | strcat(webbuff, "<html><head><title>4180 Wireless Robot</title></head>"); |
edavies | 0:47657ab4100b | 124 | strcat(webbuff, "<body>"); |
edavies | 0:47657ab4100b | 125 | strcat(webbuff, "<div style=\"text-align:center; background-color:#F4F4F4; color:#00AEDB;\"><h1>4180 Wireless Robot</h1>"); |
edavies | 0:47657ab4100b | 126 | strcat(webbuff, "Hit Count - "); |
edavies | 0:47657ab4100b | 127 | strcat(webbuff, webcount); |
edavies | 0:47657ab4100b | 128 | strcat(webbuff, "<br>Last Hit - "); |
edavies | 0:47657ab4100b | 129 | strcat(webbuff, lasthit); |
edavies | 0:47657ab4100b | 130 | strcat(webbuff, "</div><br /><hr>"); |
edavies | 0:47657ab4100b | 131 | strcat(webbuff, "<h3>Mbed RTC Time -  "); |
edavies | 0:47657ab4100b | 132 | strcat(webbuff, timebuf); |
edavies | 0:47657ab4100b | 133 | strcat(webbuff, "</h3>\r\n"); |
edavies | 0:47657ab4100b | 134 | strcat(webbuff, "<p><form method=\"POST\"><strong>"); |
edavies | 0:47657ab4100b | 135 | |
edavies | 0:47657ab4100b | 136 | // strcat(webbuff, "<p><form method=\"POST\"><strong> Analog 1:  <input type=\"text\" size=6 value=\""); |
edavies | 0:47657ab4100b | 137 | // strcat(webbuff, Temp); |
edavies | 0:47657ab4100b | 138 | // strcat(webbuff, "\"> </sup>V <form method=\"POST\"> <strong>   Analog 2:  <input type=\"text\" size=4 value=\""); |
edavies | 0:47657ab4100b | 139 | // strcat(webbuff, Vcc); |
edavies | 0:47657ab4100b | 140 | // strcat(webbuff, "\"> </sup>V"); |
edavies | 0:47657ab4100b | 141 | if(LeftWheelSpeed==0) { |
edavies | 0:47657ab4100b | 142 | strcat(webbuff, "<p><input type=\"radio\" name=\"LeftWheelSpeed\" value=\"1\" > Left Wheel on"); |
edavies | 0:47657ab4100b | 143 | strcat(webbuff, "<br><input type=\"radio\" name=\"LeftWheelSpeed\" value=\"0\" checked> Left Wheel off"); |
edavies | 0:47657ab4100b | 144 | } else { |
edavies | 0:47657ab4100b | 145 | strcat(webbuff, "<p><input type=\"radio\" name=\"LeftWheelSpeed\" value=\"1\" checked> Left Wheel on"); |
edavies | 0:47657ab4100b | 146 | strcat(webbuff, "<br><input type=\"radio\" name=\"LeftWheelSpeed\" value=\"0\" > Left Wheel off"); |
edavies | 0:47657ab4100b | 147 | } |
edavies | 0:47657ab4100b | 148 | |
edavies | 0:47657ab4100b | 149 | if(LeftWheelDir==0) { |
edavies | 0:47657ab4100b | 150 | strcat(webbuff, "<p><input type=\"radio\" name=\"LeftWheelDir\" value=\"1\" > Left Wheel Forward"); |
edavies | 0:47657ab4100b | 151 | strcat(webbuff, "<br><input type=\"radio\" name=\"LeftWheelDir\" value=\"0\" checked> Left Wheel Reverse"); |
edavies | 0:47657ab4100b | 152 | } else { |
edavies | 0:47657ab4100b | 153 | strcat(webbuff, "<p><input type=\"radio\" name=\"LeftWheelDir\" value=\"1\" checked> Left Wheel Forward"); |
edavies | 0:47657ab4100b | 154 | strcat(webbuff, "<br><input type=\"radio\" name=\"LeftWheelDir\" value=\"0\" > Left Wheel Reverse"); |
edavies | 0:47657ab4100b | 155 | } |
edavies | 0:47657ab4100b | 156 | |
edavies | 0:47657ab4100b | 157 | if(RightWheelSpeed==0) { |
edavies | 0:47657ab4100b | 158 | strcat(webbuff, "<p><input type=\"radio\" name=\"RightWheelSpeed\" value=\"1\" > Right Wheel on"); |
edavies | 0:47657ab4100b | 159 | strcat(webbuff, "<br><input type=\"radio\" name=\"RightWheelSpeed\" value=\"0\" checked> Right Wheel off"); |
edavies | 0:47657ab4100b | 160 | } else { |
edavies | 0:47657ab4100b | 161 | strcat(webbuff, "<p><input type=\"radio\" name=\"RightWheelSpeed\" value=\"1\" checked> Right Wheel on"); |
edavies | 0:47657ab4100b | 162 | strcat(webbuff, "<br><input type=\"radio\" name=\"RightWheelSpeed\" value=\"0\" > Right Wheel off"); |
edavies | 0:47657ab4100b | 163 | } |
edavies | 0:47657ab4100b | 164 | |
edavies | 0:47657ab4100b | 165 | if(RightWheelDir==0) { |
edavies | 0:47657ab4100b | 166 | strcat(webbuff, "<p><input type=\"radio\" name=\"RightWheelDir\" value=\"1\" > Right Wheel Forward"); |
edavies | 0:47657ab4100b | 167 | strcat(webbuff, "<br><input type=\"radio\" name=\"RightWheelDir\" value=\"0\" checked> Right Wheel Reverse"); |
edavies | 0:47657ab4100b | 168 | } else { |
edavies | 0:47657ab4100b | 169 | strcat(webbuff, "<p><input type=\"radio\" name=\"RightWheelDir\" value=\"1\" checked> Right Wheel Forward"); |
edavies | 0:47657ab4100b | 170 | strcat(webbuff, "<br><input type=\"radio\" name=\"RightWheelDir\" value=\"0\" > Right Wheel Reverse"); |
edavies | 0:47657ab4100b | 171 | } |
edavies | 0:47657ab4100b | 172 | |
edavies | 0:47657ab4100b | 173 | strcat(webbuff, "</strong><p><input type=\"submit\" value=\"send-refresh\" style=\"background: #3498db;"); |
edavies | 0:47657ab4100b | 174 | strcat(webbuff, "background-image:-webkit-linear-gradient(top, #3498db, #2980b9);"); |
edavies | 0:47657ab4100b | 175 | strcat(webbuff, "background-image:linear-gradient(to bottom, #3498db, #2980b9);"); |
edavies | 0:47657ab4100b | 176 | strcat(webbuff, "-webkit-border-radius:12;border-radius: 12px;font-family: Arial;color:#ffffff;font-size:20px;padding:"); |
edavies | 0:47657ab4100b | 177 | strcat(webbuff, "10px 20px 10px 20px; border:solid #103c57 3px;text-decoration: none;"); |
edavies | 0:47657ab4100b | 178 | strcat(webbuff, "background: #3cb0fd;"); |
edavies | 0:47657ab4100b | 179 | strcat(webbuff, "background-image:-webkit-linear-gradient(top,#3cb0fd,#1a5f8a);"); |
edavies | 0:47657ab4100b | 180 | strcat(webbuff, "background-image:linear-gradient(to bottom,#3cb0fd,#1a5f8a);"); |
edavies | 0:47657ab4100b | 181 | strcat(webbuff, "text-decoration:none;\"></form></span>"); |
edavies | 0:47657ab4100b | 182 | strcat(webbuff, "<p/><h2>How to use:</h2><ul>"); |
edavies | 0:47657ab4100b | 183 | strcat(webbuff, "<li>Select the Radio buttons to control the digital out pins.</li>"); |
edavies | 0:47657ab4100b | 184 | strcat(webbuff, "<li>Click 'Send-Refresh' to send.</li>"); |
edavies | 0:47657ab4100b | 185 | strcat(webbuff, "<li>Use the 'Send-Refresh' button to refresh the data.</li>"); |
edavies | 0:47657ab4100b | 186 | strcat(webbuff, "</ul>"); |
edavies | 0:47657ab4100b | 187 | strcat(webbuff, "</body></html>"); |
edavies | 0:47657ab4100b | 188 | // end of WEB page data |
edavies | 0:47657ab4100b | 189 | bufl = strlen(webbuff); // get total page buffer length |
edavies | 0:47657ab4100b | 190 | sprintf(cmdbuff,"AT+CIPSEND=%d,%d\r\n", linkID, bufl); // send IPD link channel and buffer character length. |
edavies | 0:47657ab4100b | 191 | timeout=200; |
edavies | 0:47657ab4100b | 192 | getcount=7; |
edavies | 0:47657ab4100b | 193 | SendCMD(); |
edavies | 0:47657ab4100b | 194 | getreply(); |
edavies | 0:47657ab4100b | 195 | SendWEB(); // send web page |
edavies | 0:47657ab4100b | 196 | memset(webbuff, '\0', sizeof(webbuff)); |
edavies | 0:47657ab4100b | 197 | sendcheck(); |
edavies | 0:47657ab4100b | 198 | } |
edavies | 0:47657ab4100b | 199 | |
edavies | 0:47657ab4100b | 200 | // wait for ESP "SEND OK" reply, then close IP to load web page |
edavies | 0:47657ab4100b | 201 | void sendcheck() |
edavies | 0:47657ab4100b | 202 | { |
edavies | 0:47657ab4100b | 203 | weberror=1; |
edavies | 0:47657ab4100b | 204 | timeout=500; |
edavies | 0:47657ab4100b | 205 | getcount=24; |
edavies | 0:47657ab4100b | 206 | t2.reset(); |
edavies | 0:47657ab4100b | 207 | t2.start(); |
edavies | 0:47657ab4100b | 208 | while(weberror==1 && t2.read() <5) { |
edavies | 0:47657ab4100b | 209 | getreply(); |
edavies | 0:47657ab4100b | 210 | if (strstr(replybuff, "SEND OK") != NULL) { |
edavies | 0:47657ab4100b | 211 | weberror=0; // wait for valid SEND OK |
edavies | 0:47657ab4100b | 212 | } |
edavies | 0:47657ab4100b | 213 | } |
edavies | 0:47657ab4100b | 214 | if(weberror==1) { // restart connection |
edavies | 0:47657ab4100b | 215 | strcpy(cmdbuff, "AT+CIPMUX=1\r\n"); |
edavies | 0:47657ab4100b | 216 | timeout=500; |
edavies | 0:47657ab4100b | 217 | getcount=10; |
edavies | 0:47657ab4100b | 218 | SendCMD(); |
edavies | 0:47657ab4100b | 219 | getreply(); |
edavies | 0:47657ab4100b | 220 | pc.printf(replybuff); |
edavies | 0:47657ab4100b | 221 | sprintf(cmdbuff,"AT+CIPSERVER=1,%d\r\n", port); |
edavies | 0:47657ab4100b | 222 | timeout=500; |
edavies | 0:47657ab4100b | 223 | getcount=10; |
edavies | 0:47657ab4100b | 224 | SendCMD(); |
edavies | 0:47657ab4100b | 225 | getreply(); |
edavies | 0:47657ab4100b | 226 | pc.printf(replybuff); |
edavies | 0:47657ab4100b | 227 | } else { |
edavies | 0:47657ab4100b | 228 | sprintf(cmdbuff, "AT+CIPCLOSE=%s\r\n",channel); // close current connection |
edavies | 0:47657ab4100b | 229 | SendCMD(); |
edavies | 0:47657ab4100b | 230 | getreply(); |
edavies | 0:47657ab4100b | 231 | pc.printf(replybuff); |
edavies | 0:47657ab4100b | 232 | } |
edavies | 0:47657ab4100b | 233 | t2.reset(); |
edavies | 0:47657ab4100b | 234 | } |
edavies | 0:47657ab4100b | 235 | |
edavies | 0:47657ab4100b | 236 | // Reads and processes GET and POST web data |
edavies | 0:47657ab4100b | 237 | void ReadWebData() |
edavies | 0:47657ab4100b | 238 | { |
edavies | 0:47657ab4100b | 239 | wait_ms(200); |
edavies | 0:47657ab4100b | 240 | esp.attach(NULL); |
edavies | 0:47657ab4100b | 241 | count=0; |
edavies | 0:47657ab4100b | 242 | DataRX=0; |
edavies | 0:47657ab4100b | 243 | weberror=0; |
edavies | 0:47657ab4100b | 244 | memset(webdata, '\0', sizeof(webdata)); |
edavies | 0:47657ab4100b | 245 | int x = strcspn (webbuff,"+"); |
edavies | 0:47657ab4100b | 246 | if(x) { |
edavies | 0:47657ab4100b | 247 | strcpy(webdata, webbuff + x); |
edavies | 0:47657ab4100b | 248 | weberror=0; |
edavies | 0:47657ab4100b | 249 | int numMatched = sscanf(webdata,"+IPD,%d,%d:%s", &linkID, &ipdLen, type); |
edavies | 0:47657ab4100b | 250 | if( strstr(webdata, "LeftWheelSpeed=1") != NULL ) { |
edavies | 0:47657ab4100b | 251 | LeftWheelSpeed=1; |
edavies | 0:47657ab4100b | 252 | led1 = LeftWheelSpeed; |
edavies | 0:47657ab4100b | 253 | } |
edavies | 0:47657ab4100b | 254 | if( strstr(webdata, "LeftWheelSpeed=0") != NULL ) { |
edavies | 0:47657ab4100b | 255 | LeftWheelSpeed=0; |
edavies | 0:47657ab4100b | 256 | led1 = LeftWheelSpeed; |
edavies | 0:47657ab4100b | 257 | } |
edavies | 0:47657ab4100b | 258 | |
edavies | 0:47657ab4100b | 259 | if( strstr(webdata, "LeftWheelDir=1") != NULL ) { |
edavies | 0:47657ab4100b | 260 | LeftWheelDir=1; |
edavies | 0:47657ab4100b | 261 | led2 = LeftWheelDir; |
edavies | 0:47657ab4100b | 262 | |
edavies | 0:47657ab4100b | 263 | } |
edavies | 0:47657ab4100b | 264 | if( strstr(webdata, "LeftWheelDir=0") != NULL ) { |
edavies | 0:47657ab4100b | 265 | LeftWheelDir=0; |
edavies | 0:47657ab4100b | 266 | led2 = LeftWheelDir; |
edavies | 0:47657ab4100b | 267 | } |
edavies | 0:47657ab4100b | 268 | if( strstr(webdata, "RightWheelSpeed=1") != NULL ) { |
edavies | 0:47657ab4100b | 269 | RightWheelSpeed=1; |
edavies | 0:47657ab4100b | 270 | led3 = RightWheelSpeed; |
edavies | 0:47657ab4100b | 271 | } |
edavies | 0:47657ab4100b | 272 | if( strstr(webdata, "RightWheelSpeed=0") != NULL ) { |
edavies | 0:47657ab4100b | 273 | RightWheelSpeed=0; |
edavies | 0:47657ab4100b | 274 | led3 = RightWheelSpeed; |
edavies | 0:47657ab4100b | 275 | } |
edavies | 0:47657ab4100b | 276 | |
edavies | 0:47657ab4100b | 277 | if( strstr(webdata, "RightWheelDir=1") != NULL ) { |
edavies | 0:47657ab4100b | 278 | RightWheelDir=1; |
edavies | 0:47657ab4100b | 279 | led4 = RightWheelDir; |
edavies | 0:47657ab4100b | 280 | } |
edavies | 0:47657ab4100b | 281 | if( strstr(webdata, "RightWheelDir=0") != NULL ) { |
edavies | 0:47657ab4100b | 282 | RightWheelDir=0; |
edavies | 0:47657ab4100b | 283 | led4 = RightWheelDir; |
edavies | 0:47657ab4100b | 284 | } |
edavies | 0:47657ab4100b | 285 | A.speed((LeftWheelSpeed)*((LeftWheelDir-.5))*-1); |
edavies | 0:47657ab4100b | 286 | |
edavies | 0:47657ab4100b | 287 | B.speed((RightWheelSpeed)*((RightWheelDir-.5))); |
edavies | 0:47657ab4100b | 288 | sprintf(channel, "%d",linkID); |
edavies | 0:47657ab4100b | 289 | if (strstr(webdata, "GET") != NULL) { |
edavies | 0:47657ab4100b | 290 | servreq=1; |
edavies | 0:47657ab4100b | 291 | } |
edavies | 0:47657ab4100b | 292 | if (strstr(webdata, "POST") != NULL) { |
edavies | 0:47657ab4100b | 293 | servreq=1; |
edavies | 0:47657ab4100b | 294 | } |
edavies | 0:47657ab4100b | 295 | webcounter++; |
edavies | 0:47657ab4100b | 296 | sprintf(webcount, "%d",webcounter); |
edavies | 0:47657ab4100b | 297 | } else { |
edavies | 0:47657ab4100b | 298 | memset(webbuff, '\0', sizeof(webbuff)); |
edavies | 0:47657ab4100b | 299 | esp.attach(&callback); |
edavies | 0:47657ab4100b | 300 | weberror=1; |
edavies | 0:47657ab4100b | 301 | } |
edavies | 0:47657ab4100b | 302 | } |
edavies | 0:47657ab4100b | 303 | // Starts and restarts webserver if errors detected. |
edavies | 0:47657ab4100b | 304 | void startserver() |
edavies | 0:47657ab4100b | 305 | { |
edavies | 0:47657ab4100b | 306 | |
edavies | 0:47657ab4100b | 307 | pc.printf("\n\n RTC time %s\r\n\n",timebuf); |
edavies | 0:47657ab4100b | 308 | pc.printf("++++++++++ Resetting ESP ++++++++++\r\n"); |
edavies | 0:47657ab4100b | 309 | strcpy(cmdbuff,"AT+RST\r\n"); |
edavies | 0:47657ab4100b | 310 | timeout=8000; |
edavies | 0:47657ab4100b | 311 | getcount=1000; |
edavies | 0:47657ab4100b | 312 | SendCMD(); |
edavies | 0:47657ab4100b | 313 | getreply(); |
edavies | 0:47657ab4100b | 314 | pc.printf(replybuff); |
edavies | 0:47657ab4100b | 315 | pc.printf("%d",count); |
edavies | 0:47657ab4100b | 316 | if (strstr(replybuff, "OK") != NULL) { |
edavies | 0:47657ab4100b | 317 | pc.printf("\n++++++++++ Starting Server ++++++++++\r\n"); |
edavies | 0:47657ab4100b | 318 | strcpy(cmdbuff, "AT+CIPMUX=1\r\n"); // set multiple connections. |
edavies | 0:47657ab4100b | 319 | timeout=500; |
edavies | 0:47657ab4100b | 320 | getcount=20; |
edavies | 0:47657ab4100b | 321 | SendCMD(); |
edavies | 0:47657ab4100b | 322 | getreply(); |
edavies | 0:47657ab4100b | 323 | pc.printf(replybuff); |
edavies | 0:47657ab4100b | 324 | sprintf(cmdbuff,"AT+CIPSERVER=1,%d\r\n", port); |
edavies | 0:47657ab4100b | 325 | timeout=500; |
edavies | 0:47657ab4100b | 326 | getcount=20; |
edavies | 0:47657ab4100b | 327 | SendCMD(); |
edavies | 0:47657ab4100b | 328 | getreply(); |
edavies | 0:47657ab4100b | 329 | pc.printf(replybuff); |
edavies | 0:47657ab4100b | 330 | wait(1); |
edavies | 0:47657ab4100b | 331 | sprintf(cmdbuff,"AT+CIPSTO=%d\r\n",SERVtimeout); |
edavies | 0:47657ab4100b | 332 | timeout=500; |
edavies | 0:47657ab4100b | 333 | getcount=50; |
edavies | 0:47657ab4100b | 334 | SendCMD(); |
edavies | 0:47657ab4100b | 335 | getreply(); |
edavies | 0:47657ab4100b | 336 | pc.printf(replybuff); |
edavies | 0:47657ab4100b | 337 | wait(5); |
edavies | 0:47657ab4100b | 338 | pc.printf("\n Getting Server IP \r\n"); |
edavies | 0:47657ab4100b | 339 | strcpy(cmdbuff, "AT+CIFSR\r\n"); |
edavies | 0:47657ab4100b | 340 | timeout=2500; |
edavies | 0:47657ab4100b | 341 | getcount=200; |
edavies | 0:47657ab4100b | 342 | while(weberror==0) { |
edavies | 0:47657ab4100b | 343 | SendCMD(); |
edavies | 0:47657ab4100b | 344 | getreply(); |
edavies | 0:47657ab4100b | 345 | if (strstr(replybuff, "0.0.0.0") == NULL) { |
edavies | 0:47657ab4100b | 346 | weberror=1; // wait for valid IP |
edavies | 0:47657ab4100b | 347 | } |
edavies | 0:47657ab4100b | 348 | } |
edavies | 0:47657ab4100b | 349 | pc.printf("\n Enter WEB address (IP) found below in your browser \r\n\n"); |
edavies | 0:47657ab4100b | 350 | pc.printf("\n The MAC address is also shown below,if it is needed \r\n\n"); |
edavies | 0:47657ab4100b | 351 | replybuff[strlen(replybuff)-1] = '\0'; |
edavies | 0:47657ab4100b | 352 | //char* IP = replybuff + 5; |
edavies | 0:47657ab4100b | 353 | sprintf(webdata,"%s", replybuff); |
edavies | 0:47657ab4100b | 354 | pc.printf(webdata); |
edavies | 0:47657ab4100b | 355 | LeftWheelDir=1; |
edavies | 0:47657ab4100b | 356 | bufflen=200; |
edavies | 0:47657ab4100b | 357 | count=0; |
edavies | 0:47657ab4100b | 358 | pc.printf("\n\n++++++++++ Ready ++++++++++\r\n\n"); |
edavies | 0:47657ab4100b | 359 | esp.attach(&callback); |
edavies | 0:47657ab4100b | 360 | } else { |
edavies | 0:47657ab4100b | 361 | pc.printf("\n++++++++++ ESP8266 error, check power/connections ++++++++++\r\n"); |
edavies | 0:47657ab4100b | 362 | while(1) {} |
edavies | 0:47657ab4100b | 363 | } |
edavies | 0:47657ab4100b | 364 | t2.reset(); |
edavies | 0:47657ab4100b | 365 | t2.start(); |
edavies | 0:47657ab4100b | 366 | |
edavies | 0:47657ab4100b | 367 | } |
edavies | 0:47657ab4100b | 368 | // ESP Command data send |
edavies | 0:47657ab4100b | 369 | void SendCMD() |
edavies | 0:47657ab4100b | 370 | { |
edavies | 0:47657ab4100b | 371 | esp.printf("%s", cmdbuff); |
edavies | 0:47657ab4100b | 372 | } |
edavies | 0:47657ab4100b | 373 | // Large WEB buffer data send |
edavies | 0:47657ab4100b | 374 | void SendWEB() |
edavies | 0:47657ab4100b | 375 | { |
edavies | 0:47657ab4100b | 376 | int i=0; |
edavies | 0:47657ab4100b | 377 | if(esp.writeable()) { |
edavies | 0:47657ab4100b | 378 | while(webbuff[i]!='\0') { |
edavies | 0:47657ab4100b | 379 | esp.putc(webbuff[i]); |
edavies | 0:47657ab4100b | 380 | i++; |
edavies | 0:47657ab4100b | 381 | } |
edavies | 0:47657ab4100b | 382 | } |
edavies | 0:47657ab4100b | 383 | } |
edavies | 0:47657ab4100b | 384 | // Get Command and ESP status replies |
edavies | 0:47657ab4100b | 385 | void getreply() |
edavies | 0:47657ab4100b | 386 | { |
edavies | 0:47657ab4100b | 387 | memset(replybuff, '\0', sizeof(replybuff)); |
edavies | 0:47657ab4100b | 388 | t1.reset(); |
edavies | 0:47657ab4100b | 389 | t1.start(); |
edavies | 0:47657ab4100b | 390 | replycount=0; |
edavies | 0:47657ab4100b | 391 | while(t1.read_ms()< timeout && replycount < getcount) { |
edavies | 0:47657ab4100b | 392 | if(esp.readable()) { |
edavies | 0:47657ab4100b | 393 | replybuff[replycount] = esp.getc(); |
edavies | 0:47657ab4100b | 394 | replycount++; |
edavies | 0:47657ab4100b | 395 | } |
edavies | 0:47657ab4100b | 396 | } |
edavies | 0:47657ab4100b | 397 | t1.stop(); |
edavies | 0:47657ab4100b | 398 | } |
edavies | 0:47657ab4100b | 399 | // Analog in example |
edavies | 0:47657ab4100b | 400 | |
edavies | 0:47657ab4100b | 401 | // Get RTC time |
edavies | 0:47657ab4100b | 402 | void gettime() |
edavies | 0:47657ab4100b | 403 | { |
edavies | 0:47657ab4100b | 404 | time_t seconds = time(NULL); |
edavies | 0:47657ab4100b | 405 | strftime(timebuf,50,"%H:%M:%S %a %d %b %y", localtime(&seconds)); |
edavies | 0:47657ab4100b | 406 | } |
edavies | 0:47657ab4100b | 407 | |
edavies | 0:47657ab4100b | 408 | |
edavies | 0:47657ab4100b | 409 | |
edavies | 0:47657ab4100b | 410 | void setRTC() |
edavies | 0:47657ab4100b | 411 | { |
edavies | 0:47657ab4100b | 412 | t.tm_sec = (0); // 0-59 |
edavies | 0:47657ab4100b | 413 | t.tm_min = (minute); // 0-59 |
edavies | 0:47657ab4100b | 414 | t.tm_hour = (hour); // 0-23 |
edavies | 0:47657ab4100b | 415 | t.tm_mday = (dayofmonth); // 1-31 |
edavies | 0:47657ab4100b | 416 | t.tm_mon = (month-1); // 0-11 "0" = Jan, -1 added for Mbed RCT clock format |
edavies | 0:47657ab4100b | 417 | t.tm_year = ((year)+100); // year since 1900, current DCF year + 100 + 1900 = correct year |
edavies | 0:47657ab4100b | 418 | set_time(mktime(&t)); // set RTC clock |
edavies | 0:47657ab4100b | 419 | } |