
For Adafruit HUZZAH ESP8266 Wi Fi chip on the mbed LPC1768. Sets up a web page server that can control a mbed onboard LEDs. Need to configure Wi Fi SSID and PASSWORD first with another program (one time only). See https://developer.mbed.org/users/ausdong/notebook/using-the-adafruit-huzzah-esp8266-to-add-wi-fi-to-/ .
Fork of ESP8266-WEB-Mbed-LPC1768-Controller by
main.cpp
00001 // ESP8266 Static page WEB server to control Mbed 00002 00003 #include "mbed.h" 00004 00005 Serial pc(USBTX, USBRX); 00006 Serial esp(p28, p27); // tx, rx 00007 00008 00009 // Standard Mbed LED definitions 00010 DigitalOut led1(LED1); 00011 DigitalOut led2(LED2); 00012 DigitalOut led3(LED3); 00013 DigitalOut led4(LED4); 00014 00015 // some test values to show on web page 00016 AnalogIn Ain1(p18); 00017 AnalogIn Ain2(p19); 00018 00019 /* 00020 char ssid[32] = "hsd"; // enter WiFi router ssid inside the quotes 00021 char pwd [32] = "austin123"; // enter WiFi router password inside the quotes 00022 */ 00023 float temperature, AdcIn, Ht; 00024 float R1=100000, R2=10000; // resistor values to give a 10:1 reduction of measured AnalogIn voltage 00025 char Vcc[10]; 00026 char Temp[10]; 00027 00028 // things for sending/receiving data over serial 00029 volatile int tx_in=0; 00030 volatile int tx_out=0; 00031 volatile int rx_in=0; 00032 volatile int rx_out=0; 00033 const int buffer_size = 4095; 00034 char tx_buffer[buffer_size+1]; 00035 char rx_buffer[buffer_size+1]; 00036 void Tx_interrupt(); 00037 void Rx_interrupt(); 00038 void send_line(); 00039 void read_line(); 00040 00041 int DataRX; 00042 int update; 00043 int count; 00044 char cmdbuff[1024]; 00045 char replybuff[4096]; 00046 char webdata[4096]; // This may need to be bigger depending on WEB browser used 00047 char webbuff[4096]; // Currently using 1986 characters, Increase this if more web page data added 00048 char timebuf[30]; 00049 void SendCMD(),getreply(),ReadWebData(),startserver(); 00050 void gettime(),setRTC(),gettemp(),getbattery(); 00051 char rx_line[1024]; 00052 int port =80; // set server port 00053 int SERVtimeout =5; // set server timeout in seconds in case link breaks. 00054 struct tm t; 00055 // manual set RTC values 00056 int minute =00; // 0-59 00057 int hour =12; // 2-23 00058 int dayofmonth =26; // 1-31 00059 int month =8; // 1-12 00060 int year =15; // last 2 digits 00061 00062 int main() 00063 { 00064 pc.baud(9600); 00065 esp.baud(9600); 00066 led1=1,led2=0,led3=0, led4=0; 00067 // Setup a serial interrupt function to receive data 00068 esp.attach(&Rx_interrupt, Serial::RxIrq); 00069 // Setup a serial interrupt function to transmit data 00070 esp.attach(&Tx_interrupt, Serial::TxIrq); 00071 if (time(NULL) < 1420070400) { 00072 setRTC(); 00073 } 00074 startserver(); 00075 DataRX=0; 00076 count=0; 00077 while(1) { 00078 if(DataRX==1) { 00079 ReadWebData(); 00080 esp.attach(&Rx_interrupt, Serial::RxIrq); 00081 } 00082 if(update==1) // update time, hit count, and analog levels in the HUZZAH chip 00083 { 00084 // get new values 00085 gettime(); 00086 gettemp(); 00087 getbattery(); 00088 count++; 00089 // send new values 00090 sprintf(cmdbuff, "count,time,analog1,analog2=%d,\"%s\",\"%s\",\"%s\"\r\n",count,timebuf,Temp,Vcc); 00091 SendCMD(); 00092 getreply(); 00093 update=0; 00094 } 00095 } 00096 } 00097 00098 // Reads and processes GET and POST web data 00099 void ReadWebData() 00100 { 00101 wait_ms(200); 00102 esp.attach(NULL,Serial::RxIrq); 00103 DataRX=0; 00104 memset(webdata, '\0', sizeof(webdata)); 00105 strcpy(webdata, rx_buffer); 00106 memset(rx_buffer, '\0', sizeof(rx_buffer)); 00107 rx_in = 0; 00108 rx_out = 0; 00109 // check web data for form information 00110 if( strstr(webdata, "check=led1v") != NULL ) { 00111 led1=!led1; 00112 } 00113 if( strstr(webdata, "check=led2v") != NULL ) { 00114 led2=!led2; 00115 } 00116 if( strstr(webdata, "check=led3v") != NULL ) { 00117 led3=!led3; 00118 } 00119 if( strstr(webdata, "check=led4v") != NULL ) { 00120 led4=!led4; 00121 } 00122 if( strstr(webdata, "POST") != NULL ) { // set update flag if POST request 00123 update=1; 00124 } 00125 if( strstr(webdata, "GET") != NULL && strstr(webdata, "favicon") == NULL ) { // set update flag for GET request but do not want to update for favicon requests 00126 update=1; 00127 } 00128 } 00129 // Starts webserver 00130 void startserver() 00131 { 00132 gettime(); 00133 gettemp(); 00134 getbattery(); 00135 pc.printf("++++++++++ Resetting ESP ++++++++++\r\n"); 00136 strcpy(cmdbuff,"node.restart()\r\n"); 00137 SendCMD(); 00138 wait(2); 00139 getreply(); 00140 00141 pc.printf("\n++++++++++ Starting Server ++++++++++\r\n> "); 00142 00143 // initial values 00144 sprintf(cmdbuff, "count,time,analog1,analog2=0,\"%s\",\"%s\",\"%s\"\r\n",timebuf,Temp,Vcc); 00145 SendCMD(); 00146 getreply(); 00147 wait(0.5); 00148 00149 //create server 00150 sprintf(cmdbuff, "srv=net.createServer(net.TCP,%d)\r\n",SERVtimeout); 00151 SendCMD(); 00152 getreply(); 00153 wait(0.5); 00154 strcpy(cmdbuff,"srv:listen(80,function(conn)\r\n"); 00155 SendCMD(); 00156 getreply(); 00157 wait(0.3); 00158 strcpy(cmdbuff,"conn:on(\"receive\",function(conn,payload) \r\n"); 00159 SendCMD(); 00160 getreply(); 00161 wait(0.3); 00162 00163 //print data to mbed 00164 strcpy(cmdbuff,"print(payload)\r\n"); 00165 SendCMD(); 00166 getreply(); 00167 wait(0.2); 00168 00169 //web page data 00170 strcpy(cmdbuff,"conn:send('<!DOCTYPE html><html><body><h1>ESP8266 Mbed IoT Web Controller</h1>')\r\n"); 00171 SendCMD(); 00172 getreply(); 00173 wait(0.4); 00174 strcpy(cmdbuff,"conn:send('Hit count: '..count..'')\r\n"); 00175 SendCMD(); 00176 getreply(); 00177 wait(0.2); 00178 strcpy(cmdbuff,"conn:send('<br>Last hit (based on mbed RTC time): '..time..'<br><hr>')\r\n"); 00179 SendCMD(); 00180 getreply(); 00181 wait(0.4); 00182 strcpy(cmdbuff,"conn:send('Analog 1: '..analog1..' V<br>Analog 2: '..analog2..' V<br><hr>')\r\n"); 00183 SendCMD(); 00184 getreply(); 00185 wait(0.3); 00186 strcpy(cmdbuff,"conn:send('<form method=\"POST\"')\r\n"); 00187 SendCMD(); 00188 getreply(); 00189 wait(0.3); 00190 strcpy(cmdbuff, "conn:send('<p><input type=\"checkbox\" name=\"check\" value=\"led1v\"> flip LED1')\r\n"); 00191 SendCMD(); 00192 getreply(); 00193 wait(0.3); 00194 strcpy(cmdbuff, "conn:send('<p><input type=\"checkbox\" name=\"check\" value=\"led2v\"> flip LED2')\r\n"); 00195 SendCMD(); 00196 getreply(); 00197 wait(0.3); 00198 strcpy(cmdbuff, "conn:send('<p><input type=\"checkbox\" name=\"check\" value=\"led3v\"> flip LED3')\r\n"); 00199 SendCMD(); 00200 getreply(); 00201 wait(0.3); 00202 strcpy(cmdbuff, "conn:send('<p><input type=\"checkbox\" name=\"check\" value=\"led4v\"> flip LED4')\r\n"); 00203 SendCMD(); 00204 getreply(); 00205 wait(0.3); 00206 strcpy(cmdbuff,"conn:send('<p><input type=\"submit\" value=\"send-refresh\"></form>')\r\n"); 00207 SendCMD(); 00208 getreply(); 00209 wait(0.3); 00210 strcpy(cmdbuff, "conn:send('<p><h2>How to use:</h2><ul><li>Select a checkbox to flip on/off</li><li>Click Send-Refresh to send data and refresh values</li></ul></body></html>')\r\n"); 00211 SendCMD(); 00212 getreply(); 00213 wait(0.5); 00214 // end web page data 00215 strcpy(cmdbuff, "conn:on(\"sent\",function(conn) conn:close() end)\r\n"); // close current connection 00216 SendCMD(); 00217 getreply(); 00218 wait(0.3); 00219 strcpy(cmdbuff, "end)\r\n"); 00220 SendCMD(); 00221 getreply(); 00222 wait(0.2); 00223 strcpy(cmdbuff, "end)\r\n"); 00224 SendCMD(); 00225 getreply(); 00226 wait(0.2); 00227 00228 strcpy(cmdbuff, "tmr.alarm(0, 1000, 1, function()\r\n"); 00229 SendCMD(); 00230 getreply(); 00231 wait(0.2); 00232 strcpy(cmdbuff, "if wifi.sta.getip() == nil then\r\n"); 00233 SendCMD(); 00234 getreply(); 00235 wait(0.2); 00236 strcpy(cmdbuff, "print(\"Connecting to AP...\\n\")\r\n"); 00237 SendCMD(); 00238 getreply(); 00239 wait(0.2); 00240 strcpy(cmdbuff, "else\r\n"); 00241 SendCMD(); 00242 getreply(); 00243 wait(0.2); 00244 strcpy(cmdbuff, "ip, nm, gw=wifi.sta.getip()\r\n"); 00245 SendCMD(); 00246 getreply(); 00247 wait(0.2); 00248 strcpy(cmdbuff,"print(\"IP Address: \",ip)\r\n"); 00249 SendCMD(); 00250 getreply(); 00251 wait(0.2); 00252 strcpy(cmdbuff,"tmr.stop(0)\r\n"); 00253 SendCMD(); 00254 getreply(); 00255 wait(0.2); 00256 strcpy(cmdbuff,"end\r\n"); 00257 SendCMD(); 00258 getreply(); 00259 wait(0.2); 00260 strcpy(cmdbuff,"end)\r\n"); 00261 SendCMD(); 00262 getreply(); 00263 wait(0.2); 00264 00265 pc.printf("\n\n++++++++++ Ready ++++++++++\r\n\n"); 00266 } 00267 00268 00269 // ESP Command data send 00270 void SendCMD() 00271 { 00272 int i; 00273 char temp_char; 00274 bool empty; 00275 i = 0; 00276 // Start Critical Section - don't interrupt while changing global buffer variables 00277 NVIC_DisableIRQ(UART1_IRQn); 00278 empty = (tx_in == tx_out); 00279 while ((i==0) || (cmdbuff[i-1] != '\n')) { 00280 // Wait if buffer full 00281 if (((tx_in + 1) % buffer_size) == tx_out) { 00282 // End Critical Section - need to let interrupt routine empty buffer by sending 00283 NVIC_EnableIRQ(UART1_IRQn); 00284 while (((tx_in + 1) % buffer_size) == tx_out) { 00285 } 00286 // Start Critical Section - don't interrupt while changing global buffer variables 00287 NVIC_DisableIRQ(UART1_IRQn); 00288 } 00289 tx_buffer[tx_in] = cmdbuff[i]; 00290 i++; 00291 tx_in = (tx_in + 1) % buffer_size; 00292 } 00293 if (esp.writeable() && (empty)) { 00294 temp_char = tx_buffer[tx_out]; 00295 tx_out = (tx_out + 1) % buffer_size; 00296 // Send first character to start tx interrupts, if stopped 00297 esp.putc(temp_char); 00298 } 00299 // End Critical Section 00300 NVIC_EnableIRQ(UART1_IRQn); 00301 return; 00302 } 00303 00304 // Get Command and ESP status replies 00305 void getreply() 00306 { 00307 read_line(); 00308 sscanf(rx_line,replybuff); 00309 } 00310 00311 // Read a line from the large rx buffer from rx interrupt routine 00312 void read_line() { 00313 int i; 00314 i = 0; 00315 // Start Critical Section - don't interrupt while changing global buffer variables 00316 NVIC_DisableIRQ(UART1_IRQn); 00317 // Loop reading rx buffer characters until end of line character 00318 while ((i==0) || (rx_line[i-1] != '\r')) { 00319 // Wait if buffer empty 00320 if (rx_in == rx_out) { 00321 // End Critical Section - need to allow rx interrupt to get new characters for buffer 00322 NVIC_EnableIRQ(UART1_IRQn); 00323 while (rx_in == rx_out) { 00324 } 00325 // Start Critical Section - don't interrupt while changing global buffer variables 00326 NVIC_DisableIRQ(UART1_IRQn); 00327 } 00328 rx_line[i] = rx_buffer[rx_out]; 00329 i++; 00330 rx_out = (rx_out + 1) % buffer_size; 00331 } 00332 // End Critical Section 00333 NVIC_EnableIRQ(UART1_IRQn); 00334 rx_line[i-1] = 0; 00335 return; 00336 } 00337 00338 00339 // Interupt Routine to read in data from serial port 00340 void Rx_interrupt() { 00341 DataRX=1; 00342 //led3=1; 00343 // Loop just in case more than one character is in UART's receive FIFO buffer 00344 // Stop if buffer full 00345 while ((esp.readable()) && (((rx_in + 1) % buffer_size) != rx_out)) { 00346 rx_buffer[rx_in] = esp.getc(); 00347 // Uncomment to Echo to USB serial to watch data flow 00348 pc.putc(rx_buffer[rx_in]); 00349 rx_in = (rx_in + 1) % buffer_size; 00350 } 00351 //led3=0; 00352 return; 00353 } 00354 00355 00356 // Interupt Routine to write out data to serial port 00357 void Tx_interrupt() { 00358 //led2=1; 00359 // Loop to fill more than one character in UART's transmit FIFO buffer 00360 // Stop if buffer empty 00361 while ((esp.writeable()) && (tx_in != tx_out)) { 00362 esp.putc(tx_buffer[tx_out]); 00363 tx_out = (tx_out + 1) % buffer_size; 00364 } 00365 //led2=0; 00366 return; 00367 } 00368 00369 void gettime() 00370 { 00371 time_t seconds = time(NULL); 00372 strftime(timebuf,50,"%H:%M:%S %a %d %b %y", localtime(&seconds)); 00373 } 00374 00375 void setRTC() 00376 { 00377 t.tm_sec = (0); // 0-59 00378 t.tm_min = (minute); // 0-59 00379 t.tm_hour = (hour); // 0-23 00380 t.tm_mday = (dayofmonth); // 1-31 00381 t.tm_mon = (month-1); // 0-11 "0" = Jan, -1 added for Mbed RCT clock format 00382 t.tm_year = ((year)+100); // year since 1900, current DCF year + 100 + 1900 = correct year 00383 set_time(mktime(&t)); // set RTC clock 00384 } 00385 // Analog in example 00386 void getbattery() 00387 { 00388 AdcIn=Ain1.read(); 00389 Ht = (AdcIn*3.3); // set the numeric to the exact MCU analog reference voltage for greater accuracy 00390 sprintf(Vcc,"%2.3f",Ht); 00391 } 00392 // Temperature example 00393 void gettemp() 00394 { 00395 00396 AdcIn=Ain2.read(); 00397 Ht = (AdcIn*3.3); // set the numeric to the exact MCU analog reference voltage for greater accuracy 00398 sprintf(Temp,"%2.3f",Ht); 00399 }
Generated on Wed Jul 13 2022 01:41:42 by
