For ESP8266 Wi Fi SOC from Sparkfun on the mbed LPC1768. Sets up a web page server that can control a few mbed pins. Nice IoT demo. Need to configure Wi Fi SSID and PASSWORD first with another program first (one time only). See https://developer.mbed.org/users/4180_1/notebook/using-the-esp8266-with-the-mbed-lpc1768/ for more info.

Dependencies:   mbed

Dependents:   IoT_DoorLock

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

Committer:
star297
Date:
Sun Feb 15 12:05:58 2015 +0000
Revision:
1:71ed1afbf344
Parent:
0:e2a155f50119
Child:
2:d4c6bc0f2dc4
Added set RTC function and ESP error detection at startup

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