4180 mini project

Dependencies:   LSM9DS1_Library_cal SDFileSystem mbed wave_player

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

Committer:
mikebenq
Date:
Tue Mar 14 01:03:15 2017 +0000
Revision:
6:9244b4b7485c
Parent:
5:bc0296a5ad8a
ECE4180 project

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"
mikebenq 6:9244b4b7485c 4 #include "LSM9DS1.h"
mikebenq 6:9244b4b7485c 5 #include "SDFileSystem.h"
mikebenq 6:9244b4b7485c 6 #include "wave_player.h"
mikebenq 6:9244b4b7485c 7 #include "SongPlayer.h"
mikebenq 6:9244b4b7485c 8 float note[18]= {1568.0,1396.9,1244.5,1244.5,1396.9,1568.0,1568.0,1568.0,1396.9,
mikebenq 6:9244b4b7485c 9 1244.5,1396.9,1568.0,1396.9,1244.5,1174.7,1244.5,1244.5, 0.0
mikebenq 6:9244b4b7485c 10 };
mikebenq 6:9244b4b7485c 11 float duration[18]= {0.48,0.24,0.72,0.48,0.24,0.48,0.24,0.24,0.24,
mikebenq 6:9244b4b7485c 12 0.24,0.24,0.24,0.24,0.48,0.24,0.48,0.48, 0.0
mikebenq 6:9244b4b7485c 13 };
mikebenq 6:9244b4b7485c 14 SongPlayer mySpeaker(p26);
mikebenq 6:9244b4b7485c 15 #define PI 3.14159
mikebenq 6:9244b4b7485c 16 // Earth's magnetic field varies by location. Add or subtract
mikebenq 6:9244b4b7485c 17 // a declination to get a more accurate heading. Calculate
mikebenq 6:9244b4b7485c 18 // your's here:
mikebenq 6:9244b4b7485c 19 // http://www.ngdc.noaa.gov/geomag-web/#declination
mikebenq 6:9244b4b7485c 20 #define DECLINATION -4.94 // Declination (degrees) in Atlanta,GA.
mikebenq 6:9244b4b7485c 21
star297 0:e2a155f50119 22
star297 0:e2a155f50119 23 Serial pc(USBTX, USBRX);
mikebenq 6:9244b4b7485c 24 Serial esp(p9, p10); // tx, rx
star297 0:e2a155f50119 25
star297 2:d4c6bc0f2dc4 26
star297 0:e2a155f50119 27 // Standard Mbed LED definitions
ausdong 5:bc0296a5ad8a 28 DigitalOut led1(LED1);
ausdong 5:bc0296a5ad8a 29 DigitalOut led2(LED2);
ausdong 5:bc0296a5ad8a 30 DigitalOut led3(LED3);
ausdong 5:bc0296a5ad8a 31 DigitalOut led4(LED4);
star297 0:e2a155f50119 32
ausdong 5:bc0296a5ad8a 33 // some test values to show on web page
mikebenq 6:9244b4b7485c 34 AnalogIn Ain1(p19);
mikebenq 6:9244b4b7485c 35 AnalogIn Ain2(p20);
star297 0:e2a155f50119 36
ausdong 5:bc0296a5ad8a 37 /*
ausdong 5:bc0296a5ad8a 38 char ssid[32] = "hsd"; // enter WiFi router ssid inside the quotes
ausdong 5:bc0296a5ad8a 39 char pwd [32] = "austin123"; // enter WiFi router password inside the quotes
ausdong 5:bc0296a5ad8a 40 */
4180_1 4:40dd020463ea 41 float temperature, AdcIn, Ht;
star297 0:e2a155f50119 42 float R1=100000, R2=10000; // resistor values to give a 10:1 reduction of measured AnalogIn voltage
star297 0:e2a155f50119 43 char Vcc[10];
star297 0:e2a155f50119 44 char Temp[10];
ausdong 5:bc0296a5ad8a 45
ausdong 5:bc0296a5ad8a 46 // things for sending/receiving data over serial
ausdong 5:bc0296a5ad8a 47 volatile int tx_in=0;
ausdong 5:bc0296a5ad8a 48 volatile int tx_out=0;
ausdong 5:bc0296a5ad8a 49 volatile int rx_in=0;
ausdong 5:bc0296a5ad8a 50 volatile int rx_out=0;
ausdong 5:bc0296a5ad8a 51 const int buffer_size = 4095;
ausdong 5:bc0296a5ad8a 52 char tx_buffer[buffer_size+1];
ausdong 5:bc0296a5ad8a 53 char rx_buffer[buffer_size+1];
ausdong 5:bc0296a5ad8a 54 void Tx_interrupt();
ausdong 5:bc0296a5ad8a 55 void Rx_interrupt();
ausdong 5:bc0296a5ad8a 56 void send_line();
ausdong 5:bc0296a5ad8a 57 void read_line();
ausdong 5:bc0296a5ad8a 58
ausdong 5:bc0296a5ad8a 59 int DataRX;
ausdong 5:bc0296a5ad8a 60 int update;
ausdong 5:bc0296a5ad8a 61 int count;
ausdong 5:bc0296a5ad8a 62 char cmdbuff[1024];
ausdong 5:bc0296a5ad8a 63 char replybuff[4096];
ausdong 5:bc0296a5ad8a 64 char webdata[4096]; // This may need to be bigger depending on WEB browser used
ausdong 5:bc0296a5ad8a 65 char webbuff[4096]; // Currently using 1986 characters, Increase this if more web page data added
star297 0:e2a155f50119 66 char timebuf[30];
ausdong 5:bc0296a5ad8a 67 void SendCMD(),getreply(),ReadWebData(),startserver();
ausdong 5:bc0296a5ad8a 68 void gettime(),setRTC(),gettemp(),getbattery();
ausdong 5:bc0296a5ad8a 69 char rx_line[1024];
ausdong 5:bc0296a5ad8a 70 int port =80; // set server port
ausdong 5:bc0296a5ad8a 71 int SERVtimeout =5; // set server timeout in seconds in case link breaks.
ausdong 5:bc0296a5ad8a 72 struct tm t;
star297 1:71ed1afbf344 73 // manual set RTC values
4180_1 4:40dd020463ea 74 int minute =00; // 0-59
4180_1 4:40dd020463ea 75 int hour =12; // 2-23
4180_1 4:40dd020463ea 76 int dayofmonth =26; // 1-31
4180_1 4:40dd020463ea 77 int month =8; // 1-12
star297 1:71ed1afbf344 78 int year =15; // last 2 digits
mikebenq 6:9244b4b7485c 79 //////////GYRO////////////////
mikebenq 6:9244b4b7485c 80 LSM9DS1 IMU(p28, p27, 0xD6, 0x3C);
mikebenq 6:9244b4b7485c 81 float tempx=0;
mikebenq 6:9244b4b7485c 82 float tempy=0;
mikebenq 6:9244b4b7485c 83 float tempz=0;
mikebenq 6:9244b4b7485c 84 float delx=0;
mikebenq 6:9244b4b7485c 85 float dely=0;
mikebenq 6:9244b4b7485c 86 float delz=0;
mikebenq 6:9244b4b7485c 87 float del=0;
mikebenq 6:9244b4b7485c 88 int result=0;
mikebenq 6:9244b4b7485c 89 int pre_result=0;
mikebenq 6:9244b4b7485c 90 //////////SDcard&waveplayer////////////////
mikebenq 6:9244b4b7485c 91 SDFileSystem sd(p11, p12, p13, p16, "sd"); //SD card
mikebenq 6:9244b4b7485c 92 AnalogOut DACout(p18);
mikebenq 6:9244b4b7485c 93 wave_player waver(&DACout);
4180_1 4:40dd020463ea 94 int main()
4180_1 4:40dd020463ea 95 {
ausdong 5:bc0296a5ad8a 96 pc.baud(9600);
ausdong 5:bc0296a5ad8a 97 esp.baud(9600);
ausdong 5:bc0296a5ad8a 98 led1=1,led2=0,led3=0, led4=0;
ausdong 5:bc0296a5ad8a 99 // Setup a serial interrupt function to receive data
ausdong 5:bc0296a5ad8a 100 esp.attach(&Rx_interrupt, Serial::RxIrq);
ausdong 5:bc0296a5ad8a 101 // Setup a serial interrupt function to transmit data
ausdong 5:bc0296a5ad8a 102 esp.attach(&Tx_interrupt, Serial::TxIrq);
4180_1 4:40dd020463ea 103 if (time(NULL) < 1420070400) {
4180_1 4:40dd020463ea 104 setRTC();
4180_1 4:40dd020463ea 105 }
star297 0:e2a155f50119 106 startserver();
ausdong 5:bc0296a5ad8a 107 DataRX=0;
ausdong 5:bc0296a5ad8a 108 count=0;
mikebenq 6:9244b4b7485c 109 ///////////////////////////
mikebenq 6:9244b4b7485c 110 IMU.begin();
mikebenq 6:9244b4b7485c 111 if (!IMU.begin()) {
mikebenq 6:9244b4b7485c 112 pc.printf("Failed to communicate with LSM9DS1.\n");
mikebenq 6:9244b4b7485c 113 }
mikebenq 6:9244b4b7485c 114 IMU.calibrate(1);
mikebenq 6:9244b4b7485c 115 //IMU.calibrateMag(0);
mikebenq 6:9244b4b7485c 116 FILE *wave_file;
4180_1 4:40dd020463ea 117 while(1) {
mikebenq 6:9244b4b7485c 118 wave_file=fopen("/sd/sample.wav","r");
mikebenq 6:9244b4b7485c 119 if(wave_file==NULL)
mikebenq 6:9244b4b7485c 120 error("Could not open file for write\n");
mikebenq 6:9244b4b7485c 121 while(!IMU.accelAvailable());
mikebenq 6:9244b4b7485c 122 tempx=IMU.calcAccel(IMU.ax);
mikebenq 6:9244b4b7485c 123 tempy=IMU.calcAccel(IMU.ay);
mikebenq 6:9244b4b7485c 124 tempz=IMU.calcAccel(IMU.az);
mikebenq 6:9244b4b7485c 125 IMU.readAccel();
mikebenq 6:9244b4b7485c 126 delx=tempx-IMU.calcAccel(IMU.ax);
mikebenq 6:9244b4b7485c 127 dely=tempy-IMU.calcAccel(IMU.ay);
mikebenq 6:9244b4b7485c 128 delz=tempz-IMU.calcAccel(IMU.az);
mikebenq 6:9244b4b7485c 129 del=delx*delx+dely*dely+delz*delz;
mikebenq 6:9244b4b7485c 130 //pc.printf("del=%f",del);
mikebenq 6:9244b4b7485c 131 pre_result=result;
mikebenq 6:9244b4b7485c 132 if(del>0.01)
mikebenq 6:9244b4b7485c 133 led1=1;
mikebenq 6:9244b4b7485c 134 else
mikebenq 6:9244b4b7485c 135 led1=0;
mikebenq 6:9244b4b7485c 136 if(del>0.1){
mikebenq 6:9244b4b7485c 137 led2=1;
mikebenq 6:9244b4b7485c 138 }
mikebenq 6:9244b4b7485c 139 else
mikebenq 6:9244b4b7485c 140 led2=0;
mikebenq 6:9244b4b7485c 141 if(del>1){
mikebenq 6:9244b4b7485c 142 led3=1;
mikebenq 6:9244b4b7485c 143
mikebenq 6:9244b4b7485c 144 result=1;
mikebenq 6:9244b4b7485c 145 //mySpeaker.PlaySong(note,duration);
mikebenq 6:9244b4b7485c 146 }
mikebenq 6:9244b4b7485c 147 else{
mikebenq 6:9244b4b7485c 148 led3=0;
mikebenq 6:9244b4b7485c 149 result=0;
mikebenq 6:9244b4b7485c 150 }
mikebenq 6:9244b4b7485c 151
mikebenq 6:9244b4b7485c 152 if(result==pre_result)
mikebenq 6:9244b4b7485c 153 {
mikebenq 6:9244b4b7485c 154 }
mikebenq 6:9244b4b7485c 155 else
mikebenq 6:9244b4b7485c 156 {
mikebenq 6:9244b4b7485c 157 sprintf(cmdbuff, "del,time,analog1,analog2=%d,\"%s\",\"%s\",\"%s\"\r\n",result,timebuf,Temp,Vcc);
mikebenq 6:9244b4b7485c 158 SendCMD();
mikebenq 6:9244b4b7485c 159 getreply();
mikebenq 6:9244b4b7485c 160 //sprintf(cndbuff,"\r\n")
mikebenq 6:9244b4b7485c 161 }
mikebenq 6:9244b4b7485c 162 if(result==1)
mikebenq 6:9244b4b7485c 163 waver.play(wave_file);
mikebenq 6:9244b4b7485c 164 fclose(wave_file);
4180_1 4:40dd020463ea 165 if(DataRX==1) {
star297 0:e2a155f50119 166 ReadWebData();
ausdong 5:bc0296a5ad8a 167 esp.attach(&Rx_interrupt, Serial::RxIrq);
ausdong 5:bc0296a5ad8a 168 }
ausdong 5:bc0296a5ad8a 169 if(update==1) // update time, hit count, and analog levels in the HUZZAH chip
mikebenq 6:9244b4b7485c 170 //if(1)
ausdong 5:bc0296a5ad8a 171 {
mikebenq 6:9244b4b7485c 172 led4=1;
mikebenq 6:9244b4b7485c 173 wait(1);
mikebenq 6:9244b4b7485c 174 led4=0;
mikebenq 6:9244b4b7485c 175 mkdir("/sd/mydir", 0777);
mikebenq 6:9244b4b7485c 176
mikebenq 6:9244b4b7485c 177 FILE *fp = fopen("/sd/mydir/sdtest.txt", "w");
mikebenq 6:9244b4b7485c 178 if(fp == NULL) {
mikebenq 6:9244b4b7485c 179 error("Could not open file for write\n");
mikebenq 6:9244b4b7485c 180 }
mikebenq 6:9244b4b7485c 181 char* data=strstr(webdata,"message=");
mikebenq 6:9244b4b7485c 182 fprintf(fp, &data[8]);
mikebenq 6:9244b4b7485c 183 fclose(fp);
mikebenq 6:9244b4b7485c 184 update=0;
4180_1 4:40dd020463ea 185 }
star297 0:e2a155f50119 186 }
4180_1 4:40dd020463ea 187 }
star297 0:e2a155f50119 188
4180_1 4:40dd020463ea 189 // Reads and processes GET and POST web data
star297 0:e2a155f50119 190 void ReadWebData()
4180_1 4:40dd020463ea 191 {
4180_1 4:40dd020463ea 192 wait_ms(200);
ausdong 5:bc0296a5ad8a 193 esp.attach(NULL,Serial::RxIrq);
4180_1 4:40dd020463ea 194 DataRX=0;
4180_1 4:40dd020463ea 195 memset(webdata, '\0', sizeof(webdata));
ausdong 5:bc0296a5ad8a 196 strcpy(webdata, rx_buffer);
ausdong 5:bc0296a5ad8a 197 memset(rx_buffer, '\0', sizeof(rx_buffer));
ausdong 5:bc0296a5ad8a 198 rx_in = 0;
ausdong 5:bc0296a5ad8a 199 rx_out = 0;
ausdong 5:bc0296a5ad8a 200 // check web data for form information
mikebenq 6:9244b4b7485c 201
mikebenq 6:9244b4b7485c 202 if( strstr(webdata, "message") != NULL ) { // set update flag if POST request
ausdong 5:bc0296a5ad8a 203 update=1;
ausdong 5:bc0296a5ad8a 204 }
mikebenq 6:9244b4b7485c 205 //if( strstr(webdata, "GET") != NULL && strstr(webdata, "favicon") == NULL ) { // set update flag for GET request but do not want to update for favicon requests
mikebenq 6:9244b4b7485c 206 // update=1;
mikebenq 6:9244b4b7485c 207 //}
star297 0:e2a155f50119 208 }
ausdong 5:bc0296a5ad8a 209 // Starts webserver
star297 0:e2a155f50119 210 void startserver()
star297 0:e2a155f50119 211 {
ausdong 5:bc0296a5ad8a 212 gettime();
4180_1 4:40dd020463ea 213 gettemp();
ausdong 5:bc0296a5ad8a 214 getbattery();
4180_1 4:40dd020463ea 215 pc.printf("++++++++++ Resetting ESP ++++++++++\r\n");
ausdong 5:bc0296a5ad8a 216 strcpy(cmdbuff,"node.restart()\r\n");
ausdong 5:bc0296a5ad8a 217 SendCMD();
ausdong 5:bc0296a5ad8a 218 wait(2);
ausdong 5:bc0296a5ad8a 219 getreply();
ausdong 5:bc0296a5ad8a 220
ausdong 5:bc0296a5ad8a 221 pc.printf("\n++++++++++ Starting Server ++++++++++\r\n> ");
ausdong 5:bc0296a5ad8a 222
ausdong 5:bc0296a5ad8a 223 // initial values
mikebenq 6:9244b4b7485c 224 sprintf(cmdbuff, "del,time,analog1,analog2=0,\"%s\",\"%s\",\"%s\"\r\n",timebuf,Temp,Vcc);
mikebenq 6:9244b4b7485c 225 //sprintf(cmdbuff, "del,%f\r\n",0);
ausdong 5:bc0296a5ad8a 226 SendCMD();
ausdong 5:bc0296a5ad8a 227 getreply();
ausdong 5:bc0296a5ad8a 228 wait(0.5);
ausdong 5:bc0296a5ad8a 229
ausdong 5:bc0296a5ad8a 230 //create server
ausdong 5:bc0296a5ad8a 231 sprintf(cmdbuff, "srv=net.createServer(net.TCP,%d)\r\n",SERVtimeout);
ausdong 5:bc0296a5ad8a 232 SendCMD();
ausdong 5:bc0296a5ad8a 233 getreply();
ausdong 5:bc0296a5ad8a 234 wait(0.5);
ausdong 5:bc0296a5ad8a 235 strcpy(cmdbuff,"srv:listen(80,function(conn)\r\n");
star297 0:e2a155f50119 236 SendCMD();
star297 1:71ed1afbf344 237 getreply();
ausdong 5:bc0296a5ad8a 238 wait(0.3);
ausdong 5:bc0296a5ad8a 239 strcpy(cmdbuff,"conn:on(\"receive\",function(conn,payload) \r\n");
ausdong 5:bc0296a5ad8a 240 SendCMD();
ausdong 5:bc0296a5ad8a 241 getreply();
ausdong 5:bc0296a5ad8a 242 wait(0.3);
ausdong 5:bc0296a5ad8a 243
ausdong 5:bc0296a5ad8a 244 //print data to mbed
ausdong 5:bc0296a5ad8a 245 strcpy(cmdbuff,"print(payload)\r\n");
ausdong 5:bc0296a5ad8a 246 SendCMD();
ausdong 5:bc0296a5ad8a 247 getreply();
ausdong 5:bc0296a5ad8a 248 wait(0.2);
ausdong 5:bc0296a5ad8a 249
ausdong 5:bc0296a5ad8a 250 //web page data
mikebenq 6:9244b4b7485c 251 //char*temp="conn:send('<!DOCTYPE html><html><head><meta http-equiv=\"refresh\" content=\"1\"></head><body><h1>Eatrhquake detector</h1>')\r\n";
mikebenq 6:9244b4b7485c 252 char*temp="conn:send('<!DOCTYPE html><html><body><h1>Earthquake detector</h1>')\r\n";
mikebenq 6:9244b4b7485c 253 strcpy(cmdbuff,temp);
ausdong 5:bc0296a5ad8a 254 SendCMD();
ausdong 5:bc0296a5ad8a 255 getreply();
ausdong 5:bc0296a5ad8a 256 wait(0.4);
mikebenq 6:9244b4b7485c 257 strcpy(cmdbuff,"conn:send('alarm: '..del..'')\r\n");
star297 1:71ed1afbf344 258 SendCMD();
star297 2:d4c6bc0f2dc4 259 getreply();
ausdong 5:bc0296a5ad8a 260 wait(0.2);
mikebenq 6:9244b4b7485c 261 /*strcpy(cmdbuff,"conn:send('<br>Last hit (based on mbed RTC time): '..time..'<br><hr>')\r\n");
ausdong 5:bc0296a5ad8a 262 SendCMD();
ausdong 5:bc0296a5ad8a 263 getreply();
ausdong 5:bc0296a5ad8a 264 wait(0.4);
ausdong 5:bc0296a5ad8a 265 strcpy(cmdbuff,"conn:send('Analog 1: '..analog1..' V<br>Analog 2: '..analog2..' V<br><hr>')\r\n");
star297 1:71ed1afbf344 266 SendCMD();
star297 3:f7febfa77784 267 getreply();
mikebenq 6:9244b4b7485c 268 wait(0.3);*/
ausdong 5:bc0296a5ad8a 269 strcpy(cmdbuff,"conn:send('<form method=\"POST\"')\r\n");
ausdong 5:bc0296a5ad8a 270 SendCMD();
ausdong 5:bc0296a5ad8a 271 getreply();
ausdong 5:bc0296a5ad8a 272 wait(0.3);
mikebenq 6:9244b4b7485c 273 strcpy(cmdbuff, "conn:send('<p><input type=\"text\" class=\"textboxclass\" name=\"message\" value=\"message\">')\r\n");
star297 3:f7febfa77784 274 SendCMD();
star297 2:d4c6bc0f2dc4 275 getreply();
ausdong 5:bc0296a5ad8a 276 wait(0.3);
mikebenq 6:9244b4b7485c 277 strcpy(cmdbuff, "conn:send('<p><h2>Click and send the message to the earthquake detector</h2>')\r\n");
ausdong 5:bc0296a5ad8a 278 SendCMD();
ausdong 5:bc0296a5ad8a 279 getreply();
ausdong 5:bc0296a5ad8a 280 wait(0.3);
mikebenq 6:9244b4b7485c 281 strcpy(cmdbuff, "conn:send('<p><input type=\"submit\" value=\"send message\"></form></body></html>')\r\n");
ausdong 5:bc0296a5ad8a 282 SendCMD();
ausdong 5:bc0296a5ad8a 283 getreply();
ausdong 5:bc0296a5ad8a 284 wait(0.3);
mikebenq 6:9244b4b7485c 285 \
ausdong 5:bc0296a5ad8a 286 // end web page data
ausdong 5:bc0296a5ad8a 287 strcpy(cmdbuff, "conn:on(\"sent\",function(conn) conn:close() end)\r\n"); // close current connection
ausdong 5:bc0296a5ad8a 288 SendCMD();
ausdong 5:bc0296a5ad8a 289 getreply();
ausdong 5:bc0296a5ad8a 290 wait(0.3);
ausdong 5:bc0296a5ad8a 291 strcpy(cmdbuff, "end)\r\n");
ausdong 5:bc0296a5ad8a 292 SendCMD();
ausdong 5:bc0296a5ad8a 293 getreply();
ausdong 5:bc0296a5ad8a 294 wait(0.2);
ausdong 5:bc0296a5ad8a 295 strcpy(cmdbuff, "end)\r\n");
ausdong 5:bc0296a5ad8a 296 SendCMD();
ausdong 5:bc0296a5ad8a 297 getreply();
ausdong 5:bc0296a5ad8a 298 wait(0.2);
ausdong 5:bc0296a5ad8a 299
ausdong 5:bc0296a5ad8a 300 strcpy(cmdbuff, "tmr.alarm(0, 1000, 1, function()\r\n");
ausdong 5:bc0296a5ad8a 301 SendCMD();
ausdong 5:bc0296a5ad8a 302 getreply();
ausdong 5:bc0296a5ad8a 303 wait(0.2);
ausdong 5:bc0296a5ad8a 304 strcpy(cmdbuff, "if wifi.sta.getip() == nil then\r\n");
ausdong 5:bc0296a5ad8a 305 SendCMD();
ausdong 5:bc0296a5ad8a 306 getreply();
ausdong 5:bc0296a5ad8a 307 wait(0.2);
ausdong 5:bc0296a5ad8a 308 strcpy(cmdbuff, "print(\"Connecting to AP...\\n\")\r\n");
ausdong 5:bc0296a5ad8a 309 SendCMD();
ausdong 5:bc0296a5ad8a 310 getreply();
ausdong 5:bc0296a5ad8a 311 wait(0.2);
ausdong 5:bc0296a5ad8a 312 strcpy(cmdbuff, "else\r\n");
ausdong 5:bc0296a5ad8a 313 SendCMD();
ausdong 5:bc0296a5ad8a 314 getreply();
ausdong 5:bc0296a5ad8a 315 wait(0.2);
ausdong 5:bc0296a5ad8a 316 strcpy(cmdbuff, "ip, nm, gw=wifi.sta.getip()\r\n");
ausdong 5:bc0296a5ad8a 317 SendCMD();
ausdong 5:bc0296a5ad8a 318 getreply();
ausdong 5:bc0296a5ad8a 319 wait(0.2);
ausdong 5:bc0296a5ad8a 320 strcpy(cmdbuff,"print(\"IP Address: \",ip)\r\n");
ausdong 5:bc0296a5ad8a 321 SendCMD();
ausdong 5:bc0296a5ad8a 322 getreply();
ausdong 5:bc0296a5ad8a 323 wait(0.2);
ausdong 5:bc0296a5ad8a 324 strcpy(cmdbuff,"tmr.stop(0)\r\n");
ausdong 5:bc0296a5ad8a 325 SendCMD();
ausdong 5:bc0296a5ad8a 326 getreply();
ausdong 5:bc0296a5ad8a 327 wait(0.2);
ausdong 5:bc0296a5ad8a 328 strcpy(cmdbuff,"end\r\n");
ausdong 5:bc0296a5ad8a 329 SendCMD();
ausdong 5:bc0296a5ad8a 330 getreply();
ausdong 5:bc0296a5ad8a 331 wait(0.2);
ausdong 5:bc0296a5ad8a 332 strcpy(cmdbuff,"end)\r\n");
ausdong 5:bc0296a5ad8a 333 SendCMD();
ausdong 5:bc0296a5ad8a 334 getreply();
ausdong 5:bc0296a5ad8a 335 wait(0.2);
ausdong 5:bc0296a5ad8a 336
ausdong 5:bc0296a5ad8a 337 pc.printf("\n\n++++++++++ Ready ++++++++++\r\n\n");
4180_1 4:40dd020463ea 338 }
ausdong 5:bc0296a5ad8a 339
ausdong 5:bc0296a5ad8a 340
star297 0:e2a155f50119 341 // ESP Command data send
star297 0:e2a155f50119 342 void SendCMD()
star297 0:e2a155f50119 343 {
ausdong 5:bc0296a5ad8a 344 int i;
ausdong 5:bc0296a5ad8a 345 char temp_char;
ausdong 5:bc0296a5ad8a 346 bool empty;
ausdong 5:bc0296a5ad8a 347 i = 0;
ausdong 5:bc0296a5ad8a 348 // Start Critical Section - don't interrupt while changing global buffer variables
ausdong 5:bc0296a5ad8a 349 NVIC_DisableIRQ(UART1_IRQn);
ausdong 5:bc0296a5ad8a 350 empty = (tx_in == tx_out);
ausdong 5:bc0296a5ad8a 351 while ((i==0) || (cmdbuff[i-1] != '\n')) {
ausdong 5:bc0296a5ad8a 352 // Wait if buffer full
ausdong 5:bc0296a5ad8a 353 if (((tx_in + 1) % buffer_size) == tx_out) {
ausdong 5:bc0296a5ad8a 354 // End Critical Section - need to let interrupt routine empty buffer by sending
ausdong 5:bc0296a5ad8a 355 NVIC_EnableIRQ(UART1_IRQn);
ausdong 5:bc0296a5ad8a 356 while (((tx_in + 1) % buffer_size) == tx_out) {
ausdong 5:bc0296a5ad8a 357 }
ausdong 5:bc0296a5ad8a 358 // Start Critical Section - don't interrupt while changing global buffer variables
ausdong 5:bc0296a5ad8a 359 NVIC_DisableIRQ(UART1_IRQn);
ausdong 5:bc0296a5ad8a 360 }
ausdong 5:bc0296a5ad8a 361 tx_buffer[tx_in] = cmdbuff[i];
ausdong 5:bc0296a5ad8a 362 i++;
ausdong 5:bc0296a5ad8a 363 tx_in = (tx_in + 1) % buffer_size;
ausdong 5:bc0296a5ad8a 364 }
ausdong 5:bc0296a5ad8a 365 if (esp.writeable() && (empty)) {
ausdong 5:bc0296a5ad8a 366 temp_char = tx_buffer[tx_out];
ausdong 5:bc0296a5ad8a 367 tx_out = (tx_out + 1) % buffer_size;
ausdong 5:bc0296a5ad8a 368 // Send first character to start tx interrupts, if stopped
ausdong 5:bc0296a5ad8a 369 esp.putc(temp_char);
ausdong 5:bc0296a5ad8a 370 }
ausdong 5:bc0296a5ad8a 371 // End Critical Section
ausdong 5:bc0296a5ad8a 372 NVIC_EnableIRQ(UART1_IRQn);
ausdong 5:bc0296a5ad8a 373 return;
4180_1 4:40dd020463ea 374 }
ausdong 5:bc0296a5ad8a 375
4180_1 4:40dd020463ea 376 // Get Command and ESP status replies
star297 0:e2a155f50119 377 void getreply()
4180_1 4:40dd020463ea 378 {
ausdong 5:bc0296a5ad8a 379 read_line();
ausdong 5:bc0296a5ad8a 380 sscanf(rx_line,replybuff);
ausdong 5:bc0296a5ad8a 381 }
ausdong 5:bc0296a5ad8a 382
ausdong 5:bc0296a5ad8a 383 // Read a line from the large rx buffer from rx interrupt routine
ausdong 5:bc0296a5ad8a 384 void read_line() {
ausdong 5:bc0296a5ad8a 385 int i;
ausdong 5:bc0296a5ad8a 386 i = 0;
ausdong 5:bc0296a5ad8a 387 // Start Critical Section - don't interrupt while changing global buffer variables
ausdong 5:bc0296a5ad8a 388 NVIC_DisableIRQ(UART1_IRQn);
ausdong 5:bc0296a5ad8a 389 // Loop reading rx buffer characters until end of line character
ausdong 5:bc0296a5ad8a 390 while ((i==0) || (rx_line[i-1] != '\r')) {
ausdong 5:bc0296a5ad8a 391 // Wait if buffer empty
ausdong 5:bc0296a5ad8a 392 if (rx_in == rx_out) {
ausdong 5:bc0296a5ad8a 393 // End Critical Section - need to allow rx interrupt to get new characters for buffer
ausdong 5:bc0296a5ad8a 394 NVIC_EnableIRQ(UART1_IRQn);
ausdong 5:bc0296a5ad8a 395 while (rx_in == rx_out) {
ausdong 5:bc0296a5ad8a 396 }
ausdong 5:bc0296a5ad8a 397 // Start Critical Section - don't interrupt while changing global buffer variables
ausdong 5:bc0296a5ad8a 398 NVIC_DisableIRQ(UART1_IRQn);
star297 2:d4c6bc0f2dc4 399 }
ausdong 5:bc0296a5ad8a 400 rx_line[i] = rx_buffer[rx_out];
ausdong 5:bc0296a5ad8a 401 i++;
ausdong 5:bc0296a5ad8a 402 rx_out = (rx_out + 1) % buffer_size;
4180_1 4:40dd020463ea 403 }
ausdong 5:bc0296a5ad8a 404 // End Critical Section
ausdong 5:bc0296a5ad8a 405 NVIC_EnableIRQ(UART1_IRQn);
ausdong 5:bc0296a5ad8a 406 rx_line[i-1] = 0;
ausdong 5:bc0296a5ad8a 407 return;
ausdong 5:bc0296a5ad8a 408 }
ausdong 5:bc0296a5ad8a 409
ausdong 5:bc0296a5ad8a 410
ausdong 5:bc0296a5ad8a 411 // Interupt Routine to read in data from serial port
ausdong 5:bc0296a5ad8a 412 void Rx_interrupt() {
ausdong 5:bc0296a5ad8a 413 DataRX=1;
ausdong 5:bc0296a5ad8a 414 //led3=1;
ausdong 5:bc0296a5ad8a 415 // Loop just in case more than one character is in UART's receive FIFO buffer
ausdong 5:bc0296a5ad8a 416 // Stop if buffer full
ausdong 5:bc0296a5ad8a 417 while ((esp.readable()) && (((rx_in + 1) % buffer_size) != rx_out)) {
ausdong 5:bc0296a5ad8a 418 rx_buffer[rx_in] = esp.getc();
ausdong 5:bc0296a5ad8a 419 // Uncomment to Echo to USB serial to watch data flow
ausdong 5:bc0296a5ad8a 420 pc.putc(rx_buffer[rx_in]);
ausdong 5:bc0296a5ad8a 421 rx_in = (rx_in + 1) % buffer_size;
ausdong 5:bc0296a5ad8a 422 }
ausdong 5:bc0296a5ad8a 423 //led3=0;
ausdong 5:bc0296a5ad8a 424 return;
ausdong 5:bc0296a5ad8a 425 }
ausdong 5:bc0296a5ad8a 426
ausdong 5:bc0296a5ad8a 427
ausdong 5:bc0296a5ad8a 428 // Interupt Routine to write out data to serial port
ausdong 5:bc0296a5ad8a 429 void Tx_interrupt() {
ausdong 5:bc0296a5ad8a 430 //led2=1;
ausdong 5:bc0296a5ad8a 431 // Loop to fill more than one character in UART's transmit FIFO buffer
ausdong 5:bc0296a5ad8a 432 // Stop if buffer empty
ausdong 5:bc0296a5ad8a 433 while ((esp.writeable()) && (tx_in != tx_out)) {
ausdong 5:bc0296a5ad8a 434 esp.putc(tx_buffer[tx_out]);
ausdong 5:bc0296a5ad8a 435 tx_out = (tx_out + 1) % buffer_size;
ausdong 5:bc0296a5ad8a 436 }
ausdong 5:bc0296a5ad8a 437 //led2=0;
ausdong 5:bc0296a5ad8a 438 return;
ausdong 5:bc0296a5ad8a 439 }
ausdong 5:bc0296a5ad8a 440
ausdong 5:bc0296a5ad8a 441 void gettime()
ausdong 5:bc0296a5ad8a 442 {
ausdong 5:bc0296a5ad8a 443 time_t seconds = time(NULL);
ausdong 5:bc0296a5ad8a 444 strftime(timebuf,50,"%H:%M:%S %a %d %b %y", localtime(&seconds));
ausdong 5:bc0296a5ad8a 445 }
ausdong 5:bc0296a5ad8a 446
ausdong 5:bc0296a5ad8a 447 void setRTC()
ausdong 5:bc0296a5ad8a 448 {
ausdong 5:bc0296a5ad8a 449 t.tm_sec = (0); // 0-59
ausdong 5:bc0296a5ad8a 450 t.tm_min = (minute); // 0-59
ausdong 5:bc0296a5ad8a 451 t.tm_hour = (hour); // 0-23
ausdong 5:bc0296a5ad8a 452 t.tm_mday = (dayofmonth); // 1-31
ausdong 5:bc0296a5ad8a 453 t.tm_mon = (month-1); // 0-11 "0" = Jan, -1 added for Mbed RCT clock format
ausdong 5:bc0296a5ad8a 454 t.tm_year = ((year)+100); // year since 1900, current DCF year + 100 + 1900 = correct year
ausdong 5:bc0296a5ad8a 455 set_time(mktime(&t)); // set RTC clock
star297 0:e2a155f50119 456 }
star297 0:e2a155f50119 457 // Analog in example
star297 0:e2a155f50119 458 void getbattery()
star297 0:e2a155f50119 459 {
4180_1 4:40dd020463ea 460 AdcIn=Ain1.read();
4180_1 4:40dd020463ea 461 Ht = (AdcIn*3.3); // set the numeric to the exact MCU analog reference voltage for greater accuracy
4180_1 4:40dd020463ea 462 sprintf(Vcc,"%2.3f",Ht);
star297 0:e2a155f50119 463 }
star297 0:e2a155f50119 464 // Temperature example
star297 0:e2a155f50119 465 void gettemp()
4180_1 4:40dd020463ea 466 {
4180_1 4:40dd020463ea 467
4180_1 4:40dd020463ea 468 AdcIn=Ain2.read();
4180_1 4:40dd020463ea 469 Ht = (AdcIn*3.3); // set the numeric to the exact MCU analog reference voltage for greater accuracy
4180_1 4:40dd020463ea 470 sprintf(Temp,"%2.3f",Ht);
ausdong 5:bc0296a5ad8a 471 }