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
main.cpp
- Committer:
- prabbat3
- Date:
- 2014-12-01
- Revision:
- 6:24d42cae48ad
- Parent:
- 5:be2cfaa8f4a2
- Child:
- 7:b0f2169bdf48
File content as of revision 6:24d42cae48ad:
// Includes #include "mbed.h" #include "NTPClient.h" #include "uLCD_4DGL.h" #include "EthernetInterface.h" #include <string> #include "mbed_rpc.h" #include "RPCCommand.h" #include "HTTPServer.h" #include "Formatter.h" #include "RequestHandler.h" #include "RPCType.h" #include "alarmContainer.h" // Defines #define SERVER_PORT 80 //HTTPSERVER // Global variables EthernetInterface eth; uLCD_4DGL uLCD(p28, p27, p29); // serial tx, serial rx, reset pin; alarmModel _alarm; string dayOfWeek, month, dayNum, ampm; int hour, minute, sec; 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 int alarmSignaled = 0; // pin for ZeroCross tail input // An external 1K pullup required InterruptIn zerocross(p22); // pin for PowerSSRtail output DigitalOut SSR(p21); //use timer interrupts to control dimming Timeout SSRtriggerOn; // dimmer value 0.0=off and 1.0=full on volatile float dim; // AC power line frequency const float powerlinefrequency=60.000; // 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); void triggerOn(); void dimmer(); void turn_on(); /** Weather **/ #include "HTTPClient.h" #include "spdomparser.hpp" #include "spxmlnode.hpp" #include "spxmlhandle.hpp" void getWeather(); void getTime(); void parseResults(); char buf[3000]; int main() { dim = -1.0; printf("Getting IP Address.. "); uLCD.baudrate(2000000); uLCD.cls(); uLCD.background_color(BLACK); uLCD.textbackground_color(BLACK); uLCD.locate(0,0); uLCD.color(BLUE); uLCD.text_height(2); uLCD.text_width(2); uLCD.printf("\n\nUniv Time\n Clock\n"); uLCD.text_height(1); uLCD.text_width(1); uLCD.printf(" Loading...\n"); uLCD.locate(0,0); if(eth.init()) { printf("Error while initializing the ethernet interface.\n"); return -1; } wait(5); if(eth.connect()) { printf("Error while starting the ethernet interface.\n"); return -1; } printf("IP Address is %s\n", eth.getIPAddress()); getWeather(); wait(1); parseResults(); wait(1); getTime(); wait(1); //set up interrupt routine to detect AC line zero crossings zerocross.mode(PullNone); wait(.2); zerocross.rise(&dimmer); // main program only sets dimmer level (dim) // interrupt routines dim the light //Thread t1(thread_server); //start thread_server Thread t2(thread_display); Thread t3(thread_alarm); while(1){ led1=!led1; Thread::wait(1000); } } void getWeather(){ /*** WEATHER****/ printf("Getting weather..\n"); HTTPClient http; int retHttp = http.get("http://weather.yahooapis.com/forecastrss?w=2357024&u=f", 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"); } } void parseResults(){ printf("Starting parser\n"); SP_XmlDomParser parser; printf("\n----------%s----------\n",buf); parser.append( buf, strlen(buf)); // stream current buffer data to the XML parser 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%sF High%sF\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%sF High%sF\n",forecast->getAttrValue("low"),forecast->getAttrValue("high")); } printf("Parser 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) { while(1){ if(_alarm.alarmSet && !alarmSignaled){ led3 = 1; if(_alarm.hours == hour && _alarm.minutes == minute && _alarm.amPm == ampm){ alarmSignaled = 1; turn_on(); led4 = 1; } } Thread::wait(100); } } void thread_display(void const *args){ time_t ctTime; //system time structure uLCD.cls(); uLCD.locate(0,0); uLCD.text_height(2); uLCD.text_width(1); char buffer[80]; //BUFFER TO HOLD FORMATTED TIME DATA uLCD.textbackground_color(BLACK); while (1) { uLCD.locate(0,0); uLCD.color(RED); // loop and periodically update the LCD's time display ctTime = time(NULL)-(3600*4); //TIME with offset for eastern time US //FORMAT TIME FOR DISPLAY AND STORE FORMATTED RESULT IN BUFFER strftime(buffer,80,"%a %b %d %T %p %z %Z",localtime(&ctTime)); int i=0; char* chars_array = strtok(buffer, " :"); //printf("Chars array: \n"); while(chars_array) { switch(i){ case 0: dayOfWeek = chars_array; break; case 1: month = chars_array; break; case 2: dayNum = chars_array; break; case 3: //chars_array = chars_array - 1; int hourTemp = atoi(chars_array); hourTemp--; //Daylight savings if(hourTemp > 12) hourTemp -= 12; hour = hourTemp; break; case 4: minute = atoi(chars_array); break; case 5: sec = atoi(chars_array); break; case 6: ampm = chars_array; break; } i++; chars_array = strtok(NULL, " :"); } uLCD.printf(" %s, %s %s\n %02d:%02d:%02d %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); } else{ uLCD.printf("\n\n Alarm:\n %d:%d %s", _alarm.hours, _alarm.minutes, _alarm.amPm); } } Thread::wait(100); } } void thread_server(void const *args) { RPCType::instance().register_types(); HTTPServer srv = create_interactive_server(); if(!srv.init(SERVER_PORT)) { eth.disconnect(); printf("Thread 1 error.\n"); return; } srv.run(); } HTTPServer create_simple_server() { HTTPServer srv; srv.add_request_handler("DELETE", new DeleteRequestHandler()); srv.add_request_handler("GET", new GetRequestHandler()); srv.add_request_handler("PUT", new PutRequestHandler()); return srv; } HTTPServer create_interactive_server() { HTTPServer srv(new InteractiveHTMLFormatter()); srv.add_request_handler("GET", new ComplexRequestHandler()); return srv; } // this interrupt routine is activated after a time delay set by dim value void triggerOn() { SSR = 1; led2=0; } // this interrupt routine is activated by every AC line zero crossing // it is needed to synchronize the SSR turnon time delay to the AC line void dimmer() { // turn off SSR at zero crossing SSR = 0; // compute time delay using dim value and set timer interrupt // triggers SSR after a small post zero crossing time delay SSRtriggerOn.attach(&triggerOn,(1.001-dim)/(2*powerlinefrequency)); led2=1; } void turn_on(){ //increase brightness for(dim = 0.0; dim <= 1.0; dim += 0.025) { led1 = (dim < 0 ? 0 : dim); wait(0.10); } dim = 1.0; }