TES WEBSERVER ESP8266 STM32F401-RE

Dependencies:   mbed

Fork of ESP8266-WEB-Mbed-Controller by Paul Staron

Committer:
star297
Date:
Sat Feb 14 23:53:25 2015 +0000
Revision:
0:e2a155f50119
Child:
1:71ed1afbf344
ESP8266 Wi-Fi Mbed controller v1.0

Who changed what in which revision?

UserRevisionLine numberNew 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"
star297 0:e2a155f50119 4 #include "DS18B20.h"
star297 0:e2a155f50119 5
star297 0:e2a155f50119 6 Serial pc(USBTX, USBRX);
star297 0:e2a155f50119 7 Serial esp(PTE0, PTE1); // tx, rx
star297 0:e2a155f50119 8
star297 0:e2a155f50119 9 // Standard Mbed LED definitions
star297 0:e2a155f50119 10 DigitalOut led1(LED_RED); // (PTB18)
star297 0:e2a155f50119 11 DigitalOut led2(LED_GREEN); // (PTB19)
star297 0:e2a155f50119 12 DigitalOut led3(LED_BLUE); // (PTD1)
star297 0:e2a155f50119 13
star297 0:e2a155f50119 14 // Digital Out and In pins, can be configured to any suitable pin depending on Platform
star297 0:e2a155f50119 15 DigitalOut Out1(PTC7);
star297 0:e2a155f50119 16 DigitalOut Out2(PTC0);
star297 0:e2a155f50119 17 DigitalOut Out3(PTC3);
star297 0:e2a155f50119 18
star297 0:e2a155f50119 19 DigitalIn In1(PTC12);
star297 0:e2a155f50119 20 DigitalIn In2(PTC13);
star297 0:e2a155f50119 21 DigitalIn In3(PTC16);
star297 0:e2a155f50119 22
star297 0:e2a155f50119 23 AnalogIn BATin(PTC1);
star297 0:e2a155f50119 24
star297 0:e2a155f50119 25 DS18B20 thermom(PTB0, DS18B20::RES_12_BIT);
star297 0:e2a155f50119 26
star297 0:e2a155f50119 27 Timer t;
star297 0:e2a155f50119 28 Timer t1;
star297 0:e2a155f50119 29
star297 0:e2a155f50119 30 int buflen, DataRX, count, ended, servreq, timeout;
star297 0:e2a155f50119 31 int bufl, ipdLen, linkID, weberror;
star297 0:e2a155f50119 32 float temperature, AdcIn, Ht;
star297 0:e2a155f50119 33 float R1=100000, R2=10000; // resistor values to give a 10:1 reduction of measured AnalogIn voltage
star297 0:e2a155f50119 34 char Vcc[10];
star297 0:e2a155f50119 35 char Temp[10];
star297 0:e2a155f50119 36 char temp[10];
star297 0:e2a155f50119 37 char timebuf[30];
star297 0:e2a155f50119 38 char type[16];
star297 0:e2a155f50119 39 char type1[16];
star297 0:e2a155f50119 40 char channel[2];
star297 0:e2a155f50119 41 char snd[256];
star297 0:e2a155f50119 42 char webdata[1024]; // This may need to be bigger depending on WEB browser used
star297 0:e2a155f50119 43 char buf[2048]; // Currently using 1407 characters, Increase this if more web page data added
star297 0:e2a155f50119 44
star297 0:e2a155f50119 45 void SendCMD(),getreply(),ReadWebData(),startserver(),sendpage(),SendWEB(),sendcheck();
star297 0:e2a155f50119 46 void gettime(),gettemp(),getbattery();
star297 0:e2a155f50119 47
star297 0:e2a155f50119 48 // Serial Interrupt read ESP data
star297 0:e2a155f50119 49 void callback() {
star297 0:e2a155f50119 50 while (esp.readable()) {buf[count] = esp.getc();count++;}
star297 0:e2a155f50119 51 if(strlen(buf)>buflen){DataRX=1;}
star297 0:e2a155f50119 52 }
star297 0:e2a155f50119 53
star297 0:e2a155f50119 54 int main() {
star297 0:e2a155f50119 55 led1=1,led2=1,led3=1;
star297 0:e2a155f50119 56 pc.baud(115200);
star297 0:e2a155f50119 57 esp.baud(115200); // ESP8266 baudrate.
star297 0:e2a155f50119 58 if (time(NULL) < 1420070400) {set_time(1420070400);}
star297 0:e2a155f50119 59
star297 0:e2a155f50119 60 startserver();
star297 0:e2a155f50119 61
star297 0:e2a155f50119 62 while(1){
star297 0:e2a155f50119 63
star297 0:e2a155f50119 64 if(DataRX==1){
star297 0:e2a155f50119 65 ReadWebData();
star297 0:e2a155f50119 66 if (servreq == 1 && weberror == 0){sendpage();}
star297 0:e2a155f50119 67 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);
star297 0:e2a155f50119 68 pc.printf("\n\n HTTP Packet: \n\n%s\n", webdata);
star297 0:e2a155f50119 69 pc.printf(" Web Characters sent : %d\n\n", bufl);
star297 0:e2a155f50119 70 pc.printf(" -------------------------------------\n\n");
star297 0:e2a155f50119 71 esp.attach(&callback);
star297 0:e2a155f50119 72 servreq=0;
star297 0:e2a155f50119 73 }
star297 0:e2a155f50119 74 }
star297 0:e2a155f50119 75 }
star297 0:e2a155f50119 76 // Static WEB page
star297 0:e2a155f50119 77 void sendpage()
star297 0:e2a155f50119 78 {
star297 0:e2a155f50119 79 gettemp();getbattery();gettime();
star297 0:e2a155f50119 80 // WEB page data
star297 0:e2a155f50119 81 memset(buf, '\0', sizeof(buf));
star297 0:e2a155f50119 82 strcpy(buf, "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\nPragma: no-cache\r\n\r\n");
star297 0:e2a155f50119 83 strcat(buf, "<html><head><title>ESP8266 Mbed</title></head>\r\n");
star297 0:e2a155f50119 84 strcat(buf, "<body>\r\n");
star297 0:e2a155f50119 85 strcat(buf, "<h1>ESP8266 Mbed Web Controller</h1>\r\n");
star297 0:e2a155f50119 86 strcat(buf, "<br /><hr>\r\n");
star297 0:e2a155f50119 87 strcat(buf, "<h3>Mbed RTC Time - ");
star297 0:e2a155f50119 88 strcat(buf, timebuf);
star297 0:e2a155f50119 89 strcat(buf, "</h3>\r\n");
star297 0:e2a155f50119 90 strcat(buf, "<p><form method=\"POST\"><strong> Temperature: <input type=\"text\" size=4 value=\"");
star297 0:e2a155f50119 91 strcat(buf, Temp);
star297 0:e2a155f50119 92 strcat(buf, "\"> <sup>O</sup>C <form method=\"POST\"> <strong> Battery: <input type=\"text\" size=4 value=\"");
star297 0:e2a155f50119 93 strcat(buf, Vcc);
star297 0:e2a155f50119 94 strcat(buf, "\"> </sup>V");
star297 0:e2a155f50119 95 if(led1==1){strcat(buf, "<p><input type=\"radio\" name=\"led1\" value=\"0\" checked> Red LED off");
star297 0:e2a155f50119 96 strcat(buf, "<br><input type=\"radio\" name=\"led1\" value=\"1\" > Red LED on");}
star297 0:e2a155f50119 97 else{strcat(buf, "<p><input type=\"radio\" name=\"led1\" value=\"0\" > Red LED off");
star297 0:e2a155f50119 98 strcat(buf, "<br><input type=\"radio\" name=\"led1\" value=\"1\" checked> Red LED on");}
star297 0:e2a155f50119 99 if(Out1==0){strcat(buf, "<p><input type=\"radio\" name=\"Out1\" value=\"0\" checked> Digital Out 1 off");
star297 0:e2a155f50119 100 strcat(buf, "<br><input type=\"radio\" name=\"Out1\" value=\"1\" > Digital Out 1 on");}
star297 0:e2a155f50119 101 else{strcat(buf, "<p><input type=\"radio\" name=\"Out1\" value=\"0\" > Digital Out 1 off");
star297 0:e2a155f50119 102 strcat(buf, "<br><input type=\"radio\" name=\"Out1\" value=\"1\" checked> Digital Out 1 on");}
star297 0:e2a155f50119 103 if(Out2==0){strcat(buf, "<p><input type=\"radio\" name=\"Out2\" value=\"0\" checked> Digital Out 2 off");
star297 0:e2a155f50119 104 strcat(buf, "<br><input type=\"radio\" name=\"Out2\" value=\"1\" > Digital Out 2 on");}
star297 0:e2a155f50119 105 else{strcat(buf, "<p><input type=\"radio\" name=\"Out2\" value=\"0\" > Digital Out 2 off");
star297 0:e2a155f50119 106 strcat(buf, "<br><input type=\"radio\" name=\"Out2\" value=\"1\" checked> Digital Out 2 on");}
star297 0:e2a155f50119 107 if(Out3==0){strcat(buf, "<p><input type=\"radio\" name=\"Out3\" value=\"0\" checked> Digital Out 3 off");
star297 0:e2a155f50119 108 strcat(buf, "<br><input type=\"radio\" name=\"Out3\" value=\"1\" > Digital Out 3 on");}
star297 0:e2a155f50119 109 else{strcat(buf, "<p><input type=\"radio\" name=\"Out3\" value=\"0\" > Digital Out 3 off");
star297 0:e2a155f50119 110 strcat(buf, "<br><input type=\"radio\" name=\"Out3\" value=\"1\" checked> Digital Out 3 on");}
star297 0:e2a155f50119 111 if(In1==0){strcat(buf, "<p><input type=\"radio\" name=\"In1\" value=\"0\" > Digital In 1");}
star297 0:e2a155f50119 112 else{strcat(buf, "<p><input type=\"radio\" name=\"In1\" value=\"1\" checked> Digital In 1");}
star297 0:e2a155f50119 113 if(In2==0){strcat(buf, "<br><input type=\"radio\" name=\"In2\" value=\"0\" > Digital In 2");}
star297 0:e2a155f50119 114 else{strcat(buf, "<br><input type=\"radio\" name=\"In2\" value=\"1\" checked> Digital In 2");}
star297 0:e2a155f50119 115 if(In3==0){strcat(buf, "<br><input type=\"radio\" name=\"In3\" value=\"0\" > Digital In 3");}
star297 0:e2a155f50119 116 else{strcat(buf, "<br><input type=\"radio\" name=\"In3\" value=\"1\" checked> Digital In 3");}
star297 0:e2a155f50119 117 strcat(buf, "</strong><p><input type=\"submit\"></form></span>");
star297 0:e2a155f50119 118 strcat(buf, "</body>\r\n</html>\r\n");
star297 0:e2a155f50119 119 strcat(buf, "<p/><h2>How to use:</h2><ul>");
star297 0:e2a155f50119 120 strcat(buf, "<li>Select the Radio buttons to control the digital out pins.</li>");
star297 0:e2a155f50119 121 strcat(buf, "<li>Click 'Submit Query' to send.</li>");
star297 0:e2a155f50119 122 strcat(buf, "<li>Use the 'Submit Query' button to refresh the page.</li>");
star297 0:e2a155f50119 123 strcat(buf, "</ul>");
star297 0:e2a155f50119 124 // end of WEB page data
star297 0:e2a155f50119 125 bufl = strlen(buf); // get total page buffer length
star297 0:e2a155f50119 126 sprintf(snd,"AT+CIPSEND=%s,%d\r\n", channel, bufl); // send IPD link channel and buffer character length.
star297 0:e2a155f50119 127 SendCMD();
star297 0:e2a155f50119 128 wait_ms(100);
star297 0:e2a155f50119 129 SendWEB(); // send web page
star297 0:e2a155f50119 130 sendcheck();
star297 0:e2a155f50119 131 }
star297 0:e2a155f50119 132
star297 0:e2a155f50119 133 // wait for ESP "SEND OK" reply, then close IP to load web page, if lock out, system fully restarts
star297 0:e2a155f50119 134 void sendcheck()
star297 0:e2a155f50119 135 {
star297 0:e2a155f50119 136 timeout=200;
star297 0:e2a155f50119 137 weberror=1;t1.reset();t1.start();
star297 0:e2a155f50119 138 while(weberror==1 && t1.read() <10){
star297 0:e2a155f50119 139 getreply();
star297 0:e2a155f50119 140 if (strstr(buf, "SEND OK") != NULL) {weberror=0;} // wait for valid SEND OK
star297 0:e2a155f50119 141 }
star297 0:e2a155f50119 142 t1.stop();
star297 0:e2a155f50119 143 if(weberror==1){
star297 0:e2a155f50119 144 pc.printf("\n\n\n ++++++++++ WEB Connect Error, restarting ... ++++++++++\n\n");
star297 0:e2a155f50119 145 NVIC_SystemReset();}
star297 0:e2a155f50119 146 strcpy(snd, "AT+CIPCLOSE=0\r\n");
star297 0:e2a155f50119 147 SendCMD();
star297 0:e2a155f50119 148 }
star297 0:e2a155f50119 149
star297 0:e2a155f50119 150 // Reads and processes GET and POST web data
star297 0:e2a155f50119 151 void ReadWebData()
star297 0:e2a155f50119 152 {
star297 0:e2a155f50119 153 wait_ms(200);
star297 0:e2a155f50119 154 led3=0;
star297 0:e2a155f50119 155 count=0;DataRX=0;weberror=0;
star297 0:e2a155f50119 156 esp.attach(NULL);
star297 0:e2a155f50119 157 int x = strcspn (buf,"+");
star297 0:e2a155f50119 158 if(x){
star297 0:e2a155f50119 159 strcpy(webdata, buf + x);weberror=0;
star297 0:e2a155f50119 160 int numMatched = sscanf(webdata,"+IPD,%d,%d:%s", &linkID, &ipdLen, type);
star297 0:e2a155f50119 161 if( strstr(webdata, "led1=1") != NULL ) {led1=0;}
star297 0:e2a155f50119 162 if( strstr(webdata, "led1=0") != NULL ) {led1=1;}
star297 0:e2a155f50119 163 if( strstr(webdata, "Out1=1") != NULL ) {Out1=1;}
star297 0:e2a155f50119 164 if( strstr(webdata, "Out1=0") != NULL ) {Out1=0;}
star297 0:e2a155f50119 165 if( strstr(webdata, "Out2=1") != NULL ) {Out2=1;}
star297 0:e2a155f50119 166 if( strstr(webdata, "Out2=0") != NULL ) {Out2=0;}
star297 0:e2a155f50119 167 if( strstr(webdata, "Out3=1") != NULL ) {Out3=1;}
star297 0:e2a155f50119 168 if( strstr(webdata, "Out3=0") != NULL ) {Out3=0;}
star297 0:e2a155f50119 169 sprintf(channel, "%d",linkID);
star297 0:e2a155f50119 170 if (strstr(webdata, "GET") != NULL) {servreq=1;}
star297 0:e2a155f50119 171 if (strstr(webdata, "POST") != NULL) {servreq=1;}
star297 0:e2a155f50119 172 }
star297 0:e2a155f50119 173 else {strcpy(webdata, buf);weberror=1;}
star297 0:e2a155f50119 174 led3=1;
star297 0:e2a155f50119 175 }
star297 0:e2a155f50119 176 // Starts and restarts webserver if errors detected.
star297 0:e2a155f50119 177 void startserver()
star297 0:e2a155f50119 178 {
star297 0:e2a155f50119 179 esp.attach(NULL);
star297 0:e2a155f50119 180 pc.printf("++++++++++ Resetting ESP ++++++++++\r\n");
star297 0:e2a155f50119 181 wait(1);
star297 0:e2a155f50119 182 strcpy(snd,"AT+RST\r\n");
star297 0:e2a155f50119 183 SendCMD();
star297 0:e2a155f50119 184 pc.printf("\n++++++++++ Starting Server ++++++++++\r\n");
star297 0:e2a155f50119 185 wait(3);
star297 0:e2a155f50119 186 strcpy(snd, "AT+CIPMUX=1\r\n"); // set multiple connections.
star297 0:e2a155f50119 187 SendCMD();
star297 0:e2a155f50119 188 wait_ms(50);
star297 0:e2a155f50119 189 strcpy(snd, "AT+CIPSERVER=1,80\r\n"); // set to port 80, you can change this to what you want.
star297 0:e2a155f50119 190 SendCMD();
star297 0:e2a155f50119 191 wait_ms(200);
star297 0:e2a155f50119 192 pc.printf("\n Getting Server IP \r\n");
star297 0:e2a155f50119 193 strcpy(snd, "AT+CIFSR\r\n");
star297 0:e2a155f50119 194 timeout=500;
star297 0:e2a155f50119 195 while(weberror==0){
star297 0:e2a155f50119 196 SendCMD();getreply();
star297 0:e2a155f50119 197 if (strstr(buf, "0.0.0.0") == NULL) {weberror=1;} // wait for valid IP
star297 0:e2a155f50119 198 }
star297 0:e2a155f50119 199 pc.printf("\n Enter WEB address in your browser \r\n\n");
star297 0:e2a155f50119 200 buf[strlen(buf) - 8] = '\0';
star297 0:e2a155f50119 201 char* IP = buf + 5;
star297 0:e2a155f50119 202 sprintf(snd," http://%s:80", IP);
star297 0:e2a155f50119 203 pc.printf(snd);
star297 0:e2a155f50119 204 while (esp.readable()){esp.getc();}
star297 0:e2a155f50119 205 buflen=200;count=0;
star297 0:e2a155f50119 206 memset(buf, '\0', sizeof(buf));
star297 0:e2a155f50119 207 led2=0;wait(2);led2=1;
star297 0:e2a155f50119 208 pc.printf("\n\n++++++++++ Ready ++++++++++\r\n\n");
star297 0:e2a155f50119 209 esp.attach(&callback);
star297 0:e2a155f50119 210 }
star297 0:e2a155f50119 211 // ESP Command data send
star297 0:e2a155f50119 212 void SendCMD()
star297 0:e2a155f50119 213 {
star297 0:e2a155f50119 214 esp.printf("%s", snd);
star297 0:e2a155f50119 215 }
star297 0:e2a155f50119 216 // Large WEB buffer data send
star297 0:e2a155f50119 217 void SendWEB()
star297 0:e2a155f50119 218 {
star297 0:e2a155f50119 219 int i=0;
star297 0:e2a155f50119 220 if(esp.writeable()) {
star297 0:e2a155f50119 221 while(buf[i]!='\0') {esp.putc(buf[i]);i++;}
star297 0:e2a155f50119 222 }
star297 0:e2a155f50119 223 }
star297 0:e2a155f50119 224 // Get Cammand and ESP status replies
star297 0:e2a155f50119 225 void getreply()
star297 0:e2a155f50119 226 {
star297 0:e2a155f50119 227 memset(buf, '\0', sizeof(buf));
star297 0:e2a155f50119 228 t.start();
star297 0:e2a155f50119 229 ended=0;count=0;
star297 0:e2a155f50119 230 while(!ended) {
star297 0:e2a155f50119 231 if(esp.readable()) {
star297 0:e2a155f50119 232 buf[count] = esp.getc();count++;
star297 0:e2a155f50119 233 }
star297 0:e2a155f50119 234 if(t.read_ms() > timeout) {
star297 0:e2a155f50119 235 ended = 1;t.stop();t.reset();
star297 0:e2a155f50119 236 }
star297 0:e2a155f50119 237 }
star297 0:e2a155f50119 238 }
star297 0:e2a155f50119 239 // Analog in example
star297 0:e2a155f50119 240 void getbattery()
star297 0:e2a155f50119 241 {
star297 0:e2a155f50119 242 AdcIn=BATin.read();
star297 0:e2a155f50119 243 Ht = (AdcIn*3.328*(R1+R2)/R2); // set the numeric to the exact MCU analog reference voltage for greater accuracy
star297 0:e2a155f50119 244 sprintf(Vcc,"%2.3f",Ht);
star297 0:e2a155f50119 245 }
star297 0:e2a155f50119 246 // Temperature example
star297 0:e2a155f50119 247 void gettemp()
star297 0:e2a155f50119 248 {
star297 0:e2a155f50119 249 temperature=thermom.GetTemperature(); // comment this out if no sensor connected
star297 0:e2a155f50119 250 //temperature = 21.357; // include for dummy value
star297 0:e2a155f50119 251 sprintf(Temp,"%3.3f",temperature);
star297 0:e2a155f50119 252 }
star297 0:e2a155f50119 253 // Get RTC time
star297 0:e2a155f50119 254 void gettime()
star297 0:e2a155f50119 255 {
star297 0:e2a155f50119 256 time_t seconds = time(NULL);
star297 0:e2a155f50119 257 strftime(timebuf,50,"%H:%M:%S %a %d %b %y", localtime(&seconds));
star297 0:e2a155f50119 258 }
star297 0:e2a155f50119 259