IoT Alarm System with pushbutton for door, webpage for viewing status, and keypad for disabling alarm.

Dependencies:   PinDetect mbed wave_player FATFileSystem

Fork of ESP8266-WEB-Mbed-LPC1768-Controller by jim hamblen

Committer:
gholden3
Date:
Thu Mar 17 12:52:43 2016 +0000
Revision:
6:d5b6ab598544
Parent:
5:28e4c7315a20
fix library dependencies;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
gholden3 5:28e4c7315a20 1 // ECE410 Lab 4 Alarm System Holden&Wasserman
gholden3 5:28e4c7315a20 2 /*
gholden3 5:28e4c7315a20 3 The following describes an Mbed IoT Alarm system. A push button is pressed to signify
gholden3 5:28e4c7315a20 4 an “open door”. The user accesses a web page that displays the statuses of the Mbed Alarm System.
gholden3 5:28e4c7315a20 5 The webpage displays the status of the door, as well as the time, and the background indicates the
gholden3 5:28e4c7315a20 6 alarm state. Once the door is opened, the user has a time limit to turn off the alarm or it will sound.
gholden3 5:28e4c7315a20 7 Once the correct code is keyed in, the alarm will shut off. This status is also displayed on the webpage.
gholden3 5:28e4c7315a20 8 */
gholden3 5:28e4c7315a20 9 //Webserver code based off: ESP8266-WEB-Mbed-LPC1768-Controller
gholden3 5:28e4c7315a20 10
gholden3 5:28e4c7315a20 11 //Further work: get rtos working to play a sound on the speaker for the alarm. Add more detailed states that
gholden3 5:28e4c7315a20 12 //allow the user to key in the code before the alarm sounds. Allow user to turn off alarm from webpage.
star297 0:e2a155f50119 13
star297 0:e2a155f50119 14 #include "mbed.h"
gholden3 5:28e4c7315a20 15 #include "SDFileSystem.h"
gholden3 5:28e4c7315a20 16 #include "wave_player.h"
gholden3 5:28e4c7315a20 17 #define twilight_WAVFILE "/sd/wavfiles/twilight_zone_x.wav"
gholden3 5:28e4c7315a20 18 #include "PinDetect.h"
gholden3 5:28e4c7315a20 19 #include <mbed.h>
gholden3 5:28e4c7315a20 20 #include <mpr121.h>
gholden3 5:28e4c7315a20 21 #include <string>
star297 2:d4c6bc0f2dc4 22
star297 0:e2a155f50119 23 // Standard Mbed LED definitions
gholden3 5:28e4c7315a20 24 DigitalOut led1(LED1);
gholden3 5:28e4c7315a20 25 DigitalOut led2(LED2);
gholden3 5:28e4c7315a20 26 DigitalOut led3(LED3);
gholden3 5:28e4c7315a20 27 DigitalOut led4(LED4);
star297 0:e2a155f50119 28
gholden3 5:28e4c7315a20 29 //wifi chip
gholden3 5:28e4c7315a20 30 Serial pc(USBTX, USBRX);
gholden3 5:28e4c7315a20 31 Serial esp(p28, p27); // tx, rx
gholden3 5:28e4c7315a20 32 DigitalOut reset(p25);
gholden3 5:28e4c7315a20 33
gholden3 5:28e4c7315a20 34 //timer for alarm countdown
gholden3 5:28e4c7315a20 35 Ticker alarmTimer;
gholden3 5:28e4c7315a20 36 float numSecondsRemaining = 30;
star297 0:e2a155f50119 37
gholden3 5:28e4c7315a20 38 // interrupt pin for keypad
gholden3 5:28e4c7315a20 39 InterruptIn interrupt(p26);
gholden3 5:28e4c7315a20 40 // Setup the i2c bus on pins 9 and 10 keypad
gholden3 5:28e4c7315a20 41 I2C i2c(p9, p10);
gholden3 5:28e4c7315a20 42 // Setup the Mpr121: touch sensor
gholden3 5:28e4c7315a20 43 // constructor(i2c object, i2c address of the mpr121)
gholden3 5:28e4c7315a20 44 Mpr121 mpr121(&i2c, Mpr121::ADD_VSS);
star297 0:e2a155f50119 45
gholden3 5:28e4c7315a20 46 //door is triggered by a pushbutton
gholden3 5:28e4c7315a20 47 // no external PullUp resistor needed
gholden3 5:28e4c7315a20 48 // Pushbutton from P12 to GND.
gholden3 5:28e4c7315a20 49 // A pb falling edge (hit) generates an interrupt and activates the interrupt routine
gholden3 5:28e4c7315a20 50 PinDetect doorPB(p12);
gholden3 5:28e4c7315a20 51 //door status variable
gholden3 5:28e4c7315a20 52 enum doorStatus { OPEN, CLOSED };
gholden3 5:28e4c7315a20 53 doorStatus doorStatus = CLOSED;
gholden3 5:28e4c7315a20 54
gholden3 5:28e4c7315a20 55 //alarm status variable
gholden3 5:28e4c7315a20 56 enum alarmStatus { SOUNDING, OFF };
gholden3 5:28e4c7315a20 57 alarmStatus alarm1Status = OFF;
gholden3 5:28e4c7315a20 58
gholden3 5:28e4c7315a20 59 //we need to get rtos working for this :(
4180_1 4:40dd020463ea 60 PwmOut speaker(p21);
star297 0:e2a155f50119 61
gholden3 5:28e4c7315a20 62 //timeout timer for getreply()
star297 0:e2a155f50119 63 Timer t1;
gholden3 5:28e4c7315a20 64 //timeout timer for getcheck()
star297 1:71ed1afbf344 65 Timer t2;
star297 1:71ed1afbf344 66
gholden3 5:28e4c7315a20 67 //struct used for RTC
star297 1:71ed1afbf344 68 struct tm t;
star297 0:e2a155f50119 69
star297 3:f7febfa77784 70 int bufflen, DataRX, count, getcount, replycount, servreq, timeout;
star297 2:d4c6bc0f2dc4 71 int bufl, ipdLen, linkID, weberror, webcounter;
gholden3 5:28e4c7315a20 72 char pwd[] = "1234";
gholden3 5:28e4c7315a20 73 char enterpwd[100] = "";
gholden3 5:28e4c7315a20 74 char secondsRemaining[10];
star297 2:d4c6bc0f2dc4 75 char webcount[8];
star297 3:f7febfa77784 76 char lasthit[30];
star297 0:e2a155f50119 77 char timebuf[30];
star297 0:e2a155f50119 78 char type[16];
star297 0:e2a155f50119 79 char type1[16];
star297 0:e2a155f50119 80 char channel[2];
star297 3:f7febfa77784 81 char cmdbuff[32];
4180_1 4:40dd020463ea 82 char replybuff[1024];
star297 0:e2a155f50119 83 char webdata[1024]; // This may need to be bigger depending on WEB browser used
star297 3:f7febfa77784 84 char webbuff[4096]; // Currently using 1986 characters, Increase this if more web page data added
star297 0:e2a155f50119 85
star297 0:e2a155f50119 86 void SendCMD(),getreply(),ReadWebData(),startserver(),sendpage(),SendWEB(),sendcheck();
gholden3 5:28e4c7315a20 87 void gettime(),setRTC();
gholden3 5:28e4c7315a20 88
gholden3 5:28e4c7315a20 89 void timerExpired() //called every second once door is opened
gholden3 5:28e4c7315a20 90 {
gholden3 5:28e4c7315a20 91 numSecondsRemaining--;
gholden3 5:28e4c7315a20 92 if (numSecondsRemaining == 0) { //your time to disable the alarm is up. sound alarm!
gholden3 5:28e4c7315a20 93 alarmTimer.detach();
gholden3 5:28e4c7315a20 94 alarm1Status = SOUNDING;
gholden3 5:28e4c7315a20 95 }
gholden3 5:28e4c7315a20 96 sprintf(secondsRemaining, "%2.0f",numSecondsRemaining);
gholden3 5:28e4c7315a20 97 }
gholden3 5:28e4c7315a20 98
gholden3 5:28e4c7315a20 99 void pb_hit_callback (void) //door
gholden3 5:28e4c7315a20 100 {
gholden3 5:28e4c7315a20 101 pc.printf("pushbutton hit!\r\n");
gholden3 5:28e4c7315a20 102 pc.printf("door is %s \r\n", doorStatus);
gholden3 5:28e4c7315a20 103 if (doorStatus == OPEN)
gholden3 5:28e4c7315a20 104 doorStatus = CLOSED;
gholden3 5:28e4c7315a20 105 else if (doorStatus == CLOSED) {
gholden3 5:28e4c7315a20 106 doorStatus = OPEN;
gholden3 5:28e4c7315a20 107 //start counting every second
gholden3 5:28e4c7315a20 108 alarmTimer.attach(&timerExpired, 1.0);
gholden3 5:28e4c7315a20 109 }
gholden3 5:28e4c7315a20 110 wait(.1);
gholden3 5:28e4c7315a20 111 }
gholden3 5:28e4c7315a20 112
star297 0:e2a155f50119 113
star297 1:71ed1afbf344 114 // manual set RTC values
4180_1 4:40dd020463ea 115 int minute =00; // 0-59
4180_1 4:40dd020463ea 116 int hour =12; // 2-23
4180_1 4:40dd020463ea 117 int dayofmonth =26; // 1-31
4180_1 4:40dd020463ea 118 int month =8; // 1-12
star297 1:71ed1afbf344 119 int year =15; // last 2 digits
star297 2:d4c6bc0f2dc4 120
4180_1 4:40dd020463ea 121 int port =80; // set server port
4180_1 4:40dd020463ea 122 int SERVtimeout =5; // set server timeout in seconds in case link breaks.
4180_1 4:40dd020463ea 123
star297 0:e2a155f50119 124 // Serial Interrupt read ESP data
4180_1 4:40dd020463ea 125 void callback()
4180_1 4:40dd020463ea 126 {
4180_1 4:40dd020463ea 127 while (esp.readable()) {
4180_1 4:40dd020463ea 128 webbuff[count] = esp.getc();
4180_1 4:40dd020463ea 129 count++;
4180_1 4:40dd020463ea 130 }
4180_1 4:40dd020463ea 131 if(strlen(webbuff)>bufflen) {
4180_1 4:40dd020463ea 132 DataRX=1;
4180_1 4:40dd020463ea 133 }
star297 0:e2a155f50119 134 }
star297 0:e2a155f50119 135
gholden3 5:28e4c7315a20 136 // Key hit/release interrupt routine
gholden3 5:28e4c7315a20 137 void fallInterrupt()
gholden3 5:28e4c7315a20 138 {
gholden3 5:28e4c7315a20 139 int key_code=0;
gholden3 5:28e4c7315a20 140 int i=0;
gholden3 5:28e4c7315a20 141 int value=mpr121.read(0x00);
gholden3 5:28e4c7315a20 142 value +=mpr121.read(0x01)<<8;
gholden3 5:28e4c7315a20 143 // puts key number out to LEDs for demo
gholden3 5:28e4c7315a20 144 for (i=0; i<12; i++) {
gholden3 5:28e4c7315a20 145 if (((value>>i)&0x01)==1) key_code=i+1;
gholden3 5:28e4c7315a20 146 }
gholden3 5:28e4c7315a20 147 if (key_code == 0)
gholden3 5:28e4c7315a20 148 return;
gholden3 5:28e4c7315a20 149 else key_code--;
gholden3 5:28e4c7315a20 150 led4=key_code & 0x01;
gholden3 5:28e4c7315a20 151 led3=(key_code>>1) & 0x01;
gholden3 5:28e4c7315a20 152 led2=(key_code>>2) & 0x01;
gholden3 5:28e4c7315a20 153 led1=(key_code>>3) & 0x01;
gholden3 5:28e4c7315a20 154 char result[10];
gholden3 5:28e4c7315a20 155 //convert float to cstring
gholden3 5:28e4c7315a20 156 sprintf(result,"%d",key_code);
gholden3 5:28e4c7315a20 157 pc.printf("result: %s\r\n", result);
gholden3 5:28e4c7315a20 158 //concatenate to the end
gholden3 5:28e4c7315a20 159 strcat(enterpwd,result);
gholden3 5:28e4c7315a20 160 pc.printf("enterpwd: %s\r\n",enterpwd);
gholden3 5:28e4c7315a20 161 //only compares the first four values entered. future work: make a clear/reset option
gholden3 5:28e4c7315a20 162 if((enterpwd[0]==pwd[0]) && (enterpwd[1]==pwd[1]) && (enterpwd[2]==pwd[2]) && (enterpwd[3]==pwd[3])){
gholden3 5:28e4c7315a20 163 alarm1Status=OFF;
gholden3 5:28e4c7315a20 164 alarmTimer.detach();
gholden3 5:28e4c7315a20 165 pc.printf("correct!\r\n");
gholden3 5:28e4c7315a20 166 }
gholden3 5:28e4c7315a20 167 }
gholden3 5:28e4c7315a20 168
gholden3 5:28e4c7315a20 169
4180_1 4:40dd020463ea 170 int main()
4180_1 4:40dd020463ea 171 {
gholden3 5:28e4c7315a20 172 //attach interrupt for keypad
gholden3 5:28e4c7315a20 173 interrupt.fall(&fallInterrupt);
gholden3 5:28e4c7315a20 174 interrupt.mode(PullUp);
gholden3 5:28e4c7315a20 175
gholden3 5:28e4c7315a20 176 doorPB.mode(PullUp);
gholden3 5:28e4c7315a20 177 // Delay for initial pullup to take effect
gholden3 5:28e4c7315a20 178 wait(.01);
gholden3 5:28e4c7315a20 179 // Attach the address of the interrupt handler routine for pushbutton
gholden3 5:28e4c7315a20 180 doorPB.attach_deasserted(&pb_hit_callback);
gholden3 5:28e4c7315a20 181 doorPB.setSampleFrequency();
4180_1 4:40dd020463ea 182 reset=0;
star297 0:e2a155f50119 183 pc.baud(115200);
4180_1 4:40dd020463ea 184 pc.printf("\f\n\r------------ ESP8266 Hardware Reset --------------\n\r");
4180_1 4:40dd020463ea 185 wait(0.5);
4180_1 4:40dd020463ea 186 reset=1;
4180_1 4:40dd020463ea 187 timeout=6000;
4180_1 4:40dd020463ea 188 getcount=500;
4180_1 4:40dd020463ea 189 getreply();
gholden3 5:28e4c7315a20 190 // ESP8266 baudrate. Maximum on KLxx' is 115200, 230400 works on K20 and K22F
gholden3 5:28e4c7315a20 191 esp.baud(115200);
4180_1 4:40dd020463ea 192 if (time(NULL) < 1420070400) {
4180_1 4:40dd020463ea 193 setRTC();
4180_1 4:40dd020463ea 194 }
star297 0:e2a155f50119 195 startserver();
4180_1 4:40dd020463ea 196 while(1) {
4180_1 4:40dd020463ea 197 if(DataRX==1) {
star297 0:e2a155f50119 198 ReadWebData();
4180_1 4:40dd020463ea 199 if (servreq == 1 && weberror == 0) {
4180_1 4:40dd020463ea 200 sendpage();
4180_1 4:40dd020463ea 201 }
4180_1 4:40dd020463ea 202 esp.attach(&callback);
4180_1 4:40dd020463ea 203 pc.printf(" IPD Data:\r\n\n Link ID = %d,\r\n IPD Header Length = %d \r\n IPD Type = %s\r\n", linkID, ipdLen, type);
4180_1 4:40dd020463ea 204 pc.printf("\n\n HTTP Packet: \n\n%s\n", webdata);
star297 0:e2a155f50119 205 pc.printf(" Web Characters sent : %d\n\n", bufl);
star297 0:e2a155f50119 206 pc.printf(" -------------------------------------\n\n");
star297 2:d4c6bc0f2dc4 207 strcpy(lasthit, timebuf);
4180_1 4:40dd020463ea 208 servreq=0;
4180_1 4:40dd020463ea 209 }
star297 0:e2a155f50119 210 }
4180_1 4:40dd020463ea 211 }
gholden3 5:28e4c7315a20 212
gholden3 5:28e4c7315a20 213
4180_1 4:40dd020463ea 214 // Static WEB page
star297 0:e2a155f50119 215 void sendpage()
4180_1 4:40dd020463ea 216 {
4180_1 4:40dd020463ea 217 gettime();
gholden3 5:28e4c7315a20 218 // WEB page data
star297 3:f7febfa77784 219 strcpy(webbuff, "<!DOCTYPE html>");
4180_1 4:40dd020463ea 220 strcat(webbuff, "<html><head><title>ESP8266 Mbed LPC1768</title></head>");
gholden3 5:28e4c7315a20 221 if(alarm1Status == SOUNDING) {
gholden3 5:28e4c7315a20 222 strcat(webbuff, "<body style=\"background-color:#FF0000;\" >");
gholden3 5:28e4c7315a20 223 } else if (alarm1Status==OFF) {
gholden3 5:28e4c7315a20 224 strcat(webbuff, "<body style=\" background-color:#32CD32;\" >");
gholden3 5:28e4c7315a20 225 }
gholden3 5:28e4c7315a20 226 strcat(webbuff, "<div style=\"text-align:center; background-color:#F4F4F4; color:#00AEDB;\"><h1>Mbed Alarm System</h1>");
star297 3:f7febfa77784 227 strcat(webbuff, "Hit Count - ");
star297 3:f7febfa77784 228 strcat(webbuff, webcount);
star297 3:f7febfa77784 229 strcat(webbuff, "<br>Last Hit - ");
4180_1 4:40dd020463ea 230 strcat(webbuff, lasthit);
star297 3:f7febfa77784 231 strcat(webbuff, "</div><br /><hr>");
4180_1 4:40dd020463ea 232 strcat(webbuff, "<h3>Mbed RTC Time -&nbsp&nbsp");
star297 3:f7febfa77784 233 strcat(webbuff, timebuf);
star297 3:f7febfa77784 234 strcat(webbuff, "</h3>\r\n");
gholden3 5:28e4c7315a20 235 strcat(webbuff, "<p><form method=\"POST\"><strong> Door Status:&nbsp&nbsp<input type=\"text\" size=6 value=\"");
gholden3 5:28e4c7315a20 236 if(doorStatus == CLOSED)
gholden3 5:28e4c7315a20 237 strcat(webbuff, "CLOSED");
gholden3 5:28e4c7315a20 238 else if(doorStatus == OPEN)
gholden3 5:28e4c7315a20 239 strcat(webbuff, "OPEN");
gholden3 5:28e4c7315a20 240 strcat(webbuff, "\"> </sup> <form method=\"POST\"> <strong> &nbsp&nbspSeconds Remaining:&nbsp&nbsp<input type=\"text\" size=4 value=\"");
gholden3 5:28e4c7315a20 241 strcat(webbuff, secondsRemaining);
gholden3 5:28e4c7315a20 242 strcat(webbuff, "\"> </sup>");
4180_1 4:40dd020463ea 243 strcat(webbuff, "</strong><p><input type=\"submit\" value=\"send-refresh\" style=\"background: #3498db;");
4180_1 4:40dd020463ea 244 strcat(webbuff, "background-image:-webkit-linear-gradient(top, #3498db, #2980b9);");
star297 3:f7febfa77784 245 strcat(webbuff, "background-image:linear-gradient(to bottom, #3498db, #2980b9);");
star297 3:f7febfa77784 246 strcat(webbuff, "-webkit-border-radius:12;border-radius: 12px;font-family: Arial;color:#ffffff;font-size:20px;padding:");
4180_1 4:40dd020463ea 247 strcat(webbuff, "10px 20px 10px 20px; border:solid #103c57 3px;text-decoration: none;");
star297 3:f7febfa77784 248 strcat(webbuff, "background: #3cb0fd;");
4180_1 4:40dd020463ea 249 strcat(webbuff, "background-image:-webkit-linear-gradient(top,#3cb0fd,#1a5f8a);");
star297 3:f7febfa77784 250 strcat(webbuff, "background-image:linear-gradient(to bottom,#3cb0fd,#1a5f8a);");
star297 3:f7febfa77784 251 strcat(webbuff, "text-decoration:none;\"></form></span>");
star297 3:f7febfa77784 252 strcat(webbuff, "<p/><h2>How to use:</h2><ul>");
gholden3 5:28e4c7315a20 253 strcat(webbuff, "<li>Background color reflects alarm state. Green is good red is bad. </li>");
gholden3 5:28e4c7315a20 254 strcat(webbuff, "<li>You can refresh the page with the browser button or send/refresh.</li>");
gholden3 5:28e4c7315a20 255 strcat(webbuff, "<li>Seconds remaining tells you how much time before alarm sounds</li>");
star297 3:f7febfa77784 256 strcat(webbuff, "</ul>");
4180_1 4:40dd020463ea 257 strcat(webbuff, "</body></html>");
4180_1 4:40dd020463ea 258 // end of WEB page data
4180_1 4:40dd020463ea 259 bufl = strlen(webbuff); // get total page buffer length
star297 3:f7febfa77784 260 sprintf(cmdbuff,"AT+CIPSEND=%d,%d\r\n", linkID, bufl); // send IPD link channel and buffer character length.
4180_1 4:40dd020463ea 261 timeout=200;
4180_1 4:40dd020463ea 262 getcount=7;
star297 3:f7febfa77784 263 SendCMD();
4180_1 4:40dd020463ea 264 getreply();
star297 0:e2a155f50119 265 SendWEB(); // send web page
star297 3:f7febfa77784 266 memset(webbuff, '\0', sizeof(webbuff));
4180_1 4:40dd020463ea 267 sendcheck();
star297 0:e2a155f50119 268 }
star297 0:e2a155f50119 269
star297 3:f7febfa77784 270 // wait for ESP "SEND OK" reply, then close IP to load web page
star297 0:e2a155f50119 271 void sendcheck()
star297 0:e2a155f50119 272 {
4180_1 4:40dd020463ea 273 weberror=1;
4180_1 4:40dd020463ea 274 timeout=500;
4180_1 4:40dd020463ea 275 getcount=24;
4180_1 4:40dd020463ea 276 t2.reset();
4180_1 4:40dd020463ea 277 t2.start();
4180_1 4:40dd020463ea 278 while(weberror==1 && t2.read() <5) {
star297 0:e2a155f50119 279 getreply();
4180_1 4:40dd020463ea 280 if (strstr(replybuff, "SEND OK") != NULL) {
4180_1 4:40dd020463ea 281 weberror=0; // wait for valid SEND OK
star297 0:e2a155f50119 282 }
4180_1 4:40dd020463ea 283 }
4180_1 4:40dd020463ea 284 if(weberror==1) { // restart connection
4180_1 4:40dd020463ea 285 strcpy(cmdbuff, "AT+CIPMUX=1\r\n");
4180_1 4:40dd020463ea 286 timeout=500;
4180_1 4:40dd020463ea 287 getcount=10;
4180_1 4:40dd020463ea 288 SendCMD();
4180_1 4:40dd020463ea 289 getreply();
4180_1 4:40dd020463ea 290 pc.printf(replybuff);
star297 3:f7febfa77784 291 sprintf(cmdbuff,"AT+CIPSERVER=1,%d\r\n", port);
4180_1 4:40dd020463ea 292 timeout=500;
4180_1 4:40dd020463ea 293 getcount=10;
4180_1 4:40dd020463ea 294 SendCMD();
4180_1 4:40dd020463ea 295 getreply();
4180_1 4:40dd020463ea 296 pc.printf(replybuff);
4180_1 4:40dd020463ea 297 } else {
4180_1 4:40dd020463ea 298 sprintf(cmdbuff, "AT+CIPCLOSE=%s\r\n",channel); // close current connection
4180_1 4:40dd020463ea 299 SendCMD();
4180_1 4:40dd020463ea 300 getreply();
4180_1 4:40dd020463ea 301 pc.printf(replybuff);
4180_1 4:40dd020463ea 302 }
4180_1 4:40dd020463ea 303 t2.reset();
4180_1 4:40dd020463ea 304 }
star297 0:e2a155f50119 305
4180_1 4:40dd020463ea 306 // Reads and processes GET and POST web data
star297 0:e2a155f50119 307 void ReadWebData()
4180_1 4:40dd020463ea 308 {
4180_1 4:40dd020463ea 309 wait_ms(200);
star297 3:f7febfa77784 310 esp.attach(NULL);
4180_1 4:40dd020463ea 311 count=0;
4180_1 4:40dd020463ea 312 DataRX=0;
4180_1 4:40dd020463ea 313 weberror=0;
4180_1 4:40dd020463ea 314 memset(webdata, '\0', sizeof(webdata));
star297 3:f7febfa77784 315 int x = strcspn (webbuff,"+");
4180_1 4:40dd020463ea 316 if(x) {
4180_1 4:40dd020463ea 317 strcpy(webdata, webbuff + x);
4180_1 4:40dd020463ea 318 weberror=0;
4180_1 4:40dd020463ea 319 int numMatched = sscanf(webdata,"+IPD,%d,%d:%s", &linkID, &ipdLen, type);
4180_1 4:40dd020463ea 320 sprintf(channel, "%d",linkID);
4180_1 4:40dd020463ea 321 if (strstr(webdata, "GET") != NULL) {
4180_1 4:40dd020463ea 322 servreq=1;
4180_1 4:40dd020463ea 323 }
4180_1 4:40dd020463ea 324 if (strstr(webdata, "POST") != NULL) {
4180_1 4:40dd020463ea 325 servreq=1;
4180_1 4:40dd020463ea 326 }
star297 2:d4c6bc0f2dc4 327 webcounter++;
star297 2:d4c6bc0f2dc4 328 sprintf(webcount, "%d",webcounter);
4180_1 4:40dd020463ea 329 } else {
4180_1 4:40dd020463ea 330 memset(webbuff, '\0', sizeof(webbuff));
4180_1 4:40dd020463ea 331 esp.attach(&callback);
4180_1 4:40dd020463ea 332 weberror=1;
4180_1 4:40dd020463ea 333 }
star297 0:e2a155f50119 334 }
star297 0:e2a155f50119 335 // Starts and restarts webserver if errors detected.
star297 0:e2a155f50119 336 void startserver()
star297 0:e2a155f50119 337 {
4180_1 4:40dd020463ea 338 gettime();
star297 1:71ed1afbf344 339 pc.printf("\n\n RTC time %s\r\n\n",timebuf);
4180_1 4:40dd020463ea 340 pc.printf("++++++++++ Resetting ESP ++++++++++\r\n");
star297 3:f7febfa77784 341 strcpy(cmdbuff,"AT+RST\r\n");
4180_1 4:40dd020463ea 342 timeout=8000;
4180_1 4:40dd020463ea 343 getcount=1000;
star297 0:e2a155f50119 344 SendCMD();
star297 1:71ed1afbf344 345 getreply();
star297 3:f7febfa77784 346 pc.printf(replybuff);
star297 3:f7febfa77784 347 pc.printf("%d",count);
star297 3:f7febfa77784 348 if (strstr(replybuff, "OK") != NULL) {
star297 1:71ed1afbf344 349 pc.printf("\n++++++++++ Starting Server ++++++++++\r\n");
4180_1 4:40dd020463ea 350 strcpy(cmdbuff, "AT+CIPMUX=1\r\n"); // set multiple connections.
4180_1 4:40dd020463ea 351 timeout=500;
4180_1 4:40dd020463ea 352 getcount=20;
star297 1:71ed1afbf344 353 SendCMD();
star297 2:d4c6bc0f2dc4 354 getreply();
4180_1 4:40dd020463ea 355 pc.printf(replybuff);
star297 3:f7febfa77784 356 sprintf(cmdbuff,"AT+CIPSERVER=1,%d\r\n", port);
4180_1 4:40dd020463ea 357 timeout=500;
4180_1 4:40dd020463ea 358 getcount=20;
star297 1:71ed1afbf344 359 SendCMD();
star297 3:f7febfa77784 360 getreply();
4180_1 4:40dd020463ea 361 pc.printf(replybuff);
4180_1 4:40dd020463ea 362 wait(1);
star297 3:f7febfa77784 363 sprintf(cmdbuff,"AT+CIPSTO=%d\r\n",SERVtimeout);
4180_1 4:40dd020463ea 364 timeout=500;
4180_1 4:40dd020463ea 365 getcount=50;
star297 3:f7febfa77784 366 SendCMD();
star297 2:d4c6bc0f2dc4 367 getreply();
4180_1 4:40dd020463ea 368 pc.printf(replybuff);
4180_1 4:40dd020463ea 369 wait(5);
4180_1 4:40dd020463ea 370 pc.printf("\n Getting Server IP \r\n");
star297 3:f7febfa77784 371 strcpy(cmdbuff, "AT+CIFSR\r\n");
4180_1 4:40dd020463ea 372 timeout=2500;
4180_1 4:40dd020463ea 373 getcount=200;
4180_1 4:40dd020463ea 374 while(weberror==0) {
4180_1 4:40dd020463ea 375 SendCMD();
4180_1 4:40dd020463ea 376 getreply();
4180_1 4:40dd020463ea 377 if (strstr(replybuff, "0.0.0.0") == NULL) {
4180_1 4:40dd020463ea 378 weberror=1; // wait for valid IP
star297 1:71ed1afbf344 379 }
4180_1 4:40dd020463ea 380 }
4180_1 4:40dd020463ea 381 pc.printf("\n Enter WEB address (IP) found below in your browser \r\n\n");
4180_1 4:40dd020463ea 382 pc.printf("\n The MAC address is also shown below,if it is needed \r\n\n");
4180_1 4:40dd020463ea 383 replybuff[strlen(replybuff)-1] = '\0';
4180_1 4:40dd020463ea 384 //char* IP = replybuff + 5;
4180_1 4:40dd020463ea 385 sprintf(webdata,"%s", replybuff);
4180_1 4:40dd020463ea 386 pc.printf(webdata);
gholden3 5:28e4c7315a20 387 // led2=1;
4180_1 4:40dd020463ea 388 bufflen=200;
4180_1 4:40dd020463ea 389 count=0;
star297 1:71ed1afbf344 390 pc.printf("\n\n++++++++++ Ready ++++++++++\r\n\n");
star297 1:71ed1afbf344 391 esp.attach(&callback);
4180_1 4:40dd020463ea 392 } else {
4180_1 4:40dd020463ea 393 pc.printf("\n++++++++++ ESP8266 error, check power/connections ++++++++++\r\n");
4180_1 4:40dd020463ea 394 while(1) {}
4180_1 4:40dd020463ea 395 }
4180_1 4:40dd020463ea 396 t2.reset();
4180_1 4:40dd020463ea 397 t2.start();
4180_1 4:40dd020463ea 398 }
gholden3 5:28e4c7315a20 399
star297 0:e2a155f50119 400 // ESP Command data send
star297 0:e2a155f50119 401 void SendCMD()
star297 0:e2a155f50119 402 {
4180_1 4:40dd020463ea 403 esp.printf("%s", cmdbuff);
4180_1 4:40dd020463ea 404 }
gholden3 5:28e4c7315a20 405
star297 0:e2a155f50119 406 // Large WEB buffer data send
star297 0:e2a155f50119 407 void SendWEB()
4180_1 4:40dd020463ea 408 {
star297 0:e2a155f50119 409 int i=0;
star297 0:e2a155f50119 410 if(esp.writeable()) {
4180_1 4:40dd020463ea 411 while(webbuff[i]!='\0') {
4180_1 4:40dd020463ea 412 esp.putc(webbuff[i]);
4180_1 4:40dd020463ea 413 i++;
4180_1 4:40dd020463ea 414 }
4180_1 4:40dd020463ea 415 }
4180_1 4:40dd020463ea 416 }
gholden3 5:28e4c7315a20 417
4180_1 4:40dd020463ea 418 // Get Command and ESP status replies
star297 0:e2a155f50119 419 void getreply()
4180_1 4:40dd020463ea 420 {
star297 3:f7febfa77784 421 memset(replybuff, '\0', sizeof(replybuff));
4180_1 4:40dd020463ea 422 t1.reset();
4180_1 4:40dd020463ea 423 t1.start();
4180_1 4:40dd020463ea 424 replycount=0;
star297 3:f7febfa77784 425 while(t1.read_ms()< timeout && replycount < getcount) {
star297 0:e2a155f50119 426 if(esp.readable()) {
4180_1 4:40dd020463ea 427 replybuff[replycount] = esp.getc();
4180_1 4:40dd020463ea 428 replycount++;
star297 2:d4c6bc0f2dc4 429 }
4180_1 4:40dd020463ea 430 }
4180_1 4:40dd020463ea 431 t1.stop();
star297 0:e2a155f50119 432 }
gholden3 5:28e4c7315a20 433
4180_1 4:40dd020463ea 434 // Get RTC time
star297 0:e2a155f50119 435 void gettime()
star297 0:e2a155f50119 436 {
star297 0:e2a155f50119 437 time_t seconds = time(NULL);
4180_1 4:40dd020463ea 438 strftime(timebuf,50,"%H:%M:%S %a %d %b %y", localtime(&seconds));
star297 0:e2a155f50119 439 }
star297 0:e2a155f50119 440
star297 1:71ed1afbf344 441 void setRTC()
star297 1:71ed1afbf344 442 {
4180_1 4:40dd020463ea 443 t.tm_sec = (0); // 0-59
4180_1 4:40dd020463ea 444 t.tm_min = (minute); // 0-59
4180_1 4:40dd020463ea 445 t.tm_hour = (hour); // 0-23
4180_1 4:40dd020463ea 446 t.tm_mday = (dayofmonth); // 1-31
4180_1 4:40dd020463ea 447 t.tm_mon = (month-1); // 0-11 "0" = Jan, -1 added for Mbed RCT clock format
4180_1 4:40dd020463ea 448 t.tm_year = ((year)+100); // year since 1900, current DCF year + 100 + 1900 = correct year
4180_1 4:40dd020463ea 449 set_time(mktime(&t)); // set RTC clock
4180_1 4:40dd020463ea 450 }