Program code for the project

Dependencies:   4DGL-uLCD-SE SDAlarm mbed SDFileSystem SoundAPI mbed-rtos

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 "rtos.h"
00004 #include "SDFileSystem.h"
00005 #include "SDAlarm.h"
00006 #include "wave_player.h"
00007 
00008 Serial pc(USBTX, USBRX);
00009 Serial esp(p28, p27); // tx, rx
00010 SDFileSystem sd(p5, p6, p7, p8, "sd");
00011 
00012 RawSerial  blue(p13,p14);
00013 DigitalOut reset(p26);
00014 DigitalOut led1(LED1);
00015 DigitalOut led4(LED4);
00016 Timer t;
00017 SDAlarm sdalarm;
00018 
00019 AnalogOut ao(p18);
00020 wave_player wp(&ao);
00021 
00022 time_t global_time;
00023 uLCD_4DGL uLCD(p9,p10,p11); // serial tx, serial rx, reset pin;
00024 Mutex lcd_mutex;
00025 
00026 Thread alarmThread;
00027 Thread alarmSound;
00028 
00029  
00030 bool alarmWentOff = false;
00031 bool play = false;
00032 
00033 DigitalOut one(LED1);
00034 DigitalOut two(LED2);
00035 DigitalOut three(LED3);
00036 DigitalOut four(LED4);
00037 // Global count variable
00038 
00039 int volatile hours=0;
00040 int volatile minutes=0;
00041 bool volatile alarmSet=false;
00042 int volatile alarmTimeSet = 0;
00043 int volatile timeDisp=0;
00044 bool volatile AMPM=false; //False AM, true PM
00045  
00046 int  count,ended,timeout;
00047 char buf[2024];
00048 char snd[1024];
00049 char ssid[32] = "";     // enter WiFi router ssid inside the quotes
00050 char pwd [32] = ""; // enter WiFi router password inside the quotes
00051 bool timeReady = false;
00052 
00053 char curTime[20];
00054 
00055 void SendCMD(),getreply(),ESPconfig(),ESPsetbaudrate(), getTime();
00056 int timeFromResponse();
00057  void dev_recv()
00058 {
00059     led1 = !led1;
00060     while(esp.readable()) {
00061         pc.putc(esp.getc());
00062     }
00063 }
00064  
00065 void pc_recv()
00066 {
00067     led4 = !led4;
00068     while(pc.readable()) {
00069         esp.putc(pc.getc());
00070     }
00071 }
00072  
00073 
00074  
00075 void bt_rec() {
00076     while(blue.readable()) {
00077         if (blue.getc()=='!') {
00078             char c = blue.getc();
00079             if (c=='B') { //button data
00080                 char bnum = blue.getc(); //button number
00081                 char pressedOrReleased = blue.getc();
00082                 
00083                 if (pressedOrReleased == '0') {
00084                     if (bnum=='1')  {
00085                         alarmSet = !alarmSet;
00086                     }
00087                 }
00088                 if (pressedOrReleased == '1') {
00089                     
00090                     if (bnum=='2') {
00091                         hours++;
00092                         if(hours>24){ hours= 0;}
00093                     }
00094                     if (bnum=='3') {
00095                             minutes++;
00096                             if(minutes>59) minutes= 0;
00097                     }
00098                 }
00099                 
00100             }
00101         }
00102     }
00103 }
00104 void alarm_sound() {
00105     while (true) {
00106         if (play){
00107             FILE *wave_file;
00108             wave_file=fopen("/sd/alarm_beep.wav","r");
00109 
00110             wp.play(wave_file);
00111             fclose(wave_file);
00112         }
00113     }
00114 }
00115 void alarm_thread() {
00116     time_t lastalarm = time(NULL);
00117     bool lastset = false;
00118     while(1) {
00119         time_t rawtime = hours*60*60 + minutes*60;
00120         struct tm * timeinfo;
00121         char buffer [80];
00122 
00123         timeinfo = localtime (&rawtime);
00124         strftime (buffer,80,"%I:%M %p",timeinfo);
00125         //if (lastalarm != rawtime) {
00126             lcd_mutex.lock();
00127             uLCD.locate(3,11);
00128             uLCD.color(WHITE);
00129             uLCD.text_width(1); 
00130             uLCD.text_height(1);
00131             uLCD.printf("%s", buffer);
00132             
00133             lastalarm = rawtime;
00134             
00135         //}
00136         if (lastset != alarmSet) {
00137 
00138 
00139             uLCD.color(WHITE);
00140             uLCD.text_width(1); 
00141             uLCD.text_height(1);
00142             uLCD.locate(3,12);
00143             if (alarmSet) {
00144                 
00145                 uLCD.color(WHITE);
00146                 uLCD.printf("Alarm Set");
00147                 sdalarm.setAlarm(rawtime);
00148             } else {
00149                 
00150                 uLCD.color(BLACK);
00151                 uLCD.printf("Alarm Set");
00152             }
00153             lastset = alarmSet;
00154             
00155         }
00156         
00157         lcd_mutex.unlock();
00158         Thread::wait(50);
00159     }    
00160 }
00161 int main()
00162 {
00163     uLCD.baudrate(3000000);
00164     blue.attach(&bt_rec,Serial::RxIrq);
00165 
00166     
00167     reset=0; //hardware reset for 8266
00168     pc.baud(9600);  // set what you want here depending on your terminal program speed
00169     pc.printf("\f\n\r-------------ESP8266 Hardware Reset-------------\n\r");
00170     wait(0.5);
00171     reset=1;
00172     timeout=2;
00173     getreply();
00174  
00175     esp.baud(9600);   // change this to the new ESP8266 baudrate if it is changed at any time.
00176  
00177     //ESPsetbaudrate();   //******************  include this routine to set a different ESP8266 baudrate  ******************
00178  
00179     ESPconfig();        //******************  include Config to set the ESP8266 configuration  ***********************
00180     getTime();
00181     pc.printf("%s", buf);
00182     
00183     timeDisp = timeFromResponse() + 18;
00184     set_time(timeDisp);
00185     
00186     
00187     
00188     time_t alarmTime = sdalarm.getAlarm();
00189     char timebuffer[80];
00190     struct tm * timeinfo;
00191     timeinfo = localtime (&alarmTime);
00192     
00193     strftime (timebuffer,80,"%H",timeinfo);
00194     
00195     hours = atoi(timebuffer);
00196     strftime (timebuffer,80,"%M",timeinfo);
00197     minutes = atoi(timebuffer);
00198     
00199     alarmThread.start(alarm_thread);
00200     alarmSound.start(alarm_sound);
00201 
00202     
00203     while (1) {
00204         time_t rawtime;
00205         struct tm * timeinfo;
00206         char buffer [80];
00207         time (&rawtime);
00208         global_time = rawtime;
00209         timeinfo = localtime (&rawtime);
00210         strftime (buffer,80,"%I:%M %p",timeinfo);
00211         
00212         if ((hours*60*60 + minutes*60) < (rawtime % (60*60*24))) {
00213             alarmWentOff = true;
00214         } else {
00215             alarmWentOff = false;
00216         }
00217         
00218         lcd_mutex.lock();
00219         uLCD.locate(0,0);
00220         uLCD.color(WHITE);
00221         uLCD.text_width(2); 
00222         uLCD.text_height(2);
00223         uLCD.printf("%s", buffer);
00224         lcd_mutex.unlock();
00225         Thread::wait(500);
00226         
00227         if (global_time % (60*60*24) == 0) {
00228             alarmWentOff = false;
00229         }
00230         if ((((int)global_time) % (60*60*24)) >= (hours*60*60 + minutes*60) && alarmSet && !alarmWentOff) {
00231             one = !one;
00232             lcd_mutex.lock();
00233             uLCD.locate(2,6);
00234             uLCD.color(RED);
00235             uLCD.text_width(2); 
00236             uLCD.text_height(2);
00237             uLCD.printf("ALARM");
00238             lcd_mutex.unlock();
00239             alarmWentOff = true;
00240             play = true;
00241         } else {
00242             play = false;
00243             lcd_mutex.lock();
00244             uLCD.locate(2,6);
00245             uLCD.color(BLACK);
00246             uLCD.text_width(2); 
00247             uLCD.text_height(2);
00248             uLCD.printf("ALARM");
00249             lcd_mutex.unlock();
00250         }
00251         
00252         
00253         
00254     }
00255  
00256 }
00257 
00258 // Sets new ESP8266 baurate, change the esp.baud(xxxxx) to match your new setting once this has been executed
00259 void ESPsetbaudrate()
00260 {
00261     strcpy(snd, "AT+CIOBAUD=115200\r\n");   // change the numeric value to the required baudrate
00262     SendCMD();
00263 }
00264  
00265 //  +++++++++++++++++++++++++++++++++ This is for ESP8266 config only, run this once to set up the ESP8266 +++++++++++++++
00266 void ESPconfig()
00267 {
00268 
00269     wait(5);
00270     pc.printf("\f---------- Starting ESP Config ----------\r\n\n");
00271         strcpy(snd,".\r\n.\r\n");
00272     SendCMD();
00273         wait(1);
00274     pc.printf("---------- Reset & get Firmware ----------\r\n");
00275     strcpy(snd,"node.restart()\r\n");
00276     SendCMD();
00277     timeout=5;
00278     getreply();
00279     pc.printf(buf);
00280  
00281     
00282     // set CWMODE to 1=Station,2=AP,3=BOTH, default mode 1 (Station)
00283     strcpy(snd, "wifi.setmode(wifi.STATION)\r\n");
00284     SendCMD();
00285     timeout=4;
00286     getreply();
00287     pc.printf(buf);
00288  
00289     wait(1);
00290 
00291     strcpy(snd, "wifi.sta.config(\"");
00292     strcat(snd, ssid);
00293     strcat(snd, "\",\"");
00294     strcat(snd, pwd);
00295     strcat(snd, "\")\r\n");
00296     SendCMD();
00297     timeout=10;
00298     getreply();
00299     pc.printf(buf);
00300  
00301     wait(1);
00302  
00303     strcpy(snd, "print(wifi.sta.getip())\r\n");
00304     SendCMD();
00305     timeout=3;
00306     getreply();
00307     pc.printf(buf);
00308  
00309     wait(1);
00310  
00311 
00312     strcpy(snd, "print(wifi.sta.status())\r\n");
00313     SendCMD();
00314     timeout=5;
00315     getreply();
00316     
00317 }
00318 void getTime() {
00319     wait(1);
00320     strcpy(snd,"sk=net.createConnection(net.TCP, 0)\r\n");
00321     SendCMD();
00322     timeout=3;
00323     getreply();
00324     
00325     strcpy(snd,"sk:on(\"receive\", function(sck, c) print(c) end )\r\n");
00326     SendCMD();
00327     timeout=3;
00328     getreply();
00329     
00330     strcpy(snd,"sk:connect(80,\"api.timezonedb.com\")\r\n");
00331     SendCMD();
00332     timeout=3;
00333     getreply();
00334     wait(1);
00335     
00336     strcpy(snd, "sk:send(\"GET /v2/get-time-zone?key=[API key here]&format=json&by=zone&zone=America/New_York HTTP/1.1\\r\\nHost: api.timezonedb.com\\r\\nConnection: keep-alive\\r\\nAccept: */*\\r\\n\\r\\n\")\r\n");
00337     SendCMD();
00338     timeout=17;
00339     getreply();
00340     
00341     timeReady = true;
00342     
00343 }
00344 void SendCMD()
00345 {
00346     esp.printf("%s", snd);
00347 }
00348  
00349 void getreply()
00350 {
00351     memset(buf, '\0', sizeof(buf));
00352     t.start();
00353     ended=0;
00354     count=0;
00355     while(!ended) {
00356         if(esp.readable()) {
00357             buf[count] = esp.getc();
00358             count++;
00359         }
00360         if(t.read() > timeout) {
00361             ended = 1;
00362             t.stop();
00363             t.reset();
00364         }
00365     }
00366 }
00367 
00368 int timeFromResponse() {
00369     char * timestamp;
00370     timestamp = strstr(buf, "\"timestamp\":");
00371     
00372     timestamp = timestamp + 12;
00373     int count = 0;
00374     
00375     while(timestamp[count] != ',') {
00376         count++;
00377     }
00378     
00379     strncpy(curTime, timestamp, count);
00380     return atoi(curTime);
00381 }