Program code for the project

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

main.cpp

Committer:
blazar
Date:
2017-12-13
Revision:
3:01d4b03ade7c
Parent:
2:15ba42aea6c7

File content as of revision 3:01d4b03ade7c:

#include "mbed.h"
#include "uLCD_4DGL.h"
#include "rtos.h"
#include "SDFileSystem.h"
#include "SDAlarm.h"
#include "wave_player.h"

Serial pc(USBTX, USBRX);
Serial esp(p28, p27); // tx, rx
SDFileSystem sd(p5, p6, p7, p8, "sd");

RawSerial  blue(p13,p14);
DigitalOut reset(p26);
DigitalOut led1(LED1);
DigitalOut led4(LED4);
Timer t;
SDAlarm sdalarm;

AnalogOut ao(p18);
wave_player wp(&ao);

time_t global_time;
uLCD_4DGL uLCD(p9,p10,p11); // serial tx, serial rx, reset pin;
Mutex lcd_mutex;

Thread alarmThread;
Thread alarmSound;

 
bool alarmWentOff = false;
bool play = false;

DigitalOut one(LED1);
DigitalOut two(LED2);
DigitalOut three(LED3);
DigitalOut four(LED4);
// Global count variable

int volatile hours=0;
int volatile minutes=0;
bool volatile alarmSet=false;
int volatile alarmTimeSet = 0;
int volatile timeDisp=0;
bool volatile AMPM=false; //False AM, true PM
 
int  count,ended,timeout;
char buf[2024];
char snd[1024];
char ssid[32] = "";     // enter WiFi router ssid inside the quotes
char pwd [32] = ""; // enter WiFi router password inside the quotes
bool timeReady = false;

char curTime[20];

void SendCMD(),getreply(),ESPconfig(),ESPsetbaudrate(), getTime();
int timeFromResponse();
 void dev_recv()
{
    led1 = !led1;
    while(esp.readable()) {
        pc.putc(esp.getc());
    }
}
 
void pc_recv()
{
    led4 = !led4;
    while(pc.readable()) {
        esp.putc(pc.getc());
    }
}
 

 
void bt_rec() {
    while(blue.readable()) {
        if (blue.getc()=='!') {
            char c = blue.getc();
            if (c=='B') { //button data
                char bnum = blue.getc(); //button number
                char pressedOrReleased = blue.getc();
                
                if (pressedOrReleased == '0') {
                    if (bnum=='1')  {
                        alarmSet = !alarmSet;
                    }
                }
                if (pressedOrReleased == '1') {
                    
                    if (bnum=='2') {
                        hours++;
                        if(hours>24){ hours= 0;}
                    }
                    if (bnum=='3') {
                            minutes++;
                            if(minutes>59) minutes= 0;
                    }
                }
                
            }
        }
    }
}
void alarm_sound() {
    while (true) {
        if (play){
            FILE *wave_file;
            wave_file=fopen("/sd/alarm_beep.wav","r");

            wp.play(wave_file);
            fclose(wave_file);
        }
    }
}
void alarm_thread() {
    time_t lastalarm = time(NULL);
    bool lastset = false;
    while(1) {
        time_t rawtime = hours*60*60 + minutes*60;
        struct tm * timeinfo;
        char buffer [80];

        timeinfo = localtime (&rawtime);
        strftime (buffer,80,"%I:%M %p",timeinfo);
        //if (lastalarm != rawtime) {
            lcd_mutex.lock();
            uLCD.locate(3,11);
            uLCD.color(WHITE);
            uLCD.text_width(1); 
            uLCD.text_height(1);
            uLCD.printf("%s", buffer);
            
            lastalarm = rawtime;
            
        //}
        if (lastset != alarmSet) {


            uLCD.color(WHITE);
            uLCD.text_width(1); 
            uLCD.text_height(1);
            uLCD.locate(3,12);
            if (alarmSet) {
                
                uLCD.color(WHITE);
                uLCD.printf("Alarm Set");
                sdalarm.setAlarm(rawtime);
            } else {
                
                uLCD.color(BLACK);
                uLCD.printf("Alarm Set");
            }
            lastset = alarmSet;
            
        }
        
        lcd_mutex.unlock();
        Thread::wait(50);
    }    
}
int main()
{
    uLCD.baudrate(3000000);
    blue.attach(&bt_rec,Serial::RxIrq);

    
    reset=0; //hardware reset for 8266
    pc.baud(9600);  // set what you want here depending on your terminal program speed
    pc.printf("\f\n\r-------------ESP8266 Hardware Reset-------------\n\r");
    wait(0.5);
    reset=1;
    timeout=2;
    getreply();
 
    esp.baud(9600);   // change this to the new ESP8266 baudrate if it is changed at any time.
 
    //ESPsetbaudrate();   //******************  include this routine to set a different ESP8266 baudrate  ******************
 
    ESPconfig();        //******************  include Config to set the ESP8266 configuration  ***********************
    getTime();
    pc.printf("%s", buf);
    
    timeDisp = timeFromResponse() + 18;
    set_time(timeDisp);
    
    
    
    time_t alarmTime = sdalarm.getAlarm();
    char timebuffer[80];
    struct tm * timeinfo;
    timeinfo = localtime (&alarmTime);
    
    strftime (timebuffer,80,"%H",timeinfo);
    
    hours = atoi(timebuffer);
    strftime (timebuffer,80,"%M",timeinfo);
    minutes = atoi(timebuffer);
    
    alarmThread.start(alarm_thread);
    alarmSound.start(alarm_sound);

    
    while (1) {
        time_t rawtime;
        struct tm * timeinfo;
        char buffer [80];
        time (&rawtime);
        global_time = rawtime;
        timeinfo = localtime (&rawtime);
        strftime (buffer,80,"%I:%M %p",timeinfo);
        
        if ((hours*60*60 + minutes*60) < (rawtime % (60*60*24))) {
            alarmWentOff = true;
        } else {
            alarmWentOff = false;
        }
        
        lcd_mutex.lock();
        uLCD.locate(0,0);
        uLCD.color(WHITE);
        uLCD.text_width(2); 
        uLCD.text_height(2);
        uLCD.printf("%s", buffer);
        lcd_mutex.unlock();
        Thread::wait(500);
        
        if (global_time % (60*60*24) == 0) {
            alarmWentOff = false;
        }
        if ((((int)global_time) % (60*60*24)) >= (hours*60*60 + minutes*60) && alarmSet && !alarmWentOff) {
            one = !one;
            lcd_mutex.lock();
            uLCD.locate(2,6);
            uLCD.color(RED);
            uLCD.text_width(2); 
            uLCD.text_height(2);
            uLCD.printf("ALARM");
            lcd_mutex.unlock();
            alarmWentOff = true;
            play = true;
        } else {
            play = false;
            lcd_mutex.lock();
            uLCD.locate(2,6);
            uLCD.color(BLACK);
            uLCD.text_width(2); 
            uLCD.text_height(2);
            uLCD.printf("ALARM");
            lcd_mutex.unlock();
        }
        
        
        
    }
 
}

