Alarm clock with weather forecast

ASHWIN SHIVANI

TANIA GANDHE

This project aims at creating an alarm clock with weather forecast. Initially the alarm clock establishes an Ethernet connection and then conducts an automatic network timesync.The time zone, alarm hour, alarm minutes and on/off for alarm are implemented using a switch.Once established , the current time will display on the LCD. The alarm flashes the 4 LEDs when triggered and stops after a minute or through the use of any pushbuttons.The hardwired pushbuttons were implemented as pull-up networks with 1k Ohm resistors.In software, they were implemented by using switch case.

/media/uploads/ashivani3/_scaled_dscn0935.jpg

FUNCTIONALITY

The internet alarm clock contains 5 menus and uses three hardwired push buttons to navigate those menus

MenuPushButton
1. Default - Current time1. Menu Select
2. Timezone2. Add
3. Alarm hour3. Subtract
4. Alarm Minute
5. Alarm Set

CONNECTIONS:

Ethernet
mbedadapter
TD+p1
TD-p2
RD+p7
RD-p8
Text LCD Display
mbedLCDmbedLCDmbedLCDmbedLCD
GND0VRSP5D0D4P7
VCC5VRW0VD1D5P8
VO{1k Ohm }- 0VEP6D2D6P9
D3D7P10

Once alarm time and current time match it begins ringing the alarm. Simultaneously RSS feeds are extracted from the weather.com website and the current weather is displayed on the LCD

/media/uploads/ashivani3/_scaled_photo0525.jpg /media/uploads/ashivani3/_scaled_dscn0937.jpg http://http://www.youtube.com/watch?v=mto_QIxvwcI

alarm.cpp

#include "main.h"

//DigitalIn  enable(p25);
//DigitalOut myalarm(p26);
//Ticker timer;
LocalFileSystem local("local");
void request_callback(HTTPResult r) {                                                             //change//
    result = r;
    completed = true;
}
/*******************************************************************
 * Print_lcd
 *
 * This function clears the LCD, then either prints 1. the alarm time
 * or 2. the current time and alarm clock menu state.
 ******************************************************************/
void print_lcd() {
   // printf("entering print_lcd");
    lcd.cls();
    /* Menu options 2 and above deal with the alarm */
    if (menu > 1)
        print_time(ahour, amin);
    else
        print_time(cTime->tm_hour, cTime->tm_min);
    print_menu();
}

/******************************************************************
 * Print_time
 *
 * This function takes an hour(in 24hr time) and a minute and
 * prints the correct format to the lcd.
 *****************************************************************/
void print_time(int hour, int minute) {
   // printf("entering print_time");
    /* Variable tmphr is 24 hour time adjusted for timezone */
    int tmphr = hour + timezone;
    while (tmphr < 0)
        tmphr += 24;
    tmphr %= 24;

    /* Variable printhr is 12 hour time for printing */
    int printhr = tmphr % 12;
    if (printhr == 0)
        printhr += 12;
    lcd.printf("%2d:%02d ", printhr, minute);

    /* tmphr allows for checking of AM/PM */
    if ((tmphr > 11) && (tmphr < 24))
        lcd.printf("PM\n");
    else
        lcd.printf("AM\n");
}

/*******************************************************************
 * Print_menu
 *
 * This function prints the menu state of the alarm clock to the LCD.
 * The menu options are 1. Viewing current time 2.  Setting Timezone
 * 3. Setting alarm hour 4. Setting alarm minute 5. Toggling alarm
 ******************************************************************/
void print_menu() {
   // printf("entering print_menu");
    switch (menu) {
        case 0:
            break;
        case 1:
            lcd.printf("TZ is (UTC) %+d\n", timezone);
            break;
        case 2:
            lcd.printf("Set Alarm Hour\n");
            break;
        case 3:
            lcd.printf("Set Alarm Min\n");
            break;
        case 4:
            lcd.printf("Alarm is ");
            if (alarmstate == false)
                lcd.printf("Off\n");
            else
                lcd.printf("On\n");
            break;
        default:
            lcd.printf("Invalid menu %d\n", menu);
            break;
    }
}

/******************************************************************
 * Change_menu
 *
 * This function uses a dedicated pushbutton that changes the menu.
 * The state variable, menu, gives the user the ability to cycle
 * through the 5 alarm clock menus.
 *****************************************************************/
void change_menu() {
   // printf("entering change_menu");
    alarm_snooze();
    menu = (menu + 1) % 5;
    print_lcd();
}

