IoT Door

Dependencies:   mbed

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

Committer:
ECE4180
Date:
Fri Mar 10 00:02:53 2017 +0000
Revision:
7:eee53c450d3d
Parent:
6:c31fd3a33ea2
Child:
8:e68f8edaa71f
Finished_Mostly;

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 6:c31fd3a33ea2 135 sprintf(cmdbuff, "rangecount,time,alarm=%d,\"%s\",\"%d\"\r\n",rangecount,timebuf,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 6:c31fd3a33ea2 182 /*
ausdong 5:bc0296a5ad8a 183 // initial values
ausdong 5:bc0296a5ad8a 184 sprintf(cmdbuff, "count,time,analog1,analog2=0,\"%s\",\"%s\",\"%s\"\r\n",timebuf,Temp,Vcc);
ausdong 5:bc0296a5ad8a 185 SendCMD();
ausdong 5:bc0296a5ad8a 186 getreply();
ausdong 5:bc0296a5ad8a 187 wait(0.5);
ECE4180 6:c31fd3a33ea2 188 */
ausdong 5:bc0296a5ad8a 189 //create server
ausdong 5:bc0296a5ad8a 190 sprintf(cmdbuff, "srv=net.createServer(net.TCP,%d)\r\n",SERVtimeout);
ausdong 5:bc0296a5ad8a 191 SendCMD();
ausdong 5:bc0296a5ad8a 192 getreply();
ausdong 5:bc0296a5ad8a 193 wait(0.5);
ausdong 5:bc0296a5ad8a 194 strcpy(cmdbuff,"srv:listen(80,function(conn)\r\n");
star297 0:e2a155f50119 195 SendCMD();
star297 1:71ed1afbf344 196 getreply();
ausdong 5:bc0296a5ad8a 197 wait(0.3);
ausdong 5:bc0296a5ad8a 198 strcpy(cmdbuff,"conn:on(\"receive\",function(conn,payload) \r\n");
ausdong 5:bc0296a5ad8a 199 SendCMD();
ausdong 5:bc0296a5ad8a 200 getreply();
ausdong 5:bc0296a5ad8a 201 wait(0.3);
ausdong 5:bc0296a5ad8a 202
ausdong 5:bc0296a5ad8a 203 //print data to mbed
ausdong 5:bc0296a5ad8a 204 strcpy(cmdbuff,"print(payload)\r\n");
ausdong 5:bc0296a5ad8a 205 SendCMD();
ausdong 5:bc0296a5ad8a 206 getreply();
ausdong 5:bc0296a5ad8a 207 wait(0.2);
ausdong 5:bc0296a5ad8a 208
ausdong 5:bc0296a5ad8a 209 //web page data
ECE4180 6:c31fd3a33ea2 210 strcpy(cmdbuff,"conn:send('<!DOCTYPE html><html><body><h1>IoT Door Control</h1>')\r\n");
ausdong 5:bc0296a5ad8a 211 SendCMD();
ausdong 5:bc0296a5ad8a 212 getreply();
ausdong 5:bc0296a5ad8a 213 wait(0.4);
ECE4180 7:eee53c450d3d 214 strcpy(cmdbuff,"conn:send('Alarm Count: '..alarm_cnt..')\r\n");
star297 1:71ed1afbf344 215 SendCMD();
star297 2:d4c6bc0f2dc4 216 getreply();
ausdong 5:bc0296a5ad8a 217 wait(0.2);
ECE4180 7:eee53c450d3d 218 strcpy(cmdbuff,"conn:send('Alarm Status: '..alarm..'<br><hr>')\r\n");
star297 1:71ed1afbf344 219 SendCMD();
star297 3:f7febfa77784 220 getreply();
ausdong 5:bc0296a5ad8a 221 wait(0.3);
ausdong 5:bc0296a5ad8a 222 strcpy(cmdbuff,"conn:send('<form method=\"POST\"')\r\n");
ausdong 5:bc0296a5ad8a 223 SendCMD();
ausdong 5:bc0296a5ad8a 224 getreply();
ausdong 5:bc0296a5ad8a 225 wait(0.3);
ECE4180 6:c31fd3a33ea2 226 strcpy(cmdbuff, "conn:send('<p><input type=\"checkbox\" name=\"check\" value=\"led1v\"> Arm/Disarm (LED1)')\r\n");
ausdong 5:bc0296a5ad8a 227 SendCMD();
ausdong 5:bc0296a5ad8a 228 getreply();
ausdong 5:bc0296a5ad8a 229 wait(0.3);
ECE4180 6:c31fd3a33ea2 230 strcpy(cmdbuff, "conn:send('<p><input type=\"checkbox\" name=\"check\" value=\"led2v\"> Unlock (for 3s)')\r\n");
star297 3:f7febfa77784 231 SendCMD();
star297 2:d4c6bc0f2dc4 232 getreply();
ausdong 5:bc0296a5ad8a 233 wait(0.3);
ECE4180 7:eee53c450d3d 234 strcpy(cmdbuff, "conn:send('<p><input type=\"checkbox\" name=\"check\" value=\"led3v\"> Alarm on/off')\r\n");
ausdong 5:bc0296a5ad8a 235 SendCMD();
ausdong 5:bc0296a5ad8a 236 getreply();
ECE4180 6:c31fd3a33ea2 237 wait(0.3);
ausdong 5:bc0296a5ad8a 238 strcpy(cmdbuff,"conn:send('<p><input type=\"submit\" value=\"send-refresh\"></form>')\r\n");
ausdong 5:bc0296a5ad8a 239 SendCMD();
ausdong 5:bc0296a5ad8a 240 getreply();
ausdong 5:bc0296a5ad8a 241 wait(0.3);
ausdong 5:bc0296a5ad8a 242 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 243 SendCMD();
ausdong 5:bc0296a5ad8a 244 getreply();
ausdong 5:bc0296a5ad8a 245 wait(0.5);
ausdong 5:bc0296a5ad8a 246 // end web page data
ausdong 5:bc0296a5ad8a 247 strcpy(cmdbuff, "conn:on(\"sent\",function(conn) conn:close() end)\r\n"); // close current connection
ausdong 5:bc0296a5ad8a 248 SendCMD();
ausdong 5:bc0296a5ad8a 249 getreply();
ausdong 5:bc0296a5ad8a 250 wait(0.3);
ausdong 5:bc0296a5ad8a 251 strcpy(cmdbuff, "end)\r\n");
ausdong 5:bc0296a5ad8a 252 SendCMD();
ausdong 5:bc0296a5ad8a 253 getreply();
ausdong 5:bc0296a5ad8a 254 wait(0.2);
ausdong 5:bc0296a5ad8a 255 strcpy(cmdbuff, "end)\r\n");
ausdong 5:bc0296a5ad8a 256 SendCMD();
ausdong 5:bc0296a5ad8a 257 getreply();
ausdong 5:bc0296a5ad8a 258 wait(0.2);
ausdong 5:bc0296a5ad8a 259
ausdong 5:bc0296a5ad8a 260 strcpy(cmdbuff, "tmr.alarm(0, 1000, 1, function()\r\n");
ausdong 5:bc0296a5ad8a 261 SendCMD();
ausdong 5:bc0296a5ad8a 262 getreply();
ausdong 5:bc0296a5ad8a 263 wait(0.2);
ausdong 5:bc0296a5ad8a 264 strcpy(cmdbuff, "if wifi.sta.getip() == nil then\r\n");
ausdong 5:bc0296a5ad8a 265 SendCMD();
ausdong 5:bc0296a5ad8a 266 getreply();
ausdong 5:bc0296a5ad8a 267 wait(0.2);
ausdong 5:bc0296a5ad8a 268 strcpy(cmdbuff, "print(\"Connecting to AP...\\n\")\r\n");
ausdong 5:bc0296a5ad8a 269 SendCMD();
ausdong 5:bc0296a5ad8a 270 getreply();
ausdong 5:bc0296a5ad8a 271 wait(0.2);
ausdong 5:bc0296a5ad8a 272 strcpy(cmdbuff, "else\r\n");
ausdong 5:bc0296a5ad8a 273 SendCMD();
ausdong 5:bc0296a5ad8a 274 getreply();
ausdong 5:bc0296a5ad8a 275 wait(0.2);
ausdong 5:bc0296a5ad8a 276 strcpy(cmdbuff, "ip, nm, gw=wifi.sta.getip()\r\n");
ausdong 5:bc0296a5ad8a 277 SendCMD();
ausdong 5:bc0296a5ad8a 278 getreply();
ausdong 5:bc0296a5ad8a 279 wait(0.2);
ausdong 5:bc0296a5ad8a 280 strcpy(cmdbuff,"print(\"IP Address: \",ip)\r\n");
ausdong 5:bc0296a5ad8a 281 SendCMD();
ausdong 5:bc0296a5ad8a 282 getreply();
ausdong 5:bc0296a5ad8a 283 wait(0.2);
ausdong 5:bc0296a5ad8a 284 strcpy(cmdbuff,"tmr.stop(0)\r\n");
ausdong 5:bc0296a5ad8a 285 SendCMD();
ausdong 5:bc0296a5ad8a 286 getreply();
ausdong 5:bc0296a5ad8a 287 wait(0.2);
ausdong 5:bc0296a5ad8a 288 strcpy(cmdbuff,"end\r\n");
ausdong 5:bc0296a5ad8a 289 SendCMD();
ausdong 5:bc0296a5ad8a 290 getreply();
ausdong 5:bc0296a5ad8a 291 wait(0.2);
ausdong 5:bc0296a5ad8a 292 strcpy(cmdbuff,"end)\r\n");
ausdong 5:bc0296a5ad8a 293 SendCMD();
ausdong 5:bc0296a5ad8a 294 getreply();
ausdong 5:bc0296a5ad8a 295 wait(0.2);
ausdong 5:bc0296a5ad8a 296
ausdong 5:bc0296a5ad8a 297 pc.printf("\n\n++++++++++ Ready ++++++++++\r\n\n");
4180_1 4:40dd020463ea 298 }
ausdong 5:bc0296a5ad8a 299
ausdong 5:bc0296a5ad8a 300
star297 0:e2a155f50119 301 // ESP Command data send
star297 0:e2a155f50119 302 void SendCMD()
star297 0:e2a155f50119 303 {
ausdong 5:bc0296a5ad8a 304 int i;
ausdong 5:bc0296a5ad8a 305 char temp_char;
ausdong 5:bc0296a5ad8a 306 bool empty;
ausdong 5:bc0296a5ad8a 307 i = 0;
ausdong 5:bc0296a5ad8a 308 // Start Critical Section - don't interrupt while changing global buffer variables
ausdong 5:bc0296a5ad8a 309 NVIC_DisableIRQ(UART1_IRQn);
ausdong 5:bc0296a5ad8a 310 empty = (tx_in == tx_out);
ausdong 5:bc0296a5ad8a 311 while ((i==0) || (cmdbuff[i-1] != '\n')) {
ausdong 5:bc0296a5ad8a 312 // Wait if buffer full
ausdong 5:bc0296a5ad8a 313 if (((tx_in + 1) % buffer_size) == tx_out) {
ausdong 5:bc0296a5ad8a 314 // End Critical Section - need to let interrupt routine empty buffer by sending
ausdong 5:bc0296a5ad8a 315 NVIC_EnableIRQ(UART1_IRQn);
ausdong 5:bc0296a5ad8a 316 while (((tx_in + 1) % buffer_size) == tx_out) {
ausdong 5:bc0296a5ad8a 317 }
ausdong 5:bc0296a5ad8a 318 // Start Critical Section - don't interrupt while changing global buffer variables
ausdong 5:bc0296a5ad8a 319 NVIC_DisableIRQ(UART1_IRQn);
ausdong 5:bc0296a5ad8a 320 }
ausdong 5:bc0296a5ad8a 321 tx_buffer[tx_in] = cmdbuff[i];
ausdong 5:bc0296a5ad8a 322 i++;
ausdong 5:bc0296a5ad8a 323 tx_in = (tx_in + 1) % buffer_size;
ausdong 5:bc0296a5ad8a 324 }
ausdong 5:bc0296a5ad8a 325 if (esp.writeable() && (empty)) {
ausdong 5:bc0296a5ad8a 326 temp_char = tx_buffer[tx_out];
ausdong 5:bc0296a5ad8a 327 tx_out = (tx_out + 1) % buffer_size;
ausdong 5:bc0296a5ad8a 328 // Send first character to start tx interrupts, if stopped
ausdong 5:bc0296a5ad8a 329 esp.putc(temp_char);
ausdong 5:bc0296a5ad8a 330 }
ausdong 5:bc0296a5ad8a 331 // End Critical Section
ausdong 5:bc0296a5ad8a 332 NVIC_EnableIRQ(UART1_IRQn);
ausdong 5:bc0296a5ad8a 333 return;
4180_1 4:40dd020463ea 334 }
ausdong 5:bc0296a5ad8a 335
4180_1 4:40dd020463ea 336 // Get Command and ESP status replies
star297 0:e2a155f50119 337 void getreply()
4180_1 4:40dd020463ea 338 {
ausdong 5:bc0296a5ad8a 339 read_line();
ausdong 5:bc0296a5ad8a 340 sscanf(rx_line,replybuff);
ausdong 5:bc0296a5ad8a 341 }
ausdong 5:bc0296a5ad8a 342
ausdong 5:bc0296a5ad8a 343 // Read a line from the large rx buffer from rx interrupt routine
ausdong 5:bc0296a5ad8a 344 void read_line() {
ausdong 5:bc0296a5ad8a 345 int i;
ausdong 5:bc0296a5ad8a 346 i = 0;
ausdong 5:bc0296a5ad8a 347 // Start Critical Section - don't interrupt while changing global buffer variables
ausdong 5:bc0296a5ad8a 348 NVIC_DisableIRQ(UART1_IRQn);
ausdong 5:bc0296a5ad8a 349 // Loop reading rx buffer characters until end of line character
ausdong 5:bc0296a5ad8a 350 while ((i==0) || (rx_line[i-1] != '\r')) {
ausdong 5:bc0296a5ad8a 351 // Wait if buffer empty
ausdong 5:bc0296a5ad8a 352 if (rx_in == rx_out) {
ausdong 5:bc0296a5ad8a 353 // End Critical Section - need to allow rx interrupt to get new characters for buffer
ausdong 5:bc0296a5ad8a 354 NVIC_EnableIRQ(UART1_IRQn);
ausdong 5:bc0296a5ad8a 355 while (rx_in == rx_out) {
ausdong 5:bc0296a5ad8a 356 }
ausdong 5:bc0296a5ad8a 357 // Start Critical Section - don't interrupt while changing global buffer variables
ausdong 5:bc0296a5ad8a 358 NVIC_DisableIRQ(UART1_IRQn);
star297 2:d4c6bc0f2dc4 359 }
ausdong 5:bc0296a5ad8a 360 rx_line[i] = rx_buffer[rx_out];
ausdong 5:bc0296a5ad8a 361 i++;
ausdong 5:bc0296a5ad8a 362 rx_out = (rx_out + 1) % buffer_size;
4180_1 4:40dd020463ea 363 }
ausdong 5:bc0296a5ad8a 364 // End Critical Section
ausdong 5:bc0296a5ad8a 365 NVIC_EnableIRQ(UART1_IRQn);
ausdong 5:bc0296a5ad8a 366 rx_line[i-1] = 0;
ausdong 5:bc0296a5ad8a 367 return;
ausdong 5:bc0296a5ad8a 368 }
ausdong 5:bc0296a5ad8a 369
ausdong 5:bc0296a5ad8a 370
ausdong 5:bc0296a5ad8a 371 // Interupt Routine to read in data from serial port
ausdong 5:bc0296a5ad8a 372 void Rx_interrupt() {
ausdong 5:bc0296a5ad8a 373 DataRX=1;
ECE4180 7:eee53c450d3d 374
ausdong 5:bc0296a5ad8a 375 // Loop just in case more than one character is in UART's receive FIFO buffer
ausdong 5:bc0296a5ad8a 376 // Stop if buffer full
ausdong 5:bc0296a5ad8a 377 while ((esp.readable()) && (((rx_in + 1) % buffer_size) != rx_out)) {
ausdong 5:bc0296a5ad8a 378 rx_buffer[rx_in] = esp.getc();
ausdong 5:bc0296a5ad8a 379 // Uncomment to Echo to USB serial to watch data flow
ausdong 5:bc0296a5ad8a 380 pc.putc(rx_buffer[rx_in]);
ausdong 5:bc0296a5ad8a 381 rx_in = (rx_in + 1) % buffer_size;
ausdong 5:bc0296a5ad8a 382 }
ausdong 5:bc0296a5ad8a 383 return;
ausdong 5:bc0296a5ad8a 384 }
ausdong 5:bc0296a5ad8a 385
ausdong 5:bc0296a5ad8a 386
ausdong 5:bc0296a5ad8a 387 // Interupt Routine to write out data to serial port
ausdong 5:bc0296a5ad8a 388 void Tx_interrupt() {
ECE4180 7:eee53c450d3d 389
ausdong 5:bc0296a5ad8a 390 // Loop to fill more than one character in UART's transmit FIFO buffer
ausdong 5:bc0296a5ad8a 391 // Stop if buffer empty
ausdong 5:bc0296a5ad8a 392 while ((esp.writeable()) && (tx_in != tx_out)) {
ausdong 5:bc0296a5ad8a 393 esp.putc(tx_buffer[tx_out]);
ausdong 5:bc0296a5ad8a 394 tx_out = (tx_out + 1) % buffer_size;
ausdong 5:bc0296a5ad8a 395 }
ECE4180 7:eee53c450d3d 396
ausdong 5:bc0296a5ad8a 397 return;
ausdong 5:bc0296a5ad8a 398 }
ausdong 5:bc0296a5ad8a 399
ausdong 5:bc0296a5ad8a 400 void gettime()
ausdong 5:bc0296a5ad8a 401 {
ausdong 5:bc0296a5ad8a 402 time_t seconds = time(NULL);
ausdong 5:bc0296a5ad8a 403 strftime(timebuf,50,"%H:%M:%S %a %d %b %y", localtime(&seconds));
ausdong 5:bc0296a5ad8a 404 }
ausdong 5:bc0296a5ad8a 405
ausdong 5:bc0296a5ad8a 406 void setRTC()
ausdong 5:bc0296a5ad8a 407 {
ausdong 5:bc0296a5ad8a 408 t.tm_sec = (0); // 0-59
ausdong 5:bc0296a5ad8a 409 t.tm_min = (minute); // 0-59
ausdong 5:bc0296a5ad8a 410 t.tm_hour = (hour); // 0-23
ausdong 5:bc0296a5ad8a 411 t.tm_mday = (dayofmonth); // 1-31
ausdong 5:bc0296a5ad8a 412 t.tm_mon = (month-1); // 0-11 "0" = Jan, -1 added for Mbed RCT clock format
ausdong 5:bc0296a5ad8a 413 t.tm_year = ((year)+100); // year since 1900, current DCF year + 100 + 1900 = correct year
ausdong 5:bc0296a5ad8a 414 set_time(mktime(&t)); // set RTC clock
star297 0:e2a155f50119 415 }