
4180 mini project
Dependencies: LSM9DS1_Library_cal SDFileSystem mbed wave_player
Fork of HUZZAHESP8266-web-control-LPC1768 by
Revision 6:9244b4b7485c, committed 2017-03-14
- Comitter:
- mikebenq
- Date:
- Tue Mar 14 01:03:15 2017 +0000
- Parent:
- 5:bc0296a5ad8a
- Commit message:
- ECE4180 project
Changed in this revision
diff -r bc0296a5ad8a -r 9244b4b7485c LSM9DS1_Library_cal.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/LSM9DS1_Library_cal.lib Tue Mar 14 01:03:15 2017 +0000 @@ -0,0 +1,1 @@ +https://developer.mbed.org/users/4180_1/code/LSM9DS1_Library_cal/#36abf8e18ade
diff -r bc0296a5ad8a -r 9244b4b7485c SDFileSystem.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/SDFileSystem.lib Tue Mar 14 01:03:15 2017 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/mbed_official/code/SDFileSystem/#c8f66dc765d4
diff -r bc0296a5ad8a -r 9244b4b7485c SongPlayer.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/SongPlayer.h Tue Mar 14 01:03:15 2017 +0000 @@ -0,0 +1,41 @@ +#include "mbed.h" +// new class to play a note on Speaker based on PwmOut class +class SongPlayer +{ +public: + SongPlayer(PinName pin) : _pin(pin) { +// _pin(pin) means pass pin to the constructor + } +// class method to play a note based on PwmOut class + void PlaySong(float frequency[], float duration[], float volume=1.0) { + vol = volume; + notecount = 0; + _pin.period(1.0/frequency[notecount]); + _pin = volume/2.0; + noteduration.attach(this,&SongPlayer::nextnote, duration[notecount]); + // setup timer to interrupt for next note to play + frequencyptr = frequency; + durationptr = duration; + //returns after first note starts to play + } + void nextnote(); +private: + Timeout noteduration; + PwmOut _pin; + int notecount; + float vol; + float * frequencyptr; + float * durationptr; +}; +//Interrupt Routine to play next note +void SongPlayer::nextnote() +{ + _pin = 0.0; + notecount++; //setup next note in song + if (durationptr[notecount]!=0.0) { + _pin.period(1.0/frequencyptr[notecount]); + noteduration.attach(this,&SongPlayer::nextnote, durationptr[notecount]); + _pin = vol/2.0; + } else + _pin = 0.0; //turn off on last note +} \ No newline at end of file
diff -r bc0296a5ad8a -r 9244b4b7485c main.cpp --- a/main.cpp Fri Mar 18 19:10:19 2016 +0000 +++ b/main.cpp Tue Mar 14 01:03:15 2017 +0000 @@ -1,9 +1,27 @@ // ESP8266 Static page WEB server to control Mbed #include "mbed.h" +#include "LSM9DS1.h" +#include "SDFileSystem.h" +#include "wave_player.h" +#include "SongPlayer.h" +float note[18]= {1568.0,1396.9,1244.5,1244.5,1396.9,1568.0,1568.0,1568.0,1396.9, + 1244.5,1396.9,1568.0,1396.9,1244.5,1174.7,1244.5,1244.5, 0.0 + }; +float duration[18]= {0.48,0.24,0.72,0.48,0.24,0.48,0.24,0.24,0.24, + 0.24,0.24,0.24,0.24,0.48,0.24,0.48,0.48, 0.0 + }; + SongPlayer mySpeaker(p26); +#define PI 3.14159 +// Earth's magnetic field varies by location. Add or subtract +// a declination to get a more accurate heading. Calculate +// your's here: +// http://www.ngdc.noaa.gov/geomag-web/#declination +#define DECLINATION -4.94 // Declination (degrees) in Atlanta,GA. + Serial pc(USBTX, USBRX); -Serial esp(p28, p27); // tx, rx +Serial esp(p9, p10); // tx, rx // Standard Mbed LED definitions @@ -13,8 +31,8 @@ DigitalOut led4(LED4); // some test values to show on web page -AnalogIn Ain1(p18); -AnalogIn Ain2(p19); +AnalogIn Ain1(p19); +AnalogIn Ain2(p20); /* char ssid[32] = "hsd"; // enter WiFi router ssid inside the quotes @@ -58,7 +76,21 @@ int dayofmonth =26; // 1-31 int month =8; // 1-12 int year =15; // last 2 digits - +//////////GYRO//////////////// +LSM9DS1 IMU(p28, p27, 0xD6, 0x3C); +float tempx=0; +float tempy=0; +float tempz=0; +float delx=0; +float dely=0; +float delz=0; +float del=0; +int result=0; +int pre_result=0; +//////////SDcard&waveplayer//////////////// +SDFileSystem sd(p11, p12, p13, p16, "sd"); //SD card +AnalogOut DACout(p18); +wave_player waver(&DACout); int main() { pc.baud(9600); @@ -74,23 +106,82 @@ startserver(); DataRX=0; count=0; + /////////////////////////// + IMU.begin(); + if (!IMU.begin()) { + pc.printf("Failed to communicate with LSM9DS1.\n"); + } + IMU.calibrate(1); + //IMU.calibrateMag(0); + FILE *wave_file; while(1) { + wave_file=fopen("/sd/sample.wav","r"); + if(wave_file==NULL) + error("Could not open file for write\n"); + while(!IMU.accelAvailable()); + tempx=IMU.calcAccel(IMU.ax); + tempy=IMU.calcAccel(IMU.ay); + tempz=IMU.calcAccel(IMU.az); + IMU.readAccel(); + delx=tempx-IMU.calcAccel(IMU.ax); + dely=tempy-IMU.calcAccel(IMU.ay); + delz=tempz-IMU.calcAccel(IMU.az); + del=delx*delx+dely*dely+delz*delz; + //pc.printf("del=%f",del); + pre_result=result; + if(del>0.01) + led1=1; + else + led1=0; + if(del>0.1){ + led2=1; + } + else + led2=0; + if(del>1){ + led3=1; + + result=1; + //mySpeaker.PlaySong(note,duration); + } + else{ + led3=0; + result=0; + } + + if(result==pre_result) + { + } + else + { + sprintf(cmdbuff, "del,time,analog1,analog2=%d,\"%s\",\"%s\",\"%s\"\r\n",result,timebuf,Temp,Vcc); + SendCMD(); + getreply(); + //sprintf(cndbuff,"\r\n") + } + if(result==1) + waver.play(wave_file); + fclose(wave_file); if(DataRX==1) { ReadWebData(); esp.attach(&Rx_interrupt, Serial::RxIrq); } if(update==1) // update time, hit count, and analog levels in the HUZZAH chip + //if(1) { - // get new values - gettime(); - gettemp(); - getbattery(); - count++; - // send new values - sprintf(cmdbuff, "count,time,analog1,analog2=%d,\"%s\",\"%s\",\"%s\"\r\n",count,timebuf,Temp,Vcc); - SendCMD(); - getreply(); - update=0; + led4=1; + wait(1); + led4=0; + mkdir("/sd/mydir", 0777); + + FILE *fp = fopen("/sd/mydir/sdtest.txt", "w"); + if(fp == NULL) { + error("Could not open file for write\n"); + } + char* data=strstr(webdata,"message="); + fprintf(fp, &data[8]); + fclose(fp); + update=0; } } } @@ -107,24 +198,13 @@ rx_in = 0; rx_out = 0; // check web data for form information - if( strstr(webdata, "check=led1v") != NULL ) { - led1=!led1; - } - if( strstr(webdata, "check=led2v") != NULL ) { - led2=!led2; - } - if( strstr(webdata, "check=led3v") != NULL ) { - led3=!led3; - } - if( strstr(webdata, "check=led4v") != NULL ) { - led4=!led4; - } - if( strstr(webdata, "POST") != NULL ) { // set update flag if POST request + + if( strstr(webdata, "message") != NULL ) { // set update flag if POST request update=1; } - if( strstr(webdata, "GET") != NULL && strstr(webdata, "favicon") == NULL ) { // set update flag for GET request but do not want to update for favicon requests - update=1; - } + //if( strstr(webdata, "GET") != NULL && strstr(webdata, "favicon") == NULL ) { // set update flag for GET request but do not want to update for favicon requests + // update=1; + //} } // Starts webserver void startserver() @@ -141,7 +221,8 @@ pc.printf("\n++++++++++ Starting Server ++++++++++\r\n> "); // initial values - sprintf(cmdbuff, "count,time,analog1,analog2=0,\"%s\",\"%s\",\"%s\"\r\n",timebuf,Temp,Vcc); + sprintf(cmdbuff, "del,time,analog1,analog2=0,\"%s\",\"%s\",\"%s\"\r\n",timebuf,Temp,Vcc); + //sprintf(cmdbuff, "del,%f\r\n",0); SendCMD(); getreply(); wait(0.5); @@ -167,50 +248,41 @@ wait(0.2); //web page data - strcpy(cmdbuff,"conn:send('<!DOCTYPE html><html><body><h1>ESP8266 Mbed IoT Web Controller</h1>')\r\n"); + //char*temp="conn:send('<!DOCTYPE html><html><head><meta http-equiv=\"refresh\" content=\"1\"></head><body><h1>Eatrhquake detector</h1>')\r\n"; + char*temp="conn:send('<!DOCTYPE html><html><body><h1>Earthquake detector</h1>')\r\n"; + strcpy(cmdbuff,temp); SendCMD(); getreply(); wait(0.4); - strcpy(cmdbuff,"conn:send('Hit count: '..count..'')\r\n"); + strcpy(cmdbuff,"conn:send('alarm: '..del..'')\r\n"); SendCMD(); getreply(); wait(0.2); - strcpy(cmdbuff,"conn:send('<br>Last hit (based on mbed RTC time): '..time..'<br><hr>')\r\n"); + /*strcpy(cmdbuff,"conn:send('<br>Last hit (based on mbed RTC time): '..time..'<br><hr>')\r\n"); SendCMD(); getreply(); wait(0.4); strcpy(cmdbuff,"conn:send('Analog 1: '..analog1..' V<br>Analog 2: '..analog2..' V<br><hr>')\r\n"); SendCMD(); getreply(); - wait(0.3); + wait(0.3);*/ strcpy(cmdbuff,"conn:send('<form method=\"POST\"')\r\n"); SendCMD(); getreply(); wait(0.3); - strcpy(cmdbuff, "conn:send('<p><input type=\"checkbox\" name=\"check\" value=\"led1v\"> flip LED1')\r\n"); - SendCMD(); - getreply(); - wait(0.3); - strcpy(cmdbuff, "conn:send('<p><input type=\"checkbox\" name=\"check\" value=\"led2v\"> flip LED2')\r\n"); - SendCMD(); - getreply(); - wait(0.3); - strcpy(cmdbuff, "conn:send('<p><input type=\"checkbox\" name=\"check\" value=\"led3v\"> flip LED3')\r\n"); + strcpy(cmdbuff, "conn:send('<p><input type=\"text\" class=\"textboxclass\" name=\"message\" value=\"message\">')\r\n"); SendCMD(); getreply(); wait(0.3); - strcpy(cmdbuff, "conn:send('<p><input type=\"checkbox\" name=\"check\" value=\"led4v\"> flip LED4')\r\n"); + strcpy(cmdbuff, "conn:send('<p><h2>Click and send the message to the earthquake detector</h2>')\r\n"); SendCMD(); getreply(); wait(0.3); - strcpy(cmdbuff,"conn:send('<p><input type=\"submit\" value=\"send-refresh\"></form>')\r\n"); + strcpy(cmdbuff, "conn:send('<p><input type=\"submit\" value=\"send message\"></form></body></html>')\r\n"); SendCMD(); getreply(); wait(0.3); - 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"); - SendCMD(); - getreply(); - wait(0.5); +\ // end web page data strcpy(cmdbuff, "conn:on(\"sent\",function(conn) conn:close() end)\r\n"); // close current connection SendCMD();
diff -r bc0296a5ad8a -r 9244b4b7485c wave_player.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/wave_player.lib Tue Mar 14 01:03:15 2017 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/sravet/code/wave_player/#acc3e18e77ad