/******************************************************************
 * Push_plus
 *
 * This function  uses a dedicated pushbutton to only increment or
 * "Add" the value of variables of an alarm clock i.e. minute, hour,
 * time zone
 *****************************************************************/
void push_plus() {
   // printf("entering push_plus");
    alarm_snooze();
    button_press(1);
}

/******************************************************************
 * Push_minus
 *
 * This function  uses a dedicated pushbutton to only decrement
 * or "Subtract" the value of variables of an alarm clock
 * i.e. minute, hour, time zone
 *****************************************************************/
void push_minus() {
   // printf("entering push_minus");
    alarm_snooze();
    button_press(-1);
}

/******************************************************************
 * Button_press
 *
 * This function performs an action based on which menu item is
 * currently selected. It is called whenever the "Add" or
 * "Subtract" button is pressed.
 *****************************************************************/
void button_press(int input) {
   // printf("entering button_press");
    switch (menu) {
        case 0:
            break;
        case 1:
            timezone += 12;
            timezone += input;
            while (timezone < 0)
                timezone += 24;
            timezone %= 24;
            timezone -= 12;
            break;
        case 2:
            ahour += input;
            while (ahour < 0)
                ahour += 24;
            ahour %= 24;
            break;
        case 3:
            amin += input;
            while (amin < 0)
                amin += 60;
            amin %= 60;
            break;
        case 4:
            alarmstate = !alarmstate;       // Turn alarm on/off
            break;
        default:
            lcd.printf("Invalid state %d\n", menu);
            break;
    }
    print_lcd();
}

/******************************************************************
 * Alarm_ring
 *
 * This function rings the alarm by flashing the leds
 *****************************************************************/
//int flip=0;
//int counter=0;
void alarm_ring() {
   // printf("entering alarm_ring");
    if (led == 0x0F)
        led = 0;
    else
        led = (led << 1) | 1;
       // myalarm=1;



buzzer.beep(1000,0.5); 


   // myalarm=1;
        //while(1){
        //myalarm=enable;
        //wait(0.2);
        //}

}

/******************************************************************
 * Alarm_snooze
 *
 * This function turns off the alarm
 *****************************************************************/
void alarm_snooze() {
  //  printf("entering alarm_snooze");
    if (ringflag == true) {
        ringflag = false;
        snooze = true;

        ring.detach();
        led = 0;
    }
}

/******************************************************************
 * Alarm_check
 *
 * This function compares the alarm time vs the current time. Once
 * alarm time and real time match it begins ringing the alarm.
 * Once the times differ then it turns off the alarm.
 *****************************************************************/
//myalarm=0;
void alarm_check() {
   // printf("entering alarm_check");
    if ((cTime->tm_min == amin) && (cTime->tm_hour == ahour)) {
        if ((alarmstate == true) && (ringflag == false) && (snooze == false)) {
            ringflag = true;
            ring.attach(&alarm_ring, .005); // Set up a Ticker for the alarm,
            // calls alarm_ring every .5 seconds until stopped

            // timer.attach(&alarm_ring,(1/400)); // setting up ticker for speaker
            /**************************************/

// if(myalarm==0)

            printf("Setup OK\n");
            HTTPStream stream;                                      //change//

            char BigBuf[4096 + 1] = {0};
            stream.readNext((byte*)BigBuf, 4096); //Point to buffer for the first read

            HTTPResult r = http.get("http://rss.weather.com/weather/rss/local/USGA0028?cm_ven=LWO&cm_cat=rss&par=LWO_rss", &stream, request_callback); //Load a very large page, such as the hackaday RSS feed
            //HTTPResult r = http.get("http://translate.google.com/translate_tts?tl=en&q=text", &stream, request_callback); //Load a very large page, such as the hackaday RSS feed
            FILE *fp = fopen("/local/out.txt", "w");
            while (!completed) {
                Net::poll(); //Polls the Networking stack
                if (stream.readable()) {
                    BigBuf[stream.readLen()] = 0; //Transform this buffer in a zero-terminated char* string
                    // printf("%s",BigBuf); //Display it while loading
                    //Note: some servers do not like if you throttle them too much, so printf'ing during a request is generally bad practice
                    fprintf(fp, BigBuf);
                    stream.readNext((byte*)BigBuf, 4096); //Buffer has been read, now we can put more data in it

                }
            }


            fclose(fp);
            char c;
            char buff[500];
            int count=0;




            //change///
            fp = fopen( "/local/out.txt", "r");

            if (fp==NULL) printf ("Error opening file");

            else

            {

                do

                {

                    c = fgetc (fp);

                    if (c == 'a')

                    {

                        c = fgetc(fp);

                        if (c == 'l')

                        {

                            c = fgetc(fp);

                            if (c == 't')

                            {

                                c = fgetc(fp);

                                if (c == '=')

                                {

                                    c = fgetc(fp);

                                    if ( c == '"')

                                    {

                                        c = fgetc(fp);

                                        if ( c == '"')

                                        {

                                            c = fgetc(fp);

                                            if (c == ' ')

                                            {
                                                c = fgetc(fp);
                                                if ( c == '/')

                                                {

                                                    c = fgetc(fp);
                                                    if ( c == '>')

                                                    {


                                                        int i = 0;

                                                        c = fgetc(fp);

                                                        while (c != '.')

                                                        {
                                                            count++;
                                                            buff[i] = c;

                                                            i++;

                                                            c = fgetc(fp);

                                                        }

                                                        buff[i] = '\0';

                                                        lcd.printf("%s",buff);

                                                    }

                                                }

                                            }

                                        }

                                    }

                                }

                            }

                        }

                    }


                } while (count <= 20);

                fclose (fp);
            }



        }
    } else {
         alarm_snooze();
        snooze = false;
    }
}

