Ernest Williams / Mbed 2 deprecated Bluetooth

Dependencies:   mbed Motor Servo

Fork of SerialPassthrough_LPC1768 by jim hamblen

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include <mpr121.h>
00003 #include <string>
00004 #include "Shiftbrite.h"
00005 #include "Servo.h"
00006 #include "Motor.h"
00007 
00008 //Print output to pc via usb
00009 RawSerial  pc(USBTX, USBRX);
00010 //Print output to Bluetooth device
00011 RawSerial  dev(p13,p14);
00012 Serial esp(p9, p10); // tx, rx
00013 //LEDs
00014 DigitalOut led1(LED1);
00015 DigitalOut led2(LED2);
00016 DigitalOut led3(LED3);
00017 DigitalOut led4(LED4);
00018 Motor doorPulley(p22, p5, p6); // pwm, fwd, rev
00019 Motor mailFeed(p21, p11, p12); // pwm, fwd, rev
00020 Servo lock(p24);
00021 AnalogIn   ds(p16);
00022 //Interrupt for Touchpad
00023 InterruptIn interrupt(p26);
00024 //I2C for Touchpad 
00025 I2C i2c(p28, p27);
00026 //Touchpad 
00027 Mpr121 mpr121(&i2c, Mpr121::ADD_VSS);
00028 //Shiftbrite
00029 
00030 //Code Combinations
00031 int code1[] = {1,1,1,1};
00032 int code2[] = {2,2,2,2};
00033 int code3[] = {8,8,8,8};//Key 4 (labeled 3) on touchpad
00034 
00035 volatile int input[4]; //stores input values
00036 volatile int inNdx = 0;//to place combination in input
00037 bool codeIn = false; //determine if code is being entered via bluetooth
00038 
00039 //Webserver variables
00040 char gotMail[10];
00041 char audio1[255];
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 int DataRX;
00056 int update;
00057 int count;
00058 char cmdbuff[1024];
00059 char replybuff[4096];
00060 char webdata[4096]; // This may need to be bigger depending on WEB browser used
00061 char webbuff[4096];     // Currently using 1986 characters, Increase this if more web page data added
00062 char timebuf[30];
00063 void SendCMD(),getreply(),ReadWebData(),startserver();
00064 void gettime(),setRTC(), getDS();
00065 char rx_line[1024];
00066 int port        =80;  // set server port
00067 int SERVtimeout =5;    // set server timeout in seconds in case link breaks.
00068 struct tm t;
00069 float DSin = 0.0;
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 
00077 
00078 //Interrupt method to read from touchpad
00079 void fallInterrupt() {
00080        int key_code=0;
00081        int i = 0;
00082     int value=mpr121.read(0x00);
00083     value +=mpr121.read(0x01)<<8;
00084     //output value to onboard LEDs
00085     for (i=0; i<12; i++) {
00086         if (((value>>i)&0x01)==1) key_code=i+1;
00087         }
00088     led4=key_code & 0x01;
00089     led3=(key_code>>1) & 0x01;
00090     led2=(key_code>>2) & 0x01;
00091     led1=(key_code>>3) & 0x01;
00092     //ignore second character and input value into inputNdx
00093     if (value > 0){
00094     input[inNdx] = value;
00095     inNdx++;  
00096     }
00097 
00098   
00099     
00100 }
00101 
00102 //receive value from bluetooth device
00103 void dev_recv()
00104 {
00105     string temp ="";
00106     led1 = !led1;
00107     while(dev.readable()) {
00108         //pc.putc(dev.getc());
00109         if(!codeIn){
00110             if(dev.getc() == 'a'){
00111                 dev.printf("Send code 4 digit code.\n"); 
00112                 inNdx = 0;
00113                 codeIn = true;
00114                // myShiftbrite.write(0,0,255);
00115             } 
00116             else {dev.printf("Command not recognized");}
00117         } else {
00118         //    pc.printf("BT entered: %c\n", dev.getc());
00119             input[inNdx] = dev.getc() - '0';
00120             if(input[inNdx] > -1){  
00121             dev.printf("Just entered: %d", input[inNdx]);  
00122             inNdx++;}
00123             //break;
00124         }
00125     }
00126     
00127 }
00128 //receive value from PC
00129 void pc_recv()
00130 {
00131     led4 = !led4;
00132     while(pc.readable()) {
00133         dev.putc(pc.getc());
00134     }
00135 }
00136 
00137 int main()
00138 {
00139     lock = 0.3;
00140     //reset Shiftbrite
00141     pc.baud(9600);
00142     dev.baud(9600);
00143     esp.baud(9600);
00144     led1=1,led2=0,led3=0,led4=0;
00145     // Setup a serial interrupt function to receive data
00146     esp.attach(&Rx_interrupt, Serial::RxIrq);
00147     // Setup a serial interrupt function to transmit data
00148     esp.attach(&Tx_interrupt, Serial::TxIrq);
00149     if (time(NULL) < 1420070400) {
00150         setRTC();
00151     }
00152     startserver();
00153     DataRX=0;
00154     count=0;
00155     //Set up interrupt
00156     interrupt.fall(&fallInterrupt);
00157     interrupt.mode(PullUp);
00158     //Set up read methods bluetooth and PC
00159     pc.attach(&pc_recv, Serial::RxIrq);
00160     dev.attach(&dev_recv, Serial::RxIrq);
00161     
00162     dev.printf("Monitoring Locked Device\n");
00163     dev.printf("To unlock the device remotely, send 'a'.\n");
00164 
00165     while(1) {
00166          if(DataRX==1) {
00167             ReadWebData();
00168             esp.attach(&Rx_interrupt, Serial::RxIrq);
00169         }
00170         if(update==1) // update time, hit count, and analog levels in the HUZZAH chip
00171         {
00172             // get new values
00173             gettime();
00174             getDS();
00175             count++;
00176             // send new values
00177             pc.printf("\n\n\n\n%f\r\n\n\n\n", DSin );
00178             if (DSin <= 0.5) {
00179                 strcpy(gotMail, "yes");
00180                 strcpy(audio1, "http://192.168.43.91/img/youGotmail.mp3");
00181                 sprintf(cmdbuff, "count,time,yesorno, audio1=%d,\"%s\",\"%s\",\"%s\"\r\n",count,timebuf,gotMail, audio1);
00182                 SendCMD();
00183                 getreply();
00184                // continue;
00185             } else {
00186                 strcpy(gotMail, "no");
00187                 strcpy(audio1, "#");
00188             }
00189             sprintf(cmdbuff, "count,time,yesorno,audio1=%d,\"%s\",\"%s\",\"%s\"\r\n",count,timebuf,gotMail, audio1);
00190             SendCMD();
00191             getreply();
00192             update=0;   
00193         }
00194         //If four characters have been entered, attempt to unlock
00195         if (inNdx > 3) {
00196             dev.printf("Attempting to Unlock\n");
00197             codeIn = false;
00198             bool c1 = true; bool c2 = true; bool c3 = true;
00199             //check entered code against saved codes
00200             for(int x = 0; x <= 3; x++){
00201                 //if no code matches, end loop
00202                 if(!(c1 | c2 | c3)) {
00203                     break;    
00204                 } else {
00205                     //compare codes
00206                     if (code1[x] != input[x]){c1 = false;}
00207                     if (code2[x] != input[x]){c2 = false;}
00208                     if (code3[x] != input[x]){c3 = false;}
00209                     
00210                 }   
00211             }
00212             if(c1){
00213                 //Send welcome user 1 to bluetooth
00214                 dev.printf("Welcome User 1\n");
00215                 lock = 1.0;
00216                 wait(1.0);
00217                 doorPulley.speed(-0.8);
00218                 wait(2.0);
00219                 doorPulley.speed(0.0);
00220                 wait(.3);
00221                 mailFeed.speed(-0.8);
00222                 wait(2);
00223                 mailFeed.speed(0.0);
00224                 doorPulley.speed(0.3);
00225                 wait(1.0);
00226                 doorPulley.speed(0.0);
00227             } else if(c2) {
00228                 //Send welcome user 2 to bluetooth
00229                 dev.printf("Welcome User 2\n");
00230                 lock = 1.0;
00231                 wait(1.0);
00232                 doorPulley.speed(-0.8);
00233                 wait(2.0);
00234                 doorPulley.speed(0.0);
00235                 wait(.3);
00236                 mailFeed.speed(-0.8);
00237                 wait(2);
00238                 mailFeed.speed(0.0);
00239                 doorPulley.speed(0.3);
00240                 wait(1.0);
00241                 doorPulley.speed(0.0);
00242                 doorPulley.speed(.3);
00243                 wait(.8);
00244                 doorPulley.speed(0.0);
00245                 
00246 
00247             } else if(c3) {
00248                 //Send welcome user 3 to bluetooth
00249                 dev.printf("Welcome User 3\n");
00250                 lock = 1.0;
00251                 wait(1.0);
00252                 doorPulley.speed(-0.8);
00253                 wait(2.0);
00254                 doorPulley.speed(0.0);
00255                 wait(.3);
00256                 mailFeed.speed(-0.8);
00257                 wait(2);
00258                 mailFeed.speed(0.0);
00259                 doorPulley.speed(0.3);
00260                 wait(1.0);
00261                 doorPulley.speed(0.0);
00262 
00263             } else {
00264                 //Send welcome Try Again message to bluetooth
00265                 dev.printf("Try Again\n");
00266                 lock = 0.3;
00267 
00268  
00269             }
00270             //reset indexing variable when checking is complete
00271             inNdx = 0;
00272             
00273         }
00274     }
00275     
00276 }
00277 // Reads and processes GET and POST web data
00278 void ReadWebData()
00279 {
00280     wait_ms(200);
00281     esp.attach(NULL,Serial::RxIrq);
00282     DataRX=0;
00283     memset(webdata, '\0', sizeof(webdata));
00284     strcpy(webdata, rx_buffer);
00285     memset(rx_buffer, '\0', sizeof(rx_buffer));
00286     rx_in = 0;
00287     rx_out = 0;
00288     // check web data for form information
00289     if( strstr(webdata, "check=led1v") != NULL ) {
00290         led1=!led1;
00291         DSin = 0;
00292     }
00293     if( strstr(webdata, "check=led2v") != NULL ) {
00294         led2=!led2;
00295     }
00296     if( strstr(webdata, "check=led3v") != NULL ) {
00297         led3=!led3;
00298     }
00299     if( strstr(webdata, "check=led4v") != NULL ) {
00300         led4=!led4;
00301     }
00302     if( strstr(webdata, "POST") != NULL ) { // set update flag if POST request
00303         update=1;
00304     }
00305     if( strstr(webdata, "GET") != NULL && strstr(webdata, "favicon.ico") == NULL ) { // set update flag for GET request but do not want to update for favicon requests
00306         update=1;
00307     }
00308 }
00309 // Starts webserver
00310 void startserver()
00311 {
00312     gettime();
00313     strcpy(audio1, "#");
00314     pc.printf("++++++++++ Resetting ESP ++++++++++\r\n");
00315     strcpy(cmdbuff,"node.restart()\r\n");
00316     SendCMD();
00317     wait(2);
00318     getreply();
00319     
00320     pc.printf("\n++++++++++ Starting Server ++++++++++\r\n> ");
00321 
00322     // initial values
00323     sprintf(cmdbuff, "count,time,yesorno,audio1=0,\"%s\",\"%s\",\"%s\"\r\n",timebuf,gotMail, audio1);
00324     SendCMD();
00325     getreply();
00326     wait(0.5);
00327 
00328     //create server
00329     sprintf(cmdbuff, "srv=net.createServer(net.TCP,%d)\r\n",SERVtimeout);
00330     SendCMD();
00331     getreply();
00332     wait(0.5);
00333     strcpy(cmdbuff,"srv:listen(80,function(conn)\r\n");
00334     SendCMD();
00335     getreply();
00336     wait(0.3);
00337         strcpy(cmdbuff,"conn:on(\"receive\",function(conn,payload) \r\n");
00338         SendCMD();
00339         getreply();
00340         wait(0.3);
00341         
00342         //print data to mbed
00343         strcpy(cmdbuff,"print(payload)\r\n");
00344         SendCMD();
00345         getreply();
00346         wait(0.2);
00347        
00348         //web page data
00349         strcpy(cmdbuff,"conn:send('<!DOCTYPE html><html><style> h1 {background: #00000;} </style>')\r\n");
00350         SendCMD();
00351         getreply();
00352         wait(0.4);
00353         strcpy(cmdbuff, "conn:send('<body><h1>Huzzah Mail Receive Notification Center</h1>')\r\n");
00354         SendCMD();
00355         getreply();
00356         wait(0.4);
00357         strcpy(cmdbuff,"conn:send('Hit count: '..count..'')\r\n");
00358         SendCMD();
00359         getreply();
00360         wait(0.2);
00361         strcpy(cmdbuff,"conn:send('<br>Last hit (based on mbed RTC time): '..time..'<br><hr>')\r\n");
00362         SendCMD();
00363         getreply();
00364         wait(0.4);
00365         strcpy(cmdbuff,"conn:send('<h2>Do you have mail?: '..yesorno..' </h2>')\r\n");
00366         SendCMD();
00367         getreply();
00368         wait(0.3);
00369         strcpy(cmdbuff, "conn:send('<audio autoplay src= '..audio1..' ></audio><hr>')\r\n");
00370         SendCMD();
00371         getreply();
00372         wait(0.4);
00373 
00374 
00375 
00376         strcpy(cmdbuff,"conn:send('<form name=\"form1\" method=\"POST\"')\r\n");
00377         SendCMD();
00378         getreply();
00379         wait(0.3);
00380         strcpy(cmdbuff, "conn:send('<p><input type=\"checkbox\" name=\"check\" value=\"led1v\"> Unlock mailbox?')\r\n");
00381         SendCMD();
00382         getreply();
00383         wait(0.3);
00384         strcpy(cmdbuff, "conn:send('<p><input type=\"checkbox\" name=\"check\" value=\"led2v\"> flip LED2')\r\n");
00385         SendCMD();
00386         getreply();
00387         wait(0.3);
00388         strcpy(cmdbuff, "conn:send('<p><input type=\"checkbox\" name=\"check\" value=\"led3v\"> flip LED3')\r\n");
00389         SendCMD();
00390         getreply();
00391         wait(0.3);
00392         strcpy(cmdbuff, "conn:send('<p><input type=\"checkbox\" name=\"check\" value=\"led4v\"> flip LED4')\r\n");
00393         SendCMD();
00394         getreply();
00395         wait(0.3);
00396         strcpy(cmdbuff,"conn:send('<p><input type=\"submit\" value=\"Check Mail\"></form>')\r\n");
00397         SendCMD();
00398         getreply();
00399         wait(0.3);
00400         strcpy(cmdbuff, "conn:send('<script type=\"text/javascript\"> window.onload=function(){window.setTimeout(\"document.form1.submit();\", 5000);}</script>')\r\n");
00401         SendCMD();
00402         wait(0.4);
00403         strcpy(cmdbuff, "conn:send('<p><h2>How to use:</h2><ul><li>Data will automatically update every 5 seconds</li><li>Click Check Mail to manually send data and refresh values</li></ul></body></html>')\r\n");
00404         SendCMD();
00405         getreply();
00406         wait(0.5); 
00407         // end web page data
00408         strcpy(cmdbuff, "conn:on(\"sent\",function(conn) conn:close() end)\r\n"); // close current connection
00409         SendCMD();
00410         getreply();
00411         wait(0.3);
00412         strcpy(cmdbuff, "end)\r\n");
00413         SendCMD();
00414         getreply();
00415         wait(0.2);
00416     strcpy(cmdbuff, "end)\r\n");
00417     SendCMD();
00418     getreply();
00419     wait(0.2);
00420 
00421     strcpy(cmdbuff, "tmr.alarm(0, 1000, 1, function()\r\n");
00422     SendCMD();
00423     getreply();
00424     wait(0.2);
00425     strcpy(cmdbuff, "if wifi.sta.getip() == nil then\r\n");
00426     SendCMD();
00427     getreply();
00428     wait(0.2);
00429     strcpy(cmdbuff, "print(\"Connecting to AP...\\n\")\r\n");
00430     SendCMD();
00431     getreply();
00432     wait(0.2);
00433     strcpy(cmdbuff, "else\r\n");
00434     SendCMD();
00435     getreply();
00436     wait(0.2);
00437     strcpy(cmdbuff, "ip, nm, gw=wifi.sta.getip()\r\n");
00438     SendCMD();
00439     getreply();
00440     wait(0.2);
00441     strcpy(cmdbuff,"print(\"IP Address: \",ip)\r\n");
00442     SendCMD();
00443     getreply();
00444     wait(0.2);
00445     strcpy(cmdbuff,"tmr.stop(0)\r\n");
00446     SendCMD();
00447     getreply();
00448     wait(0.2);
00449     strcpy(cmdbuff,"end\r\n");
00450     SendCMD();
00451     getreply();
00452     wait(0.2);
00453     strcpy(cmdbuff,"end)\r\n");
00454     SendCMD();
00455     getreply();
00456     wait(0.2);
00457     
00458     pc.printf("\n\n++++++++++ Ready ++++++++++\r\n\n");
00459 }
00460 
00461 
00462 // ESP Command data send
00463 void SendCMD()
00464 {
00465     int i;
00466     char temp_char;
00467     bool empty;
00468     i = 0;
00469 // Start Critical Section - don't interrupt while changing global buffer variables
00470     NVIC_DisableIRQ(UART1_IRQn);
00471     empty = (tx_in == tx_out);
00472     while ((i==0) || (cmdbuff[i-1] != '\n')) {
00473 // Wait if buffer full
00474         if (((tx_in + 1) % buffer_size) == tx_out) {
00475 // End Critical Section - need to let interrupt routine empty buffer by sending
00476             NVIC_EnableIRQ(UART1_IRQn);
00477             while (((tx_in + 1) % buffer_size) == tx_out) {
00478             }
00479 // Start Critical Section - don't interrupt while changing global buffer variables
00480             NVIC_DisableIRQ(UART1_IRQn);
00481         }
00482         tx_buffer[tx_in] = cmdbuff[i];
00483         i++;
00484         tx_in = (tx_in + 1) % buffer_size;
00485     }
00486     if (esp.writeable() && (empty)) {
00487         temp_char = tx_buffer[tx_out];
00488         tx_out = (tx_out + 1) % buffer_size;
00489 // Send first character to start tx interrupts, if stopped
00490         esp.putc(temp_char);
00491     }
00492 // End Critical Section
00493     NVIC_EnableIRQ(UART1_IRQn);
00494     return;
00495 }
00496 
00497 // Get Command and ESP status replies
00498 void getreply()
00499 {
00500     read_line();
00501     sscanf(rx_line,replybuff);
00502 }
00503  
00504 // Read a line from the large rx buffer from rx interrupt routine
00505 void read_line() {
00506     int i;
00507     i = 0;
00508 // Start Critical Section - don't interrupt while changing global buffer variables
00509     NVIC_DisableIRQ(UART1_IRQn);
00510 // Loop reading rx buffer characters until end of line character
00511     while ((i==0) || (rx_line[i-1] != '\r')) {
00512 // Wait if buffer empty
00513         if (rx_in == rx_out) {
00514 // End Critical Section - need to allow rx interrupt to get new characters for buffer
00515             NVIC_EnableIRQ(UART1_IRQn);
00516             while (rx_in == rx_out) {
00517             }
00518 // Start Critical Section - don't interrupt while changing global buffer variables
00519             NVIC_DisableIRQ(UART1_IRQn);
00520         }
00521         rx_line[i] = rx_buffer[rx_out];
00522         i++;
00523         rx_out = (rx_out + 1) % buffer_size;
00524     }
00525 // End Critical Section
00526     NVIC_EnableIRQ(UART1_IRQn);
00527     rx_line[i-1] = 0;
00528     return;
00529 }
00530  
00531  
00532 // Interupt Routine to read in data from serial port
00533 void Rx_interrupt() {
00534     DataRX=1;
00535     //getDS();
00536     //led3=1;
00537 // Loop just in case more than one character is in UART's receive FIFO buffer
00538 // Stop if buffer full
00539     while ((esp.readable()) && (((rx_in + 1) % buffer_size) != rx_out)) {
00540         rx_buffer[rx_in] = esp.getc();
00541 // Uncomment to Echo to USB serial to watch data flow
00542         pc.putc(rx_buffer[rx_in]);
00543         rx_in = (rx_in + 1) % buffer_size;
00544     }
00545     //led3=0;
00546     return;
00547 }
00548  
00549  
00550 // Interupt Routine to write out data to serial port
00551 void Tx_interrupt() {
00552     //led2=1;
00553 // Loop to fill more than one character in UART's transmit FIFO buffer
00554 // Stop if buffer empty
00555     while ((esp.writeable()) && (tx_in != tx_out)) {
00556         esp.putc(tx_buffer[tx_out]);
00557         tx_out = (tx_out + 1) % buffer_size;
00558     }
00559     //led2=0;
00560     return;
00561 }
00562 
00563 void gettime()
00564 {
00565     time_t seconds = time(NULL);
00566     strftime(timebuf,50,"%H:%M:%S %a %d %b %y", localtime(&seconds));
00567 }
00568 
00569 void setRTC()
00570 {
00571     t.tm_sec = (0);             // 0-59
00572     t.tm_min = (minute);        // 0-59
00573     t.tm_hour = (hour);         // 0-23
00574     t.tm_mday = (dayofmonth);   // 1-31
00575     t.tm_mon = (month-1);       // 0-11  "0" = Jan, -1 added for Mbed RCT clock format
00576     t.tm_year = ((year)+100);   // year since 1900,  current DCF year + 100 + 1900 = correct year
00577     set_time(mktime(&t));       // set RTC clock
00578 }
00579 
00580 void getDS()
00581 {
00582     DSin = ds.read();
00583 }