Kimberly Lie / Mbed 2 deprecated ECE4180_MasterCode

Dependencies:   mbed mbed-rtos SDFileSystem PinDetect ESP8266NodeMCUInterface

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "uLCD_4DGL.h" 
00003 #include "ultrasonic.h"
00004 #include "rtos.h"
00005 #include "PinDetect.h"
00006 #include "SDFileSystem.h"
00007 #include "wave_player.h"
00008 #include "PowerControl/PowerControl.h"
00009 #include "PowerControl/EthernetPowerControl.h"
00010 #include <iostream>
00011 #include <string>
00012 #include <sstream>
00013 #include <time.h>
00014  
00015 // --------------------------------------- Pin Declarations ---------------------------------------
00016 // Pins used for WiFi
00017 Serial pc(USBTX, USBRX);
00018 Serial esp(p9, p10); // tx, rx
00019 DigitalOut reset(p26);
00020 Timer t;
00021 
00022 // Pins used for uLCD and Sonar
00023 uLCD_4DGL uLCD(p28, p27, p29); // serial tx, serial rx, reset pin;
00024 DigitalOut led1(LED1);
00025 PinDetect pb(p25);
00026 Mutex howMany;
00027 
00028 // Pins used for SD card and speaker
00029 SDFileSystem sd(p5, p6, p7, p8, "sd"); //SD card
00030 AnalogOut DACout(p18);
00031 wave_player waver(&DACout);
00032 
00033 // Pins used for Bluetooth
00034 RawSerial blue(p13,p14);
00035  
00036 // --------------------------------------- Variable Declarations --------------------------------------- 
00037 // Variables used for WiFi
00038 int  lcount,lended,ltimeout;
00039 char buf[2024];
00040 char snd[1024];
00041 char ssid[32] = "ssid";         // enter WiFi router ssid inside the quotes
00042 char pwd [32] = "password";    // enter WiFi router password inside the quotes
00043 
00044 // Variables used for uLCD and Sonar
00045 int year, month, day, hr, mn, sec; 
00046 int howManyTodayold = 0;
00047 int howManyToday = 0;
00048 int dailyLimit;
00049 double increment = 0; // DEFAULT: USE DATABASE TO UPDATE THE AMOUNT
00050 std::string foodName = "";  // so save the food name
00051 std::string macroName = ""; // to save user's desired macro
00052 std::string daily = ""; // to save daily amount
00053 std:: string stringTime  = ""; // to save current time and date
00054 std:: string todayIs = "";
00055 
00056 // --------------------------------------- Function Declarations ---------------------------------------
00057 // Function declarations for WiFi
00058 double queryMFP(string, string);
00059 double MFP_LookupTest(string, string);
00060 void SendCMD(),getreply(),ESPconfig();
00061 
00062 // Function declarations for uLCD and Sonar
00063 time_t asUnixTime(int, int, int, int, int, int);
00064 void dist(int);
00065 void updateHowManyToday(void);
00066 
00067 // Function declarations for speaker and sd card
00068 void play_song();
00069 void updateTime();
00070  
00071 // --------------------------------------- Begin Code --------------------------------------- 
00072 // Functions used for WiFi 
00073 double queryMFP(string food, string macro)
00074 {
00075     wait(1);
00076     strcpy(snd,"srv = net.createConnection(net.TCP, 0)\r\n");
00077     SendCMD();
00078     wait(1);
00079     strcpy(snd,"srv:on(\"receive\", function(sck, c) print(c) end)\r\n");
00080     SendCMD();
00081     wait(1);
00082     strcpy(snd,"srv:connect(80,\"spectrosam.org\")\r\n");
00083     SendCMD();
00084     wait(1);
00085     sprintf(snd, "sck:send(\"GET /trial.php?query=%s HTTP/1.1\r\nHost: http://spectrosam.org/\r\nAccept: */*\r\n\r\n\")", food);
00086     SendCMD();
00087     wait(1);
00088     ltimeout=15;
00089     getreply();
00090     pc.printf(buf);
00091     
00092     string resp(buf);
00093     size_t found = resp.find(macro);
00094     int start = found + macro.size() + 2; //after finding the macro string, go to the end of it, and add two for the quotes and colon
00095     int end = start + 3; // D.D of macro value
00096     string input = resp.substr(start,end);
00097   
00098     // convert string to char array
00099     int n = input.length(); 
00100     // declaring character array 
00101     char char_array[n + 1]; 
00102     // copying the contents of the 
00103     // string to char array 
00104     strcpy(char_array, input.c_str());
00105     return atoll(char_array);
00106 }
00107 
00108 void ESPconfig()
00109 {
00110     wait(5);
00111     pc.printf("\f---------- Starting ESP Config ----------\r\n\n");
00112     strcpy(snd,".\r\n.\r\n");
00113     SendCMD();
00114     wait(1);
00115     pc.printf("---------- Reset & get Firmware ----------\r\n");
00116     strcpy(snd,"node.restart()\r\n");
00117     SendCMD();
00118     ltimeout=5;
00119     getreply();
00120     pc.printf(buf);
00121     wait(2);
00122 
00123     // set CWMODE to 1=Station,2=AP,3=BOTH, default mode 1 (Station)
00124     pc.printf("\n---------- Setting Mode ----------\r\n");
00125     strcpy(snd, "wifi.setmode(wifi.STATION)\r\n");
00126     SendCMD();
00127     ltimeout=4;
00128     getreply();
00129     pc.printf(buf);
00130     wait(2);
00131 
00132     pc.printf("\n---------- Connecting to AP ----------\r\n");
00133     pc.printf("ssid = %s   pwd = %s\r\n",ssid,pwd);
00134     strcpy(snd, "wifi.sta.config(\"");
00135     strcat(snd, ssid);
00136     strcat(snd, "\",\"");
00137     strcat(snd, pwd);
00138     strcat(snd, "\")\r\n");
00139     SendCMD();
00140     ltimeout=10;
00141     getreply();
00142     pc.printf(buf);
00143 
00144     wait(5);
00145 
00146 }
00147 
00148 void SendCMD()
00149 {
00150     esp.printf("%s", snd);
00151 }
00152 
00153 void getreply()
00154 {
00155     memset(buf, '\0', sizeof(buf));
00156     t.start();
00157     lended=0;
00158     lcount=0;
00159     while(!lended) {
00160         if(esp.readable()) {
00161             buf[lcount] = esp.getc();
00162             lcount++;
00163         }
00164         if(t.read() > ltimeout) {
00165             lended = 1;
00166             t.stop();
00167             t.reset();
00168         }
00169     }
00170 }
00171 
00172 time_t asUnixTime(int year, int mon, int mday, int hour, int min, int sec) {
00173     struct tm   t;
00174     t.tm_year = year - 1900;
00175     t.tm_mon =  mon - 1;        // convert to 0 based month
00176     t.tm_mday = mday;
00177     t.tm_hour = hour;
00178     t.tm_min = min;
00179     t.tm_sec = sec;
00180     t.tm_isdst = -1;            // Is Daylight saving time on? 1 = yes, 0 = no, -1 = unknown
00181  
00182     return mktime(&t);          // returns seconds elapsed since January 1, 1970 (begin of the Epoch)
00183 }
00184 
00185 void dist(int distance)
00186 {
00187     //put code here to execute when the distance has changed
00188     led1  = !led1;
00189     howMany.lock();
00190     if (howManyToday != howManyTodayold) {
00191         updateTime();
00192         if (howManyToday >= dailyLimit) {
00193             uLCD.cls();
00194             uLCD.text_width(2);
00195             uLCD.text_height(2);
00196             uLCD.filled_rectangle(0,0,127,127,RED);
00197             uLCD.filled_rectangle(15,10,110,90,0x50C878);
00198             uLCD.textbackground_color(0x50C878);
00199             uLCD.color(BLACK);
00200             uLCD.locate(2,1);
00201             uLCD.printf("Hello!");
00202             uLCD.locate(3,3);
00203             uLCD.text_width(1);
00204             uLCD.text_height(2);
00205             uLCD.printf("%d / %d\n", howManyToday, dailyLimit);
00206             uLCD.color(BLACK);
00207             uLCD.text_width(1);
00208             uLCD.text_height(2);
00209             uLCD.textbackground_color(RED);
00210             uLCD.locate(6,14);
00211             uLCD.printf("%s", macroName);
00212 
00213             // Play warning song
00214             play_song();
00215             
00216             } else {
00217                 uLCD.cls();
00218                 uLCD.text_width(2);
00219                 uLCD.text_height(2);
00220                 uLCD.filled_rectangle(0,0,127,127,0x86c5da);
00221                 uLCD.filled_rectangle(15,10,110,90,0x50C878);
00222                 uLCD.textbackground_color(0x50C878);
00223                 uLCD.color(BLACK);
00224                 uLCD.locate(2,1);
00225                 uLCD.printf("Hello!");
00226                 uLCD.locate(3,3);
00227                 uLCD.text_width(1);
00228                 uLCD.text_height(2);
00229                 uLCD.printf("%d / %d\n", howManyToday, dailyLimit);
00230                 uLCD.color(BLACK);
00231                 uLCD.text_width(1);
00232                 uLCD.text_height(1);
00233                 uLCD.locate(6,14);
00234                 uLCD.textbackground_color(0x86c5da);
00235                 uLCD.printf("%s", macroName);
00236       
00237             }
00238                 howManyTodayold = howManyToday;
00239             }
00240             howMany.unlock();
00241     //pc.printf("Distance %d mm\r\n", distance);
00242 }
00243 
00244 ultrasonic mu(p11, p12, .1, 1, &dist);      //Set the trigger pin to D12 and the echo pin to D13
00245                                             //have updates every .1 seconds and a timeout after 1
00246                                             //second, and call dist when the distance changes
00247 
00248 void updateHowManyToday(void) {
00249     if (mu.getCurrentDistance() < 50) {
00250         howMany.lock();
00251         howManyTodayold = howManyToday;
00252         howManyToday = howManyToday + (int)increment;
00253         howMany.unlock();
00254     }
00255 }
00256 
00257 // Functions used for speaker and sd card
00258 void play_song() {
00259     std::string song = "/sd/NoNoNo3.wav";
00260     FILE *wave_file;
00261     wave_file=fopen(song.c_str(),"r");
00262     waver.play(wave_file);
00263     fclose(wave_file);
00264 }
00265 
00266 void updateTime(){
00267     time_t curTime = time(NULL);
00268     FILE *fp = fopen("/sd/Logging.csv", "a");
00269     std:: string test = asctime(localtime(&curTime));
00270     if (todayIs.compare(test.substr(0,10)) != 0){
00271         howManyToday = 0;
00272         todayIs = test.substr(0,10);
00273         fprintf(fp, "\n%s\n\n", todayIs.c_str());
00274     }
00275     fprintf(fp, "%s, %d g, %s\n", foodName.c_str(), (int)increment, macroName.c_str());  
00276     fclose(fp);
00277 }
00278 
00279 int main() {
00280     // Start of uLCD code
00281     bool startUp = 1, name = 1, limit = 1, settingTime = 1;
00282     uLCD.cls();
00283     uLCD.filled_rectangle(0,0,127,127,0x86c5da);
00284     uLCD.line(0, 9, 127, 9, GREEN);
00285     uLCD.filled_rectangle(0,0,127,9,GREEN);
00286     uLCD.line(0, 118, 127, 118, GREEN);
00287     uLCD.filled_rectangle(0,118,127,127,GREEN);
00288     uLCD.textbackground_color(0x86c5da);
00289     uLCD.color(DGREY);
00290     uLCD.text_height(1.80);
00291     
00292     uLCD.locate(4,2);
00293     uLCD.printf("WELCOME!");
00294     uLCD.color(BLACK);
00295     uLCD.locate(1,4);
00296     uLCD.printf("Please enter the name");
00297     uLCD.printf(" of the food.");
00298     uLCD.color(BLACK);
00299     uLCD.locate(1,7);
00300     
00301     while (name) {
00302         if (blue.readable()) {
00303             char v = blue.getc();
00304             if (v == '!'){
00305                 name = 0;
00306             } else if (int(v) == 8) {
00307                  foodName.erase(foodName.end()-1);
00308                 } else {
00309                 foodName.push_back(v);
00310                 uLCD.printf("%c" , v);
00311                 }
00312             }
00313         }
00314         
00315         uLCD.cls();
00316         uLCD.filled_rectangle(0,0,127,127,0x86c5da);
00317         uLCD.line(0, 9, 127, 9, GREEN);
00318         uLCD.filled_rectangle(0,0,127,9,GREEN);
00319         uLCD.line(0, 118, 127, 118, GREEN);
00320         uLCD.filled_rectangle(0,118,127,127,GREEN);
00321         uLCD.textbackground_color(0x86c5da);
00322         uLCD.color(BLACK);
00323         uLCD.text_height(1);
00324             
00325         uLCD.locate(1,1);
00326         uLCD.printf("What macro are you monitoring? : \n\n1. Carbs\n2. Protein\n3. Fat \n4. Calories \n");
00327         uLCD.color(BLACK);
00328         while(startUp) {
00329             if (blue.readable() ){
00330                 char v = blue.getc();
00331                 if (v == '1') {
00332                     startUp = 0;
00333                     macroName = "Carbs";
00334                 } else if (v == '2') {
00335                     startUp = 0;
00336                     macroName = "Protein";
00337                 } else if (v == '3') {
00338                     startUp = 0;
00339                     macroName = "Fat";
00340                 } else if (v == '4') {
00341                     startUp = 0;
00342                     macroName = "Calories";
00343                     
00344                     }
00345             }
00346         }
00347         
00348         uLCD.cls();
00349         uLCD.filled_rectangle(0,0,127,127,0x86c5da);
00350         uLCD.line(0, 9, 127, 9, GREEN);
00351         uLCD.filled_rectangle(0,0,127,9,GREEN);
00352         uLCD.line(0, 118, 127, 118, GREEN);
00353         uLCD.filled_rectangle(0,118,127,127,GREEN);
00354         uLCD.textbackground_color(0x86c5da);
00355         uLCD.color(BLACK);
00356         uLCD.locate(1,2);
00357         uLCD.text_height(1);
00358             
00359         uLCD.printf("What will be your daily limit?\n ");
00360         uLCD.color(BLACK);
00361         
00362         while (limit) {
00363             if (blue.readable()){
00364                 char v = blue.getc();
00365                 if (v == '!') {
00366                     limit = 0;
00367                 } else {
00368                     daily.push_back(v);
00369                     uLCD.printf("%c" , v);
00370                 }
00371             }
00372             
00373         }
00374         
00375         std::istringstream iss (daily);
00376         iss >> dailyLimit;
00377         
00378         uLCD.cls();
00379         uLCD.filled_rectangle(0,0,127,127,0x86c5da);
00380         uLCD.line(0, 9, 127, 9, GREEN);
00381         uLCD.filled_rectangle(0,0,127,9,GREEN);
00382         uLCD.line(0, 118, 127, 118, GREEN);
00383         uLCD.filled_rectangle(0,118,127,127,GREEN);
00384         uLCD.textbackground_color(0x86c5da);
00385         uLCD.color(BLACK);
00386         uLCD.text_height(1);
00387         uLCD.locate(1,1);
00388         uLCD.printf("Please enter date and military time\nMM/DD/YYYY HH:MM:SS)\n");
00389         uLCD.color(BLACK);
00390               
00391         while (settingTime) {
00392             if (blue.readable()){
00393                 char v = blue.getc();
00394                 if (v == '!') {
00395                     settingTime = 0;
00396                 } else {
00397                     stringTime.push_back(v);
00398                     uLCD.printf("%c" , v);
00399                 }
00400             }
00401         }
00402         
00403     // Start of WiFi code
00404     reset=0;
00405     pc.baud(9600);      
00406     pc.printf("\f\n\r-------------ESP8266 Hardware Reset-------------\n\r");
00407     wait(0.5);
00408     reset=1;
00409     ltimeout=2;
00410     getreply();
00411     esp.baud(9600);     // ESP baud rate
00412     ESPconfig();        
00413     increment = queryMFP(foodName, macro);
00414     
00415     // After calling database
00416     // TIME
00417     // converts epoch time to day, month, year
00418     std::istringstream mon (stringTime.substr(0,2));
00419     mon >> month; 
00420     std::istringstream d (stringTime.substr(3,2));
00421     d >> day; 
00422     std::istringstream y (stringTime.substr(6,4));
00423     y >> year; 
00424     std::istringstream h (stringTime.substr(11,2));
00425     h >> hr; 
00426     std::istringstream m (stringTime.substr(14,2));
00427     m >> mn; 
00428     std::istringstream sc (stringTime.substr(17,2));
00429     sc >> sec; 
00430                
00431     time_t result = asUnixTime(year, month, day, hr, mn, sec);
00432     
00433     pc.printf("%s\n",asctime(localtime(&result)));
00434     
00435     set_time(result);
00436     
00437     todayIs = asctime(localtime(&result));
00438     todayIs = todayIs.substr(0,3);
00439     
00440     uLCD.cls();
00441     uLCD.filled_rectangle(0,0,127,127,0x86c5da);
00442     uLCD.filled_rectangle(15,10,110,90,0x50C878);
00443     uLCD.textbackground_color(0x50C878);
00444     uLCD.color(BLACK);
00445     uLCD.text_width(2);
00446     uLCD.text_height(2);
00447     uLCD.locate(2,1);
00448     uLCD.printf("Hello!");
00449     uLCD.locate(3,3);
00450     uLCD.text_width(1);
00451     uLCD.text_height(2);
00452     uLCD.printf("%d / %d", howManyToday, dailyLimit);
00453     uLCD.color(BLACK);
00454     uLCD.textbackground_color(0x86c5da);
00455     uLCD.text_width(1);
00456     uLCD.text_height(1);
00457     uLCD.locate(6,14);
00458     uLCD.printf("%s", macroName);
00459     
00460     pc.baud(9600);
00461     mu.startUpdates();//start measuring the distance
00462     pb.attach_deasserted(&updateHowManyToday);
00463     pb.mode(PullUp);
00464     pb.setSampleFrequency();
00465     
00466     //checkTime.attach(&updateTime, 10);
00467     
00468     // PWR MNGT
00469     PHY_PowerDown();
00470     //Peripheral_PowerDown(0xFEF6FFCF);
00471     Peripheral_PowerDown(LPC1768_PCONP_PCTIM0);  // Timer/Counter 0
00472     Peripheral_PowerDown(LPC1768_PCONP_PCTIM1);  // Timer/Counter 1
00473     Peripheral_PowerDown(LPC1768_PCONP_PCUART0);  //UART 0
00474     Peripheral_PowerDown(LPC1768_PCONP_PCUART1); //UART 1
00475     Peripheral_PowerDown(LPC1768_PCONP_PCPWM1); // PWM
00476     Peripheral_PowerDown(LPC1768_PCONP_PCI2C0); //bit 7: I2c interface
00477     Peripheral_PowerDown(LPC1768_PCONP_PCSPI);  //SPI
00478     Peripheral_PowerDown(LPC1768_PCONP_PCRTC);  //RTC
00479     Peripheral_PowerDown(LPC1768_PCONP_PCSSP1); //SSP
00480     
00481     // bit 11: Reserved
00482     //Peripheral_PowerDown(LPC1768_PCONP_PCADC); // bit12: A/D converter power/clock enable
00483     Peripheral_PowerDown(LPC1768_PCONP_PCCAN1); // bit 13: CAN controller 1 power/clock enable
00484     Peripheral_PowerDown(LPC1768_PCONP_PCCAN2);   // bit 14: CAN controller 2 power/clock enable
00485     
00486     // bit 15: PCGPIO: GPIOs power/clock enable
00487     //Peripheral_PowerDown(LPC1768_PCONP_PCGPIO); 
00488 
00489     Peripheral_PowerDown(LPC1768_PCONP_PCRIT); //bit 16: Repetitive interrupt timer power/clock enable
00490     Peripheral_PowerDown(LPC1768_PCONP_PCMCPWM);  // bit 17: Motor control PWM power/clock enable
00491     Peripheral_PowerDown(LPC1768_PCONP_PCQEI); // bit 18: Quadrature encoder interface power/clock enable
00492     Peripheral_PowerDown(LPC1768_PCONP_PCI2C1); // bit 19: I2C interface 1 power/clock enable
00493     
00494     // bit 20: Reserved
00495     Peripheral_PowerDown(LPC1768_PCONP_PCSSP0); // bit 21: PCSSP0: SSP interface 0 power/clock enable 
00496     Peripheral_PowerDown(LPC1768_PCONP_PCTIM2); // bit 22: PCTIM2: Timer 2 power/clock enable
00497     
00498     // bit 23: PCTIM3: Timer 3 power/clock enable
00499     //Peripheral_PowerDown(LPC1768_PCONP_PCQTIM3); 
00500     
00501     // bit 24: PCUART2: UART 2 power/clock enable
00502     //Peripheral_PowerDown(LPC1768_PCONP_PCUART2); 
00503     
00504     Peripheral_PowerDown(LPC1768_PCONP_PCUART3); // bit 25: UART 3 power/clock enable
00505     Peripheral_PowerDown(LPC1768_PCONP_PCI2C2); // bit 26: I2C interface 2 power/clock enable
00506     Peripheral_PowerDown(LPC1768_PCONP_PCI2S);  // bit 27: PCI2S: I2S interface power/clock enable      
00507     
00508     // bit 28: Reserved
00509     Peripheral_PowerDown(LPC1768_PCONP_PCGPDMA);  // bit 29:GP DMA function power/clock enable
00510     Peripheral_PowerDown(LPC1768_PCONP_PCENET); // bit 30:Ethernet block power/clock enable
00511     Peripheral_PowerDown(LPC1768_PCONP_PCUSB); // bit 31: PCUSB: USB interface power/clock enable     
00512   
00513      // only BIT 23, 24, 15 need to be on, but htis ths isnt working
00514      //Peripheral_PowerDown(0xFFFEFE7F);
00515     //Peripheral_PowerDown(0x7BEEF677);
00516     
00517     // Sonar code
00518     while(1)
00519     {
00520         //pc.printf("Distance %d mm\r\n", mu.getCurrentDistance());
00521         Sleep();
00522         mu.checkDistance();     //call checkDistance() as much as possible, as this is where
00523                                 //the class checks if dist needs to be called.
00524         wait(0.002);                           
00525     }
00526 }
00527 
00528 // --------------------------------------- End Code ---------------------------------------