int main() {
   // printf("entering int main");
    /* Set up Ethernet */
    lcd.cls();
    lcd.printf("Setting up Eth\n");
    EthernetErr ethErr = eth.setup();
    if (ethErr) {
        lcd.cls();
        lcd.printf("Error with Eth\nNum: %d", ethErr);
        return -1;
    }
     //ring.attach(&alarm_ring, .0025);

    /* Set up NTP */
    lcd.printf("Setting up NTP\n");
    Host server(IpAddr(), 123, "0.uk.pool.ntp.org");
    ntp.setTime(server);

    /* Initialize all the interrupts for each of the pushbuttons */
    statechange.rise(&change_menu);
    plus1.rise(&push_plus);
    minus1.rise(&push_minus);

    /* Temporary variables for control loop */
    time_t rtc_time = time(NULL);
    int minute = -1;

    /* Initialize alarm time */
    ahour = 0;
    amin = 0;

    /* Main control loop */
    while (1) {
        /* Update current time */
        rtc_time = time(NULL);
        cTime = localtime(&rtc_time);
        //printf("\n\nupdating new time \n\n");
        /* Only redraw the lcd display if anything changes */
        if (cTime->tm_min != minute) {
            minute = cTime->tm_min;
            print_lcd();

            /* Update time from NTP server if it's midnight UTC */
            if ((cTime->tm_min == 0) && (cTime->tm_hour == 0)) {
                Host server(IpAddr(), 123, "0.north-america.pool.ntp.org");
                ntp.setTime(server);
            }
        }

        /* Check to see if the alarm should be started/stopped */
        alarm_check();
    }
    return 0;
}

alarm.h

#include "mbed.h"
#include "EthernetNetIf.h"
#include "NTPClient.h"
#include "TextLCD.h"
#include "HTTPClient.h"
#include "beep.h"

/****************** Class Global Declartions **********************/
EthernetNetIf eth; 
NTPClient ntp;
HTTPClient http;
HTTPResult result;
TextLCD lcd(p5, p6, p7, p8, p9, p10);
InterruptIn statechange(p23);
InterruptIn plus1(p22);
InterruptIn minus1(p21);
Ticker ring;
BusOut led(LED1, LED2, LED3, LED4);
Beep buzzer(p26);

/******************************************************************/
/***************** Standard Global Declartions ********************/

int menu = 0;           // Holds the current menu information 
int timezone = 0;       // Timezone is configured as UTC + timezone - 12
bool alarmstate = false;// Determines whether the alarm is set or not
bool ringflag = false;  // Determines if alarm is ringing
bool snooze = false;    // True if the alarm has been turned off by a button
struct tm *cTime;       // Stucture that holds the current time
int ahour;              // Value that holds alarm hour
int amin;               // Value that holds alarm minute
bool completed = false;
/******************************************************************/
/*********************** Function Declartions *********************/
void print_lcd();
void print_time(int, int);
void print_menu();
void change_menu();
void button_press(int);
void push_plus();
void push_minus();
void alarm_ring();
void alarm_snooze();
void alarm_check();
void request_callback(HTTPResult);
/******************************************************************/


Please log in to post comments.