IoT Door

Dependencies:   mbed

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

Committer:
ECE4180
Date:
Thu Mar 09 23:17:18 2017 +0000
Revision:
6:c31fd3a33ea2
Parent:
5:bc0296a5ad8a
Child:
7:eee53c450d3d
Wifi server working. no ip assignment. has sensor and solenoid but no alarm noise;

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;
ausdong 5:bc0296a5ad8a 23 /*
ausdong 5:bc0296a5ad8a 24 char ssid[32] = "hsd"; // enter WiFi router ssid inside the quotes
ausdong 5:bc0296a5ad8a 25 char pwd [32] = "austin123"; // enter WiFi router password inside the quotes
ausdong 5:bc0296a5ad8a 26 */
ECE4180 6:c31fd3a33ea2 27
ausdong 5:bc0296a5ad8a 28
ausdong 5:bc0296a5ad8a 29 // things for sending/receiving data over serial
ausdong 5:bc0296a5ad8a 30 volatile int tx_in=0;
ausdong 5:bc0296a5ad8a 31 volatile int tx_out=0;
ausdong 5:bc0296a5ad8a 32 volatile int rx_in=0;
ausdong 5:bc0296a5ad8a 33 volatile int rx_out=0;
ausdong 5:bc0296a5ad8a 34 const int buffer_size = 4095;
ausdong 5:bc0296a5ad8a 35 char tx_buffer[buffer_size+1];
ausdong 5:bc0296a5ad8a 36 char rx_buffer[buffer_size+1];
ausdong 5:bc0296a5ad8a 37 void Tx_interrupt();
ausdong 5:bc0296a5ad8a 38 void Rx_interrupt();
ausdong 5:bc0296a5ad8a 39 void send_line();
ausdong 5:bc0296a5ad8a 40 void read_line();
ausdong 5:bc0296a5ad8a 41
ausdong 5:bc0296a5ad8a 42 int DataRX;
ausdong 5:bc0296a5ad8a 43 int update;
ausdong 5:bc0296a5ad8a 44 int count;
ausdong 5:bc0296a5ad8a 45 char cmdbuff[1024];
ausdong 5:bc0296a5ad8a 46 char replybuff[4096];
ausdong 5:bc0296a5ad8a 47 char webdata[4096]; // This may need to be bigger depending on WEB browser used
ausdong 5:bc0296a5ad8a 48 char webbuff[4096]; // Currently using 1986 characters, Increase this if more web page data added
star297 0:e2a155f50119 49 char timebuf[30];
ausdong 5:bc0296a5ad8a 50 void SendCMD(),getreply(),ReadWebData(),startserver();
ECE4180 6:c31fd3a33ea2 51 void gettime(),setRTC();
ausdong 5:bc0296a5ad8a 52 char rx_line[1024];
ausdong 5:bc0296a5ad8a 53 int port =80; // set server port
ausdong 5:bc0296a5ad8a 54 int SERVtimeout =5; // set server timeout in seconds in case link breaks.
ausdong 5:bc0296a5ad8a 55 struct tm t;
star297 1:71ed1afbf344 56 // manual set RTC values
4180_1 4:40dd020463ea 57 int minute =00; // 0-59
4180_1 4:40dd020463ea 58 int hour =12; // 2-23
4180_1 4:40dd020463ea 59 int dayofmonth =26; // 1-31
4180_1 4:40dd020463ea 60 int month =8; // 1-12
star297 1:71ed1afbf344 61 int year =15; // last 2 digits
star297 2:d4c6bc0f2dc4 62
ECE4180 6:c31fd3a33ea2 63 void sensorcheck(){
ECE4180 6:c31fd3a33ea2 64 led2 = alarm;
ECE4180 6:c31fd3a33ea2 65 if(armed){
ECE4180 6:c31fd3a33ea2 66 if((ir_sensor.read() > 0.5) && (rangecount < 15)){
ECE4180 6:c31fd3a33ea2 67 if(rangecount == 0) tim.start();
ECE4180 6:c31fd3a33ea2 68 rangecount++;
ECE4180 6:c31fd3a33ea2 69 }
ECE4180 6:c31fd3a33ea2 70 else if(rangecount == 15 && tim.read() > 6 && alarm==0){
ECE4180 6:c31fd3a33ea2 71 alarm = 1;
ECE4180 6:c31fd3a33ea2 72
ECE4180 6:c31fd3a33ea2 73 t_alarm.start();
ECE4180 6:c31fd3a33ea2 74 }
ECE4180 6:c31fd3a33ea2 75 else if(ir_sensor.read() < 0.5 && alarm == 0){
ECE4180 6:c31fd3a33ea2 76 tim.reset();
ECE4180 6:c31fd3a33ea2 77 rangecount = 0;
ECE4180 6:c31fd3a33ea2 78 led3 =!led3;
ECE4180 6:c31fd3a33ea2 79 }
ECE4180 6:c31fd3a33ea2 80 }
ECE4180 6:c31fd3a33ea2 81 else alarm = 0;
ECE4180 6:c31fd3a33ea2 82 }
ECE4180 6:c31fd3a33ea2 83
ECE4180 6:c31fd3a33ea2 84
4180_1 4:40dd020463ea 85 int main()
4180_1 4:40dd020463ea 86 {
ECE4180 6:c31fd3a33ea2 87
ausdong 5:bc0296a5ad8a 88 pc.baud(9600);
ausdong 5:bc0296a5ad8a 89 esp.baud(9600);
ECE4180 6:c31fd3a33ea2 90 led1 = 1;
ausdong 5:bc0296a5ad8a 91 // Setup a serial interrupt function to receive data
ausdong 5:bc0296a5ad8a 92 esp.attach(&Rx_interrupt, Serial::RxIrq);
ausdong 5:bc0296a5ad8a 93 // Setup a serial interrupt function to transmit data
ausdong 5:bc0296a5ad8a 94 esp.attach(&Tx_interrupt, Serial::TxIrq);
4180_1 4:40dd020463ea 95 if (time(NULL) < 1420070400) {
4180_1 4:40dd020463ea 96 setRTC();
4180_1 4:40dd020463ea 97 }
star297 0:e2a155f50119 98 startserver();
ausdong 5:bc0296a5ad8a 99 DataRX=0;
ausdong 5:bc0296a5ad8a 100 count=0;
ECE4180 6:c31fd3a33ea2 101 led2=1;
ECE4180 6:c31fd3a33ea2 102 door_sense.attach(&sensorcheck, 1.0);
4180_1 4:40dd020463ea 103 while(1) {
ECE4180 6:c31fd3a33ea2 104 led4 =! led4;
ECE4180 6:c31fd3a33ea2 105 if(unlock){
ECE4180 6:c31fd3a33ea2 106 t_unlock.start();
ECE4180 6:c31fd3a33ea2 107 door = 1;
ECE4180 6:c31fd3a33ea2 108 if(t_unlock.read() > 3){
ECE4180 6:c31fd3a33ea2 109 door = 0;
ECE4180 6:c31fd3a33ea2 110 t_unlock.reset();
ECE4180 6:c31fd3a33ea2 111 t_unlock.stop();
ECE4180 6:c31fd3a33ea2 112 unlock = 0;
ECE4180 6:c31fd3a33ea2 113 }
ECE4180 6:c31fd3a33ea2 114 }
4180_1 4:40dd020463ea 115 if(DataRX==1) {
star297 0:e2a155f50119 116 ReadWebData();
ausdong 5:bc0296a5ad8a 117 esp.attach(&Rx_interrupt, Serial::RxIrq);
ausdong 5:bc0296a5ad8a 118 }
ausdong 5:bc0296a5ad8a 119 if(update==1) // update time, hit count, and analog levels in the HUZZAH chip
ausdong 5:bc0296a5ad8a 120 {
ausdong 5:bc0296a5ad8a 121 // get new values
ausdong 5:bc0296a5ad8a 122 gettime();
ausdong 5:bc0296a5ad8a 123 // send new values
ECE4180 6:c31fd3a33ea2 124 sprintf(cmdbuff, "rangecount,time,alarm=%d,\"%s\",\"%d\"\r\n",rangecount,timebuf,alarm);
ausdong 5:bc0296a5ad8a 125 SendCMD();
ausdong 5:bc0296a5ad8a 126 getreply();
ausdong 5:bc0296a5ad8a 127 update=0;
4180_1 4:40dd020463ea 128 }
star297 0:e2a155f50119 129 }
4180_1 4:40dd020463ea 130 }
star297 0:e2a155f50119 131
4180_1 4:40dd020463ea 132 // Reads and processes GET and POST web data
star297 0:e2a155f50119 133 void ReadWebData()
4180_1 4:40dd020463ea 134 {
4180_1 4:40dd020463ea 135 wait_ms(200);
ausdong 5:bc0296a5ad8a 136 esp.attach(NULL,Serial::RxIrq);
4180_1 4:40dd020463ea 137 DataRX=0;
4180_1 4:40dd020463ea 138 memset(webdata, '\0', sizeof(webdata));
ausdong 5:bc0296a5ad8a 139 strcpy(webdata, rx_buffer);
ausdong 5:bc0296a5ad8a 140 memset(rx_buffer, '\0', sizeof(rx_buffer));
ausdong 5:bc0296a5ad8a 141 rx_in = 0;
ausdong 5:bc0296a5ad8a 142 rx_out = 0;
ausdong 5:bc0296a5ad8a 143 // check web data for form information
ausdong 5:bc0296a5ad8a 144 if( strstr(webdata, "check=led1v") != NULL ) {
ECE4180 6:c31fd3a33ea2 145 armed =! armed;
ausdong 5:bc0296a5ad8a 146 }
ausdong 5:bc0296a5ad8a 147 if( strstr(webdata, "check=led2v") != NULL ) {
ECE4180 6:c31fd3a33ea2 148 unlock=!unlock;
ausdong 5:bc0296a5ad8a 149 }
ausdong 5:bc0296a5ad8a 150 if( strstr(webdata, "check=led3v") != NULL ) {
ECE4180 6:c31fd3a33ea2 151 alarm = 0;
ausdong 5:bc0296a5ad8a 152 }
ausdong 5:bc0296a5ad8a 153 if( strstr(webdata, "POST") != NULL ) { // set update flag if POST request
ausdong 5:bc0296a5ad8a 154 update=1;
ausdong 5:bc0296a5ad8a 155 }
ausdong 5:bc0296a5ad8a 156 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 157 update=1;
4180_1 4:40dd020463ea 158 }
star297 0:e2a155f50119 159 }
ausdong 5:bc0296a5ad8a 160 // Starts webserver
star297 0:e2a155f50119 161 void startserver()
star297 0:e2a155f50119 162 {
ausdong 5:bc0296a5ad8a 163 gettime();
4180_1 4:40dd020463ea 164 pc.printf("++++++++++ Resetting ESP ++++++++++\r\n");
ausdong 5:bc0296a5ad8a 165 strcpy(cmdbuff,"node.restart()\r\n");
ausdong 5:bc0296a5ad8a 166 SendCMD();
ausdong 5:bc0296a5ad8a 167 wait(2);
ausdong 5:bc0296a5ad8a 168 getreply();
ausdong 5:bc0296a5ad8a 169
ausdong 5:bc0296a5ad8a 170 pc.printf("\n++++++++++ Starting Server ++++++++++\r\n> ");
ECE4180 6:c31fd3a33ea2 171 /*
ausdong 5:bc0296a5ad8a 172 // initial values
ausdong 5:bc0296a5ad8a 173 sprintf(cmdbuff, "count,time,analog1,analog2=0,\"%s\",\"%s\",\"%s\"\r\n",timebuf,Temp,Vcc);
ausdong 5:bc0296a5ad8a 174 SendCMD();
ausdong 5:bc0296a5ad8a 175 getreply();
ausdong 5:bc0296a5ad8a 176 wait(0.5);
ECE4180 6:c31fd3a33ea2 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 6:c31fd3a33ea2 203 strcpy(cmdbuff,"conn:send('Hit count: '..rangecount..'')\r\n");
star297 1:71ed1afbf344 204 SendCMD();
star297 2:d4c6bc0f2dc4 205 getreply();
ausdong 5:bc0296a5ad8a 206 wait(0.2);
ECE4180 6:c31fd3a33ea2 207 strcpy(cmdbuff,"conn:send('<br>Sens Count: '..time..'<br><hr>')\r\n");
ausdong 5:bc0296a5ad8a 208 SendCMD();
ausdong 5:bc0296a5ad8a 209 getreply();
ausdong 5:bc0296a5ad8a 210 wait(0.4);
ECE4180 6:c31fd3a33ea2 211 strcpy(cmdbuff,"conn:send('Alarm Status: '..alarm..' V<br><hr>')\r\n");
star297 1:71ed1afbf344 212 SendCMD();
star297 3:f7febfa77784 213 getreply();
ausdong 5:bc0296a5ad8a 214 wait(0.3);
ausdong 5:bc0296a5ad8a 215 strcpy(cmdbuff,"conn:send('<form method=\"POST\"')\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=\"led1v\"> Arm/Disarm (LED1)')\r\n");
ausdong 5:bc0296a5ad8a 220 SendCMD();
ausdong 5:bc0296a5ad8a 221 getreply();
ausdong 5:bc0296a5ad8a 222 wait(0.3);
ECE4180 6:c31fd3a33ea2 223 strcpy(cmdbuff, "conn:send('<p><input type=\"checkbox\" name=\"check\" value=\"led2v\"> Unlock (for 3s)')\r\n");
star297 3:f7febfa77784 224 SendCMD();
star297 2:d4c6bc0f2dc4 225 getreply();
ausdong 5:bc0296a5ad8a 226 wait(0.3);
ECE4180 6:c31fd3a33ea2 227 strcpy(cmdbuff, "conn:send('<p><input type=\"checkbox\" name=\"check\" value=\"alarmv\"> Alarm on/off')\r\n");
ausdong 5:bc0296a5ad8a 228 SendCMD();
ausdong 5:bc0296a5ad8a 229 getreply();
ECE4180 6:c31fd3a33ea2 230 wait(0.3);
ausdong 5:bc0296a5ad8a 231 strcpy(cmdbuff,"conn:send('<p><input type=\"submit\" value=\"send-refresh\"></form>')\r\n");
ausdong 5:bc0296a5ad8a 232 SendCMD();
ausdong 5:bc0296a5ad8a 233 getreply();
ausdong 5:bc0296a5ad8a 234 wait(0.3);
ausdong 5:bc0296a5ad8a 235 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 236 SendCMD();
ausdong 5:bc0296a5ad8a 237 getreply();
ausdong 5:bc0296a5ad8a 238 wait(0.5);
ausdong 5:bc0296a5ad8a 239 // end web page data
ausdong 5:bc0296a5ad8a 240 strcpy(cmdbuff, "conn:on(\"sent\",function(conn) conn:close() end)\r\n"); // close current connection
ausdong 5:bc0296a5ad8a 241 SendCMD();
ausdong 5:bc0296a5ad8a 242 getreply();
ausdong 5:bc0296a5ad8a 243 wait(0.3);
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 strcpy(cmdbuff, "end)\r\n");
ausdong 5:bc0296a5ad8a 249 SendCMD();
ausdong 5:bc0296a5ad8a 250 getreply();
ausdong 5:bc0296a5ad8a 251 wait(0.2);
ausdong 5:bc0296a5ad8a 252
ausdong 5:bc0296a5ad8a 253 strcpy(cmdbuff, "tmr.alarm(0, 1000, 1, function()\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, "if wifi.sta.getip() == nil then\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, "print(\"Connecting to AP...\\n\")\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, "else\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, "ip, nm, gw=wifi.sta.getip()\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,"print(\"IP Address: \",ip)\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,"tmr.stop(0)\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 strcpy(cmdbuff,"end)\r\n");
ausdong 5:bc0296a5ad8a 286 SendCMD();
ausdong 5:bc0296a5ad8a 287 getreply();
ausdong 5:bc0296a5ad8a 288 wait(0.2);
ausdong 5:bc0296a5ad8a 289
ausdong 5:bc0296a5ad8a 290 pc.printf("\n\n++++++++++ Ready ++++++++++\r\n\n");
4180_1 4:40dd020463ea 291 }
ausdong 5:bc0296a5ad8a 292
ausdong 5:bc0296a5ad8a 293
star297 0:e2a155f50119 294 // ESP Command data send
star297 0:e2a155f50119 295 void SendCMD()
star297 0:e2a155f50119 296 {
ausdong 5:bc0296a5ad8a 297 int i;
ausdong 5:bc0296a5ad8a 298 char temp_char;
ausdong 5:bc0296a5ad8a 299 bool empty;
ausdong 5:bc0296a5ad8a 300 i = 0;
ausdong 5:bc0296a5ad8a 301 // Start Critical Section - don't interrupt while changing global buffer variables
ausdong 5:bc0296a5ad8a 302 NVIC_DisableIRQ(UART1_IRQn);
ausdong 5:bc0296a5ad8a 303 empty = (tx_in == tx_out);
ausdong 5:bc0296a5ad8a 304 while ((i==0) || (cmdbuff[i-1] != '\n')) {
ausdong 5:bc0296a5ad8a 305 // Wait if buffer full
ausdong 5:bc0296a5ad8a 306 if (((tx_in + 1) % buffer_size) == tx_out) {
ausdong 5:bc0296a5ad8a 307 // End Critical Section - need to let interrupt routine empty buffer by sending
ausdong 5:bc0296a5ad8a 308 NVIC_EnableIRQ(UART1_IRQn);
ausdong 5:bc0296a5ad8a 309 while (((tx_in + 1) % buffer_size) == tx_out) {
ausdong 5:bc0296a5ad8a 310 }
ausdong 5:bc0296a5ad8a 311 // Start Critical Section - don't interrupt while changing global buffer variables
ausdong 5:bc0296a5ad8a 312 NVIC_DisableIRQ(UART1_IRQn);
ausdong 5:bc0296a5ad8a 313 }
ausdong 5:bc0296a5ad8a 314 tx_buffer[tx_in] = cmdbuff[i];
ausdong 5:bc0296a5ad8a 315 i++;
ausdong 5:bc0296a5ad8a 316 tx_in = (tx_in + 1) % buffer_size;
ausdong 5:bc0296a5ad8a 317 }
ausdong 5:bc0296a5ad8a 318 if (esp.writeable() && (empty)) {
ausdong 5:bc0296a5ad8a 319 temp_char = tx_buffer[tx_out];
ausdong 5:bc0296a5ad8a 320 tx_out = (tx_out + 1) % buffer_size;
ausdong 5:bc0296a5ad8a 321 // Send first character to start tx interrupts, if stopped
ausdong 5:bc0296a5ad8a 322 esp.putc(temp_char);
ausdong 5:bc0296a5ad8a 323 }
ausdong 5:bc0296a5ad8a 324 // End Critical Section
ausdong 5:bc0296a5ad8a 325 NVIC_EnableIRQ(UART1_IRQn);
ausdong 5:bc0296a5ad8a 326 return;
4180_1 4:40dd020463ea 327 }
ausdong 5:bc0296a5ad8a 328
4180_1 4:40dd020463ea 329 // Get Command and ESP status replies
star297 0:e2a155f50119 330 void getreply()
4180_1 4:40dd020463ea 331 {
ausdong 5:bc0296a5ad8a 332 read_line();
ausdong 5:bc0296a5ad8a 333 sscanf(rx_line,replybuff);
ausdong 5:bc0296a5ad8a 334 }
ausdong 5:bc0296a5ad8a 335
ausdong 5:bc0296a5ad8a 336 // Read a line from the large rx buffer from rx interrupt routine
ausdong 5:bc0296a5ad8a 337 void read_line() {
ausdong 5:bc0296a5ad8a 338 int i;
ausdong 5:bc0296a5ad8a 339 i = 0;
ausdong 5:bc0296a5ad8a 340 // Start Critical Section - don't interrupt while changing global buffer variables
ausdong 5:bc0296a5ad8a 341 NVIC_DisableIRQ(UART1_IRQn);
ausdong 5:bc0296a5ad8a 342 // Loop reading rx buffer characters until end of line character
ausdong 5:bc0296a5ad8a 343 while ((i==0) || (rx_line[i-1] != '\r')) {
ausdong 5:bc0296a5ad8a 344 // Wait if buffer empty
ausdong 5:bc0296a5ad8a 345 if (rx_in == rx_out) {
ausdong 5:bc0296a5ad8a 346 // End Critical Section - need to allow rx interrupt to get new characters for buffer
ausdong 5:bc0296a5ad8a 347 NVIC_EnableIRQ(UART1_IRQn);
ausdong 5:bc0296a5ad8a 348 while (rx_in == rx_out) {
ausdong 5:bc0296a5ad8a 349 }
ausdong 5:bc0296a5ad8a 350 // Start Critical Section - don't interrupt while changing global buffer variables
ausdong 5:bc0296a5ad8a 351 NVIC_DisableIRQ(UART1_IRQn);
star297 2:d4c6bc0f2dc4 352 }
ausdong 5:bc0296a5ad8a 353 rx_line[i] = rx_buffer[rx_out];
ausdong 5:bc0296a5ad8a 354 i++;
ausdong 5:bc0296a5ad8a 355 rx_out = (rx_out + 1) % buffer_size;
4180_1 4:40dd020463ea 356 }
ausdong 5:bc0296a5ad8a 357 // End Critical Section
ausdong 5:bc0296a5ad8a 358 NVIC_EnableIRQ(UART1_IRQn);
ausdong 5:bc0296a5ad8a 359 rx_line[i-1] = 0;
ausdong 5:bc0296a5ad8a 360 return;
ausdong 5:bc0296a5ad8a 361 }
ausdong 5:bc0296a5ad8a 362
ausdong 5:bc0296a5ad8a 363
ausdong 5:bc0296a5ad8a 364 // Interupt Routine to read in data from serial port
ausdong 5:bc0296a5ad8a 365 void Rx_interrupt() {
ausdong 5:bc0296a5ad8a 366 DataRX=1;
ausdong 5:bc0296a5ad8a 367 //led3=1;
ausdong 5:bc0296a5ad8a 368 // Loop just in case more than one character is in UART's receive FIFO buffer
ausdong 5:bc0296a5ad8a 369 // Stop if buffer full
ausdong 5:bc0296a5ad8a 370 while ((esp.readable()) && (((rx_in + 1) % buffer_size) != rx_out)) {
ausdong 5:bc0296a5ad8a 371 rx_buffer[rx_in] = esp.getc();
ausdong 5:bc0296a5ad8a 372 // Uncomment to Echo to USB serial to watch data flow
ausdong 5:bc0296a5ad8a 373 pc.putc(rx_buffer[rx_in]);
ausdong 5:bc0296a5ad8a 374 rx_in = (rx_in + 1) % buffer_size;
ausdong 5:bc0296a5ad8a 375 }
ausdong 5:bc0296a5ad8a 376 //led3=0;
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() {
ausdong 5:bc0296a5ad8a 383 //led2=1;
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 }
ausdong 5:bc0296a5ad8a 390 //led2=0;
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 }