IoT Door

Dependencies:   mbed

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

Committer:
ECE4180
Date:
Fri Mar 10 00:14:39 2017 +0000
Revision:
8:e68f8edaa71f
Parent:
7:eee53c450d3d
Child:
9:2259b8443931
Working - MJF

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