Fork
Dependencies: Motordriver mbed-rtos mbed
Fork of ESP8266-WEB-Mbed-LPC1768-Controller by
main.cpp@5:cfceccd5ccb1, 2015-10-20 (annotated)
- Committer:
- chrisprice92
- Date:
- Tue Oct 20 18:21:16 2015 +0000
- Revision:
- 5:cfceccd5ccb1
- Parent:
- 4:40dd020463ea
Working with two threads.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
star297 | 0:e2a155f50119 | 1 | // ESP8266 Static page WEB server to control Mbed |
star297 | 0:e2a155f50119 | 2 | |
star297 | 0:e2a155f50119 | 3 | #include "mbed.h" |
4180_1 | 4:40dd020463ea | 4 | //#include "DS18B20.h" |
chrisprice92 | 5:cfceccd5ccb1 | 5 | #include "motordriver.h" |
chrisprice92 | 5:cfceccd5ccb1 | 6 | #include "rtos.h" |
star297 | 0:e2a155f50119 | 7 | |
chrisprice92 | 5:cfceccd5ccb1 | 8 | RawSerial pc(USBTX, USBRX); |
chrisprice92 | 5:cfceccd5ccb1 | 9 | RawSerial esp(p28, p27); // tx, rx |
star297 | 0:e2a155f50119 | 10 | |
4180_1 | 4:40dd020463ea | 11 | //DS18B20 thermom(A0, DS18B20::RES_12_BIT); |
star297 | 2:d4c6bc0f2dc4 | 12 | |
star297 | 0:e2a155f50119 | 13 | // Standard Mbed LED definitions |
4180_1 | 4:40dd020463ea | 14 | DigitalOut led1(LED1); // (PTB18) |
4180_1 | 4:40dd020463ea | 15 | DigitalOut led2(LED2); // (PTB19) |
4180_1 | 4:40dd020463ea | 16 | DigitalOut led3(LED3); // (PTD1) |
chrisprice92 | 5:cfceccd5ccb1 | 17 | DigitalOut led4(LED4); |
chrisprice92 | 5:cfceccd5ccb1 | 18 | Motor left(p21, p23, p22, 1); // pwm, fwd, rev, has brake feature |
chrisprice92 | 5:cfceccd5ccb1 | 19 | Motor right(p26, p25, p24, 1); |
star297 | 0:e2a155f50119 | 20 | |
star297 | 0:e2a155f50119 | 21 | // Digital Out and In pins, can be configured to any suitable pin depending on Platform |
4180_1 | 4:40dd020463ea | 22 | DigitalOut Out1(p6); |
4180_1 | 4:40dd020463ea | 23 | DigitalOut Out2(p7); |
4180_1 | 4:40dd020463ea | 24 | DigitalOut Out3(p8); |
chrisprice92 | 5:cfceccd5ccb1 | 25 | DigitalOut reset(p29); |
star297 | 0:e2a155f50119 | 26 | |
4180_1 | 4:40dd020463ea | 27 | DigitalIn In1(p9); |
4180_1 | 4:40dd020463ea | 28 | DigitalIn In2(p10); |
4180_1 | 4:40dd020463ea | 29 | DigitalIn In3(p11); |
star297 | 0:e2a155f50119 | 30 | |
4180_1 | 4:40dd020463ea | 31 | PwmOut speaker(p21); |
4180_1 | 4:40dd020463ea | 32 | AnalogIn Ain1(p18); |
4180_1 | 4:40dd020463ea | 33 | AnalogIn Ain2(p19); |
star297 | 0:e2a155f50119 | 34 | |
star297 | 0:e2a155f50119 | 35 | Timer t1; |
star297 | 1:71ed1afbf344 | 36 | Timer t2; |
star297 | 1:71ed1afbf344 | 37 | |
chrisprice92 | 5:cfceccd5ccb1 | 38 | void motorThread(void const *args); |
chrisprice92 | 5:cfceccd5ccb1 | 39 | bool gMotorLatch = false; |
chrisprice92 | 5:cfceccd5ccb1 | 40 | int gMotorCommand = 0; |
chrisprice92 | 5:cfceccd5ccb1 | 41 | #define CMD_FWD 1 |
chrisprice92 | 5:cfceccd5ccb1 | 42 | #define CMD_BACK 2 |
chrisprice92 | 5:cfceccd5ccb1 | 43 | #define CMD_LEFT 3 |
chrisprice92 | 5:cfceccd5ccb1 | 44 | #define CMD_RIGHT 4 |
chrisprice92 | 5:cfceccd5ccb1 | 45 | |
star297 | 1:71ed1afbf344 | 46 | struct tm t; |
star297 | 0:e2a155f50119 | 47 | |
star297 | 3:f7febfa77784 | 48 | int bufflen, DataRX, count, getcount, replycount, servreq, timeout; |
star297 | 2:d4c6bc0f2dc4 | 49 | int bufl, ipdLen, linkID, weberror, webcounter; |
4180_1 | 4:40dd020463ea | 50 | float temperature, AdcIn, Ht; |
star297 | 0:e2a155f50119 | 51 | float R1=100000, R2=10000; // resistor values to give a 10:1 reduction of measured AnalogIn voltage |
star297 | 0:e2a155f50119 | 52 | char Vcc[10]; |
star297 | 0:e2a155f50119 | 53 | char Temp[10]; |
star297 | 0:e2a155f50119 | 54 | char temp[10]; |
star297 | 2:d4c6bc0f2dc4 | 55 | char webcount[8]; |
star297 | 3:f7febfa77784 | 56 | char lasthit[30]; |
star297 | 0:e2a155f50119 | 57 | char timebuf[30]; |
star297 | 0:e2a155f50119 | 58 | char type[16]; |
star297 | 0:e2a155f50119 | 59 | char type1[16]; |
star297 | 0:e2a155f50119 | 60 | char channel[2]; |
star297 | 3:f7febfa77784 | 61 | char cmdbuff[32]; |
4180_1 | 4:40dd020463ea | 62 | char replybuff[1024]; |
star297 | 0:e2a155f50119 | 63 | char webdata[1024]; // This may need to be bigger depending on WEB browser used |
star297 | 3:f7febfa77784 | 64 | char webbuff[4096]; // Currently using 1986 characters, Increase this if more web page data added |
star297 | 0:e2a155f50119 | 65 | |
star297 | 0:e2a155f50119 | 66 | void SendCMD(),getreply(),ReadWebData(),startserver(),sendpage(),SendWEB(),sendcheck(); |
star297 | 2:d4c6bc0f2dc4 | 67 | void gettime(),gettemp(),getbattery(),setRTC(),beep(); |
star297 | 0:e2a155f50119 | 68 | |
chrisprice92 | 5:cfceccd5ccb1 | 69 | bool simpleResponse = false; |
chrisprice92 | 5:cfceccd5ccb1 | 70 | void sendSimplePage(); |
chrisprice92 | 5:cfceccd5ccb1 | 71 | |
star297 | 1:71ed1afbf344 | 72 | // manual set RTC values |
4180_1 | 4:40dd020463ea | 73 | int minute =00; // 0-59 |
4180_1 | 4:40dd020463ea | 74 | int hour =12; // 2-23 |
4180_1 | 4:40dd020463ea | 75 | int dayofmonth =26; // 1-31 |
4180_1 | 4:40dd020463ea | 76 | int month =8; // 1-12 |
star297 | 1:71ed1afbf344 | 77 | int year =15; // last 2 digits |
star297 | 2:d4c6bc0f2dc4 | 78 | |
4180_1 | 4:40dd020463ea | 79 | int port =80; // set server port |
4180_1 | 4:40dd020463ea | 80 | int SERVtimeout =5; // set server timeout in seconds in case link breaks. |
4180_1 | 4:40dd020463ea | 81 | |
star297 | 0:e2a155f50119 | 82 | // Serial Interrupt read ESP data |
4180_1 | 4:40dd020463ea | 83 | void callback() |
4180_1 | 4:40dd020463ea | 84 | { |
4180_1 | 4:40dd020463ea | 85 | led3=1; |
chrisprice92 | 5:cfceccd5ccb1 | 86 | led4 = 0; |
4180_1 | 4:40dd020463ea | 87 | while (esp.readable()) { |
chrisprice92 | 5:cfceccd5ccb1 | 88 | led4 = !led4; |
4180_1 | 4:40dd020463ea | 89 | webbuff[count] = esp.getc(); |
4180_1 | 4:40dd020463ea | 90 | count++; |
4180_1 | 4:40dd020463ea | 91 | } |
chrisprice92 | 5:cfceccd5ccb1 | 92 | |
chrisprice92 | 5:cfceccd5ccb1 | 93 | led2 = 0; |
4180_1 | 4:40dd020463ea | 94 | if(strlen(webbuff)>bufflen) { |
4180_1 | 4:40dd020463ea | 95 | DataRX=1; |
4180_1 | 4:40dd020463ea | 96 | led3=0; |
4180_1 | 4:40dd020463ea | 97 | } |
star297 | 0:e2a155f50119 | 98 | } |
star297 | 0:e2a155f50119 | 99 | |
4180_1 | 4:40dd020463ea | 100 | int main() |
chrisprice92 | 5:cfceccd5ccb1 | 101 | { |
4180_1 | 4:40dd020463ea | 102 | reset=0; |
star297 | 0:e2a155f50119 | 103 | pc.baud(115200); |
4180_1 | 4:40dd020463ea | 104 | |
4180_1 | 4:40dd020463ea | 105 | pc.printf("\f\n\r------------ ESP8266 Hardware Reset --------------\n\r"); |
4180_1 | 4:40dd020463ea | 106 | wait(0.5); |
4180_1 | 4:40dd020463ea | 107 | reset=1; |
4180_1 | 4:40dd020463ea | 108 | led1=1,led2=0,led3=0; |
4180_1 | 4:40dd020463ea | 109 | timeout=6000; |
4180_1 | 4:40dd020463ea | 110 | getcount=500; |
4180_1 | 4:40dd020463ea | 111 | getreply(); |
star297 | 3:f7febfa77784 | 112 | esp.baud(115200); // ESP8266 baudrate. Maximum on KLxx' is 115200, 230400 works on K20 and K22F |
4180_1 | 4:40dd020463ea | 113 | if (time(NULL) < 1420070400) { |
4180_1 | 4:40dd020463ea | 114 | setRTC(); |
4180_1 | 4:40dd020463ea | 115 | } |
star297 | 2:d4c6bc0f2dc4 | 116 | beep(); |
star297 | 0:e2a155f50119 | 117 | startserver(); |
4180_1 | 4:40dd020463ea | 118 | |
chrisprice92 | 5:cfceccd5ccb1 | 119 | Thread mThreadYo(motorThread); |
chrisprice92 | 5:cfceccd5ccb1 | 120 | |
4180_1 | 4:40dd020463ea | 121 | while(1) { |
4180_1 | 4:40dd020463ea | 122 | if(DataRX==1) { |
star297 | 0:e2a155f50119 | 123 | ReadWebData(); |
star297 | 2:d4c6bc0f2dc4 | 124 | beep(); |
4180_1 | 4:40dd020463ea | 125 | if (servreq == 1 && weberror == 0) { |
chrisprice92 | 5:cfceccd5ccb1 | 126 | if (simpleResponse == true) { |
chrisprice92 | 5:cfceccd5ccb1 | 127 | sendSimplePage(); |
chrisprice92 | 5:cfceccd5ccb1 | 128 | simpleResponse = false; |
chrisprice92 | 5:cfceccd5ccb1 | 129 | } else { |
chrisprice92 | 5:cfceccd5ccb1 | 130 | sendpage(); |
chrisprice92 | 5:cfceccd5ccb1 | 131 | } |
4180_1 | 4:40dd020463ea | 132 | } |
4180_1 | 4:40dd020463ea | 133 | esp.attach(&callback); |
4180_1 | 4:40dd020463ea | 134 | 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); |
4180_1 | 4:40dd020463ea | 135 | pc.printf("\n\n HTTP Packet: \n\n%s\n", webdata); |
star297 | 0:e2a155f50119 | 136 | pc.printf(" Web Characters sent : %d\n\n", bufl); |
star297 | 0:e2a155f50119 | 137 | pc.printf(" -------------------------------------\n\n"); |
star297 | 2:d4c6bc0f2dc4 | 138 | strcpy(lasthit, timebuf); |
4180_1 | 4:40dd020463ea | 139 | servreq=0; |
4180_1 | 4:40dd020463ea | 140 | } |
star297 | 0:e2a155f50119 | 141 | } |
4180_1 | 4:40dd020463ea | 142 | } |
chrisprice92 | 5:cfceccd5ccb1 | 143 | void sendSimplePage() { |
chrisprice92 | 5:cfceccd5ccb1 | 144 | strcpy(webbuff, "<!DOCTYPE html><html><head></head><body>OK</body></html>"); |
chrisprice92 | 5:cfceccd5ccb1 | 145 | |
chrisprice92 | 5:cfceccd5ccb1 | 146 | // end of WEB page data |
chrisprice92 | 5:cfceccd5ccb1 | 147 | bufl = strlen(webbuff); // get total page buffer length |
chrisprice92 | 5:cfceccd5ccb1 | 148 | sprintf(cmdbuff,"AT+CIPSEND=%d,%d\r\n", linkID, bufl); // send IPD link channel and buffer character length. |
chrisprice92 | 5:cfceccd5ccb1 | 149 | timeout=200; |
chrisprice92 | 5:cfceccd5ccb1 | 150 | getcount=7; |
chrisprice92 | 5:cfceccd5ccb1 | 151 | SendCMD(); |
chrisprice92 | 5:cfceccd5ccb1 | 152 | getreply(); |
chrisprice92 | 5:cfceccd5ccb1 | 153 | SendWEB(); // send web page |
chrisprice92 | 5:cfceccd5ccb1 | 154 | memset(webbuff, '\0', sizeof(webbuff)); |
chrisprice92 | 5:cfceccd5ccb1 | 155 | sendcheck(); |
chrisprice92 | 5:cfceccd5ccb1 | 156 | } |
chrisprice92 | 5:cfceccd5ccb1 | 157 | |
4180_1 | 4:40dd020463ea | 158 | // Static WEB page |
star297 | 0:e2a155f50119 | 159 | void sendpage() |
4180_1 | 4:40dd020463ea | 160 | { |
star297 | 3:f7febfa77784 | 161 | gettemp(); |
4180_1 | 4:40dd020463ea | 162 | getbattery(); |
4180_1 | 4:40dd020463ea | 163 | gettime(); |
4180_1 | 4:40dd020463ea | 164 | |
4180_1 | 4:40dd020463ea | 165 | // WEB page data |
star297 | 3:f7febfa77784 | 166 | strcpy(webbuff, "<!DOCTYPE html>"); |
4180_1 | 4:40dd020463ea | 167 | strcat(webbuff, "<html><head><title>ESP8266 Mbed LPC1768</title></head>"); |
4180_1 | 4:40dd020463ea | 168 | strcat(webbuff, "<body>"); |
4180_1 | 4:40dd020463ea | 169 | strcat(webbuff, "<div style=\"text-align:center; background-color:#F4F4F4; color:#00AEDB;\"><h1>ESP8266 Mbed IoT Web Controller</h1>"); |
star297 | 3:f7febfa77784 | 170 | strcat(webbuff, "Hit Count - "); |
star297 | 3:f7febfa77784 | 171 | strcat(webbuff, webcount); |
star297 | 3:f7febfa77784 | 172 | strcat(webbuff, "<br>Last Hit - "); |
4180_1 | 4:40dd020463ea | 173 | strcat(webbuff, lasthit); |
star297 | 3:f7febfa77784 | 174 | strcat(webbuff, "</div><br /><hr>"); |
4180_1 | 4:40dd020463ea | 175 | strcat(webbuff, "<h3>Mbed RTC Time -  "); |
star297 | 3:f7febfa77784 | 176 | strcat(webbuff, timebuf); |
star297 | 3:f7febfa77784 | 177 | strcat(webbuff, "</h3>\r\n"); |
4180_1 | 4:40dd020463ea | 178 | strcat(webbuff, "<p><form method=\"POST\"><strong> Analog 1:  <input type=\"text\" size=6 value=\""); |
star297 | 3:f7febfa77784 | 179 | strcat(webbuff, Temp); |
4180_1 | 4:40dd020463ea | 180 | strcat(webbuff, "\"> </sup>V <form method=\"POST\"> <strong>   Analog 2:  <input type=\"text\" size=4 value=\""); |
star297 | 3:f7febfa77784 | 181 | strcat(webbuff, Vcc); |
4180_1 | 4:40dd020463ea | 182 | strcat(webbuff, "\"> </sup>V"); |
4180_1 | 4:40dd020463ea | 183 | if(led1==0) { |
4180_1 | 4:40dd020463ea | 184 | strcat(webbuff, "<p><input type=\"radio\" name=\"led1\" value=\"0\" checked> LED 1 off"); |
4180_1 | 4:40dd020463ea | 185 | strcat(webbuff, "<br><input type=\"radio\" name=\"led1\" value=\"1\" > LED 1 on"); |
4180_1 | 4:40dd020463ea | 186 | } else { |
4180_1 | 4:40dd020463ea | 187 | strcat(webbuff, "<p><input type=\"radio\" name=\"led1\" value=\"0\" > LED 1 off"); |
4180_1 | 4:40dd020463ea | 188 | strcat(webbuff, "<br><input type=\"radio\" name=\"led1\" value=\"1\" checked> LED 1 on"); |
4180_1 | 4:40dd020463ea | 189 | } |
4180_1 | 4:40dd020463ea | 190 | if(Out1==0) { |
4180_1 | 4:40dd020463ea | 191 | strcat(webbuff, "<p><input type=\"radio\" name=\"Out1\" value=\"0\" checked> Digital Out 1 off"); |
4180_1 | 4:40dd020463ea | 192 | strcat(webbuff, "<br><input type=\"radio\" name=\"Out1\" value=\"1\" > Digital Out 1 on"); |
4180_1 | 4:40dd020463ea | 193 | } else { |
4180_1 | 4:40dd020463ea | 194 | strcat(webbuff, "<p><input type=\"radio\" name=\"Out1\" value=\"0\" > Digital Out 1 off"); |
4180_1 | 4:40dd020463ea | 195 | strcat(webbuff, "<br><input type=\"radio\" name=\"Out1\" value=\"1\" checked> Digital Out 1 on"); |
4180_1 | 4:40dd020463ea | 196 | } |
4180_1 | 4:40dd020463ea | 197 | if(Out2==0) { |
4180_1 | 4:40dd020463ea | 198 | strcat(webbuff, "<p><input type=\"radio\" name=\"Out2\" value=\"0\" checked> Digital Out 2 off"); |
4180_1 | 4:40dd020463ea | 199 | strcat(webbuff, "<br><input type=\"radio\" name=\"Out2\" value=\"1\" > Digital Out 2 on"); |
4180_1 | 4:40dd020463ea | 200 | } else { |
4180_1 | 4:40dd020463ea | 201 | strcat(webbuff, "<p><input type=\"radio\" name=\"Out2\" value=\"0\" > Digital Out 2 off"); |
4180_1 | 4:40dd020463ea | 202 | strcat(webbuff, "<br><input type=\"radio\" name=\"Out2\" value=\"1\" checked> Digital Out 2 on"); |
4180_1 | 4:40dd020463ea | 203 | } |
4180_1 | 4:40dd020463ea | 204 | if(Out3==0) { |
4180_1 | 4:40dd020463ea | 205 | strcat(webbuff, "<p><input type=\"radio\" name=\"Out3\" value=\"0\" checked> Digital Out 3 off"); |
4180_1 | 4:40dd020463ea | 206 | strcat(webbuff, "<br><input type=\"radio\" name=\"Out3\" value=\"1\" > Digital Out 3 on"); |
4180_1 | 4:40dd020463ea | 207 | } else { |
4180_1 | 4:40dd020463ea | 208 | strcat(webbuff, "<p><input type=\"radio\" name=\"Out3\" value=\"0\" > Digital Out 3 off"); |
4180_1 | 4:40dd020463ea | 209 | strcat(webbuff, "<br><input type=\"radio\" name=\"Out3\" value=\"1\" checked> Digital Out 3 on"); |
4180_1 | 4:40dd020463ea | 210 | } |
4180_1 | 4:40dd020463ea | 211 | if(In1==0) { |
4180_1 | 4:40dd020463ea | 212 | strcat(webbuff, "<p><input type=\"radio\" name=\"In1\" value=\"0\" > Digital In 1"); |
4180_1 | 4:40dd020463ea | 213 | } else { |
4180_1 | 4:40dd020463ea | 214 | strcat(webbuff, "<p><input type=\"radio\" name=\"In1\" value=\"1\" checked> Digital In 1"); |
4180_1 | 4:40dd020463ea | 215 | } |
4180_1 | 4:40dd020463ea | 216 | if(In2==0) { |
4180_1 | 4:40dd020463ea | 217 | strcat(webbuff, "<br><input type=\"radio\" name=\"In2\" value=\"0\" > Digital In 2"); |
4180_1 | 4:40dd020463ea | 218 | } else { |
4180_1 | 4:40dd020463ea | 219 | strcat(webbuff, "<br><input type=\"radio\" name=\"In2\" value=\"1\" checked> Digital In 2"); |
4180_1 | 4:40dd020463ea | 220 | } |
4180_1 | 4:40dd020463ea | 221 | if(In3==0) { |
4180_1 | 4:40dd020463ea | 222 | strcat(webbuff, "<br><input type=\"radio\" name=\"In3\" value=\"0\" > Digital In 3"); |
4180_1 | 4:40dd020463ea | 223 | } else { |
4180_1 | 4:40dd020463ea | 224 | strcat(webbuff, "<br><input type=\"radio\" name=\"In3\" value=\"1\" checked> Digital In 3"); |
4180_1 | 4:40dd020463ea | 225 | } |
4180_1 | 4:40dd020463ea | 226 | strcat(webbuff, "</strong><p><input type=\"submit\" value=\"send-refresh\" style=\"background: #3498db;"); |
4180_1 | 4:40dd020463ea | 227 | strcat(webbuff, "background-image:-webkit-linear-gradient(top, #3498db, #2980b9);"); |
star297 | 3:f7febfa77784 | 228 | strcat(webbuff, "background-image:linear-gradient(to bottom, #3498db, #2980b9);"); |
star297 | 3:f7febfa77784 | 229 | strcat(webbuff, "-webkit-border-radius:12;border-radius: 12px;font-family: Arial;color:#ffffff;font-size:20px;padding:"); |
4180_1 | 4:40dd020463ea | 230 | strcat(webbuff, "10px 20px 10px 20px; border:solid #103c57 3px;text-decoration: none;"); |
star297 | 3:f7febfa77784 | 231 | strcat(webbuff, "background: #3cb0fd;"); |
4180_1 | 4:40dd020463ea | 232 | strcat(webbuff, "background-image:-webkit-linear-gradient(top,#3cb0fd,#1a5f8a);"); |
star297 | 3:f7febfa77784 | 233 | strcat(webbuff, "background-image:linear-gradient(to bottom,#3cb0fd,#1a5f8a);"); |
star297 | 3:f7febfa77784 | 234 | strcat(webbuff, "text-decoration:none;\"></form></span>"); |
star297 | 3:f7febfa77784 | 235 | strcat(webbuff, "<p/><h2>How to use:</h2><ul>"); |
star297 | 3:f7febfa77784 | 236 | strcat(webbuff, "<li>Select the Radio buttons to control the digital out pins.</li>"); |
4180_1 | 4:40dd020463ea | 237 | strcat(webbuff, "<li>Click 'Send-Refresh' to send.</li>"); |
4180_1 | 4:40dd020463ea | 238 | strcat(webbuff, "<li>Use the 'Send-Refresh' button to refresh the data.</li>"); |
star297 | 3:f7febfa77784 | 239 | strcat(webbuff, "</ul>"); |
4180_1 | 4:40dd020463ea | 240 | strcat(webbuff, "</body></html>"); |
4180_1 | 4:40dd020463ea | 241 | // end of WEB page data |
4180_1 | 4:40dd020463ea | 242 | bufl = strlen(webbuff); // get total page buffer length |
star297 | 3:f7febfa77784 | 243 | sprintf(cmdbuff,"AT+CIPSEND=%d,%d\r\n", linkID, bufl); // send IPD link channel and buffer character length. |
4180_1 | 4:40dd020463ea | 244 | timeout=200; |
4180_1 | 4:40dd020463ea | 245 | getcount=7; |
star297 | 3:f7febfa77784 | 246 | SendCMD(); |
4180_1 | 4:40dd020463ea | 247 | getreply(); |
star297 | 0:e2a155f50119 | 248 | SendWEB(); // send web page |
star297 | 3:f7febfa77784 | 249 | memset(webbuff, '\0', sizeof(webbuff)); |
4180_1 | 4:40dd020463ea | 250 | sendcheck(); |
star297 | 0:e2a155f50119 | 251 | } |
star297 | 0:e2a155f50119 | 252 | |
star297 | 3:f7febfa77784 | 253 | // wait for ESP "SEND OK" reply, then close IP to load web page |
star297 | 0:e2a155f50119 | 254 | void sendcheck() |
star297 | 0:e2a155f50119 | 255 | { |
4180_1 | 4:40dd020463ea | 256 | weberror=1; |
4180_1 | 4:40dd020463ea | 257 | timeout=500; |
4180_1 | 4:40dd020463ea | 258 | getcount=24; |
4180_1 | 4:40dd020463ea | 259 | t2.reset(); |
4180_1 | 4:40dd020463ea | 260 | t2.start(); |
4180_1 | 4:40dd020463ea | 261 | while(weberror==1 && t2.read() <5) { |
star297 | 0:e2a155f50119 | 262 | getreply(); |
4180_1 | 4:40dd020463ea | 263 | if (strstr(replybuff, "SEND OK") != NULL) { |
4180_1 | 4:40dd020463ea | 264 | weberror=0; // wait for valid SEND OK |
star297 | 0:e2a155f50119 | 265 | } |
4180_1 | 4:40dd020463ea | 266 | } |
4180_1 | 4:40dd020463ea | 267 | if(weberror==1) { // restart connection |
4180_1 | 4:40dd020463ea | 268 | strcpy(cmdbuff, "AT+CIPMUX=1\r\n"); |
4180_1 | 4:40dd020463ea | 269 | timeout=500; |
4180_1 | 4:40dd020463ea | 270 | getcount=10; |
4180_1 | 4:40dd020463ea | 271 | SendCMD(); |
4180_1 | 4:40dd020463ea | 272 | getreply(); |
4180_1 | 4:40dd020463ea | 273 | pc.printf(replybuff); |
star297 | 3:f7febfa77784 | 274 | sprintf(cmdbuff,"AT+CIPSERVER=1,%d\r\n", port); |
4180_1 | 4:40dd020463ea | 275 | timeout=500; |
4180_1 | 4:40dd020463ea | 276 | getcount=10; |
4180_1 | 4:40dd020463ea | 277 | SendCMD(); |
4180_1 | 4:40dd020463ea | 278 | getreply(); |
4180_1 | 4:40dd020463ea | 279 | pc.printf(replybuff); |
4180_1 | 4:40dd020463ea | 280 | } else { |
4180_1 | 4:40dd020463ea | 281 | sprintf(cmdbuff, "AT+CIPCLOSE=%s\r\n",channel); // close current connection |
4180_1 | 4:40dd020463ea | 282 | SendCMD(); |
4180_1 | 4:40dd020463ea | 283 | getreply(); |
4180_1 | 4:40dd020463ea | 284 | pc.printf(replybuff); |
4180_1 | 4:40dd020463ea | 285 | } |
4180_1 | 4:40dd020463ea | 286 | t2.reset(); |
4180_1 | 4:40dd020463ea | 287 | } |
star297 | 0:e2a155f50119 | 288 | |
4180_1 | 4:40dd020463ea | 289 | // Reads and processes GET and POST web data |
star297 | 0:e2a155f50119 | 290 | void ReadWebData() |
4180_1 | 4:40dd020463ea | 291 | { |
4180_1 | 4:40dd020463ea | 292 | wait_ms(200); |
star297 | 3:f7febfa77784 | 293 | esp.attach(NULL); |
4180_1 | 4:40dd020463ea | 294 | count=0; |
4180_1 | 4:40dd020463ea | 295 | DataRX=0; |
4180_1 | 4:40dd020463ea | 296 | weberror=0; |
4180_1 | 4:40dd020463ea | 297 | memset(webdata, '\0', sizeof(webdata)); |
star297 | 3:f7febfa77784 | 298 | int x = strcspn (webbuff,"+"); |
4180_1 | 4:40dd020463ea | 299 | if(x) { |
chrisprice92 | 5:cfceccd5ccb1 | 300 | pc.printf("DATA: %s", webdata); |
4180_1 | 4:40dd020463ea | 301 | strcpy(webdata, webbuff + x); |
4180_1 | 4:40dd020463ea | 302 | weberror=0; |
4180_1 | 4:40dd020463ea | 303 | int numMatched = sscanf(webdata,"+IPD,%d,%d:%s", &linkID, &ipdLen, type); |
chrisprice92 | 5:cfceccd5ccb1 | 304 | if( strstr(webdata, "led1=1") != NULL || strstr(webdata, "keycode=fwd") != NULL) { |
chrisprice92 | 5:cfceccd5ccb1 | 305 | gMotorLatch = true; |
chrisprice92 | 5:cfceccd5ccb1 | 306 | gMotorCommand = CMD_FWD; |
chrisprice92 | 5:cfceccd5ccb1 | 307 | |
chrisprice92 | 5:cfceccd5ccb1 | 308 | simpleResponse = true; |
4180_1 | 4:40dd020463ea | 309 | } |
4180_1 | 4:40dd020463ea | 310 | if( strstr(webdata, "led1=0") != NULL ) { |
4180_1 | 4:40dd020463ea | 311 | led1=0; |
4180_1 | 4:40dd020463ea | 312 | } |
chrisprice92 | 5:cfceccd5ccb1 | 313 | if( strstr(webdata, "Out1=1") != NULL || strstr(webdata, "keycode=back") != NULL) { |
chrisprice92 | 5:cfceccd5ccb1 | 314 | gMotorLatch = true; |
chrisprice92 | 5:cfceccd5ccb1 | 315 | gMotorCommand = CMD_BACK; |
chrisprice92 | 5:cfceccd5ccb1 | 316 | simpleResponse = true; |
4180_1 | 4:40dd020463ea | 317 | } |
4180_1 | 4:40dd020463ea | 318 | if( strstr(webdata, "Out1=0") != NULL ) { |
4180_1 | 4:40dd020463ea | 319 | Out1=0; |
4180_1 | 4:40dd020463ea | 320 | } |
chrisprice92 | 5:cfceccd5ccb1 | 321 | if( strstr(webdata, "Out2=1") != NULL || strstr(webdata, "keycode=left") != NULL) { |
chrisprice92 | 5:cfceccd5ccb1 | 322 | gMotorLatch = true; |
chrisprice92 | 5:cfceccd5ccb1 | 323 | gMotorCommand = CMD_LEFT; |
chrisprice92 | 5:cfceccd5ccb1 | 324 | simpleResponse = true; |
4180_1 | 4:40dd020463ea | 325 | } |
4180_1 | 4:40dd020463ea | 326 | if( strstr(webdata, "Out2=0") != NULL ) { |
4180_1 | 4:40dd020463ea | 327 | Out2=0; |
4180_1 | 4:40dd020463ea | 328 | } |
chrisprice92 | 5:cfceccd5ccb1 | 329 | if( strstr(webdata, "Out3=1") != NULL || strstr(webdata, "keycode=right") != NULL) { |
chrisprice92 | 5:cfceccd5ccb1 | 330 | gMotorLatch = true; |
chrisprice92 | 5:cfceccd5ccb1 | 331 | gMotorCommand = CMD_RIGHT; |
chrisprice92 | 5:cfceccd5ccb1 | 332 | simpleResponse = true; |
4180_1 | 4:40dd020463ea | 333 | } |
4180_1 | 4:40dd020463ea | 334 | if( strstr(webdata, "Out3=0") != NULL ) { |
4180_1 | 4:40dd020463ea | 335 | Out3=0; |
4180_1 | 4:40dd020463ea | 336 | } |
4180_1 | 4:40dd020463ea | 337 | sprintf(channel, "%d",linkID); |
4180_1 | 4:40dd020463ea | 338 | if (strstr(webdata, "GET") != NULL) { |
4180_1 | 4:40dd020463ea | 339 | servreq=1; |
4180_1 | 4:40dd020463ea | 340 | } |
4180_1 | 4:40dd020463ea | 341 | if (strstr(webdata, "POST") != NULL) { |
4180_1 | 4:40dd020463ea | 342 | servreq=1; |
4180_1 | 4:40dd020463ea | 343 | } |
star297 | 2:d4c6bc0f2dc4 | 344 | webcounter++; |
star297 | 2:d4c6bc0f2dc4 | 345 | sprintf(webcount, "%d",webcounter); |
4180_1 | 4:40dd020463ea | 346 | } else { |
4180_1 | 4:40dd020463ea | 347 | memset(webbuff, '\0', sizeof(webbuff)); |
4180_1 | 4:40dd020463ea | 348 | esp.attach(&callback); |
4180_1 | 4:40dd020463ea | 349 | weberror=1; |
4180_1 | 4:40dd020463ea | 350 | } |
star297 | 0:e2a155f50119 | 351 | } |
star297 | 0:e2a155f50119 | 352 | // Starts and restarts webserver if errors detected. |
star297 | 0:e2a155f50119 | 353 | void startserver() |
star297 | 0:e2a155f50119 | 354 | { |
4180_1 | 4:40dd020463ea | 355 | gettemp(); |
4180_1 | 4:40dd020463ea | 356 | gettime(); |
star297 | 1:71ed1afbf344 | 357 | pc.printf("\n\n RTC time %s\r\n\n",timebuf); |
4180_1 | 4:40dd020463ea | 358 | pc.printf("++++++++++ Resetting ESP ++++++++++\r\n"); |
star297 | 3:f7febfa77784 | 359 | strcpy(cmdbuff,"AT+RST\r\n"); |
4180_1 | 4:40dd020463ea | 360 | timeout=8000; |
4180_1 | 4:40dd020463ea | 361 | getcount=1000; |
star297 | 0:e2a155f50119 | 362 | SendCMD(); |
star297 | 1:71ed1afbf344 | 363 | getreply(); |
star297 | 3:f7febfa77784 | 364 | pc.printf(replybuff); |
star297 | 3:f7febfa77784 | 365 | pc.printf("%d",count); |
star297 | 3:f7febfa77784 | 366 | if (strstr(replybuff, "OK") != NULL) { |
star297 | 1:71ed1afbf344 | 367 | pc.printf("\n++++++++++ Starting Server ++++++++++\r\n"); |
4180_1 | 4:40dd020463ea | 368 | strcpy(cmdbuff, "AT+CIPMUX=1\r\n"); // set multiple connections. |
4180_1 | 4:40dd020463ea | 369 | timeout=500; |
4180_1 | 4:40dd020463ea | 370 | getcount=20; |
star297 | 1:71ed1afbf344 | 371 | SendCMD(); |
star297 | 2:d4c6bc0f2dc4 | 372 | getreply(); |
4180_1 | 4:40dd020463ea | 373 | pc.printf(replybuff); |
star297 | 3:f7febfa77784 | 374 | sprintf(cmdbuff,"AT+CIPSERVER=1,%d\r\n", port); |
4180_1 | 4:40dd020463ea | 375 | timeout=500; |
4180_1 | 4:40dd020463ea | 376 | getcount=20; |
star297 | 1:71ed1afbf344 | 377 | SendCMD(); |
star297 | 3:f7febfa77784 | 378 | getreply(); |
4180_1 | 4:40dd020463ea | 379 | pc.printf(replybuff); |
4180_1 | 4:40dd020463ea | 380 | wait(1); |
star297 | 3:f7febfa77784 | 381 | sprintf(cmdbuff,"AT+CIPSTO=%d\r\n",SERVtimeout); |
4180_1 | 4:40dd020463ea | 382 | timeout=500; |
4180_1 | 4:40dd020463ea | 383 | getcount=50; |
star297 | 3:f7febfa77784 | 384 | SendCMD(); |
star297 | 2:d4c6bc0f2dc4 | 385 | getreply(); |
4180_1 | 4:40dd020463ea | 386 | pc.printf(replybuff); |
4180_1 | 4:40dd020463ea | 387 | wait(5); |
4180_1 | 4:40dd020463ea | 388 | pc.printf("\n Getting Server IP \r\n"); |
star297 | 3:f7febfa77784 | 389 | strcpy(cmdbuff, "AT+CIFSR\r\n"); |
4180_1 | 4:40dd020463ea | 390 | timeout=2500; |
4180_1 | 4:40dd020463ea | 391 | getcount=200; |
4180_1 | 4:40dd020463ea | 392 | while(weberror==0) { |
4180_1 | 4:40dd020463ea | 393 | SendCMD(); |
4180_1 | 4:40dd020463ea | 394 | getreply(); |
4180_1 | 4:40dd020463ea | 395 | if (strstr(replybuff, "0.0.0.0") == NULL) { |
4180_1 | 4:40dd020463ea | 396 | weberror=1; // wait for valid IP |
star297 | 1:71ed1afbf344 | 397 | } |
4180_1 | 4:40dd020463ea | 398 | } |
4180_1 | 4:40dd020463ea | 399 | pc.printf("\n Enter WEB address (IP) found below in your browser \r\n\n"); |
4180_1 | 4:40dd020463ea | 400 | pc.printf("\n The MAC address is also shown below,if it is needed \r\n\n"); |
4180_1 | 4:40dd020463ea | 401 | replybuff[strlen(replybuff)-1] = '\0'; |
4180_1 | 4:40dd020463ea | 402 | //char* IP = replybuff + 5; |
4180_1 | 4:40dd020463ea | 403 | sprintf(webdata,"%s", replybuff); |
4180_1 | 4:40dd020463ea | 404 | pc.printf(webdata); |
4180_1 | 4:40dd020463ea | 405 | led2=1; |
4180_1 | 4:40dd020463ea | 406 | bufflen=200; |
4180_1 | 4:40dd020463ea | 407 | count=0; |
star297 | 1:71ed1afbf344 | 408 | pc.printf("\n\n++++++++++ Ready ++++++++++\r\n\n"); |
star297 | 1:71ed1afbf344 | 409 | esp.attach(&callback); |
4180_1 | 4:40dd020463ea | 410 | } else { |
4180_1 | 4:40dd020463ea | 411 | pc.printf("\n++++++++++ ESP8266 error, check power/connections ++++++++++\r\n"); |
4180_1 | 4:40dd020463ea | 412 | while(1) {} |
4180_1 | 4:40dd020463ea | 413 | } |
4180_1 | 4:40dd020463ea | 414 | t2.reset(); |
4180_1 | 4:40dd020463ea | 415 | t2.start(); |
4180_1 | 4:40dd020463ea | 416 | beep(); |
4180_1 | 4:40dd020463ea | 417 | } |
star297 | 0:e2a155f50119 | 418 | // ESP Command data send |
star297 | 0:e2a155f50119 | 419 | void SendCMD() |
star297 | 0:e2a155f50119 | 420 | { |
4180_1 | 4:40dd020463ea | 421 | esp.printf("%s", cmdbuff); |
4180_1 | 4:40dd020463ea | 422 | } |
star297 | 0:e2a155f50119 | 423 | // Large WEB buffer data send |
star297 | 0:e2a155f50119 | 424 | void SendWEB() |
4180_1 | 4:40dd020463ea | 425 | { |
star297 | 0:e2a155f50119 | 426 | int i=0; |
star297 | 0:e2a155f50119 | 427 | if(esp.writeable()) { |
4180_1 | 4:40dd020463ea | 428 | while(webbuff[i]!='\0') { |
4180_1 | 4:40dd020463ea | 429 | esp.putc(webbuff[i]); |
4180_1 | 4:40dd020463ea | 430 | i++; |
4180_1 | 4:40dd020463ea | 431 | } |
4180_1 | 4:40dd020463ea | 432 | } |
4180_1 | 4:40dd020463ea | 433 | } |
4180_1 | 4:40dd020463ea | 434 | // Get Command and ESP status replies |
star297 | 0:e2a155f50119 | 435 | void getreply() |
4180_1 | 4:40dd020463ea | 436 | { |
star297 | 3:f7febfa77784 | 437 | memset(replybuff, '\0', sizeof(replybuff)); |
4180_1 | 4:40dd020463ea | 438 | t1.reset(); |
4180_1 | 4:40dd020463ea | 439 | t1.start(); |
4180_1 | 4:40dd020463ea | 440 | replycount=0; |
star297 | 3:f7febfa77784 | 441 | while(t1.read_ms()< timeout && replycount < getcount) { |
star297 | 0:e2a155f50119 | 442 | if(esp.readable()) { |
4180_1 | 4:40dd020463ea | 443 | replybuff[replycount] = esp.getc(); |
4180_1 | 4:40dd020463ea | 444 | replycount++; |
star297 | 2:d4c6bc0f2dc4 | 445 | } |
4180_1 | 4:40dd020463ea | 446 | } |
4180_1 | 4:40dd020463ea | 447 | t1.stop(); |
star297 | 0:e2a155f50119 | 448 | } |
star297 | 0:e2a155f50119 | 449 | // Analog in example |
star297 | 0:e2a155f50119 | 450 | void getbattery() |
star297 | 0:e2a155f50119 | 451 | { |
4180_1 | 4:40dd020463ea | 452 | AdcIn=Ain1.read(); |
4180_1 | 4:40dd020463ea | 453 | Ht = (AdcIn*3.3); // set the numeric to the exact MCU analog reference voltage for greater accuracy |
4180_1 | 4:40dd020463ea | 454 | sprintf(Vcc,"%2.3f",Ht); |
star297 | 0:e2a155f50119 | 455 | } |
star297 | 0:e2a155f50119 | 456 | // Temperature example |
star297 | 0:e2a155f50119 | 457 | void gettemp() |
4180_1 | 4:40dd020463ea | 458 | { |
4180_1 | 4:40dd020463ea | 459 | |
4180_1 | 4:40dd020463ea | 460 | AdcIn=Ain2.read(); |
4180_1 | 4:40dd020463ea | 461 | Ht = (AdcIn*3.3); // set the numeric to the exact MCU analog reference voltage for greater accuracy |
4180_1 | 4:40dd020463ea | 462 | sprintf(Temp,"%2.3f",Ht); |
star297 | 0:e2a155f50119 | 463 | } |
4180_1 | 4:40dd020463ea | 464 | // Get RTC time |
star297 | 0:e2a155f50119 | 465 | void gettime() |
star297 | 0:e2a155f50119 | 466 | { |
star297 | 0:e2a155f50119 | 467 | time_t seconds = time(NULL); |
4180_1 | 4:40dd020463ea | 468 | strftime(timebuf,50,"%H:%M:%S %a %d %b %y", localtime(&seconds)); |
star297 | 0:e2a155f50119 | 469 | } |
star297 | 0:e2a155f50119 | 470 | |
star297 | 2:d4c6bc0f2dc4 | 471 | void beep() |
4180_1 | 4:40dd020463ea | 472 | { |
star297 | 2:d4c6bc0f2dc4 | 473 | speaker.period(1.0/2000); // 2000hz period |
star297 | 2:d4c6bc0f2dc4 | 474 | speaker = 0.5; //50% duty cycle - max volume |
star297 | 2:d4c6bc0f2dc4 | 475 | wait_ms(60); |
star297 | 2:d4c6bc0f2dc4 | 476 | speaker=0.0; // turn off audio |
star297 | 2:d4c6bc0f2dc4 | 477 | } |
star297 | 2:d4c6bc0f2dc4 | 478 | |
star297 | 1:71ed1afbf344 | 479 | void setRTC() |
star297 | 1:71ed1afbf344 | 480 | { |
4180_1 | 4:40dd020463ea | 481 | t.tm_sec = (0); // 0-59 |
4180_1 | 4:40dd020463ea | 482 | t.tm_min = (minute); // 0-59 |
4180_1 | 4:40dd020463ea | 483 | t.tm_hour = (hour); // 0-23 |
4180_1 | 4:40dd020463ea | 484 | t.tm_mday = (dayofmonth); // 1-31 |
4180_1 | 4:40dd020463ea | 485 | t.tm_mon = (month-1); // 0-11 "0" = Jan, -1 added for Mbed RCT clock format |
4180_1 | 4:40dd020463ea | 486 | t.tm_year = ((year)+100); // year since 1900, current DCF year + 100 + 1900 = correct year |
4180_1 | 4:40dd020463ea | 487 | set_time(mktime(&t)); // set RTC clock |
4180_1 | 4:40dd020463ea | 488 | } |
chrisprice92 | 5:cfceccd5ccb1 | 489 | |
chrisprice92 | 5:cfceccd5ccb1 | 490 | // Control motor related things |
chrisprice92 | 5:cfceccd5ccb1 | 491 | void motorThread(void const *args) { |
chrisprice92 | 5:cfceccd5ccb1 | 492 | pc.printf("motorThread started\n\r"); |
chrisprice92 | 5:cfceccd5ccb1 | 493 | |
chrisprice92 | 5:cfceccd5ccb1 | 494 | while (1) { |
chrisprice92 | 5:cfceccd5ccb1 | 495 | while (gMotorLatch == false) { |
chrisprice92 | 5:cfceccd5ccb1 | 496 | Thread::wait(100); |
chrisprice92 | 5:cfceccd5ccb1 | 497 | } |
chrisprice92 | 5:cfceccd5ccb1 | 498 | |
chrisprice92 | 5:cfceccd5ccb1 | 499 | pc.printf("motorThread Latched!\n\r"); |
chrisprice92 | 5:cfceccd5ccb1 | 500 | |
chrisprice92 | 5:cfceccd5ccb1 | 501 | gMotorLatch = false; |
chrisprice92 | 5:cfceccd5ccb1 | 502 | |
chrisprice92 | 5:cfceccd5ccb1 | 503 | float lSpeed = 0.0f; |
chrisprice92 | 5:cfceccd5ccb1 | 504 | float rSpeed = 0.0f; |
chrisprice92 | 5:cfceccd5ccb1 | 505 | float delayMs = 1000; |
chrisprice92 | 5:cfceccd5ccb1 | 506 | switch(gMotorCommand) { |
chrisprice92 | 5:cfceccd5ccb1 | 507 | case CMD_FWD: |
chrisprice92 | 5:cfceccd5ccb1 | 508 | lSpeed = 0.5f; |
chrisprice92 | 5:cfceccd5ccb1 | 509 | rSpeed = 0.5f; |
chrisprice92 | 5:cfceccd5ccb1 | 510 | break; |
chrisprice92 | 5:cfceccd5ccb1 | 511 | |
chrisprice92 | 5:cfceccd5ccb1 | 512 | case CMD_BACK: |
chrisprice92 | 5:cfceccd5ccb1 | 513 | lSpeed = -0.5f; |
chrisprice92 | 5:cfceccd5ccb1 | 514 | rSpeed = -0.5f; |
chrisprice92 | 5:cfceccd5ccb1 | 515 | break; |
chrisprice92 | 5:cfceccd5ccb1 | 516 | |
chrisprice92 | 5:cfceccd5ccb1 | 517 | case CMD_LEFT: |
chrisprice92 | 5:cfceccd5ccb1 | 518 | lSpeed = -0.5f; |
chrisprice92 | 5:cfceccd5ccb1 | 519 | rSpeed = 0.5f; |
chrisprice92 | 5:cfceccd5ccb1 | 520 | delayMs = 500; |
chrisprice92 | 5:cfceccd5ccb1 | 521 | break; |
chrisprice92 | 5:cfceccd5ccb1 | 522 | |
chrisprice92 | 5:cfceccd5ccb1 | 523 | case CMD_RIGHT: |
chrisprice92 | 5:cfceccd5ccb1 | 524 | lSpeed = 0.5f; |
chrisprice92 | 5:cfceccd5ccb1 | 525 | rSpeed = -0.5f; |
chrisprice92 | 5:cfceccd5ccb1 | 526 | delayMs = 500; |
chrisprice92 | 5:cfceccd5ccb1 | 527 | break; |
chrisprice92 | 5:cfceccd5ccb1 | 528 | |
chrisprice92 | 5:cfceccd5ccb1 | 529 | default: |
chrisprice92 | 5:cfceccd5ccb1 | 530 | delayMs = 0; |
chrisprice92 | 5:cfceccd5ccb1 | 531 | break; |
chrisprice92 | 5:cfceccd5ccb1 | 532 | } |
chrisprice92 | 5:cfceccd5ccb1 | 533 | |
chrisprice92 | 5:cfceccd5ccb1 | 534 | left.speed(lSpeed); |
chrisprice92 | 5:cfceccd5ccb1 | 535 | right.speed(rSpeed); |
chrisprice92 | 5:cfceccd5ccb1 | 536 | Thread::wait(delayMs); |
chrisprice92 | 5:cfceccd5ccb1 | 537 | left.speed(0); |
chrisprice92 | 5:cfceccd5ccb1 | 538 | right.speed(0); |
chrisprice92 | 5:cfceccd5ccb1 | 539 | } |
chrisprice92 | 5:cfceccd5ccb1 | 540 | } |