IoT Door

Dependencies:   mbed

Fork of HUZZAHESP8266-web-control-LPC1768 by Austin Dong

Committer:
ECE4180
Date:
Sun Mar 12 13:37:31 2017 +0000
Revision:
10:e47b6d245db3
Parent:
9:2259b8443931
final

Who changed what in which revision?

UserRevisionLine numberNew contents of line
star297 0:e2a155f50119 1 // ESP8266 Static page WEB server to control Mbed
star297 0:e2a155f50119 2
star297 0:e2a155f50119 3 #include "mbed.h"
star297 0:e2a155f50119 4
star297 0:e2a155f50119 5 Serial pc(USBTX, USBRX);
ECE4180 6:c31fd3a33ea2 6 Serial esp(p9, p10); // tx, rx
ECE4180 6:c31fd3a33ea2 7 DigitalOut led1(LED1) ;
ECE4180 6:c31fd3a33ea2 8 DigitalOut led2(LED2) ;
ECE4180 6:c31fd3a33ea2 9 DigitalOut led3(LED3) ;
ECE4180 6:c31fd3a33ea2 10 DigitalOut led4(LED4) ;
ECE4180 6:c31fd3a33ea2 11 DigitalOut door(p23); //Solenoid For Door. 1 = Open
ECE4180 6:c31fd3a33ea2 12 AnalogIn ir_sensor(p19); //Input from IR Sensor
ECE4180 6:c31fd3a33ea2 13 PwmOut speaker(p22); //Speaker Driving Signal
ECE4180 6:c31fd3a33ea2 14 DigitalOut speaker_on(p21); //Turn Speaker and Amp off when not in use, 1=ON
ECE4180 6:c31fd3a33ea2 15 volatile int alarm = 0;
ECE4180 6:c31fd3a33ea2 16 volatile int armed = 0;
ECE4180 6:c31fd3a33ea2 17 int rangecount = 0;
ECE4180 6:c31fd3a33ea2 18 int unlock = 0;
ECE4180 6:c31fd3a33ea2 19 Timer tim;
ECE4180 6:c31fd3a33ea2 20 Timer t_unlock;
ECE4180 6:c31fd3a33ea2 21 Timer t_alarm;
ECE4180 6:c31fd3a33ea2 22 Ticker door_sense;
ECE4180 7:eee53c450d3d 23 int alarm_cnt = 0;
ausdong 5:bc0296a5ad8a 24
ausdong 5:bc0296a5ad8a 25 // things for sending/receiving data over serial
ausdong 5:bc0296a5ad8a 26 volatile int tx_in=0;
ausdong 5:bc0296a5ad8a 27 volatile int tx_out=0;
ausdong 5:bc0296a5ad8a 28 volatile int rx_in=0;
ausdong 5:bc0296a5ad8a 29 volatile int rx_out=0;
ausdong 5:bc0296a5ad8a 30 const int buffer_size = 4095;
ausdong 5:bc0296a5ad8a 31 char tx_buffer[buffer_size+1];
ausdong 5:bc0296a5ad8a 32 char rx_buffer[buffer_size+1];
ausdong 5:bc0296a5ad8a 33 void Tx_interrupt();
ausdong 5:bc0296a5ad8a 34 void Rx_interrupt();
ausdong 5:bc0296a5ad8a 35 void send_line();
ausdong 5:bc0296a5ad8a 36 void read_line();
ausdong 5:bc0296a5ad8a 37
ausdong 5:bc0296a5ad8a 38 int DataRX;
ausdong 5:bc0296a5ad8a 39 int update;
ausdong 5:bc0296a5ad8a 40 int count;
ausdong 5:bc0296a5ad8a 41 char cmdbuff[1024];
ausdong 5:bc0296a5ad8a 42 char replybuff[4096];
ausdong 5:bc0296a5ad8a 43 char webdata[4096]; // This may need to be bigger depending on WEB browser used
ausdong 5:bc0296a5ad8a 44 char webbuff[4096]; // Currently using 1986 characters, Increase this if more web page data added
star297 0:e2a155f50119 45 char timebuf[30];
ausdong 5:bc0296a5ad8a 46 void SendCMD(),getreply(),ReadWebData(),startserver();
ECE4180 6:c31fd3a33ea2 47 void gettime(),setRTC();
ausdong 5:bc0296a5ad8a 48 char rx_line[1024];
ausdong 5:bc0296a5ad8a 49 int port =80; // set server port
ausdong 5:bc0296a5ad8a 50 int SERVtimeout =5; // set server timeout in seconds in case link breaks.
ausdong 5:bc0296a5ad8a 51 struct tm t;
star297 1:71ed1afbf344 52 // manual set RTC values
4180_1 4:40dd020463ea 53 int minute =00; // 0-59
4180_1 4:40dd020463ea 54 int hour =12; // 2-23
4180_1 4:40dd020463ea 55 int dayofmonth =26; // 1-31
4180_1 4:40dd020463ea 56 int month =8; // 1-12
star297 1:71ed1afbf344 57 int year =15; // last 2 digits
star297 2:d4c6bc0f2dc4 58
ECE4180 6:c31fd3a33ea2 59 void sensorcheck(){
ECE4180 7:eee53c450d3d 60 led3 = alarm;
ECE4180 7:eee53c450d3d 61 led2 = armed;
ECE4180 7:eee53c450d3d 62 speaker_on = 0;
ECE4180 6:c31fd3a33ea2 63 if(armed){
ECE4180 7:eee53c450d3d 64 if((alarm == 0) && (ir_sensor.read() > 0.5) && (rangecount < 3)){
ECE4180 6:c31fd3a33ea2 65 if(rangecount == 0) tim.start();
ECE4180 6:c31fd3a33ea2 66 rangecount++;
ECE4180 6:c31fd3a33ea2 67 }
ECE4180 7:eee53c450d3d 68 else if(rangecount == 3 && tim.read() > 3 && alarm==0){
ECE4180 6:c31fd3a33ea2 69 alarm = 1;
ECE4180 7:eee53c450d3d 70 alarm_cnt++;
ECE4180 7:eee53c450d3d 71 rangecount = 0;
ECE4180 6:c31fd3a33ea2 72 t_alarm.start();
ECE4180 6:c31fd3a33ea2 73 }
ECE4180 6:c31fd3a33ea2 74 else if(ir_sensor.read() < 0.5 && alarm == 0){
ECE4180 6:c31fd3a33ea2 75 tim.reset();
ECE4180 6:c31fd3a33ea2 76 rangecount = 0;
ECE4180 6:c31fd3a33ea2 77 }
ECE4180 6:c31fd3a33ea2 78 }
ECE4180 7:eee53c450d3d 79 else {alarm = 0;
ECE4180 7:eee53c450d3d 80 rangecount = 0;
ECE4180 7:eee53c450d3d 81 }
ECE4180 7:eee53c450d3d 82 if(alarm){
ECE4180 7:eee53c450d3d 83 speaker_on = 1;
ECE4180 7:eee53c450d3d 84 speaker = 0.10;
ECE4180 7:eee53c450d3d 85 if (t_alarm < 5.0) speaker.period(1.0/969.0);
ECE4180 7:eee53c450d3d 86 else if (t_alarm < 10.0) speaker.period(1.0/800.0);
ECE4180 7:eee53c450d3d 87 else if (t_alarm >= 10.0) t_alarm.reset();
ECE4180 7:eee53c450d3d 88 }
ECE4180 6:c31fd3a33ea2 89 }
ECE4180 6:c31fd3a33ea2 90
ECE4180 6:c31fd3a33ea2 91
4180_1 4:40dd020463ea 92 int main()
4180_1 4:40dd020463ea 93 {
ECE4180 6:c31fd3a33ea2 94
ausdong 5:bc0296a5ad8a 95 pc.baud(9600);
ausdong 5:bc0296a5ad8a 96 esp.baud(9600);
ECE4180 6:c31fd3a33ea2 97 led1 = 1;
ausdong 5:bc0296a5ad8a 98 // Setup a serial interrupt function to receive data
ausdong 5:bc0296a5ad8a 99 esp.attach(&Rx_interrupt, Serial::RxIrq);
ausdong 5:bc0296a5ad8a 100 // Setup a serial interrupt function to transmit data
ausdong 5:bc0296a5ad8a 101 esp.attach(&Tx_interrupt, Serial::TxIrq);
4180_1 4:40dd020463ea 102 if (time(NULL) < 1420070400) {
4180_1 4:40dd020463ea 103 setRTC();
4180_1 4:40dd020463ea 104 }
star297 0:e2a155f50119 105 startserver();
ausdong 5:bc0296a5ad8a 106 DataRX=0;
ausdong 5:bc0296a5ad8a 107 count=0;
ECE4180 6:c31fd3a33ea2 108 door_sense.attach(&sensorcheck, 1.0);
4180_1 4:40dd020463ea 109 while(1) {
ECE4180 6:c31fd3a33ea2 110 led4 =! led4;
ECE4180 6:c31fd3a33ea2 111 if(unlock){
ECE4180 6:c31fd3a33ea2 112 t_unlock.start();
ECE4180 6:c31fd3a33ea2 113 door = 1;
ECE4180 6:c31fd3a33ea2 114 if(t_unlock.read() > 3){
ECE4180 6:c31fd3a33ea2 115 door = 0;
ECE4180 6:c31fd3a33ea2 116 t_unlock.reset();
ECE4180 6:c31fd3a33ea2 117 t_unlock.stop();
ECE4180 6:c31fd3a33ea2 118 unlock = 0;
ECE4180 6:c31fd3a33ea2 119 }
ECE4180 6:c31fd3a33ea2 120 }
4180_1 4:40dd020463ea 121 if(DataRX==1) {
star297 0:e2a155f50119 122 ReadWebData();
ausdong 5:bc0296a5ad8a 123 esp.attach(&Rx_interrupt, Serial::RxIrq);
ausdong 5:bc0296a5ad8a 124 }
ausdong 5:bc0296a5ad8a 125 if(update==1) // update time, hit count, and analog levels in the HUZZAH chip
ausdong 5:bc0296a5ad8a 126 {
ausdong 5:bc0296a5ad8a 127 // get new values
ausdong 5:bc0296a5ad8a 128 gettime();
ausdong 5:bc0296a5ad8a 129 // send new values
ECE4180 8:e68f8edaa71f 130 sprintf(cmdbuff, "rangecount,alarm=%d,\"%d\"\r\n",alarm_cnt,alarm);
ausdong 5:bc0296a5ad8a 131 SendCMD();
ausdong 5:bc0296a5ad8a 132 getreply();
ausdong 5:bc0296a5ad8a 133 update=0;
4180_1 4:40dd020463ea 134 }
star297 0:e2a155f50119 135 }
4180_1 4:40dd020463ea 136 }
star297 0:e2a155f50119 137
4180_1 4:40dd020463ea 138 // Reads and processes GET and POST web data
star297 0:e2a155f50119 139 void ReadWebData()
4180_1 4:40dd020463ea 140 {
4180_1 4:40dd020463ea 141 wait_ms(200);
ausdong 5:bc0296a5ad8a 142 esp.attach(NULL,Serial::RxIrq);
4180_1 4:40dd020463ea 143 DataRX=0;
4180_1 4:40dd020463ea 144 memset(webdata, '\0', sizeof(webdata));
ausdong 5:bc0296a5ad8a 145 strcpy(webdata, rx_buffer);
ausdong 5:bc0296a5ad8a 146 memset(rx_buffer, '\0', sizeof(rx_buffer));
ausdong 5:bc0296a5ad8a 147 rx_in = 0;
ausdong 5:bc0296a5ad8a 148 rx_out = 0;
ausdong 5:bc0296a5ad8a 149 // check web data for form information
ausdong 5:bc0296a5ad8a 150 if( strstr(webdata, "check=led1v") != NULL ) {
ECE4180 6:c31fd3a33ea2 151 armed =! armed;
ausdong 5:bc0296a5ad8a 152 }
ausdong 5:bc0296a5ad8a 153 if( strstr(webdata, "check=led2v") != NULL ) {
ECE4180 9:2259b8443931 154 unlock= 1;
ausdong 5:bc0296a5ad8a 155 }
ausdong 5:bc0296a5ad8a 156 if( strstr(webdata, "check=led3v") != NULL ) {
ECE4180 6:c31fd3a33ea2 157 alarm = 0;
ausdong 5:bc0296a5ad8a 158 }
ausdong 5:bc0296a5ad8a 159 if( strstr(webdata, "POST") != NULL ) { // set update flag if POST request
ausdong 5:bc0296a5ad8a 160 update=1;
ausdong 5:bc0296a5ad8a 161 }
ausdong 5:bc0296a5ad8a 162 if( strstr(webdata, "GET") != NULL && strstr(webdata, "favicon") == NULL ) { // set update flag for GET request but do not want to update for favicon requests
ausdong 5:bc0296a5ad8a 163 update=1;
4180_1 4:40dd020463ea 164 }
star297 0:e2a155f50119 165 }
ausdong 5:bc0296a5ad8a 166 // Starts webserver
star297 0:e2a155f50119 167 void startserver()
star297 0:e2a155f50119 168 {
ausdong 5:bc0296a5ad8a 169 gettime();
4180_1 4:40dd020463ea 170 pc.printf("++++++++++ Resetting ESP ++++++++++\r\n");
ausdong 5:bc0296a5ad8a 171 strcpy(cmdbuff,"node.restart()\r\n");
ausdong 5:bc0296a5ad8a 172 SendCMD();
ausdong 5:bc0296a5ad8a 173 wait(2);
ausdong 5:bc0296a5ad8a 174 getreply();
ausdong 5:bc0296a5ad8a 175
ausdong 5:bc0296a5ad8a 176 pc.printf("\n++++++++++ Starting Server ++++++++++\r\n> ");
ECE4180 8:e68f8edaa71f 177
ausdong 5:bc0296a5ad8a 178 //create server
ausdong 5:bc0296a5ad8a 179 sprintf(cmdbuff, "srv=net.createServer(net.TCP,%d)\r\n",SERVtimeout);
ausdong 5:bc0296a5ad8a 180 SendCMD();
ausdong 5:bc0296a5ad8a 181 getreply();
ausdong 5:bc0296a5ad8a 182 wait(0.5);
ausdong 5:bc0296a5ad8a 183 strcpy(cmdbuff,"srv:listen(80,function(conn)\r\n");
star297 0:e2a155f50119 184 SendCMD();
star297 1:71ed1afbf344 185 getreply();
ausdong 5:bc0296a5ad8a 186 wait(0.3);
ausdong 5:bc0296a5ad8a 187 strcpy(cmdbuff,"conn:on(\"receive\",function(conn,payload) \r\n");
ausdong 5:bc0296a5ad8a 188 SendCMD();
ausdong 5:bc0296a5ad8a 189 getreply();
ausdong 5:bc0296a5ad8a 190 wait(0.3);
ausdong 5:bc0296a5ad8a 191
ausdong 5:bc0296a5ad8a 192 //print data to mbed
ausdong 5:bc0296a5ad8a 193 strcpy(cmdbuff,"print(payload)\r\n");
ausdong 5:bc0296a5ad8a 194 SendCMD();
ausdong 5:bc0296a5ad8a 195 getreply();
ausdong 5:bc0296a5ad8a 196 wait(0.2);
ausdong 5:bc0296a5ad8a 197
ausdong 5:bc0296a5ad8a 198 //web page data
ECE4180 6:c31fd3a33ea2 199 strcpy(cmdbuff,"conn:send('<!DOCTYPE html><html><body><h1>IoT Door Control</h1>')\r\n");
ausdong 5:bc0296a5ad8a 200 SendCMD();
ausdong 5:bc0296a5ad8a 201 getreply();
ausdong 5:bc0296a5ad8a 202 wait(0.4);
ECE4180 8:e68f8edaa71f 203 strcpy(cmdbuff,"conn:send('Total Alarm Trips: '..rangecount..'<br><hr>')\r\n");
star297 1:71ed1afbf344 204 SendCMD();
star297 2:d4c6bc0f2dc4 205 getreply();
ausdong 5:bc0296a5ad8a 206 wait(0.2);
ECE4180 7:eee53c450d3d 207 strcpy(cmdbuff,"conn:send('Alarm Status: '..alarm..'<br><hr>')\r\n");
star297 1:71ed1afbf344 208 SendCMD();
star297 3:f7febfa77784 209 getreply();
ausdong 5:bc0296a5ad8a 210 wait(0.3);
ausdong 5:bc0296a5ad8a 211 strcpy(cmdbuff,"conn:send('<form method=\"POST\"')\r\n");
ausdong 5:bc0296a5ad8a 212 SendCMD();
ausdong 5:bc0296a5ad8a 213 getreply();
ausdong 5:bc0296a5ad8a 214 wait(0.3);
ECE4180 8:e68f8edaa71f 215 strcpy(cmdbuff, "conn:send('<p><input type=\"checkbox\" name=\"check\" value=\"led1v\"> Arm/Disarm (LED2)')\r\n");
ausdong 5:bc0296a5ad8a 216 SendCMD();
ausdong 5:bc0296a5ad8a 217 getreply();
ausdong 5:bc0296a5ad8a 218 wait(0.3);
ECE4180 6:c31fd3a33ea2 219 strcpy(cmdbuff, "conn:send('<p><input type=\"checkbox\" name=\"check\" value=\"led2v\"> Unlock (for 3s)')\r\n");
star297 3:f7febfa77784 220 SendCMD();
star297 2:d4c6bc0f2dc4 221 getreply();
ausdong 5:bc0296a5ad8a 222 wait(0.3);
ECE4180 8:e68f8edaa71f 223 strcpy(cmdbuff, "conn:send('<p><input type=\"checkbox\" name=\"check\" value=\"led3v\"> Alarm Off')\r\n");
ausdong 5:bc0296a5ad8a 224 SendCMD();
ausdong 5:bc0296a5ad8a 225 getreply();
ECE4180 6:c31fd3a33ea2 226 wait(0.3);
ausdong 5:bc0296a5ad8a 227 strcpy(cmdbuff,"conn:send('<p><input type=\"submit\" value=\"send-refresh\"></form>')\r\n");
ausdong 5:bc0296a5ad8a 228 SendCMD();
ausdong 5:bc0296a5ad8a 229 getreply();
ausdong 5:bc0296a5ad8a 230 wait(0.3);
ausdong 5:bc0296a5ad8a 231 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");
ausdong 5:bc0296a5ad8a 232 SendCMD();
ausdong 5:bc0296a5ad8a 233 getreply();
ausdong 5:bc0296a5ad8a 234 wait(0.5);
ausdong 5:bc0296a5ad8a 235 // end web page data
ausdong 5:bc0296a5ad8a 236 strcpy(cmdbuff, "conn:on(\"sent\",function(conn) conn:close() end)\r\n"); // close current connection
ausdong 5:bc0296a5ad8a 237 SendCMD();
ausdong 5:bc0296a5ad8a 238 getreply();
ausdong 5:bc0296a5ad8a 239 wait(0.3);
ausdong 5:bc0296a5ad8a 240 strcpy(cmdbuff, "end)\r\n");
ausdong 5:bc0296a5ad8a 241 SendCMD();
ausdong 5:bc0296a5ad8a 242 getreply();
ausdong 5:bc0296a5ad8a 243 wait(0.2);
ausdong 5:bc0296a5ad8a 244 strcpy(cmdbuff, "end)\r\n");
ausdong 5:bc0296a5ad8a 245 SendCMD();
ausdong 5:bc0296a5ad8a 246 getreply();
ausdong 5:bc0296a5ad8a 247 wait(0.2);
ausdong 5:bc0296a5ad8a 248
ausdong 5:bc0296a5ad8a 249 strcpy(cmdbuff, "tmr.alarm(0, 1000, 1, function()\r\n");
ausdong 5:bc0296a5ad8a 250 SendCMD();
ausdong 5:bc0296a5ad8a 251 getreply();
ausdong 5:bc0296a5ad8a 252 wait(0.2);
ausdong 5:bc0296a5ad8a 253 strcpy(cmdbuff, "if wifi.sta.getip() == nil then\r\n");
ausdong 5:bc0296a5ad8a 254 SendCMD();
ausdong 5:bc0296a5ad8a 255 getreply();
ausdong 5:bc0296a5ad8a 256 wait(0.2);
ausdong 5:bc0296a5ad8a 257 strcpy(cmdbuff, "print(\"Connecting to AP...\\n\")\r\n");
ausdong 5:bc0296a5ad8a 258 SendCMD();
ausdong 5:bc0296a5ad8a 259 getreply();
ausdong 5:bc0296a5ad8a 260 wait(0.2);
ausdong 5:bc0296a5ad8a 261 strcpy(cmdbuff, "else\r\n");
ausdong 5:bc0296a5ad8a 262 SendCMD();
ausdong 5:bc0296a5ad8a 263 getreply();
ausdong 5:bc0296a5ad8a 264 wait(0.2);
ausdong 5:bc0296a5ad8a 265 strcpy(cmdbuff, "ip, nm, gw=wifi.sta.getip()\r\n");
ausdong 5:bc0296a5ad8a 266 SendCMD();
ausdong 5:bc0296a5ad8a 267 getreply();
ausdong 5:bc0296a5ad8a 268 wait(0.2);
ausdong 5:bc0296a5ad8a 269 strcpy(cmdbuff,"print(\"IP Address: \",ip)\r\n");
ausdong 5:bc0296a5ad8a 270 SendCMD();
ausdong 5:bc0296a5ad8a 271 getreply();
ausdong 5:bc0296a5ad8a 272 wait(0.2);
ausdong 5:bc0296a5ad8a 273 strcpy(cmdbuff,"tmr.stop(0)\r\n");
ausdong 5:bc0296a5ad8a 274 SendCMD();
ausdong 5:bc0296a5ad8a 275 getreply();
ausdong 5:bc0296a5ad8a 276 wait(0.2);
ausdong 5:bc0296a5ad8a 277 strcpy(cmdbuff,"end\r\n");
ausdong 5:bc0296a5ad8a 278 SendCMD();
ausdong 5:bc0296a5ad8a 279 getreply();
ausdong 5:bc0296a5ad8a 280 wait(0.2);
ausdong 5:bc0296a5ad8a 281 strcpy(cmdbuff,"end)\r\n");
ausdong 5:bc0296a5ad8a 282 SendCMD();
ausdong 5:bc0296a5ad8a 283 getreply();
ausdong 5:bc0296a5ad8a 284 wait(0.2);
ausdong 5:bc0296a5ad8a 285
ausdong 5:bc0296a5ad8a 286 pc.printf("\n\n++++++++++ Ready ++++++++++\r\n\n");
4180_1 4:40dd020463ea 287 }
ausdong 5:bc0296a5ad8a 288
ausdong 5:bc0296a5ad8a 289
star297 0:e2a155f50119 290 // ESP Command data send
star297 0:e2a155f50119 291 void SendCMD()
star297 0:e2a155f50119 292 {
ausdong 5:bc0296a5ad8a 293 int i;
ausdong 5:bc0296a5ad8a 294 char temp_char;
ausdong 5:bc0296a5ad8a 295 bool empty;
ausdong 5:bc0296a5ad8a 296 i = 0;
ausdong 5:bc0296a5ad8a 297 // Start Critical Section - don't interrupt while changing global buffer variables
ausdong 5:bc0296a5ad8a 298 NVIC_DisableIRQ(UART1_IRQn);
ausdong 5:bc0296a5ad8a 299 empty = (tx_in == tx_out);
ausdong 5:bc0296a5ad8a 300 while ((i==0) || (cmdbuff[i-1] != '\n')) {
ausdong 5:bc0296a5ad8a 301 // Wait if buffer full
ausdong 5:bc0296a5ad8a 302 if (((tx_in + 1) % buffer_size) == tx_out) {
ausdong 5:bc0296a5ad8a 303 // End Critical Section - need to let interrupt routine empty buffer by sending
ausdong 5:bc0296a5ad8a 304 NVIC_EnableIRQ(UART1_IRQn);
ausdong 5:bc0296a5ad8a 305 while (((tx_in + 1) % buffer_size) == tx_out) {
ausdong 5:bc0296a5ad8a 306 }
ausdong 5:bc0296a5ad8a 307 // Start Critical Section - don't interrupt while changing global buffer variables
ausdong 5:bc0296a5ad8a 308 NVIC_DisableIRQ(UART1_IRQn);
ausdong 5:bc0296a5ad8a 309 }
ausdong 5:bc0296a5ad8a 310 tx_buffer[tx_in] = cmdbuff[i];
ausdong 5:bc0296a5ad8a 311 i++;
ausdong 5:bc0296a5ad8a 312 tx_in = (tx_in + 1) % buffer_size;
ausdong 5:bc0296a5ad8a 313 }
ausdong 5:bc0296a5ad8a 314 if (esp.writeable() && (empty)) {
ausdong 5:bc0296a5ad8a 315 temp_char = tx_buffer[tx_out];
ausdong 5:bc0296a5ad8a 316 tx_out = (tx_out + 1) % buffer_size;
ausdong 5:bc0296a5ad8a 317 // Send first character to start tx interrupts, if stopped
ausdong 5:bc0296a5ad8a 318 esp.putc(temp_char);
ausdong 5:bc0296a5ad8a 319 }
ausdong 5:bc0296a5ad8a 320 // End Critical Section
ausdong 5:bc0296a5ad8a 321 NVIC_EnableIRQ(UART1_IRQn);
ausdong 5:bc0296a5ad8a 322 return;
4180_1 4:40dd020463ea 323 }
ausdong 5:bc0296a5ad8a 324
4180_1 4:40dd020463ea 325 // Get Command and ESP status replies
star297 0:e2a155f50119 326 void getreply()
4180_1 4:40dd020463ea 327 {
ausdong 5:bc0296a5ad8a 328 read_line();
ausdong 5:bc0296a5ad8a 329 sscanf(rx_line,replybuff);
ausdong 5:bc0296a5ad8a 330 }
ausdong 5:bc0296a5ad8a 331
ausdong 5:bc0296a5ad8a 332 // Read a line from the large rx buffer from rx interrupt routine
ausdong 5:bc0296a5ad8a 333 void read_line() {
ausdong 5:bc0296a5ad8a 334 int i;
ausdong 5:bc0296a5ad8a 335 i = 0;
ausdong 5:bc0296a5ad8a 336 // Start Critical Section - don't interrupt while changing global buffer variables
ausdong 5:bc0296a5ad8a 337 NVIC_DisableIRQ(UART1_IRQn);
ausdong 5:bc0296a5ad8a 338 // Loop reading rx buffer characters until end of line character
ausdong 5:bc0296a5ad8a 339 while ((i==0) || (rx_line[i-1] != '\r')) {
ausdong 5:bc0296a5ad8a 340 // Wait if buffer empty
ausdong 5:bc0296a5ad8a 341 if (rx_in == rx_out) {
ausdong 5:bc0296a5ad8a 342 // End Critical Section - need to allow rx interrupt to get new characters for buffer
ausdong 5:bc0296a5ad8a 343 NVIC_EnableIRQ(UART1_IRQn);
ausdong 5:bc0296a5ad8a 344 while (rx_in == rx_out) {
ausdong 5:bc0296a5ad8a 345 }
ausdong 5:bc0296a5ad8a 346 // Start Critical Section - don't interrupt while changing global buffer variables
ausdong 5:bc0296a5ad8a 347 NVIC_DisableIRQ(UART1_IRQn);
star297 2:d4c6bc0f2dc4 348 }
ausdong 5:bc0296a5ad8a 349 rx_line[i] = rx_buffer[rx_out];
ausdong 5:bc0296a5ad8a 350 i++;
ausdong 5:bc0296a5ad8a 351 rx_out = (rx_out + 1) % buffer_size;
4180_1 4:40dd020463ea 352 }
ausdong 5:bc0296a5ad8a 353 // End Critical Section
ausdong 5:bc0296a5ad8a 354 NVIC_EnableIRQ(UART1_IRQn);
ausdong 5:bc0296a5ad8a 355 rx_line[i-1] = 0;
ausdong 5:bc0296a5ad8a 356 return;
ausdong 5:bc0296a5ad8a 357 }
ausdong 5:bc0296a5ad8a 358
ausdong 5:bc0296a5ad8a 359
ausdong 5:bc0296a5ad8a 360 // Interupt Routine to read in data from serial port
ausdong 5:bc0296a5ad8a 361 void Rx_interrupt() {
ausdong 5:bc0296a5ad8a 362 DataRX=1;
ECE4180 7:eee53c450d3d 363
ausdong 5:bc0296a5ad8a 364 // Loop just in case more than one character is in UART's receive FIFO buffer
ausdong 5:bc0296a5ad8a 365 // Stop if buffer full
ausdong 5:bc0296a5ad8a 366 while ((esp.readable()) && (((rx_in + 1) % buffer_size) != rx_out)) {
ausdong 5:bc0296a5ad8a 367 rx_buffer[rx_in] = esp.getc();
ausdong 5:bc0296a5ad8a 368 // Uncomment to Echo to USB serial to watch data flow
ausdong 5:bc0296a5ad8a 369 pc.putc(rx_buffer[rx_in]);
ausdong 5:bc0296a5ad8a 370 rx_in = (rx_in + 1) % buffer_size;
ausdong 5:bc0296a5ad8a 371 }
ausdong 5:bc0296a5ad8a 372 return;
ausdong 5:bc0296a5ad8a 373 }
ausdong 5:bc0296a5ad8a 374
ausdong 5:bc0296a5ad8a 375
ausdong 5:bc0296a5ad8a 376 // Interupt Routine to write out data to serial port
ausdong 5:bc0296a5ad8a 377 void Tx_interrupt() {
ECE4180 7:eee53c450d3d 378
ausdong 5:bc0296a5ad8a 379 // Loop to fill more than one character in UART's transmit FIFO buffer
ausdong 5:bc0296a5ad8a 380 // Stop if buffer empty
ausdong 5:bc0296a5ad8a 381 while ((esp.writeable()) && (tx_in != tx_out)) {
ausdong 5:bc0296a5ad8a 382 esp.putc(tx_buffer[tx_out]);
ausdong 5:bc0296a5ad8a 383 tx_out = (tx_out + 1) % buffer_size;
ausdong 5:bc0296a5ad8a 384 }
ECE4180 7:eee53c450d3d 385
ausdong 5:bc0296a5ad8a 386 return;
ausdong 5:bc0296a5ad8a 387 }
ausdong 5:bc0296a5ad8a 388
ausdong 5:bc0296a5ad8a 389 void gettime()
ausdong 5:bc0296a5ad8a 390 {
ausdong 5:bc0296a5ad8a 391 time_t seconds = time(NULL);
ausdong 5:bc0296a5ad8a 392 strftime(timebuf,50,"%H:%M:%S %a %d %b %y", localtime(&seconds));
ausdong 5:bc0296a5ad8a 393 }
ausdong 5:bc0296a5ad8a 394
ausdong 5:bc0296a5ad8a 395 void setRTC()
ausdong 5:bc0296a5ad8a 396 {
ausdong 5:bc0296a5ad8a 397 t.tm_sec = (0); // 0-59
ausdong 5:bc0296a5ad8a 398 t.tm_min = (minute); // 0-59
ausdong 5:bc0296a5ad8a 399 t.tm_hour = (hour); // 0-23
ausdong 5:bc0296a5ad8a 400 t.tm_mday = (dayofmonth); // 1-31
ausdong 5:bc0296a5ad8a 401 t.tm_mon = (month-1); // 0-11 "0" = Jan, -1 added for Mbed RCT clock format
ausdong 5:bc0296a5ad8a 402 t.tm_year = ((year)+100); // year since 1900, current DCF year + 100 + 1900 = correct year
ausdong 5:bc0296a5ad8a 403 set_time(mktime(&t)); // set RTC clock
star297 0:e2a155f50119 404 }