The purpose of this project was to create a system that would allow users to monitor a locked device using a Bluetooth device. This Bluetooth device will show the last user that unlocked the device, and also allows the user to unlock the device using the Bluetooth device. This device can be physically unlocked using a capacitive touch keypad sensor.
Dependencies: mbed Motor Servo
Fork of SerialPassthrough_LPC1768 by
main.cpp@9:c216d7b63f92, 2016-04-30 (annotated)
- Committer:
- ewilliams61
- Date:
- Sat Apr 30 19:47:54 2016 +0000
- Revision:
- 9:c216d7b63f92
- Parent:
- 8:39172c01c0f1
Final working code for IOT mailbox
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mbedAustin | 0:59bec1fd956e | 1 | #include "mbed.h" |
ewilliams61 | 7:79d0b30fedb4 | 2 | #include <mpr121.h> |
ewilliams61 | 7:79d0b30fedb4 | 3 | #include <string> |
ewilliams61 | 7:79d0b30fedb4 | 4 | #include "Shiftbrite.h" |
ewilliams61 | 9:c216d7b63f92 | 5 | #include "Servo.h" |
ewilliams61 | 9:c216d7b63f92 | 6 | #include "Motor.h" |
ewilliams61 | 9:c216d7b63f92 | 7 | |
ewilliams61 | 8:39172c01c0f1 | 8 | //Print output to pc via usb |
sam_grove | 5:96cb82af9996 | 9 | RawSerial pc(USBTX, USBRX); |
ewilliams61 | 8:39172c01c0f1 | 10 | //Print output to Bluetooth device |
ewilliams61 | 9:c216d7b63f92 | 11 | RawSerial dev(p13,p14); |
ewilliams61 | 9:c216d7b63f92 | 12 | Serial esp(p9, p10); // tx, rx |
ewilliams61 | 8:39172c01c0f1 | 13 | //LEDs |
sam_grove | 5:96cb82af9996 | 14 | DigitalOut led1(LED1); |
ewilliams61 | 7:79d0b30fedb4 | 15 | DigitalOut led2(LED2); |
ewilliams61 | 7:79d0b30fedb4 | 16 | DigitalOut led3(LED3); |
ewilliams61 | 8:39172c01c0f1 | 17 | DigitalOut led4(LED4); |
ewilliams61 | 9:c216d7b63f92 | 18 | Motor doorPulley(p22, p5, p6); // pwm, fwd, rev |
ewilliams61 | 9:c216d7b63f92 | 19 | Motor mailFeed(p21, p11, p12); // pwm, fwd, rev |
ewilliams61 | 9:c216d7b63f92 | 20 | Servo lock(p24); |
ewilliams61 | 9:c216d7b63f92 | 21 | AnalogIn ds(p16); |
ewilliams61 | 8:39172c01c0f1 | 22 | //Interrupt for Touchpad |
ewilliams61 | 8:39172c01c0f1 | 23 | InterruptIn interrupt(p26); |
ewilliams61 | 8:39172c01c0f1 | 24 | //I2C for Touchpad |
ewilliams61 | 8:39172c01c0f1 | 25 | I2C i2c(p28, p27); |
ewilliams61 | 8:39172c01c0f1 | 26 | //Touchpad |
ewilliams61 | 8:39172c01c0f1 | 27 | Mpr121 mpr121(&i2c, Mpr121::ADD_VSS); |
ewilliams61 | 8:39172c01c0f1 | 28 | //Shiftbrite |
ewilliams61 | 8:39172c01c0f1 | 29 | |
ewilliams61 | 8:39172c01c0f1 | 30 | //Code Combinations |
ewilliams61 | 7:79d0b30fedb4 | 31 | int code1[] = {1,1,1,1}; |
ewilliams61 | 7:79d0b30fedb4 | 32 | int code2[] = {2,2,2,2}; |
ewilliams61 | 8:39172c01c0f1 | 33 | int code3[] = {8,8,8,8};//Key 4 (labeled 3) on touchpad |
ewilliams61 | 7:79d0b30fedb4 | 34 | |
ewilliams61 | 8:39172c01c0f1 | 35 | volatile int input[4]; //stores input values |
ewilliams61 | 8:39172c01c0f1 | 36 | volatile int inNdx = 0;//to place combination in input |
ewilliams61 | 8:39172c01c0f1 | 37 | bool codeIn = false; //determine if code is being entered via bluetooth |
ewilliams61 | 7:79d0b30fedb4 | 38 | |
ewilliams61 | 9:c216d7b63f92 | 39 | //Webserver variables |
ewilliams61 | 9:c216d7b63f92 | 40 | char gotMail[10]; |
ewilliams61 | 9:c216d7b63f92 | 41 | char audio1[255]; |
ewilliams61 | 9:c216d7b63f92 | 42 | |
ewilliams61 | 9:c216d7b63f92 | 43 | // things for sending/receiving data over serial |
ewilliams61 | 9:c216d7b63f92 | 44 | volatile int tx_in=0; |
ewilliams61 | 9:c216d7b63f92 | 45 | volatile int tx_out=0; |
ewilliams61 | 9:c216d7b63f92 | 46 | volatile int rx_in=0; |
ewilliams61 | 9:c216d7b63f92 | 47 | volatile int rx_out=0; |
ewilliams61 | 9:c216d7b63f92 | 48 | const int buffer_size = 4095; |
ewilliams61 | 9:c216d7b63f92 | 49 | char tx_buffer[buffer_size+1]; |
ewilliams61 | 9:c216d7b63f92 | 50 | char rx_buffer[buffer_size+1]; |
ewilliams61 | 9:c216d7b63f92 | 51 | void Tx_interrupt(); |
ewilliams61 | 9:c216d7b63f92 | 52 | void Rx_interrupt(); |
ewilliams61 | 9:c216d7b63f92 | 53 | void send_line(); |
ewilliams61 | 9:c216d7b63f92 | 54 | void read_line(); |
ewilliams61 | 9:c216d7b63f92 | 55 | int DataRX; |
ewilliams61 | 9:c216d7b63f92 | 56 | int update; |
ewilliams61 | 9:c216d7b63f92 | 57 | int count; |
ewilliams61 | 9:c216d7b63f92 | 58 | char cmdbuff[1024]; |
ewilliams61 | 9:c216d7b63f92 | 59 | char replybuff[4096]; |
ewilliams61 | 9:c216d7b63f92 | 60 | char webdata[4096]; // This may need to be bigger depending on WEB browser used |
ewilliams61 | 9:c216d7b63f92 | 61 | char webbuff[4096]; // Currently using 1986 characters, Increase this if more web page data added |
ewilliams61 | 9:c216d7b63f92 | 62 | char timebuf[30]; |
ewilliams61 | 9:c216d7b63f92 | 63 | void SendCMD(),getreply(),ReadWebData(),startserver(); |
ewilliams61 | 9:c216d7b63f92 | 64 | void gettime(),setRTC(), getDS(); |
ewilliams61 | 9:c216d7b63f92 | 65 | char rx_line[1024]; |
ewilliams61 | 9:c216d7b63f92 | 66 | int port =80; // set server port |
ewilliams61 | 9:c216d7b63f92 | 67 | int SERVtimeout =5; // set server timeout in seconds in case link breaks. |
ewilliams61 | 9:c216d7b63f92 | 68 | struct tm t; |
ewilliams61 | 9:c216d7b63f92 | 69 | float DSin = 0.0; |
ewilliams61 | 9:c216d7b63f92 | 70 | // manual set RTC values |
ewilliams61 | 9:c216d7b63f92 | 71 | int minute =00; // 0-59 |
ewilliams61 | 9:c216d7b63f92 | 72 | int hour =12; // 2-23 |
ewilliams61 | 9:c216d7b63f92 | 73 | int dayofmonth =26; // 1-31 |
ewilliams61 | 9:c216d7b63f92 | 74 | int month =8; // 1-12 |
ewilliams61 | 9:c216d7b63f92 | 75 | int year =15; // last 2 digits |
ewilliams61 | 9:c216d7b63f92 | 76 | |
ewilliams61 | 8:39172c01c0f1 | 77 | |
ewilliams61 | 8:39172c01c0f1 | 78 | //Interrupt method to read from touchpad |
ewilliams61 | 7:79d0b30fedb4 | 79 | void fallInterrupt() { |
ewilliams61 | 7:79d0b30fedb4 | 80 | int key_code=0; |
ewilliams61 | 7:79d0b30fedb4 | 81 | int i = 0; |
ewilliams61 | 7:79d0b30fedb4 | 82 | int value=mpr121.read(0x00); |
ewilliams61 | 7:79d0b30fedb4 | 83 | value +=mpr121.read(0x01)<<8; |
ewilliams61 | 8:39172c01c0f1 | 84 | //output value to onboard LEDs |
ewilliams61 | 7:79d0b30fedb4 | 85 | for (i=0; i<12; i++) { |
ewilliams61 | 7:79d0b30fedb4 | 86 | if (((value>>i)&0x01)==1) key_code=i+1; |
ewilliams61 | 7:79d0b30fedb4 | 87 | } |
ewilliams61 | 7:79d0b30fedb4 | 88 | led4=key_code & 0x01; |
ewilliams61 | 7:79d0b30fedb4 | 89 | led3=(key_code>>1) & 0x01; |
ewilliams61 | 7:79d0b30fedb4 | 90 | led2=(key_code>>2) & 0x01; |
ewilliams61 | 7:79d0b30fedb4 | 91 | led1=(key_code>>3) & 0x01; |
ewilliams61 | 8:39172c01c0f1 | 92 | //ignore second character and input value into inputNdx |
ewilliams61 | 7:79d0b30fedb4 | 93 | if (value > 0){ |
ewilliams61 | 7:79d0b30fedb4 | 94 | input[inNdx] = value; |
ewilliams61 | 7:79d0b30fedb4 | 95 | inNdx++; |
ewilliams61 | 7:79d0b30fedb4 | 96 | } |
ewilliams61 | 7:79d0b30fedb4 | 97 | |
ewilliams61 | 7:79d0b30fedb4 | 98 | |
ewilliams61 | 7:79d0b30fedb4 | 99 | |
ewilliams61 | 7:79d0b30fedb4 | 100 | } |
mbedAustin | 2:a8dcb07a1d00 | 101 | |
ewilliams61 | 8:39172c01c0f1 | 102 | //receive value from bluetooth device |
sam_grove | 5:96cb82af9996 | 103 | void dev_recv() |
mbedAustin | 2:a8dcb07a1d00 | 104 | { |
ewilliams61 | 7:79d0b30fedb4 | 105 | string temp =""; |
sam_grove | 5:96cb82af9996 | 106 | led1 = !led1; |
sam_grove | 5:96cb82af9996 | 107 | while(dev.readable()) { |
ewilliams61 | 7:79d0b30fedb4 | 108 | //pc.putc(dev.getc()); |
ewilliams61 | 7:79d0b30fedb4 | 109 | if(!codeIn){ |
ewilliams61 | 7:79d0b30fedb4 | 110 | if(dev.getc() == 'a'){ |
ewilliams61 | 7:79d0b30fedb4 | 111 | dev.printf("Send code 4 digit code.\n"); |
ewilliams61 | 7:79d0b30fedb4 | 112 | inNdx = 0; |
ewilliams61 | 7:79d0b30fedb4 | 113 | codeIn = true; |
ewilliams61 | 7:79d0b30fedb4 | 114 | // myShiftbrite.write(0,0,255); |
ewilliams61 | 7:79d0b30fedb4 | 115 | } |
ewilliams61 | 7:79d0b30fedb4 | 116 | else {dev.printf("Command not recognized");} |
ewilliams61 | 7:79d0b30fedb4 | 117 | } else { |
ewilliams61 | 7:79d0b30fedb4 | 118 | // pc.printf("BT entered: %c\n", dev.getc()); |
ewilliams61 | 7:79d0b30fedb4 | 119 | input[inNdx] = dev.getc() - '0'; |
ewilliams61 | 7:79d0b30fedb4 | 120 | if(input[inNdx] > -1){ |
ewilliams61 | 7:79d0b30fedb4 | 121 | dev.printf("Just entered: %d", input[inNdx]); |
ewilliams61 | 7:79d0b30fedb4 | 122 | inNdx++;} |
ewilliams61 | 7:79d0b30fedb4 | 123 | //break; |
ewilliams61 | 7:79d0b30fedb4 | 124 | } |
sam_grove | 5:96cb82af9996 | 125 | } |
ewilliams61 | 7:79d0b30fedb4 | 126 | |
sam_grove | 5:96cb82af9996 | 127 | } |
ewilliams61 | 8:39172c01c0f1 | 128 | //receive value from PC |
sam_grove | 5:96cb82af9996 | 129 | void pc_recv() |
sam_grove | 5:96cb82af9996 | 130 | { |
sam_grove | 5:96cb82af9996 | 131 | led4 = !led4; |
sam_grove | 5:96cb82af9996 | 132 | while(pc.readable()) { |
mbedAustin | 4:ba9100d52e48 | 133 | dev.putc(pc.getc()); |
mbedAustin | 0:59bec1fd956e | 134 | } |
mbedAustin | 0:59bec1fd956e | 135 | } |
mbedAustin | 4:ba9100d52e48 | 136 | |
mbedAustin | 4:ba9100d52e48 | 137 | int main() |
mbedAustin | 4:ba9100d52e48 | 138 | { |
ewilliams61 | 9:c216d7b63f92 | 139 | lock = 0.3; |
ewilliams61 | 8:39172c01c0f1 | 140 | //reset Shiftbrite |
mbedAustin | 4:ba9100d52e48 | 141 | pc.baud(9600); |
mbedAustin | 4:ba9100d52e48 | 142 | dev.baud(9600); |
ewilliams61 | 9:c216d7b63f92 | 143 | esp.baud(9600); |
ewilliams61 | 9:c216d7b63f92 | 144 | led1=1,led2=0,led3=0,led4=0; |
ewilliams61 | 9:c216d7b63f92 | 145 | // Setup a serial interrupt function to receive data |
ewilliams61 | 9:c216d7b63f92 | 146 | esp.attach(&Rx_interrupt, Serial::RxIrq); |
ewilliams61 | 9:c216d7b63f92 | 147 | // Setup a serial interrupt function to transmit data |
ewilliams61 | 9:c216d7b63f92 | 148 | esp.attach(&Tx_interrupt, Serial::TxIrq); |
ewilliams61 | 9:c216d7b63f92 | 149 | if (time(NULL) < 1420070400) { |
ewilliams61 | 9:c216d7b63f92 | 150 | setRTC(); |
ewilliams61 | 9:c216d7b63f92 | 151 | } |
ewilliams61 | 9:c216d7b63f92 | 152 | startserver(); |
ewilliams61 | 9:c216d7b63f92 | 153 | DataRX=0; |
ewilliams61 | 9:c216d7b63f92 | 154 | count=0; |
ewilliams61 | 8:39172c01c0f1 | 155 | //Set up interrupt |
ewilliams61 | 7:79d0b30fedb4 | 156 | interrupt.fall(&fallInterrupt); |
ewilliams61 | 7:79d0b30fedb4 | 157 | interrupt.mode(PullUp); |
ewilliams61 | 8:39172c01c0f1 | 158 | //Set up read methods bluetooth and PC |
sam_grove | 5:96cb82af9996 | 159 | pc.attach(&pc_recv, Serial::RxIrq); |
sam_grove | 5:96cb82af9996 | 160 | dev.attach(&dev_recv, Serial::RxIrq); |
ewilliams61 | 8:39172c01c0f1 | 161 | |
ewilliams61 | 7:79d0b30fedb4 | 162 | dev.printf("Monitoring Locked Device\n"); |
ewilliams61 | 7:79d0b30fedb4 | 163 | dev.printf("To unlock the device remotely, send 'a'.\n"); |
sam_grove | 5:96cb82af9996 | 164 | |
mbedAustin | 4:ba9100d52e48 | 165 | while(1) { |
ewilliams61 | 9:c216d7b63f92 | 166 | if(DataRX==1) { |
ewilliams61 | 9:c216d7b63f92 | 167 | ReadWebData(); |
ewilliams61 | 9:c216d7b63f92 | 168 | esp.attach(&Rx_interrupt, Serial::RxIrq); |
ewilliams61 | 9:c216d7b63f92 | 169 | } |
ewilliams61 | 9:c216d7b63f92 | 170 | if(update==1) // update time, hit count, and analog levels in the HUZZAH chip |
ewilliams61 | 9:c216d7b63f92 | 171 | { |
ewilliams61 | 9:c216d7b63f92 | 172 | // get new values |
ewilliams61 | 9:c216d7b63f92 | 173 | gettime(); |
ewilliams61 | 9:c216d7b63f92 | 174 | getDS(); |
ewilliams61 | 9:c216d7b63f92 | 175 | count++; |
ewilliams61 | 9:c216d7b63f92 | 176 | // send new values |
ewilliams61 | 9:c216d7b63f92 | 177 | pc.printf("\n\n\n\n%f\r\n\n\n\n", DSin ); |
ewilliams61 | 9:c216d7b63f92 | 178 | if (DSin <= 0.5) { |
ewilliams61 | 9:c216d7b63f92 | 179 | strcpy(gotMail, "yes"); |
ewilliams61 | 9:c216d7b63f92 | 180 | strcpy(audio1, "http://192.168.43.91/img/youGotmail.mp3"); |
ewilliams61 | 9:c216d7b63f92 | 181 | sprintf(cmdbuff, "count,time,yesorno, audio1=%d,\"%s\",\"%s\",\"%s\"\r\n",count,timebuf,gotMail, audio1); |
ewilliams61 | 9:c216d7b63f92 | 182 | SendCMD(); |
ewilliams61 | 9:c216d7b63f92 | 183 | getreply(); |
ewilliams61 | 9:c216d7b63f92 | 184 | // continue; |
ewilliams61 | 9:c216d7b63f92 | 185 | } else { |
ewilliams61 | 9:c216d7b63f92 | 186 | strcpy(gotMail, "no"); |
ewilliams61 | 9:c216d7b63f92 | 187 | strcpy(audio1, "#"); |
ewilliams61 | 9:c216d7b63f92 | 188 | } |
ewilliams61 | 9:c216d7b63f92 | 189 | sprintf(cmdbuff, "count,time,yesorno,audio1=%d,\"%s\",\"%s\",\"%s\"\r\n",count,timebuf,gotMail, audio1); |
ewilliams61 | 9:c216d7b63f92 | 190 | SendCMD(); |
ewilliams61 | 9:c216d7b63f92 | 191 | getreply(); |
ewilliams61 | 9:c216d7b63f92 | 192 | update=0; |
ewilliams61 | 9:c216d7b63f92 | 193 | } |
ewilliams61 | 9:c216d7b63f92 | 194 | //If four characters have been entered, attempt to unlock |
ewilliams61 | 7:79d0b30fedb4 | 195 | if (inNdx > 3) { |
ewilliams61 | 7:79d0b30fedb4 | 196 | dev.printf("Attempting to Unlock\n"); |
ewilliams61 | 7:79d0b30fedb4 | 197 | codeIn = false; |
ewilliams61 | 7:79d0b30fedb4 | 198 | bool c1 = true; bool c2 = true; bool c3 = true; |
ewilliams61 | 8:39172c01c0f1 | 199 | //check entered code against saved codes |
ewilliams61 | 7:79d0b30fedb4 | 200 | for(int x = 0; x <= 3; x++){ |
ewilliams61 | 8:39172c01c0f1 | 201 | //if no code matches, end loop |
ewilliams61 | 7:79d0b30fedb4 | 202 | if(!(c1 | c2 | c3)) { |
ewilliams61 | 7:79d0b30fedb4 | 203 | break; |
ewilliams61 | 7:79d0b30fedb4 | 204 | } else { |
ewilliams61 | 8:39172c01c0f1 | 205 | //compare codes |
ewilliams61 | 7:79d0b30fedb4 | 206 | if (code1[x] != input[x]){c1 = false;} |
ewilliams61 | 7:79d0b30fedb4 | 207 | if (code2[x] != input[x]){c2 = false;} |
ewilliams61 | 7:79d0b30fedb4 | 208 | if (code3[x] != input[x]){c3 = false;} |
ewilliams61 | 7:79d0b30fedb4 | 209 | |
ewilliams61 | 7:79d0b30fedb4 | 210 | } |
ewilliams61 | 7:79d0b30fedb4 | 211 | } |
ewilliams61 | 7:79d0b30fedb4 | 212 | if(c1){ |
ewilliams61 | 8:39172c01c0f1 | 213 | //Send welcome user 1 to bluetooth |
ewilliams61 | 7:79d0b30fedb4 | 214 | dev.printf("Welcome User 1\n"); |
ewilliams61 | 9:c216d7b63f92 | 215 | lock = 1.0; |
ewilliams61 | 9:c216d7b63f92 | 216 | wait(1.0); |
ewilliams61 | 9:c216d7b63f92 | 217 | doorPulley.speed(-0.8); |
ewilliams61 | 9:c216d7b63f92 | 218 | wait(2.0); |
ewilliams61 | 9:c216d7b63f92 | 219 | doorPulley.speed(0.0); |
ewilliams61 | 9:c216d7b63f92 | 220 | wait(.3); |
ewilliams61 | 9:c216d7b63f92 | 221 | mailFeed.speed(-0.8); |
ewilliams61 | 9:c216d7b63f92 | 222 | wait(2); |
ewilliams61 | 9:c216d7b63f92 | 223 | mailFeed.speed(0.0); |
ewilliams61 | 9:c216d7b63f92 | 224 | doorPulley.speed(0.3); |
ewilliams61 | 9:c216d7b63f92 | 225 | wait(1.0); |
ewilliams61 | 9:c216d7b63f92 | 226 | doorPulley.speed(0.0); |
ewilliams61 | 7:79d0b30fedb4 | 227 | } else if(c2) { |
ewilliams61 | 8:39172c01c0f1 | 228 | //Send welcome user 2 to bluetooth |
ewilliams61 | 7:79d0b30fedb4 | 229 | dev.printf("Welcome User 2\n"); |
ewilliams61 | 9:c216d7b63f92 | 230 | lock = 1.0; |
ewilliams61 | 9:c216d7b63f92 | 231 | wait(1.0); |
ewilliams61 | 9:c216d7b63f92 | 232 | doorPulley.speed(-0.8); |
ewilliams61 | 9:c216d7b63f92 | 233 | wait(2.0); |
ewilliams61 | 9:c216d7b63f92 | 234 | doorPulley.speed(0.0); |
ewilliams61 | 9:c216d7b63f92 | 235 | wait(.3); |
ewilliams61 | 9:c216d7b63f92 | 236 | mailFeed.speed(-0.8); |
ewilliams61 | 9:c216d7b63f92 | 237 | wait(2); |
ewilliams61 | 9:c216d7b63f92 | 238 | mailFeed.speed(0.0); |
ewilliams61 | 9:c216d7b63f92 | 239 | doorPulley.speed(0.3); |
ewilliams61 | 9:c216d7b63f92 | 240 | wait(1.0); |
ewilliams61 | 9:c216d7b63f92 | 241 | doorPulley.speed(0.0); |
ewilliams61 | 9:c216d7b63f92 | 242 | doorPulley.speed(.3); |
ewilliams61 | 9:c216d7b63f92 | 243 | wait(.8); |
ewilliams61 | 9:c216d7b63f92 | 244 | doorPulley.speed(0.0); |
ewilliams61 | 9:c216d7b63f92 | 245 | |
ewilliams61 | 9:c216d7b63f92 | 246 | |
ewilliams61 | 7:79d0b30fedb4 | 247 | } else if(c3) { |
ewilliams61 | 8:39172c01c0f1 | 248 | //Send welcome user 3 to bluetooth |
ewilliams61 | 7:79d0b30fedb4 | 249 | dev.printf("Welcome User 3\n"); |
ewilliams61 | 9:c216d7b63f92 | 250 | lock = 1.0; |
ewilliams61 | 9:c216d7b63f92 | 251 | wait(1.0); |
ewilliams61 | 9:c216d7b63f92 | 252 | doorPulley.speed(-0.8); |
ewilliams61 | 9:c216d7b63f92 | 253 | wait(2.0); |
ewilliams61 | 9:c216d7b63f92 | 254 | doorPulley.speed(0.0); |
ewilliams61 | 9:c216d7b63f92 | 255 | wait(.3); |
ewilliams61 | 9:c216d7b63f92 | 256 | mailFeed.speed(-0.8); |
ewilliams61 | 9:c216d7b63f92 | 257 | wait(2); |
ewilliams61 | 9:c216d7b63f92 | 258 | mailFeed.speed(0.0); |
ewilliams61 | 9:c216d7b63f92 | 259 | doorPulley.speed(0.3); |
ewilliams61 | 9:c216d7b63f92 | 260 | wait(1.0); |
ewilliams61 | 9:c216d7b63f92 | 261 | doorPulley.speed(0.0); |
ewilliams61 | 9:c216d7b63f92 | 262 | |
ewilliams61 | 7:79d0b30fedb4 | 263 | } else { |
ewilliams61 | 8:39172c01c0f1 | 264 | //Send welcome Try Again message to bluetooth |
ewilliams61 | 7:79d0b30fedb4 | 265 | dev.printf("Try Again\n"); |
ewilliams61 | 9:c216d7b63f92 | 266 | lock = 0.3; |
ewilliams61 | 9:c216d7b63f92 | 267 | |
ewilliams61 | 7:79d0b30fedb4 | 268 | |
ewilliams61 | 7:79d0b30fedb4 | 269 | } |
ewilliams61 | 8:39172c01c0f1 | 270 | //reset indexing variable when checking is complete |
ewilliams61 | 7:79d0b30fedb4 | 271 | inNdx = 0; |
ewilliams61 | 7:79d0b30fedb4 | 272 | |
ewilliams61 | 7:79d0b30fedb4 | 273 | } |
mbedAustin | 4:ba9100d52e48 | 274 | } |
ewilliams61 | 7:79d0b30fedb4 | 275 | |
mbedAustin | 4:ba9100d52e48 | 276 | } |
ewilliams61 | 9:c216d7b63f92 | 277 | // Reads and processes GET and POST web data |
ewilliams61 | 9:c216d7b63f92 | 278 | void ReadWebData() |
ewilliams61 | 9:c216d7b63f92 | 279 | { |
ewilliams61 | 9:c216d7b63f92 | 280 | wait_ms(200); |
ewilliams61 | 9:c216d7b63f92 | 281 | esp.attach(NULL,Serial::RxIrq); |
ewilliams61 | 9:c216d7b63f92 | 282 | DataRX=0; |
ewilliams61 | 9:c216d7b63f92 | 283 | memset(webdata, '\0', sizeof(webdata)); |
ewilliams61 | 9:c216d7b63f92 | 284 | strcpy(webdata, rx_buffer); |
ewilliams61 | 9:c216d7b63f92 | 285 | memset(rx_buffer, '\0', sizeof(rx_buffer)); |
ewilliams61 | 9:c216d7b63f92 | 286 | rx_in = 0; |
ewilliams61 | 9:c216d7b63f92 | 287 | rx_out = 0; |
ewilliams61 | 9:c216d7b63f92 | 288 | // check web data for form information |
ewilliams61 | 9:c216d7b63f92 | 289 | if( strstr(webdata, "check=led1v") != NULL ) { |
ewilliams61 | 9:c216d7b63f92 | 290 | led1=!led1; |
ewilliams61 | 9:c216d7b63f92 | 291 | DSin = 0; |
ewilliams61 | 9:c216d7b63f92 | 292 | } |
ewilliams61 | 9:c216d7b63f92 | 293 | if( strstr(webdata, "check=led2v") != NULL ) { |
ewilliams61 | 9:c216d7b63f92 | 294 | led2=!led2; |
ewilliams61 | 9:c216d7b63f92 | 295 | } |
ewilliams61 | 9:c216d7b63f92 | 296 | if( strstr(webdata, "check=led3v") != NULL ) { |
ewilliams61 | 9:c216d7b63f92 | 297 | led3=!led3; |
ewilliams61 | 9:c216d7b63f92 | 298 | } |
ewilliams61 | 9:c216d7b63f92 | 299 | if( strstr(webdata, "check=led4v") != NULL ) { |
ewilliams61 | 9:c216d7b63f92 | 300 | led4=!led4; |
ewilliams61 | 9:c216d7b63f92 | 301 | } |
ewilliams61 | 9:c216d7b63f92 | 302 | if( strstr(webdata, "POST") != NULL ) { // set update flag if POST request |
ewilliams61 | 9:c216d7b63f92 | 303 | update=1; |
ewilliams61 | 9:c216d7b63f92 | 304 | } |
ewilliams61 | 9:c216d7b63f92 | 305 | 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 |
ewilliams61 | 9:c216d7b63f92 | 306 | update=1; |
ewilliams61 | 9:c216d7b63f92 | 307 | } |
ewilliams61 | 9:c216d7b63f92 | 308 | } |
ewilliams61 | 9:c216d7b63f92 | 309 | // Starts webserver |
ewilliams61 | 9:c216d7b63f92 | 310 | void startserver() |
ewilliams61 | 9:c216d7b63f92 | 311 | { |
ewilliams61 | 9:c216d7b63f92 | 312 | gettime(); |
ewilliams61 | 9:c216d7b63f92 | 313 | strcpy(audio1, "#"); |
ewilliams61 | 9:c216d7b63f92 | 314 | pc.printf("++++++++++ Resetting ESP ++++++++++\r\n"); |
ewilliams61 | 9:c216d7b63f92 | 315 | strcpy(cmdbuff,"node.restart()\r\n"); |
ewilliams61 | 9:c216d7b63f92 | 316 | SendCMD(); |
ewilliams61 | 9:c216d7b63f92 | 317 | wait(2); |
ewilliams61 | 9:c216d7b63f92 | 318 | getreply(); |
ewilliams61 | 9:c216d7b63f92 | 319 | |
ewilliams61 | 9:c216d7b63f92 | 320 | pc.printf("\n++++++++++ Starting Server ++++++++++\r\n> "); |
ewilliams61 | 9:c216d7b63f92 | 321 | |
ewilliams61 | 9:c216d7b63f92 | 322 | // initial values |
ewilliams61 | 9:c216d7b63f92 | 323 | sprintf(cmdbuff, "count,time,yesorno,audio1=0,\"%s\",\"%s\",\"%s\"\r\n",timebuf,gotMail, audio1); |
ewilliams61 | 9:c216d7b63f92 | 324 | SendCMD(); |
ewilliams61 | 9:c216d7b63f92 | 325 | getreply(); |
ewilliams61 | 9:c216d7b63f92 | 326 | wait(0.5); |
ewilliams61 | 9:c216d7b63f92 | 327 | |
ewilliams61 | 9:c216d7b63f92 | 328 | //create server |
ewilliams61 | 9:c216d7b63f92 | 329 | sprintf(cmdbuff, "srv=net.createServer(net.TCP,%d)\r\n",SERVtimeout); |
ewilliams61 | 9:c216d7b63f92 | 330 | SendCMD(); |
ewilliams61 | 9:c216d7b63f92 | 331 | getreply(); |
ewilliams61 | 9:c216d7b63f92 | 332 | wait(0.5); |
ewilliams61 | 9:c216d7b63f92 | 333 | strcpy(cmdbuff,"srv:listen(80,function(conn)\r\n"); |
ewilliams61 | 9:c216d7b63f92 | 334 | SendCMD(); |
ewilliams61 | 9:c216d7b63f92 | 335 | getreply(); |
ewilliams61 | 9:c216d7b63f92 | 336 | wait(0.3); |
ewilliams61 | 9:c216d7b63f92 | 337 | strcpy(cmdbuff,"conn:on(\"receive\",function(conn,payload) \r\n"); |
ewilliams61 | 9:c216d7b63f92 | 338 | SendCMD(); |
ewilliams61 | 9:c216d7b63f92 | 339 | getreply(); |
ewilliams61 | 9:c216d7b63f92 | 340 | wait(0.3); |
ewilliams61 | 9:c216d7b63f92 | 341 | |
ewilliams61 | 9:c216d7b63f92 | 342 | //print data to mbed |
ewilliams61 | 9:c216d7b63f92 | 343 | strcpy(cmdbuff,"print(payload)\r\n"); |
ewilliams61 | 9:c216d7b63f92 | 344 | SendCMD(); |
ewilliams61 | 9:c216d7b63f92 | 345 | getreply(); |
ewilliams61 | 9:c216d7b63f92 | 346 | wait(0.2); |
ewilliams61 | 9:c216d7b63f92 | 347 | |
ewilliams61 | 9:c216d7b63f92 | 348 | //web page data |
ewilliams61 | 9:c216d7b63f92 | 349 | strcpy(cmdbuff,"conn:send('<!DOCTYPE html><html><style> h1 {background: #00000;} </style>')\r\n"); |
ewilliams61 | 9:c216d7b63f92 | 350 | SendCMD(); |
ewilliams61 | 9:c216d7b63f92 | 351 | getreply(); |
ewilliams61 | 9:c216d7b63f92 | 352 | wait(0.4); |
ewilliams61 | 9:c216d7b63f92 | 353 | strcpy(cmdbuff, "conn:send('<body><h1>Huzzah Mail Receive Notification Center</h1>')\r\n"); |
ewilliams61 | 9:c216d7b63f92 | 354 | SendCMD(); |
ewilliams61 | 9:c216d7b63f92 | 355 | getreply(); |
ewilliams61 | 9:c216d7b63f92 | 356 | wait(0.4); |
ewilliams61 | 9:c216d7b63f92 | 357 | strcpy(cmdbuff,"conn:send('Hit count: '..count..'')\r\n"); |
ewilliams61 | 9:c216d7b63f92 | 358 | SendCMD(); |
ewilliams61 | 9:c216d7b63f92 | 359 | getreply(); |
ewilliams61 | 9:c216d7b63f92 | 360 | wait(0.2); |
ewilliams61 | 9:c216d7b63f92 | 361 | strcpy(cmdbuff,"conn:send('<br>Last hit (based on mbed RTC time): '..time..'<br><hr>')\r\n"); |
ewilliams61 | 9:c216d7b63f92 | 362 | SendCMD(); |
ewilliams61 | 9:c216d7b63f92 | 363 | getreply(); |
ewilliams61 | 9:c216d7b63f92 | 364 | wait(0.4); |
ewilliams61 | 9:c216d7b63f92 | 365 | strcpy(cmdbuff,"conn:send('<h2>Do you have mail?: '..yesorno..' </h2>')\r\n"); |
ewilliams61 | 9:c216d7b63f92 | 366 | SendCMD(); |
ewilliams61 | 9:c216d7b63f92 | 367 | getreply(); |
ewilliams61 | 9:c216d7b63f92 | 368 | wait(0.3); |
ewilliams61 | 9:c216d7b63f92 | 369 | strcpy(cmdbuff, "conn:send('<audio autoplay src= '..audio1..' ></audio><hr>')\r\n"); |
ewilliams61 | 9:c216d7b63f92 | 370 | SendCMD(); |
ewilliams61 | 9:c216d7b63f92 | 371 | getreply(); |
ewilliams61 | 9:c216d7b63f92 | 372 | wait(0.4); |
ewilliams61 | 9:c216d7b63f92 | 373 | |
ewilliams61 | 9:c216d7b63f92 | 374 | |
ewilliams61 | 9:c216d7b63f92 | 375 | |
ewilliams61 | 9:c216d7b63f92 | 376 | strcpy(cmdbuff,"conn:send('<form name=\"form1\" method=\"POST\"')\r\n"); |
ewilliams61 | 9:c216d7b63f92 | 377 | SendCMD(); |
ewilliams61 | 9:c216d7b63f92 | 378 | getreply(); |
ewilliams61 | 9:c216d7b63f92 | 379 | wait(0.3); |
ewilliams61 | 9:c216d7b63f92 | 380 | strcpy(cmdbuff, "conn:send('<p><input type=\"checkbox\" name=\"check\" value=\"led1v\"> Unlock mailbox?')\r\n"); |
ewilliams61 | 9:c216d7b63f92 | 381 | SendCMD(); |
ewilliams61 | 9:c216d7b63f92 | 382 | getreply(); |
ewilliams61 | 9:c216d7b63f92 | 383 | wait(0.3); |
ewilliams61 | 9:c216d7b63f92 | 384 | strcpy(cmdbuff, "conn:send('<p><input type=\"checkbox\" name=\"check\" value=\"led2v\"> flip LED2')\r\n"); |
ewilliams61 | 9:c216d7b63f92 | 385 | SendCMD(); |
ewilliams61 | 9:c216d7b63f92 | 386 | getreply(); |
ewilliams61 | 9:c216d7b63f92 | 387 | wait(0.3); |
ewilliams61 | 9:c216d7b63f92 | 388 | strcpy(cmdbuff, "conn:send('<p><input type=\"checkbox\" name=\"check\" value=\"led3v\"> flip LED3')\r\n"); |
ewilliams61 | 9:c216d7b63f92 | 389 | SendCMD(); |
ewilliams61 | 9:c216d7b63f92 | 390 | getreply(); |
ewilliams61 | 9:c216d7b63f92 | 391 | wait(0.3); |
ewilliams61 | 9:c216d7b63f92 | 392 | strcpy(cmdbuff, "conn:send('<p><input type=\"checkbox\" name=\"check\" value=\"led4v\"> flip LED4')\r\n"); |
ewilliams61 | 9:c216d7b63f92 | 393 | SendCMD(); |
ewilliams61 | 9:c216d7b63f92 | 394 | getreply(); |
ewilliams61 | 9:c216d7b63f92 | 395 | wait(0.3); |
ewilliams61 | 9:c216d7b63f92 | 396 | strcpy(cmdbuff,"conn:send('<p><input type=\"submit\" value=\"Check Mail\"></form>')\r\n"); |
ewilliams61 | 9:c216d7b63f92 | 397 | SendCMD(); |
ewilliams61 | 9:c216d7b63f92 | 398 | getreply(); |
ewilliams61 | 9:c216d7b63f92 | 399 | wait(0.3); |
ewilliams61 | 9:c216d7b63f92 | 400 | strcpy(cmdbuff, "conn:send('<script type=\"text/javascript\"> window.onload=function(){window.setTimeout(\"document.form1.submit();\", 5000);}</script>')\r\n"); |
ewilliams61 | 9:c216d7b63f92 | 401 | SendCMD(); |
ewilliams61 | 9:c216d7b63f92 | 402 | wait(0.4); |
ewilliams61 | 9:c216d7b63f92 | 403 | 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"); |
ewilliams61 | 9:c216d7b63f92 | 404 | SendCMD(); |
ewilliams61 | 9:c216d7b63f92 | 405 | getreply(); |
ewilliams61 | 9:c216d7b63f92 | 406 | wait(0.5); |
ewilliams61 | 9:c216d7b63f92 | 407 | // end web page data |
ewilliams61 | 9:c216d7b63f92 | 408 | strcpy(cmdbuff, "conn:on(\"sent\",function(conn) conn:close() end)\r\n"); // close current connection |
ewilliams61 | 9:c216d7b63f92 | 409 | SendCMD(); |
ewilliams61 | 9:c216d7b63f92 | 410 | getreply(); |
ewilliams61 | 9:c216d7b63f92 | 411 | wait(0.3); |
ewilliams61 | 9:c216d7b63f92 | 412 | strcpy(cmdbuff, "end)\r\n"); |
ewilliams61 | 9:c216d7b63f92 | 413 | SendCMD(); |
ewilliams61 | 9:c216d7b63f92 | 414 | getreply(); |
ewilliams61 | 9:c216d7b63f92 | 415 | wait(0.2); |
ewilliams61 | 9:c216d7b63f92 | 416 | strcpy(cmdbuff, "end)\r\n"); |
ewilliams61 | 9:c216d7b63f92 | 417 | SendCMD(); |
ewilliams61 | 9:c216d7b63f92 | 418 | getreply(); |
ewilliams61 | 9:c216d7b63f92 | 419 | wait(0.2); |
ewilliams61 | 9:c216d7b63f92 | 420 | |
ewilliams61 | 9:c216d7b63f92 | 421 | strcpy(cmdbuff, "tmr.alarm(0, 1000, 1, function()\r\n"); |
ewilliams61 | 9:c216d7b63f92 | 422 | SendCMD(); |
ewilliams61 | 9:c216d7b63f92 | 423 | getreply(); |
ewilliams61 | 9:c216d7b63f92 | 424 | wait(0.2); |
ewilliams61 | 9:c216d7b63f92 | 425 | strcpy(cmdbuff, "if wifi.sta.getip() == nil then\r\n"); |
ewilliams61 | 9:c216d7b63f92 | 426 | SendCMD(); |
ewilliams61 | 9:c216d7b63f92 | 427 | getreply(); |
ewilliams61 | 9:c216d7b63f92 | 428 | wait(0.2); |
ewilliams61 | 9:c216d7b63f92 | 429 | strcpy(cmdbuff, "print(\"Connecting to AP...\\n\")\r\n"); |
ewilliams61 | 9:c216d7b63f92 | 430 | SendCMD(); |
ewilliams61 | 9:c216d7b63f92 | 431 | getreply(); |
ewilliams61 | 9:c216d7b63f92 | 432 | wait(0.2); |
ewilliams61 | 9:c216d7b63f92 | 433 | strcpy(cmdbuff, "else\r\n"); |
ewilliams61 | 9:c216d7b63f92 | 434 | SendCMD(); |
ewilliams61 | 9:c216d7b63f92 | 435 | getreply(); |
ewilliams61 | 9:c216d7b63f92 | 436 | wait(0.2); |
ewilliams61 | 9:c216d7b63f92 | 437 | strcpy(cmdbuff, "ip, nm, gw=wifi.sta.getip()\r\n"); |
ewilliams61 | 9:c216d7b63f92 | 438 | SendCMD(); |
ewilliams61 | 9:c216d7b63f92 | 439 | getreply(); |
ewilliams61 | 9:c216d7b63f92 | 440 | wait(0.2); |
ewilliams61 | 9:c216d7b63f92 | 441 | strcpy(cmdbuff,"print(\"IP Address: \",ip)\r\n"); |
ewilliams61 | 9:c216d7b63f92 | 442 | SendCMD(); |
ewilliams61 | 9:c216d7b63f92 | 443 | getreply(); |
ewilliams61 | 9:c216d7b63f92 | 444 | wait(0.2); |
ewilliams61 | 9:c216d7b63f92 | 445 | strcpy(cmdbuff,"tmr.stop(0)\r\n"); |
ewilliams61 | 9:c216d7b63f92 | 446 | SendCMD(); |
ewilliams61 | 9:c216d7b63f92 | 447 | getreply(); |
ewilliams61 | 9:c216d7b63f92 | 448 | wait(0.2); |
ewilliams61 | 9:c216d7b63f92 | 449 | strcpy(cmdbuff,"end\r\n"); |
ewilliams61 | 9:c216d7b63f92 | 450 | SendCMD(); |
ewilliams61 | 9:c216d7b63f92 | 451 | getreply(); |
ewilliams61 | 9:c216d7b63f92 | 452 | wait(0.2); |
ewilliams61 | 9:c216d7b63f92 | 453 | strcpy(cmdbuff,"end)\r\n"); |
ewilliams61 | 9:c216d7b63f92 | 454 | SendCMD(); |
ewilliams61 | 9:c216d7b63f92 | 455 | getreply(); |
ewilliams61 | 9:c216d7b63f92 | 456 | wait(0.2); |
ewilliams61 | 9:c216d7b63f92 | 457 | |
ewilliams61 | 9:c216d7b63f92 | 458 | pc.printf("\n\n++++++++++ Ready ++++++++++\r\n\n"); |
ewilliams61 | 9:c216d7b63f92 | 459 | } |
ewilliams61 | 9:c216d7b63f92 | 460 | |
ewilliams61 | 9:c216d7b63f92 | 461 | |
ewilliams61 | 9:c216d7b63f92 | 462 | // ESP Command data send |
ewilliams61 | 9:c216d7b63f92 | 463 | void SendCMD() |
ewilliams61 | 9:c216d7b63f92 | 464 | { |
ewilliams61 | 9:c216d7b63f92 | 465 | int i; |
ewilliams61 | 9:c216d7b63f92 | 466 | char temp_char; |
ewilliams61 | 9:c216d7b63f92 | 467 | bool empty; |
ewilliams61 | 9:c216d7b63f92 | 468 | i = 0; |
ewilliams61 | 9:c216d7b63f92 | 469 | // Start Critical Section - don't interrupt while changing global buffer variables |
ewilliams61 | 9:c216d7b63f92 | 470 | NVIC_DisableIRQ(UART1_IRQn); |
ewilliams61 | 9:c216d7b63f92 | 471 | empty = (tx_in == tx_out); |
ewilliams61 | 9:c216d7b63f92 | 472 | while ((i==0) || (cmdbuff[i-1] != '\n')) { |
ewilliams61 | 9:c216d7b63f92 | 473 | // Wait if buffer full |
ewilliams61 | 9:c216d7b63f92 | 474 | if (((tx_in + 1) % buffer_size) == tx_out) { |
ewilliams61 | 9:c216d7b63f92 | 475 | // End Critical Section - need to let interrupt routine empty buffer by sending |
ewilliams61 | 9:c216d7b63f92 | 476 | NVIC_EnableIRQ(UART1_IRQn); |
ewilliams61 | 9:c216d7b63f92 | 477 | while (((tx_in + 1) % buffer_size) == tx_out) { |
ewilliams61 | 9:c216d7b63f92 | 478 | } |
ewilliams61 | 9:c216d7b63f92 | 479 | // Start Critical Section - don't interrupt while changing global buffer variables |
ewilliams61 | 9:c216d7b63f92 | 480 | NVIC_DisableIRQ(UART1_IRQn); |
ewilliams61 | 9:c216d7b63f92 | 481 | } |
ewilliams61 | 9:c216d7b63f92 | 482 | tx_buffer[tx_in] = cmdbuff[i]; |
ewilliams61 | 9:c216d7b63f92 | 483 | i++; |
ewilliams61 | 9:c216d7b63f92 | 484 | tx_in = (tx_in + 1) % buffer_size; |
ewilliams61 | 9:c216d7b63f92 | 485 | } |
ewilliams61 | 9:c216d7b63f92 | 486 | if (esp.writeable() && (empty)) { |
ewilliams61 | 9:c216d7b63f92 | 487 | temp_char = tx_buffer[tx_out]; |
ewilliams61 | 9:c216d7b63f92 | 488 | tx_out = (tx_out + 1) % buffer_size; |
ewilliams61 | 9:c216d7b63f92 | 489 | // Send first character to start tx interrupts, if stopped |
ewilliams61 | 9:c216d7b63f92 | 490 | esp.putc(temp_char); |
ewilliams61 | 9:c216d7b63f92 | 491 | } |
ewilliams61 | 9:c216d7b63f92 | 492 | // End Critical Section |
ewilliams61 | 9:c216d7b63f92 | 493 | NVIC_EnableIRQ(UART1_IRQn); |
ewilliams61 | 9:c216d7b63f92 | 494 | return; |
ewilliams61 | 9:c216d7b63f92 | 495 | } |
ewilliams61 | 9:c216d7b63f92 | 496 | |
ewilliams61 | 9:c216d7b63f92 | 497 | // Get Command and ESP status replies |
ewilliams61 | 9:c216d7b63f92 | 498 | void getreply() |
ewilliams61 | 9:c216d7b63f92 | 499 | { |
ewilliams61 | 9:c216d7b63f92 | 500 | read_line(); |
ewilliams61 | 9:c216d7b63f92 | 501 | sscanf(rx_line,replybuff); |
ewilliams61 | 9:c216d7b63f92 | 502 | } |
ewilliams61 | 9:c216d7b63f92 | 503 | |
ewilliams61 | 9:c216d7b63f92 | 504 | // Read a line from the large rx buffer from rx interrupt routine |
ewilliams61 | 9:c216d7b63f92 | 505 | void read_line() { |
ewilliams61 | 9:c216d7b63f92 | 506 | int i; |
ewilliams61 | 9:c216d7b63f92 | 507 | i = 0; |
ewilliams61 | 9:c216d7b63f92 | 508 | // Start Critical Section - don't interrupt while changing global buffer variables |
ewilliams61 | 9:c216d7b63f92 | 509 | NVIC_DisableIRQ(UART1_IRQn); |
ewilliams61 | 9:c216d7b63f92 | 510 | // Loop reading rx buffer characters until end of line character |
ewilliams61 | 9:c216d7b63f92 | 511 | while ((i==0) || (rx_line[i-1] != '\r')) { |
ewilliams61 | 9:c216d7b63f92 | 512 | // Wait if buffer empty |
ewilliams61 | 9:c216d7b63f92 | 513 | if (rx_in == rx_out) { |
ewilliams61 | 9:c216d7b63f92 | 514 | // End Critical Section - need to allow rx interrupt to get new characters for buffer |
ewilliams61 | 9:c216d7b63f92 | 515 | NVIC_EnableIRQ(UART1_IRQn); |
ewilliams61 | 9:c216d7b63f92 | 516 | while (rx_in == rx_out) { |
ewilliams61 | 9:c216d7b63f92 | 517 | } |
ewilliams61 | 9:c216d7b63f92 | 518 | // Start Critical Section - don't interrupt while changing global buffer variables |
ewilliams61 | 9:c216d7b63f92 | 519 | NVIC_DisableIRQ(UART1_IRQn); |
ewilliams61 | 9:c216d7b63f92 | 520 | } |
ewilliams61 | 9:c216d7b63f92 | 521 | rx_line[i] = rx_buffer[rx_out]; |
ewilliams61 | 9:c216d7b63f92 | 522 | i++; |
ewilliams61 | 9:c216d7b63f92 | 523 | rx_out = (rx_out + 1) % buffer_size; |
ewilliams61 | 9:c216d7b63f92 | 524 | } |
ewilliams61 | 9:c216d7b63f92 | 525 | // End Critical Section |
ewilliams61 | 9:c216d7b63f92 | 526 | NVIC_EnableIRQ(UART1_IRQn); |
ewilliams61 | 9:c216d7b63f92 | 527 | rx_line[i-1] = 0; |
ewilliams61 | 9:c216d7b63f92 | 528 | return; |
ewilliams61 | 9:c216d7b63f92 | 529 | } |
ewilliams61 | 9:c216d7b63f92 | 530 | |
ewilliams61 | 9:c216d7b63f92 | 531 | |
ewilliams61 | 9:c216d7b63f92 | 532 | // Interupt Routine to read in data from serial port |
ewilliams61 | 9:c216d7b63f92 | 533 | void Rx_interrupt() { |
ewilliams61 | 9:c216d7b63f92 | 534 | DataRX=1; |
ewilliams61 | 9:c216d7b63f92 | 535 | //getDS(); |
ewilliams61 | 9:c216d7b63f92 | 536 | //led3=1; |
ewilliams61 | 9:c216d7b63f92 | 537 | // Loop just in case more than one character is in UART's receive FIFO buffer |
ewilliams61 | 9:c216d7b63f92 | 538 | // Stop if buffer full |
ewilliams61 | 9:c216d7b63f92 | 539 | while ((esp.readable()) && (((rx_in + 1) % buffer_size) != rx_out)) { |
ewilliams61 | 9:c216d7b63f92 | 540 | rx_buffer[rx_in] = esp.getc(); |
ewilliams61 | 9:c216d7b63f92 | 541 | // Uncomment to Echo to USB serial to watch data flow |
ewilliams61 | 9:c216d7b63f92 | 542 | pc.putc(rx_buffer[rx_in]); |
ewilliams61 | 9:c216d7b63f92 | 543 | rx_in = (rx_in + 1) % buffer_size; |
ewilliams61 | 9:c216d7b63f92 | 544 | } |
ewilliams61 | 9:c216d7b63f92 | 545 | //led3=0; |
ewilliams61 | 9:c216d7b63f92 | 546 | return; |
ewilliams61 | 9:c216d7b63f92 | 547 | } |
ewilliams61 | 9:c216d7b63f92 | 548 | |
ewilliams61 | 9:c216d7b63f92 | 549 | |
ewilliams61 | 9:c216d7b63f92 | 550 | // Interupt Routine to write out data to serial port |
ewilliams61 | 9:c216d7b63f92 | 551 | void Tx_interrupt() { |
ewilliams61 | 9:c216d7b63f92 | 552 | //led2=1; |
ewilliams61 | 9:c216d7b63f92 | 553 | // Loop to fill more than one character in UART's transmit FIFO buffer |
ewilliams61 | 9:c216d7b63f92 | 554 | // Stop if buffer empty |
ewilliams61 | 9:c216d7b63f92 | 555 | while ((esp.writeable()) && (tx_in != tx_out)) { |
ewilliams61 | 9:c216d7b63f92 | 556 | esp.putc(tx_buffer[tx_out]); |
ewilliams61 | 9:c216d7b63f92 | 557 | tx_out = (tx_out + 1) % buffer_size; |
ewilliams61 | 9:c216d7b63f92 | 558 | } |
ewilliams61 | 9:c216d7b63f92 | 559 | //led2=0; |
ewilliams61 | 9:c216d7b63f92 | 560 | return; |
ewilliams61 | 9:c216d7b63f92 | 561 | } |
ewilliams61 | 9:c216d7b63f92 | 562 | |
ewilliams61 | 9:c216d7b63f92 | 563 | void gettime() |
ewilliams61 | 9:c216d7b63f92 | 564 | { |
ewilliams61 | 9:c216d7b63f92 | 565 | time_t seconds = time(NULL); |
ewilliams61 | 9:c216d7b63f92 | 566 | strftime(timebuf,50,"%H:%M:%S %a %d %b %y", localtime(&seconds)); |
ewilliams61 | 9:c216d7b63f92 | 567 | } |
ewilliams61 | 9:c216d7b63f92 | 568 | |
ewilliams61 | 9:c216d7b63f92 | 569 | void setRTC() |
ewilliams61 | 9:c216d7b63f92 | 570 | { |
ewilliams61 | 9:c216d7b63f92 | 571 | t.tm_sec = (0); // 0-59 |
ewilliams61 | 9:c216d7b63f92 | 572 | t.tm_min = (minute); // 0-59 |
ewilliams61 | 9:c216d7b63f92 | 573 | t.tm_hour = (hour); // 0-23 |
ewilliams61 | 9:c216d7b63f92 | 574 | t.tm_mday = (dayofmonth); // 1-31 |
ewilliams61 | 9:c216d7b63f92 | 575 | t.tm_mon = (month-1); // 0-11 "0" = Jan, -1 added for Mbed RCT clock format |
ewilliams61 | 9:c216d7b63f92 | 576 | t.tm_year = ((year)+100); // year since 1900, current DCF year + 100 + 1900 = correct year |
ewilliams61 | 9:c216d7b63f92 | 577 | set_time(mktime(&t)); // set RTC clock |
ewilliams61 | 9:c216d7b63f92 | 578 | } |
ewilliams61 | 9:c216d7b63f92 | 579 | |
ewilliams61 | 9:c216d7b63f92 | 580 | void getDS() |
ewilliams61 | 9:c216d7b63f92 | 581 | { |
ewilliams61 | 9:c216d7b63f92 | 582 | DSin = ds.read(); |
ewilliams61 | 9:c216d7b63f92 | 583 | } |