Environmental Simulator
Dependencies: 4DGL-uLCD-SE ShiftBrite TMP36 GZ mbed
main.cpp
00001 // ESP8266 Static page WEB server to control Mbed 00002 00003 #include "mbed.h" 00004 #include "uLCD_4DGL.h" 00005 #include "ShiftBrite.h" 00006 #include "TMP36GZ.h" 00007 00008 uLCD_4DGL uLCD(p28, p27, p11); //On board LCD display 00009 00010 Serial pc(USBTX, USBRX); 00011 Serial esp(p13, p14); // tx, rx 00012 00013 SPI spi(p5, p6, p7); //SPI setup for ShiftBrite 00014 ShiftBrite myBrite(p15,p16,spi); //latch, enable, spi 00015 00016 00017 TMP36GZ temp_sensor(p20); // temp 00018 00019 // Standard Mbed LED definitions 00020 DigitalOut led1(LED1); 00021 DigitalOut led2(LED2); 00022 DigitalOut led3(LED3); 00023 DigitalOut led4(LED4); 00024 00025 // some test values to show on web page 00026 AnalogIn Ain1(p18); 00027 AnalogIn Ain2(p20); 00028 00029 inline float random_number() 00030 { 00031 return (rand()/(float(RAND_MAX))); 00032 } 00033 00034 00035 char ssid[32] = "TayKyr4180"; // enter WiFi router ssid inside the quotes 00036 char pwd [32] = "123456789"; // enter WiFi router password inside the quotes 00037 00038 float temperature, AdcIn, Ht; 00039 float R1=100000, R2=10000; // resistor values to give a 10:1 reduction of measured AnalogIn voltage 00040 char Vcc[10]; 00041 char Temp[10]; 00042 00043 // things for sending/receiving data over serial 00044 volatile int tx_in=0; 00045 volatile int tx_out=0; 00046 volatile int rx_in=0; 00047 volatile int rx_out=0; 00048 const int buffer_size = 4095; 00049 char tx_buffer[buffer_size+1]; 00050 char rx_buffer[buffer_size+1]; 00051 void Tx_interrupt(); 00052 void Rx_interrupt(); 00053 void send_line(); 00054 void read_line(); 00055 00056 int DataRX; 00057 int update; 00058 int count; 00059 char cmdbuff[1024]; 00060 char replybuff[4096]; 00061 char webdata[4096]; // This may need to be bigger depending on WEB browser used 00062 char webbuff[4096]; // Currently using 1986 characters, Increase this if more web page data added 00063 char timebuf[30]; 00064 void SendCMD(),getreply(),ReadWebData(),startserver(); 00065 void gettime(),setRTC(),gettemp(),getbattery(); 00066 char rx_line[1024]; 00067 int port =80; // set server port 00068 int SERVtimeout =5; // set server timeout in seconds in case link breaks. 00069 struct tm t; 00070 // manual set RTC values 00071 int minute =00; // 0-59 00072 int hour =12; // 2-23 00073 int dayofmonth =26; // 1-31 00074 int month =8; // 1-12 00075 int year =15; // last 2 digits 00076 int x =0; 00077 int countB =0; 00078 00079 int main() 00080 { 00081 pc.baud(115200); 00082 esp.baud(9600); 00083 led1=1,led2=0,led3=0, led4=0; 00084 // Setup a serial interrupt function to receive data 00085 esp.attach(&Rx_interrupt, Serial::RxIrq); 00086 // Setup a serial interrupt function to transmit data 00087 esp.attach(&Tx_interrupt, Serial::TxIrq); 00088 if (time(NULL) < 1420070400) { 00089 setRTC(); 00090 } 00091 startserver(); 00092 DataRX=0; 00093 count=0; 00094 while(1) { 00095 if(DataRX==1) { 00096 ReadWebData(); 00097 esp.attach(&Rx_interrupt, Serial::RxIrq); 00098 } 00099 if(update==1) // update time, hit count, and analog levels in the HUZZAH chip 00100 { 00101 // get new values 00102 gettime(); 00103 gettemp(); 00104 getbattery(); 00105 count++; 00106 // send new values 00107 sprintf(cmdbuff, "count,time,analog1,analog2=%d,\"%s\",\"%s\",\"%s\"\r\n",count,timebuf,Temp,Vcc); 00108 SendCMD(); 00109 getreply(); 00110 update=0; 00111 } 00112 } 00113 } 00114 00115 // Reads and processes GET and POST web data 00116 void ReadWebData() 00117 { 00118 wait_ms(200); 00119 esp.attach(NULL,Serial::RxIrq); 00120 DataRX=0; 00121 memset(webdata, '\0', sizeof(webdata)); 00122 strcpy(webdata, rx_buffer); 00123 memset(rx_buffer, '\0', sizeof(rx_buffer)); 00124 rx_in = 0; 00125 rx_out = 0; 00126 // check web data for form information 00127 00128 if(strstr(webdata, "check=Storm") != NULL ) { 00129 uLCD.media_init(); 00130 uLCD.set_sector_address(0x0000, 0x0104); 00131 uLCD.display_image(0,0); 00132 // Lightning loop 00133 while(strstr(webdata, "check=Storm") != NULL && countB < 100 ){ 00134 //get a new random number 00135 x = rand() % 256 + 20; 00136 myBrite.Write(125,249,255); 00137 myBrite.Brightness(x,x,x); 00138 //fast update rate for flashes 00139 wait(0.02); 00140 myBrite.Write(0,0,0); 00141 //add a random pause between flashes 00142 if (random_number()>0.9025) { 00143 myBrite.Write(0,0,0); 00144 wait(x/100); 00145 } 00146 countB = countB+1; 00147 } 00148 countB = 0; 00149 } 00150 if(strstr(webdata, "check=SunnyDay") != NULL ) { 00151 myBrite.Write(255,255,255); 00152 myBrite.Brightness(255,255,255); 00153 uLCD.media_init(); 00154 uLCD.set_sector_address(0x0000, 0x00C3); 00155 uLCD.display_image(0,0); 00156 } 00157 if(strstr(webdata, "check=Dawn") != NULL ) { 00158 myBrite.Write(255,214,170); 00159 myBrite.Brightness(255,255,255); 00160 uLCD.media_init(); 00161 uLCD.set_sector_address(0x0000, 0x0041); 00162 uLCD.display_image(0,0); 00163 } 00164 if(strstr(webdata, "check=Dusk") != NULL ) { 00165 myBrite.Write(255,214,170); 00166 myBrite.Brightness(255,255,255); 00167 uLCD.media_init(); 00168 uLCD.set_sector_address(0x0000, 0x0082); 00169 uLCD.display_image(0,0); 00170 } 00171 if(strstr(webdata, "check=CloudyDay") != NULL ) { 00172 myBrite.Write(201,226,255); 00173 myBrite.Brightness(255,255,255); 00174 uLCD.media_init(); 00175 uLCD.set_sector_address(0x0000, 0x0000); 00176 uLCD.display_image(0,0); 00177 } 00178 00179 if(strstr(webdata, "POST") != NULL ) { // set update flag if POST request 00180 update=1; 00181 } 00182 if(strstr(webdata, "GET") != NULL && strstr(webdata, "favicon") == NULL ) { // set update flag for GET request but do not want to update for favicon requests 00183 update=1; 00184 } 00185 } 00186 // Starts webserver 00187 void startserver() 00188 { 00189 gettime(); 00190 gettemp(); 00191 getbattery(); 00192 pc.printf("++++++++++ Resetting ESP ++++++++++\r\n"); 00193 strcpy(cmdbuff,"node.restart()\r\n"); 00194 SendCMD(); 00195 wait(2); 00196 getreply(); 00197 00198 pc.printf("\n++++++++++ Starting Server ++++++++++\r\n> "); 00199 00200 // initial values 00201 sprintf(cmdbuff, "count,time,analog1,analog2=0,\"%s\",\"%s\",\"%s\"\r\n",timebuf,Temp,Vcc); 00202 SendCMD(); 00203 getreply(); 00204 wait(0.5); 00205 00206 //create server 00207 sprintf(cmdbuff, "srv=net.createServer(net.TCP,%d)\r\n",SERVtimeout); 00208 SendCMD(); 00209 getreply(); 00210 wait(0.5); 00211 strcpy(cmdbuff,"srv:listen(80,function(conn)\r\n"); 00212 SendCMD(); 00213 getreply(); 00214 wait(0.3); 00215 strcpy(cmdbuff,"conn:on(\"receive\",function(conn,payload) \r\n"); 00216 SendCMD(); 00217 getreply(); 00218 wait(0.3); 00219 00220 //print data to mbed 00221 strcpy(cmdbuff,"print(payload)\r\n"); 00222 SendCMD(); 00223 getreply(); 00224 wait(0.2); 00225 00226 //web page data 00227 strcpy(cmdbuff,"conn:send('<!DOCTYPE html><html><body><h1>Lab 4 Mini Project - Environmental Simulator</h1>')\r\n"); 00228 SendCMD(); 00229 getreply(); 00230 wait(0.4); 00231 strcpy(cmdbuff,"conn:send('Hit count: '..count..'')\r\n"); 00232 SendCMD(); 00233 getreply(); 00234 wait(0.2); 00235 strcpy(cmdbuff,"conn:send('<br>Last hit (based on mbed RTC time): '..time..'<br><hr>')\r\n"); 00236 SendCMD(); 00237 getreply(); 00238 wait(0.4); 00239 strcpy(cmdbuff,"conn:send('Temperature: '..analog1..' <sup>o</sup>C <br><hr>')\r\n"); 00240 SendCMD(); 00241 getreply(); 00242 wait(0.3); 00243 strcpy(cmdbuff,"conn:send('<form method=\"POST\"')\r\n"); 00244 SendCMD(); 00245 getreply(); 00246 wait(0.3); 00247 /* 00248 strcpy(cmdbuff,"conn:send('<form method=\"POST\"><strong> Temperature:  <input type=\"text\" size=6 value=\""); 00249 SendCMD(); 00250 getreply(); 00251 wait(0.3); 00252 strcpy(cmdbuff,Temp); 00253 SendCMD(); 00254 getreply(); 00255 wait(0.3); 00256 */ 00257 00258 strcpy(cmdbuff, "conn:send('<p><input type=\"text\" name=\"check\" value=\"\"> Lighting Conditions (choose from: Storm, Dawn, SunnyDay, CloudyDay, or Dusk)')\r\n"); 00259 SendCMD(); 00260 getreply(); 00261 wait(0.3); 00262 00263 strcpy(cmdbuff,"conn:send('<p><input type=\"submit\" value=\"send-refresh\"></form>')\r\n"); 00264 SendCMD(); 00265 getreply(); 00266 wait(0.3); 00267 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"); 00268 SendCMD(); 00269 getreply(); 00270 wait(0.5); 00271 // end web page data 00272 strcpy(cmdbuff, "conn:on(\"sent\",function(conn) conn:close() end)\r\n"); // close current connection 00273 SendCMD(); 00274 getreply(); 00275 wait(0.3); 00276 strcpy(cmdbuff, "end)\r\n"); 00277 SendCMD(); 00278 getreply(); 00279 wait(0.2); 00280 strcpy(cmdbuff, "end)\r\n"); 00281 SendCMD(); 00282 getreply(); 00283 wait(0.2); 00284 00285 strcpy(cmdbuff, "tmr.alarm(0, 1000, 1, function()\r\n"); 00286 SendCMD(); 00287 getreply(); 00288 wait(0.2); 00289 strcpy(cmdbuff, "if wifi.sta.getip() == nil then\r\n"); 00290 SendCMD(); 00291 getreply(); 00292 wait(0.2); 00293 strcpy(cmdbuff, "print(\"Connecting to AP...\\n\")\r\n"); 00294 SendCMD(); 00295 getreply(); 00296 wait(0.2); 00297 strcpy(cmdbuff, "else\r\n"); 00298 SendCMD(); 00299 getreply(); 00300 wait(0.2); 00301 strcpy(cmdbuff, "ip, nm, gw=wifi.sta.getip()\r\n"); 00302 SendCMD(); 00303 getreply(); 00304 wait(0.2); 00305 strcpy(cmdbuff,"print(\"IP Address: \",ip)\r\n"); 00306 SendCMD(); 00307 getreply(); 00308 wait(0.2); 00309 strcpy(cmdbuff,"tmr.stop(0)\r\n"); 00310 SendCMD(); 00311 getreply(); 00312 wait(0.2); 00313 strcpy(cmdbuff,"end\r\n"); 00314 SendCMD(); 00315 getreply(); 00316 wait(0.2); 00317 strcpy(cmdbuff,"end)\r\n"); 00318 SendCMD(); 00319 getreply(); 00320 wait(0.2); 00321 00322 pc.printf("\n\n++++++++++ Ready ++++++++++\r\n\n"); 00323 } 00324 00325 00326 // ESP Command data send 00327 void SendCMD() 00328 { 00329 int i; 00330 char temp_char; 00331 bool empty; 00332 i = 0; 00333 // Start Critical Section - don't interrupt while changing global buffer variables 00334 NVIC_DisableIRQ(UART1_IRQn); 00335 empty = (tx_in == tx_out); 00336 while ((i==0) || (cmdbuff[i-1] != '\n')) { 00337 // Wait if buffer full 00338 if (((tx_in + 1) % buffer_size) == tx_out) { 00339 // End Critical Section - need to let interrupt routine empty buffer by sending 00340 NVIC_EnableIRQ(UART1_IRQn); 00341 while (((tx_in + 1) % buffer_size) == tx_out) { 00342 } 00343 // Start Critical Section - don't interrupt while changing global buffer variables 00344 NVIC_DisableIRQ(UART1_IRQn); 00345 } 00346 tx_buffer[tx_in] = cmdbuff[i]; 00347 i++; 00348 tx_in = (tx_in + 1) % buffer_size; 00349 } 00350 if (esp.writeable() && (empty)) { 00351 temp_char = tx_buffer[tx_out]; 00352 tx_out = (tx_out + 1) % buffer_size; 00353 // Send first character to start tx interrupts, if stopped 00354 esp.putc(temp_char); 00355 } 00356 // End Critical Section 00357 NVIC_EnableIRQ(UART1_IRQn); 00358 return; 00359 } 00360 00361 // Get Command and ESP status replies 00362 void getreply() 00363 { 00364 read_line(); 00365 sscanf(rx_line,replybuff); 00366 } 00367 00368 // Read a line from the large rx buffer from rx interrupt routine 00369 void read_line() { 00370 int i; 00371 i = 0; 00372 // Start Critical Section - don't interrupt while changing global buffer variables 00373 NVIC_DisableIRQ(UART1_IRQn); 00374 // Loop reading rx buffer characters until end of line character 00375 while ((i==0) || (rx_line[i-1] != '\r')) { 00376 // Wait if buffer empty 00377 if (rx_in == rx_out) { 00378 // End Critical Section - need to allow rx interrupt to get new characters for buffer 00379 NVIC_EnableIRQ(UART1_IRQn); 00380 while (rx_in == rx_out) { 00381 } 00382 // Start Critical Section - don't interrupt while changing global buffer variables 00383 NVIC_DisableIRQ(UART1_IRQn); 00384 } 00385 rx_line[i] = rx_buffer[rx_out]; 00386 i++; 00387 rx_out = (rx_out + 1) % buffer_size; 00388 } 00389 // End Critical Section 00390 NVIC_EnableIRQ(UART1_IRQn); 00391 rx_line[i-1] = 0; 00392 return; 00393 } 00394 00395 00396 // Interupt Routine to read in data from serial port 00397 void Rx_interrupt() { 00398 DataRX=1; 00399 //led3=1; 00400 // Loop just in case more than one character is in UART's receive FIFO buffer 00401 // Stop if buffer full 00402 while ((esp.readable()) && (((rx_in + 1) % buffer_size) != rx_out)) { 00403 rx_buffer[rx_in] = esp.getc(); 00404 // Uncomment to Echo to USB serial to watch data flow 00405 pc.putc(rx_buffer[rx_in]); 00406 rx_in = (rx_in + 1) % buffer_size; 00407 } 00408 //led3=0; 00409 return; 00410 } 00411 00412 00413 // Interupt Routine to write out data to serial port 00414 void Tx_interrupt() { 00415 //led2=1; 00416 // Loop to fill more than one character in UART's transmit FIFO buffer 00417 // Stop if buffer empty 00418 while ((esp.writeable()) && (tx_in != tx_out)) { 00419 esp.putc(tx_buffer[tx_out]); 00420 tx_out = (tx_out + 1) % buffer_size; 00421 } 00422 //led2=0; 00423 return; 00424 } 00425 00426 void gettime() 00427 { 00428 time_t seconds = time(NULL); 00429 strftime(timebuf,50,"%H:%M:%S %a %d %b %y", localtime(&seconds)); 00430 } 00431 00432 void setRTC() 00433 { 00434 t.tm_sec = (0); // 0-59 00435 t.tm_min = (minute); // 0-59 00436 t.tm_hour = (hour); // 0-23 00437 t.tm_mday = (dayofmonth); // 1-31 00438 t.tm_mon = (month-1); // 0-11 "0" = Jan, -1 added for Mbed RCT clock format 00439 t.tm_year = ((year)+100); // year since 1900, current DCF year + 100 + 1900 = correct year 00440 set_time(mktime(&t)); // set RTC clock 00441 } 00442 // Analog in example 00443 void getbattery() 00444 { 00445 AdcIn=Ain1.read(); 00446 Ht = (AdcIn*3.3); // set the numeric to the exact MCU analog reference voltage for greater accuracy 00447 sprintf(Vcc,"%2.3f",Ht); 00448 } 00449 // Temperature example 00450 void gettemp() 00451 { 00452 00453 //Temp=temp_sensor.sample(); 00454 sprintf(Temp,"%3.3f",temp_sensor.sample()); 00455 }
Generated on Sun Jul 24 2022 01:01:15 by 1.7.2