Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of Thread_Communication_V4_fortest by
main.cpp
00001 #include "mbed.h" 00002 #include "main.h" 00003 #include "stdio.h" 00004 00005 #define FallingEdge 0 00006 #define RisingEdge 1 00007 #define USER_BUTTON_PRESSED 1 00008 00009 LCD lcd(PD_15, PF_12, PF_13, PE_9, PF_14, PF_15); 00010 BMP280 Sensor(D14, D15); 00011 00012 //Define Functions 00013 void PrintLCD (); 00014 void Rx_interrupt(); 00015 void serialCMD(); 00016 void sensorRead(); 00017 void readISR(); 00018 void circBuff(); 00019 void writeRemove_SD(); 00020 void Network1(); 00021 void LCD_timerISR(); 00022 00023 // USER_BUTTON ISRs (Debounce) 00024 00025 void userButtonRise(); 00026 void userButtonFall(); 00027 void userButtonTimeoutHandler(); 00028 00029 // Tickers & Timeouts 00030 Timeout userButtonTimeout; // FOR debouncing User Switch 00031 Ticker read; // ***Sets sampling period!*** (ISR Signals sampling Thread) 00032 Ticker refresh; 00033 /* LOCKS */ 00034 Mutex DataBuffer; 00035 Mutex dataLock; 00036 00037 /* THREADS */ 00038 Thread _PrintLCD, _serialCMD, _circBuff; 00039 Thread _sensorRead (osPriorityRealtime); //dataLock Thread 00040 Thread _writeRemove_SD; 00041 Thread _Network1; 00042 00043 /* GLOBAL DATA */ 00044 volatile float LDR = 0; 00045 volatile double PRES = 0; 00046 volatile double TEMP = 0; 00047 volatile char TIME[21]; 00048 00049 volatile double sampleTime = 15.0; 00050 //int LCD_refresh = 1; 00051 // int to hold current switch state 00052 int userButtonState = FallingEdge; 00053 00054 /* DEBOUNCER INTERRUPTS */ 00055 InterruptIn userButton(USER_BUTTON); 00056 00057 void userButtonRise () 00058 { 00059 userButton.rise(NULL); 00060 userButtonState = RisingEdge; 00061 userButtonTimeout.attach(&userButtonTimeoutHandler, 0.1); 00062 } 00063 00064 void userButtonFall () 00065 { 00066 userButton.fall(NULL); 00067 _writeRemove_SD.signal_set(USER_BUTTON_PRESSED); 00068 userButtonState = FallingEdge; 00069 userButtonTimeout.attach(&userButtonTimeoutHandler, 0.1); 00070 } 00071 00072 void userButtonTimeoutHandler() 00073 { 00074 userButtonTimeout.detach(); 00075 00076 switch (userButtonState) { 00077 case RisingEdge: 00078 userButton.fall(&userButtonFall); 00079 break; 00080 case FallingEdge: 00081 userButton.rise(userButtonRise); 00082 break; 00083 }// End Switch 00084 } //End ISR 00085 /*--------------------------------MAIN--------------------------------*/ 00086 int main() 00087 { 00088 00089 pc.baud(57600); 00090 pc.attach(&Rx_interrupt, Serial::RxIrq); 00091 POST(); 00092 00093 pc.printf("\n\n\nType HELP for list of available Commands\n\n\n\r"); 00094 _serialCMD.start(serialCMD); 00095 _sensorRead.start(sensorRead); 00096 _PrintLCD.start(PrintLCD); 00097 _circBuff.start(circBuff); 00098 _writeRemove_SD.start(writeRemove_SD); 00099 _Network1.start(Network1); 00100 00101 userButton.rise(&userButtonRise); 00102 read.attach(&readISR, sampleTime); 00103 refresh.attach(&LCD_timerISR, LCD_REFRESH); 00104 00105 while (1) { 00106 Yellow_ext = ON; 00107 Thread::wait (200); 00108 Yellow_ext = OFF; 00109 Thread::wait(200); 00110 }// End While 00111 } // End Main 00112 /*--------------------------------------------------------------------*/ 00113 /*-----------------Circular Buffer------------------------------------*/ 00114 void circBuff () 00115 { 00116 00117 while(1) { 00118 Thread::signal_wait(DATA_READY); // wait for signal from sensorRead 00119 00120 //Lock data buffer 00121 DataBuffer.lock(); 00122 00123 //Format samples, send to FIFO buffer head 00124 memset(data_buffer[sample_h],NULL,64); 00125 00126 osEvent evt = mail_FIFO.get(); 00127 00128 if (evt.status == osEventMail) { 00129 mail_t *mail = (mail_t*)evt.value.p; 00130 char sample_time[21]; 00131 int rTime = mail->Time_Date; 00132 time((time_t*)rTime); 00133 sample_epoch = localtime((time_t*)rTime); 00134 strftime(sample_time, 21,"%d/%m/%Y, %X", sample_epoch); 00135 sprintf(data_buffer[sample_h],"%s, %2.2f, %4.2f, %.4f\n\r", sample_time, mail->temp_Value, mail->press_Value, mail->LDR_Value); 00136 00137 mail_FIFO.free(mail); 00138 } 00139 00140 00141 00142 00143 //Set seperate FIFO head and tail for printing data 00144 data_h = sample_h; 00145 data_t = sample_t; 00146 00147 //Move sample FIFO buffer head to next row in buffer 00148 sample_h++; 00149 //Check sample FIFO buffer head 00150 if(sample_h >= MAX_SAMPLES) { 00151 sample_h = 0; 00152 } 00153 //Check sample FIFO buffer tail 00154 if(sample_t == sample_h) { 00155 sample_t++; 00156 if(sample_t >= (MAX_SAMPLES)) { 00157 sample_t = 0; 00158 } 00159 } 00160 //Unlock data buffer 00161 DataBuffer.unlock(); 00162 }// End While 00163 }// End Circular buffer 00164 /*-------------------------------------------------------------*/ 00165 /*---------------------Read Sensors ---------------------------*/ 00166 00167 void readISR () // Ticker interrupt defined in main 00168 { 00169 00170 _sensorRead.signal_set(SENSOR_UPDATE); 00171 } 00172 // Keep short 00173 void sensorRead () 00174 { 00175 volatile float LDR = 0; 00176 volatile double PRES = 0; 00177 volatile double TEMP = 0; 00178 00179 while (1) { 00180 00181 dataLock.lock(); // Entering Critial Section 00182 00183 // Store Data in global Variables 00184 LDR = LDR_In.read(); 00185 TEMP = Sensor.getTemperature(); 00186 PRES = Sensor.getPressure(); 00187 00188 dataLock.unlock(); // Exiting Critical Section 00189 00190 Green_int = !Green_int; // debugging 00191 00192 //Read sensors, send to mail-queue 00193 mail_t *mail = mail_LCD.alloc(); 00194 mail->LDR_Value = LDR; 00195 mail->temp_Value = TEMP; 00196 mail->press_Value = PRES; 00197 mail_LCD.put(mail); 00198 00199 mail_t *mail1 = mail_FIFO.alloc(); 00200 mail1->Time_Date = raw_time; 00201 mail1->LDR_Value = LDR; 00202 mail1->temp_Value = TEMP; 00203 mail1->press_Value = PRES; 00204 mail_FIFO.put(mail); 00205 00206 mail_t *mail2 = mail_Network.alloc(); 00207 mail1->Time_Date = raw_time; 00208 mail2->LDR_Value = LDR; 00209 mail2->temp_Value = TEMP; 00210 mail2->press_Value = PRES; 00211 mail_Network.put(mail); 00212 00213 00214 00215 _circBuff.signal_set(DATA_READY); // Set signal to buffer to store updated values 00216 Thread::signal_wait(SENSOR_UPDATE); // Wait for the Timer interrupt 00217 } 00218 } 00219 /*--------------------------------------------------------------------*/ 00220 00221 /*--------------------------------LCD---------------------------------*/ 00222 void LCD_timerISR () 00223 { 00224 _PrintLCD.signal_set(LCD_READY); 00225 } 00226 00227 void PrintLCD () 00228 { 00229 00230 int i = 0; 00231 int j = 4; 00232 char lightString[16]; 00233 char tempString[16]; 00234 char pressString[16]; 00235 char lcd_TIME[21]; 00236 00237 while(1) { 00238 00239 lcd.RowSelect(3); 00240 00241 00242 if (j == 4) { 00243 00244 lcd.Clear(); 00245 lcd.RowSelect(0); 00246 switch (i) { 00247 case 0: { 00248 osEvent evt = mail_LCD.get(); 00249 00250 if (evt.status == osEventMail) { 00251 mail_t *mail = (mail_t*)evt.value.p; 00252 00253 sprintf(lightString,"%.4f", mail->LDR_Value); 00254 sprintf(tempString,"%2.2f", mail->temp_Value); 00255 sprintf(pressString,"%4.2f", mail->press_Value); 00256 00257 mail_LCD.free(mail); 00258 } 00259 00260 lcd.Write("Light:"); 00261 lcd.RowSelect(1); 00262 lcd.Write(lightString); 00263 i++; 00264 j = 0; 00265 break; 00266 } 00267 case 1: 00268 lcd.Write("Temperature(C):"); 00269 lcd.RowSelect(1); 00270 lcd.Write(tempString); 00271 i++; 00272 j = 0; 00273 break; 00274 00275 case 2: 00276 lcd.Write("Pressure(mBar):"); 00277 lcd.RowSelect(1); 00278 lcd.Write(pressString); 00279 i =0; 00280 j = 0; 00281 break; 00282 00283 default: 00284 i = 0; 00285 j = 0; 00286 break; 00287 }//end switch 00288 }// end if 00289 else { 00290 j++; 00291 } 00292 lcd.RowSelect(3); 00293 00294 dataLock.lock(); 00295 memset(lcd_TIME, NULL, 21); 00296 time(&raw_time); 00297 sample_epoch = localtime(&raw_time); 00298 strftime( lcd_TIME,21,"%d/%m/%Y %X", sample_epoch); 00299 dataLock.unlock(); 00300 00301 lcd.Write(lcd_TIME); 00302 Thread::signal_wait(LCD_READY); 00303 Red_int = !Red_int; 00304 }//end while 00305 }// end thread 00306 /*--------------------------------------------------------------------*/ 00307 00308 /*------------------------------SERIAL_CMD----------------------------*/ 00309 //Interrupt when recieving from serial port 00310 void Rx_interrupt() 00311 { 00312 00313 //Wait for serial input 00314 while (pc.readable()) { 00315 00316 //Return input to serial 00317 rx_buffer[rx_in] = pc.getc(); 00318 pc.putc(rx_buffer[rx_in]); 00319 00320 //If enter key is pressed, set serial thread signal 00321 if(rx_buffer[rx_in] == 0xD) { 00322 _serialCMD.signal_set(ENTER_KEY); 00323 } 00324 00325 //Increment buffer head 00326 else { 00327 if(rx_in>=32) { 00328 puts("\n\rERROR - Stop typing so much!\n\r"); 00329 rx_in = 0; 00330 memset(rx_buffer, NULL, 32); 00331 } else { 00332 rx_in = (rx_in + 1); 00333 } 00334 } 00335 } 00336 } 00337 00338 //Check what command what recieved and execute 00339 void serialCMD() 00340 { 00341 bool xx = 1; // State for CMD STATE 00342 while(1) { 00343 //Wait for thread signal 00344 Thread::signal_wait(ENTER_KEY); 00345 00346 //Detach serial interrupt 00347 pc.attach(NULL, Serial::RxIrq); 00348 00349 struct tm * s_time; 00350 char tm_n[4]; 00351 00352 /*----CARRAGE RETURN-------------*/ 00353 if(rx_buffer[0] == 0xD) { 00354 pc.puts("\n\r"); 00355 } 00356 /*----READ ALL----------------------------------*/ 00357 else if(strstr(rx_buffer, "READ ALL")) { 00358 pc.puts("\n\r Reading all samples...\n\r"); 00359 00360 //Lock data buffer 00361 DataBuffer.lock(); 00362 00363 //Print all samples to serial 00364 for(int n=data_t; n<=MAX_SAMPLES; n++) { 00365 pc.puts(data_buffer[n]); 00366 } 00367 if(data_t>data_h) { 00368 for(int n=0; n<=(data_t-1); n++) { 00369 pc.puts(data_buffer[n]); 00370 } 00371 } 00372 00373 //Lock data buffer 00374 DataBuffer.unlock(); 00375 pc.puts(" All Samples read!\n\r"); 00376 } 00377 /*----DELETE ALL----------------------------------*/ 00378 else if(strstr(rx_buffer, "DELETE ALL")) { 00379 pc.puts("\n\r Deleting all samples...\n\r"); 00380 00381 //Lock data buffer 00382 DataBuffer.lock(); 00383 00384 //Delete all sampled data 00385 for(int n=0; n<=MAX_SAMPLES; n++) { 00386 memset(data_buffer[n], NULL, 64); 00387 } 00388 data_h = data_t; 00389 sample_h = sample_t; 00390 00391 //Unlock data buffer 00392 DataBuffer.unlock(); 00393 pc.puts(" All previous samples deleted!\n\r"); 00394 } 00395 /*----READ----------------------------------*/ 00396 else if(strstr(rx_buffer, "READ")) { 00397 pc.puts("\n\r Reading N samples...\n\r"); 00398 int N = atoi(strncpy(tm_n,&rx_buffer[5],4)); 00399 int S = 0; 00400 00401 //Lock data buffer 00402 DataBuffer.lock(); 00403 00404 //Check if N is greater than buffer size 00405 if(N >= MAX_SAMPLES) { 00406 N = MAX_SAMPLES; 00407 } 00408 00409 //Read N samples from FIFO buffer 00410 if(N <= 0) { 00411 pc.puts("ERROR - N must be greater than 0\n\r"); 00412 N = 0; 00413 } else { 00414 for(int n=data_h; n>=0; n--) { 00415 if(S>=N) {} 00416 else { 00417 pc.puts(data_buffer[n]); 00418 S++; 00419 } 00420 } 00421 for(int n=MAX_SAMPLES-1; n<=data_t; n--) { 00422 if(S>=N) {} 00423 else { 00424 pc.puts(data_buffer[n]); 00425 S++; 00426 } 00427 } 00428 } 00429 pc.printf(" Read %d samples\n\r",N); 00430 //Unlock data buffer 00431 DataBuffer.unlock(); 00432 } 00433 /*----DELETE----------------------------------*/ 00434 else if(strstr(rx_buffer, "DELETE")) { 00435 pc.puts("\n\r Deleting N samples...\n\r"); 00436 int N = atoi(strncpy(tm_n,&rx_buffer[6],4)); 00437 int S = 0; 00438 00439 //Lock data buffer 00440 DataBuffer.lock(); 00441 00442 //Check if N is greater than buffer size 00443 if(N >= MAX_SAMPLES) { 00444 N = MAX_SAMPLES; 00445 } 00446 00447 //Read N samples from FIFO buffer 00448 if(N <= 0) { 00449 pc.puts("ERROR - N must be greater than 0\n\r"); 00450 N = 0; 00451 } else { 00452 for(int n=data_t; n<=MAX_SAMPLES; n++) { 00453 if(S>=N) {} 00454 else { 00455 memset(data_buffer[n], NULL, 64); 00456 S++; 00457 data_t = n; 00458 sample_t = n; 00459 } 00460 } 00461 for(int n=MAX_SAMPLES-1; n<=data_t; n--) { 00462 if(S>=N) {} 00463 else { 00464 memset(data_buffer[n], NULL, 64); 00465 S++; 00466 data_t = n; 00467 sample_t = n; 00468 } 00469 } 00470 } 00471 pc.printf(" Deleted %d samples\n\r",N); 00472 //Unlock data buffer 00473 DataBuffer.unlock(); 00474 } 00475 /*----SETDATE----------------------------------*/ 00476 else if(strstr(rx_buffer, "SETDATE")) { 00477 time(&raw_time); 00478 s_time = localtime(&raw_time); 00479 00480 //Update day in time structure 00481 int dd = atoi(strncpy(tm_n,&rx_buffer[8],2)); 00482 s_time->tm_mday = dd; 00483 memset(tm_n, NULL, 4); 00484 00485 //Update month in time structure 00486 int mm = atoi(strncpy(tm_n,&rx_buffer[11],2)); 00487 s_time->tm_mon = mm-1; 00488 memset(tm_n, NULL, 4); 00489 00490 //Update year in time structure 00491 int yyyy = atoi(strncpy(tm_n,&rx_buffer[14],4)); 00492 s_time->tm_year = yyyy-1900; 00493 memset(tm_n, NULL, 4); 00494 00495 //Set date from updated time structure 00496 set_time(mktime(s_time)); 00497 strftime(serial_buffer, 80, "\n\r Date updated to: %d/%m/%Y\n\r", s_time); 00498 pc.puts(serial_buffer); 00499 } 00500 /*----SETTIME---------------------------------*/ 00501 else if(strstr(rx_buffer, "SETTIME")) { 00502 time(&raw_time); 00503 s_time = localtime(&raw_time); 00504 00505 //Update seconds in time structure 00506 int ss = atoi(strncpy(tm_n,&rx_buffer[14],2)); 00507 s_time->tm_sec = ss; 00508 memset(tm_n, NULL, 4); 00509 00510 //Update minutes in time structure 00511 int mm = atoi(strncpy(tm_n,&rx_buffer[11],2)); 00512 s_time->tm_min = mm; 00513 memset(tm_n, NULL, 4); 00514 00515 //Update hour in time structure 00516 int hh = atoi(strncpy(tm_n,&rx_buffer[8],2)); 00517 s_time->tm_hour = hh; 00518 memset(tm_n, NULL, 4); 00519 00520 //Set time from updated time structure 00521 set_time(mktime(s_time)); 00522 strftime(serial_buffer, 80, "\n\r Time updated to: %X\n\r", s_time); 00523 pc.puts(serial_buffer); 00524 } 00525 /*----SETT----------------------------------*/ 00526 else if(strstr(rx_buffer, "SETT")) { 00527 read.detach(); 00528 double AA = atof(strncpy(tm_n,&rx_buffer[5],4)); 00529 00530 if (AA < 0.1 || AA > 60) { 00531 AA = 15; 00532 pc.puts("ERROR - Sample Time out of range (0.1<=T<=60)\n\r"); 00533 } 00534 sampleTime = AA; 00535 pc.printf("\n\r Sample Time updated to: %2.1f seconds\n\r", sampleTime); 00536 read.attach(readISR, sampleTime); 00537 memset(tm_n, NULL, 4); 00538 } 00539 /*----STATE----------------------------------*/ 00540 else if(strstr(rx_buffer, "STATE")) { 00541 00542 strncpy(tm_n,&rx_buffer[6],3); 00543 if (strstr(tm_n, "ON")) { 00544 if (xx == 1) { 00545 pc.puts("\n\r Already Sampling\n\r"); 00546 } else { 00547 read.attach(&readISR, sampleTime); 00548 pc.puts("\n\r Sampling ON\n\r"); 00549 xx = 1; 00550 } 00551 } else if (strstr(tm_n, "OFF")) { 00552 if ( xx == 0 ) { 00553 pc.puts("\n\r Already not Sampling\n\r"); 00554 } else { 00555 read.detach(); 00556 pc.puts("\n\r Sampling OFF\n\r"); 00557 xx = 0; 00558 } 00559 } else { 00560 pc.puts("Error - STATE can only be ON or OFF\n\r"); 00561 } 00562 //pc.puts(" STATE\n\r"); 00563 } 00564 /*----LOGGING----------------------------------*/ 00565 else if(strstr(rx_buffer, "LOGGING")) { 00566 pc.puts(" LOGGING\n\r"); 00567 } 00568 /*----HELP--------------------------------------*/ 00569 else if (strstr(rx_buffer, "HELP")) { 00570 pc.puts("\n\n\r Currently Available Commands:\n\r"); 00571 pc.puts("\tREAD n - Read n previous samples\n\r"); 00572 pc.puts("\tREAD ALL - Read All previous samples held in memory\n\r"); 00573 pc.puts("\tSETTIME hh:mm::ss - Set time in 24hr format\n\r"); 00574 pc.puts("\tSETDATE dd/mm/yyyy - Set time in specified format\n\r"); 00575 pc.puts("\tDELETE ALL - Delete all sampled held in internal memory\n\r"); 00576 pc.puts("\tSETT - Set sample period. must be an integer in range 0 < T < 61\n\r"); 00577 } else if (strstr(rx_buffer,"tell me a joke")) { 00578 pc.puts("\n\r Why do programmers always get Halloween and Christmas mixed up...?\n\r"); 00579 Thread::wait(5000); 00580 pc.puts(" Because Oct 31 == Dec 25! LOL"); 00581 } 00582 /*----ERROR---*/ 00583 else { 00584 pc.puts("Error - Command not recognised. Type HELP for a list of available commands\n\r"); 00585 } 00586 /*----------------------------------------------*/ 00587 00588 //Clear serial buffers 00589 memset(serial_buffer, NULL, 80); 00590 memset(rx_buffer, NULL, 32); 00591 rx_in = 0; 00592 00593 //Attach serial interrupt 00594 pc.attach(&Rx_interrupt, Serial::RxIrq); 00595 } 00596 } 00597 /*------------------------------------------------*/ 00598 /*---------------SD THread------------------------*/ 00599 void writeRemove_SD() 00600 { 00601 00602 while(1) { 00603 Thread::signal_wait(USER_BUTTON_PRESSED); //wait for debounce signal 00604 int sd_state = sdIn; 00605 00606 switch (sd_state) { 00607 case 1: 00608 pc.printf("SD Card not inserted!\n\r"); 00609 pc.printf("Insert SD Card and press User button again\n\r"); 00610 break; 00611 default: 00612 pc.printf("This should never happen\n\r"); 00613 break; 00614 case 0: 00615 pc.printf("Initalising SD Card\n\r"); 00616 00617 //check init 00618 if (sd.init() != 0) { 00619 pc.printf(" ERROR - SD card failed to initialise.\n\rRestart board\n\r PROBABLY wires come out\n\r"); 00620 } else { 00621 // Create Filing system for SD Card 00622 FATFileSystem fs("sd", &sd); 00623 00624 //OpenFiles to write/append to 00625 pc.printf("Writing to SDC\n\r"); 00626 00627 FILE* fp = fopen("/sd/TheChamberOfSecrets.txt", "a"); //"w" to overwrite file ftb 00628 00629 // Check for error in opening file 00630 if (fp == NULL) { 00631 pc.printf("*****ERROR - Could not open file for write*****\n\r"); 00632 } 00633 //HERE IS WHERE TO PRINT DATA TO SD CARD FROM BUFFER (REMEMBER TO EMPTY BUFFER???) 00634 //Lock data buffer 00635 DataBuffer.lock(); 00636 dataLock.lock(); 00637 //Print all samples to SD 00638 for(int n=data_t; n<=MAX_SAMPLES; n++) { 00639 fputs(data_buffer[n], fp); 00640 fprintf(fp, "\n\r"); 00641 } 00642 if(data_t>data_h) { 00643 for(int n=0; n<=(data_t-1); n++) { 00644 fputs(data_buffer[n], fp); 00645 00646 } 00647 } 00648 00649 //Lock data buffer 00650 DataBuffer.unlock(); 00651 dataLock.unlock(); 00652 //fprintf(fp, "dd/mm/yy hh:mm:ss, TEMPERATURE, PRESSURE, LIGHT\n\r"); 00653 00654 fclose(fp); 00655 00656 pc.printf("Write Sucessful!\n\r"); 00657 00658 sd.deinit(); 00659 pc.printf("SD Card Ready to Remove\n\r"); 00660 Green_ext = 1; 00661 Thread::wait(500); 00662 Green_ext = 0; 00663 Thread::wait(500); 00664 Green_ext = 1; 00665 Thread::wait(500); 00666 Green_ext = 0; 00667 Thread::wait(500); 00668 Green_ext = 1; 00669 Thread::wait(500); 00670 Green_ext = 0; 00671 Thread::wait(500); 00672 Green_ext = 1; 00673 Thread::wait(500); 00674 Green_ext = 0; 00675 Thread::wait(500); 00676 }// end if 00677 }//End Switch 00678 }// End While 00679 }// End Thread 00680 /*--------------------------------------------------------------------------*/ 00681 00682 /*---------------------------Networking Thread------------------------------*/ 00683 void Network1 () 00684 { 00685 00686 printf("Setting up server\n\r"); 00687 00688 //Configure an ethernet connection 00689 EthernetInterface eth; 00690 eth.set_network(IP, NETMASK, GATEWAY); 00691 eth.connect(); 00692 if (eth.get_ip_address() == NULL) { 00693 pc.printf("Error - Can't get IP. Network not setup\n\r"); 00694 pc.printf("Reset Required. Make sure network cables are plugged in\n\r"); 00695 } else { 00696 printf("The target IP address is '%s'\n\r", eth.get_ip_address()); 00697 00698 //Now setup a web server 00699 TCPServer srv; //TCP/IP Server 00700 00701 SocketAddress clt_addr; //Address of incoming connection 00702 00703 /* Open the server on ethernet stack */ 00704 srv.open(ð); 00705 00706 /* Bind the HTTP port (TCP 80) to the server */ 00707 srv.bind(eth.get_ip_address(), 80); 00708 00709 /* Can handle 5 simultaneous connections */ 00710 srv.listen(5); 00711 00712 TCPSocket clt_sock; //Socket for communication 00713 00714 pc.printf("\rServer Ready\n\r"); 00715 00716 while (true) { 00717 00718 using namespace std; 00719 //Block and wait on an incoming connection 00720 srv.accept(&clt_sock, &clt_addr); 00721 //printf("accept %s:%d\n\r", clt_addr.get_ip_address(), clt_addr.get_port()); 00722 00723 //Uses a C++ string to make it easier to concatinate 00724 string response; 00725 string strL = "LDR:"; 00726 string strP = ", Pressure(mBar): "; 00727 string strT = ", Temp(C): "; 00728 string strDT ="Date/Time: ,"; 00729 00730 //This is a C string 00731 00732 char X_str[128]; 00733 00734 /*//Read the LDR value 00735 dataLock.lock(); // add watchdog? 00736 float L = LDR ; 00737 float T = TEMP; 00738 float P = PRES; 00739 char DT[21]; 00740 strcpy((char*)TIME,DT); 00741 dataLock.unlock(); 00742 00743 //Convert to a C String 00744 //sprintf(l_str, "%1.3f", L ); // try \n\r?? 00745 //sprintf(t_str, "%2.2f", T); 00746 //sprintf(p_str, "%4.2f", P);*/ 00747 osEvent evt = mail_Network.get(); 00748 00749 if (evt.status == osEventMail) { 00750 mail_t *mail = (mail_t*)evt.value.p; 00751 char sample_time[21]; 00752 int rTime = mail->Time_Date; 00753 time((time_t*)rTime); 00754 sample_epoch = localtime((time_t*)rTime); 00755 strftime(sample_time, 21,"%d/%m/%Y, %X", sample_epoch); 00756 sprintf(X_str,"%s, \n\rTemperature: %2.2f, \n\rPressure: %4.2f, \n\rLight: %.4f", sample_time, mail->temp_Value, mail->press_Value, mail->LDR_Value); 00757 00758 mail_Network.free(mail); 00759 } 00760 00761 00762 00763 //Build the C++ string response 00764 response = HTTP_MESSAGE_BODY1; 00765 response += X_str; 00766 /* response += strL; 00767 response += strDT; 00768 response += strL; 00769 response += l_str; 00770 response += strT; 00771 response += t_str; 00772 response += strP; 00773 response += p_str;*/ 00774 response += HTTP_MESSAGE_BODY2; 00775 00776 //Send static HTML response (as a C string) 00777 clt_sock.send(response.c_str(), response.size()+6); 00778 00779 }// end While 00780 }//end if 00781 }// end thread 00782 /*---------------------------POST--------------------------------------------*/ 00783 void POST () 00784 { 00785 pc.printf(" Basic POST\n\r"); 00786 pc.printf(" ALL Leds should be flashing\n\r"); 00787 00788 for(unsigned int n = 0; n<10; n++) { 00789 Green_int = ON; 00790 Blue_int = ON; 00791 Red_int = ON; 00792 Green_ext = ON; 00793 Yellow_ext = ON; 00794 Red_ext = ON; 00795 00796 wait (0.2); 00797 Green_int = OFF; 00798 Blue_int = OFF; 00799 Red_int = OFF; 00800 Green_ext = OFF; 00801 Yellow_ext = OFF; 00802 Red_ext = OFF; 00803 wait (0.2); 00804 } 00805 00806 pc.printf("Switch states:\n\r"); 00807 pc.printf("\tSW_L: %d\n\r\tSW_R %d\n\r", SW_L.read(), SW_R.read()); 00808 00809 float Temp = Sensor.getTemperature(); 00810 float Pres = Sensor.getPressure(); 00811 float ldrs = LDR_In.read(); 00812 00813 pc.printf("Sensor test:\n\r"); 00814 pc.printf("T: %f\tP: %f\tL: %f\n\r",Temp,Pres,ldrs); 00815 00816 pc.printf("LCD Test\n\r"); 00817 00818 lcd.Clear(); 00819 lcd.RowSelect(0); 00820 lcd.Write("1******LCD*********1"); 00821 lcd.RowSelect(1); 00822 lcd.Write("2******TEST********2"); 00823 lcd.RowSelect(2); 00824 lcd.Write("3******LCD*********3"); 00825 lcd.RowSelect(3); 00826 lcd.Write("4******TEST********4"); 00827 00828 wait(1); 00829 lcd.Clear(); 00830 pc.printf("Basic POST end\n\r"); 00831 }
Generated on Tue Jul 19 2022 13:19:00 by