// Sets new ESP8266 baurate, change the esp.baud(xxxxx) to match your new setting once this has been executed
void ESPsetbaudrate()
{
    strcpy(snd, "AT+CIOBAUD=115200\r\n");   // change the numeric value to the required baudrate
    SendCMD();
}
 
//  +++++++++++++++++++++++++++++++++ This is for ESP8266 config only, run this once to set up the ESP8266 +++++++++++++++
void ESPconfig()
{

    wait(5);
    pc.printf("\f---------- Starting ESP Config ----------\r\n\n");
        strcpy(snd,".\r\n.\r\n");
    SendCMD();
        wait(1);
    pc.printf("---------- Reset & get Firmware ----------\r\n");
    strcpy(snd,"node.restart()\r\n");
    SendCMD();
    timeout=5;
    getreply();
    pc.printf(buf);
 
    
    // set CWMODE to 1=Station,2=AP,3=BOTH, default mode 1 (Station)
    strcpy(snd, "wifi.setmode(wifi.STATION)\r\n");
    SendCMD();
    timeout=4;
    getreply();
    pc.printf(buf);
 
    wait(1);

    strcpy(snd, "wifi.sta.config(\"");
    strcat(snd, ssid);
    strcat(snd, "\",\"");
    strcat(snd, pwd);
    strcat(snd, "\")\r\n");
    SendCMD();
    timeout=10;
    getreply();
    pc.printf(buf);
 
    wait(1);
 
    strcpy(snd, "print(wifi.sta.getip())\r\n");
    SendCMD();
    timeout=3;
    getreply();
    pc.printf(buf);
 
    wait(1);
 

    strcpy(snd, "print(wifi.sta.status())\r\n");
    SendCMD();
    timeout=5;
    getreply();
    
}
void getTime() {
    wait(1);
    strcpy(snd,"sk=net.createConnection(net.TCP, 0)\r\n");
    SendCMD();
    timeout=3;
    getreply();
    
    strcpy(snd,"sk:on(\"receive\", function(sck, c) print(c) end )\r\n");
    SendCMD();
    timeout=3;
    getreply();
    
    strcpy(snd,"sk:connect(80,\"api.timezonedb.com\")\r\n");
    SendCMD();
    timeout=3;
    getreply();
    wait(1);
    
    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");
    SendCMD();
    timeout=17;
    getreply();
    
    timeReady = true;
    
}
void SendCMD()
{
    esp.printf("%s", snd);
}
 
void getreply()
{
    memset(buf, '\0', sizeof(buf));
    t.start();
    ended=0;
    count=0;
    while(!ended) {
        if(esp.readable()) {
            buf[count] = esp.getc();
            count++;
        }
        if(t.read() > timeout) {
            ended = 1;
            t.stop();
            t.reset();
        }
    }
}

int timeFromResponse() {
    char * timestamp;
    timestamp = strstr(buf, "\"timestamp\":");
    
    timestamp = timestamp + 12;
    int count = 0;
    
    while(timestamp[count] != ',') {
        count++;
    }
    
    strncpy(curTime, timestamp, count);
    return atoi(curTime);
}