For Adafruit HUZZAH ESP8266 Wi Fi chip on the mbed LPC1768. Sets up a web page server that can control a mbed onboard LEDs. Need to configure Wi Fi SSID and PASSWORD first with another program (one time only). See https://developer.mbed.org/users/ausdong/notebook/using-the-adafruit-huzzah-esp8266-to-add-wi-fi-to-/ .

Dependencies:   mbed

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

Committer:
ausdong
Date:
Fri Mar 18 19:10:19 2016 +0000
Revision:
5:bc0296a5ad8a
Parent:
4:40dd020463ea
initial

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