Code written by Paul Rabbat, Angel Daruna, Jarel Hawkins, and Cordel Williams
Dependencies: 4DGL-uLCD-SE EthernetInterface HTTPClient NTPClient PinDetect SDFileSystem mbed-rpc mbed-rtos mbed wave_player
Fork of ECE4180_FinalProject by
Diff: main.cpp
- Revision:
- 4:bb1bb1fc0cf5
- Parent:
- 3:1ab8276a3cdc
- Child:
- 5:be2cfaa8f4a2
--- a/main.cpp Mon Nov 24 23:18:01 2014 +0000 +++ b/main.cpp Mon Dec 01 18:19:21 2014 +0000 @@ -18,11 +18,10 @@ // Global variables EthernetInterface eth; uLCD_4DGL uLCD(p28, p27, p29); // serial tx, serial rx, reset pin; -NTPClient ntpClient; alarmModel _alarm; string dayOfWeek, month, dayNum, ampm; int hour, minute, sec; -PwmOut led(LED1); // led 1 indicates dim value +PwmOut led1(LED1); // led 1 indicates dim value DigitalOut led2(LED2); // led 2 indicates delay time for interrupts DigitalOut led3(LED3); // Indicates the alarm has been set DigitalOut led4(LED4); // Indiciates the alarm is going off @@ -48,6 +47,7 @@ // Function prototypes HTTPServer create_simple_server(); HTTPServer create_interactive_server(); + void thread_server(void const *args); void thread_display(void const *args); void thread_alarm(void const *args); @@ -55,6 +55,14 @@ void dimmer(); void turn_on(); +/** Weather **/ +#include "HTTPClient.h" +#include "spdomparser.hpp" +#include "spxmlnode.hpp" +#include "spxmlhandle.hpp" +void getWeather(); +void getTime(); + int main() { dim = -1.0; printf("Getting IP Address.. "); @@ -86,16 +94,10 @@ printf("IP Address is %s\n", eth.getIPAddress()); - printf("Reading time..\n"); - char* domainName="us.pool.ntp.org"; //SET TO DOMAIN NAME OF SERVER GETTING TIME FROM - //GETS THE TIME FROM THE SERVER - //setTime(DOMAIN_NAME,PORT_NUMBER,TIME_OUT) - //DOMAIN_NAME= domain name - //PORT NUMBER=port number (123 for NTP) - //TIME_OUT= timeout value for request - ntpClient.setTime(domainName,123,0x00005000); - printf("Time set.\n"); - + getWeather(); + wait(1); + getTime(); + wait(1); //set up interrupt routine to detect AC line zero crossings zerocross.mode(PullNone); @@ -105,11 +107,94 @@ // interrupt routines dim the light - Thread t1(thread_server); //start thread_server + //Thread t1(thread_server); //start thread_server Thread t2(thread_display); Thread t3(thread_alarm); - while(1){} + while(1){ + led1=!led1; + Thread::wait(1000); + } +} + +void getWeather(){ + /*** WEATHER****/ + char buf[3000]; + printf("Getting weather..\n"); + HTTPClient http; + SP_XmlDomParser parser; + int retHttp = http.get("http://weather.yahooapis.com/forecastrss?w=28426187&u=c", buf, sizeof(buf)); + + switch(retHttp){ + case HTTP_OK: + printf("Read completely\n"); + break; + case HTTP_TIMEOUT: + printf("Connection Timeout\n"); + break; + case HTTP_CONN: + printf("Connection Error\n"); + break; + default: + printf("Error\n"); + } + + printf("\n----------%s----------\n",buf); + wait(5.0); + printf("Appending to parser..\n"); + + + /** FREEZING HERE **/ + parser.append( buf, strlen(buf)); // stream current buffer data to the XML parser + wait(5.0); + printf("Parser appended\n"); + + SP_XmlHandle rootHandle( parser.getDocument()->getRootElement() ); + + /* + SP_XmlCDataNode * title = rootHandle.getChild( "channel" ).getChild("title").getChild(0).toCData(); + printf("\r\n === Title:%s === \r\n",title->getText()); + SP_XmlCDataNode * title1 = rootHandle.getChild( "channel" ).getChild("image").getChild("title").getChild(0).toCData(); + printf("\r\n === Title:%s === \r\n",title1->getText()); + SP_XmlCDataNode * title2 = rootHandle.getChild( "channel" ).getChild("item").getChild("title").getChild(0).toCData(); + printf("\r\n === Title:%s === \r\n",title2->getText()); + */ + + SP_XmlElementNode * location = rootHandle.getChild( "channel" ).getChild( "yweather:location" ).toElement(); + if (location) { + printf("\r\n === Location:%s === \r\n",location->getAttrValue("city")); + } + + SP_XmlElementNode * forecast; + + forecast = rootHandle.getChild( "channel" ).getChild("item").getChild( "yweather:forecast",0).toElement(); + if (forecast) { + printf("\r\n ----- Date:%s(%s) ----- \r\n",forecast->getAttrValue("date"),forecast->getAttrValue("day")); + printf("Condition:%s \n",forecast->getAttrValue("text")); + printf("Temp:Low%sC High%sC\n",forecast->getAttrValue("low"),forecast->getAttrValue("high")); + } + + forecast = rootHandle.getChild( "channel" ).getChild("item").getChild( "yweather:forecast",1).toElement(); + if (forecast) { + printf("\r\n ----- Date:%s(%s) ----- \r\n",forecast->getAttrValue("date"),forecast->getAttrValue("day")); + printf("Condition:%s \n",forecast->getAttrValue("text")); + printf("Temp:Low%sC High%sC\n",forecast->getAttrValue("low"),forecast->getAttrValue("high")); + } + + printf("Weather complete!!!!!\n"); +} + +void getTime(){ + NTPClient ntpClient; + printf("Reading time..\n"); + char* domainName="us.pool.ntp.org"; //SET TO DOMAIN NAME OF SERVER GETTING TIME FROM + //GETS THE TIME FROM THE SERVER + //setTime(DOMAIN_NAME,PORT_NUMBER,TIME_OUT) + //DOMAIN_NAME= domain name + //PORT NUMBER=port number (123 for NTP) + //TIME_OUT= timeout value for request + ntpClient.setTime(domainName,123,0x00005000); + printf("Time set.\n"); } void thread_alarm(void const *args) @@ -182,14 +267,7 @@ chars_array = strtok(NULL, " :"); } - // use %02d uLCD.printf(" %s, %s %s\n %02d:%02d:%02d %s", dayOfWeek, month, dayNum, hour, minute, sec, ampm); - /* - if(minute < 10 && sec < 10){ uLCD.printf(" %s, %s %s\n %02d:%02d:%02d %s", dayOfWeek, month, dayNum, hour, minute, sec, ampm); } - else if(minute < 10){ uLCD.printf(" %s, %s %s\n %d:0%d:%d %s", dayOfWeek, month, dayNum, hour, minute, sec, ampm); } - else if (sec < 10){ uLCD.printf(" %s, %s %s\n %d:%d:0%d %s", dayOfWeek, month, dayNum, hour, minute, sec, ampm); } - else{ uLCD.printf(" %s, %s %s\n %d:%d:%d %s", dayOfWeek, month, dayNum, hour, minute, sec, ampm); } - */ if(_alarm.alarmSet){ uLCD.color(GREEN); if(_alarm.minutes < 10){ uLCD.printf("\n\n Alarm:\n %d:0%d %s", _alarm.hours, _alarm.minutes, _alarm.amPm); } @@ -253,7 +331,7 @@ void turn_on(){ //increase brightness for(dim = 0.0; dim <= 1.0; dim += 0.025) { - led = (dim < 0 ? 0 : dim); + led1 = (dim < 0 ? 0 : dim); wait(0.10); } dim = 1.0;