Adam Zuravleff / Mbed 2 deprecated 4180_Final_Project

Dependencies:   4DGL-uLCD-SE EthernetInterface NTPClient PinDetect SDFileSystem mbed-rtos mbed wave_player HTTPClient

Fork of 4180_Final_Project_WaveProblems by 4180

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 //WavePlayer_HelloWorld4180
00002 //internet_clock
00003 
00004 #include "mbed.h"
00005 #include "SDFileSystem.h"
00006 #include "wave_player.h"
00007 #include "EthernetInterface.h"
00008 #include "NTPClient.h"
00009 #include "uLCD_4DGL.h"
00010 #include "rtos.h"
00011 #include "PinDetect.h"
00012 #include "HTTPClient.h"
00013 
00014 //pinouts
00015 // Graphic LCD - TX, RX, and RES pins
00016 uLCD_4DGL uLCD(p28,p27,p29);
00017 SDFileSystem sd(p11, p12, p13, p14, "sd"); //SD card
00018 AnalogOut DACout(p18);
00019 wave_player waver(&DACout);
00020 PinDetect snooze(p19);  //snooze button
00021 PinDetect off(p20);     //turn alarm off
00022 DigitalOut myled1(LED1);
00023 DigitalOut myled2(LED2);
00024 DigitalOut myled3(LED3);
00025 //Mutex lcd_mutex;
00026 
00027 // Parameters
00028 char* time_domain_name = "0.uk.pool.ntp.org";
00029 //char* weth_domain_name = "http://weather.yahooapis.com/forecastrss?w=2502265";
00030 //char* weth_domain_name = "https://query.yahooapis.com/v1/public/yql?q=select%20item.condition%20from%20weather.forecast%20where%20woeid%20in%20%28select%20woeid%20from%20geo.places%281%29%20where%20text%3D%22atlanta%2C%20ga%22%29&format=json&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys";
00031 char* weth_domain_name = "https://query.yahooapis.com/v1/public/yql?q=select%20item.condition%20from%20weather.forecast%20where%20woeid%20in%20%28select%20woeid%20from%20geo.places%281%29%20where%20text%3D%22atlanta%2C%20ga%22%29&format=json&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys";
00032 int port_number = 123;
00033 
00034 // Networking
00035 EthernetInterface eth;
00036 NTPClient ntp_client;
00037 
00038 #define snoozeTime 10
00039 
00040 //global variables
00041 time_t ct_time;
00042 // Base alarm time-24 hour clock
00043 int baseAlarmHour = 0; //0-23
00044 int baseAlarmMin = 0;
00045 // Current alarm time
00046 int curAlarmHour = 18; //0-23
00047 int curAlarmMin = 9;
00048 bool play = true;
00049 
00050 //time thread
00051 void time_thread(void const *args)
00052 {
00053     char time_buffer[80];
00054     // Loop and update clock
00055     while (1) {
00056         //lcd_mutex.lock();
00057         uLCD.locate(0, 1);
00058         ct_time = time(NULL);
00059         strftime(time_buffer, 80, "    %a %b %d\n    %T %p %z\n    %Z\n", \
00060                  localtime(&ct_time));
00061         uLCD.printf("    UTC/GMT:\n%s", time_buffer);
00062         //lcd_mutex.unlock();
00063         Thread::wait(100);
00064     }
00065 }
00066 
00067 void parseWeather(char* buf)
00068 {
00069     char * pch;
00070     char * key;
00071     pch = strtok (buf,"{");
00072     while (pch != NULL) {
00073         pch = strtok (NULL,"{");
00074     }
00075     pch = strtok (buf,",");
00076     while (pch != NULL) {
00077         key = strtok (pch,":");
00078         if(strcmp(key, "\"text\"")) {
00079             uLCD.printf("Condition = %s",pch);
00080         } else if(strcmp(key, "\"temp\"")) {
00081             uLCD.printf("temp = %s",pch);
00082         }
00083     }
00084 }
00085 
00086 void getWeather()
00087 {
00088     //lcd_mutex.lock();
00089     /*** WEATHER****/
00090     char buf[500];
00091     uLCD.printf("Getting weather..\n");
00092     HTTPClient http;
00093     int retHttp = http.get(weth_domain_name, buf, sizeof(buf));
00094     uLCD.printf("%d", retHttp);
00095     switch(retHttp) {
00096         case HTTP_OK:
00097             uLCD.printf("Read completely\n");
00098             uLCD.printf("%c",buf);
00099             wait(2);
00100             uLCD.printf("%s",buf);
00101             wait(2);
00102             parseWeather(buf);
00103             break;
00104         case HTTP_TIMEOUT:
00105             uLCD.printf("Connection Timeout\n");
00106             break;
00107         case HTTP_CONN:
00108             uLCD.printf("Connection Error\n");
00109             break;
00110         default:
00111             uLCD.printf("Error\n");
00112     }
00113     //lcd_mutex.unlock();
00114 }
00115 
00116 void weth_thread(void const *args)
00117 {
00118 
00119     Thread::wait(3600000);
00120 }
00121 
00122 //pushbutton (p19)
00123 void snooze_hit_callback (void)
00124 {
00125     myled1 = !myled1;
00126     play = false;
00127     time_t newtime;
00128     struct tm * timeinfo;
00129     newtime = ct_time + snoozeTime;
00130 //time (&newtime);
00131     timeinfo = localtime (&newtime);
00132     curAlarmMin = timeinfo->tm_min;
00133     curAlarmHour = timeinfo->tm_hour;
00134 }
00135 
00136 void off_hit_callback (void)
00137 {
00138     myled2 = !myled2;
00139     play = false;
00140     curAlarmMin = baseAlarmMin;
00141     curAlarmHour = baseAlarmHour;
00142 }
00143 
00144 void play_file()
00145 {
00146     bool* play_point = &play;
00147     FILE *wave_file;
00148     printf("\n\n\nHello, wave world!\n");
00149     wave_file=fopen("/sd/bob.wav","r");
00150     waver.play(wave_file, play_point);
00151     fclose(wave_file);
00152 }
00153 
00154 void timeCompare()
00155 {
00156     struct tm * timeinfo;
00157     timeinfo = localtime (&ct_time);
00158     if (timeinfo->tm_min == curAlarmMin && timeinfo->tm_hour == curAlarmHour) {
00159         play = true;
00160         myled3 = true;
00161         play_file();
00162     }
00163 }
00164 
00165 int main()
00166 {
00167     snooze.mode(PullUp);
00168     off.mode(PullUp);
00169     wait(0.01);
00170     snooze.attach_deasserted(&snooze_hit_callback);
00171     off.attach_deasserted(&off_hit_callback);
00172     snooze.setSampleFrequency();
00173     off.setSampleFrequency();
00174 
00175     //play_file();
00176 
00177 
00178     // Initialize LCD
00179     uLCD.baudrate(115200);
00180     uLCD.background_color(BLACK);
00181     uLCD.cls();
00182 
00183     // Connect to network and wait for DHCP
00184     uLCD.locate(0,0);
00185     uLCD.printf("Getting IP Address\n");
00186     eth.init();
00187     if ( eth.connect(60000) == -1 ) {
00188         uLCD.printf("ERROR: Could not\nget IP address");
00189         return -1;
00190     }
00191     uLCD.printf("IP address is \n%s\n\n",eth.getIPAddress());
00192     Thread::wait(1000);
00193     getWeather();
00194     wait(500);
00195     // Read time from server
00196     uLCD.printf("Reading time...\n\r");
00197     ntp_client.setTime(time_domain_name, port_number);
00198     uLCD.printf("Time set\n");
00199     Thread::wait(2000);
00200     eth.disconnect();
00201 
00202     // Reset LCD
00203     uLCD.background_color(WHITE);
00204     uLCD.textbackground_color(WHITE);
00205     uLCD.color(RED);
00206     uLCD.cls();
00207     uLCD.text_height(2);
00208 
00209     //Thread t1(time_thread);
00210     //Thread t2(weth_thread);
00211     while(true) {
00212         timeCompare();
00213         Thread::wait(100);
00214     }
00215 
00216 }