Karl Fischer / Mbed 2 deprecated Final_Lab_v1

Dependencies:   4DGL-uLCD-SE NixieTube MBed_Adafruit-GPS-Library NetServices wave_player mbed spxml

Committer:
fischk08
Date:
Thu Dec 15 23:06:56 2016 +0000
Revision:
4:9651e706b36f
Parent:
3:d62bb9ee0aa0
Updated for final lab presentation;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
fischk08 0:b35237c80489 1 // Notes https://developer.mbed.org/users/edodm85/notebook/HC-05-bluetooth/
fischk08 0:b35237c80489 2 // https://developer.mbed.org/cookbook/Bluetooth-Android-Controlled-MBED
fischk08 0:b35237c80489 3 // https://developer.mbed.org/users/mlee350/notebook/adafruit-ultimate-gps-breakout-board/
fischk08 0:b35237c80489 4 // https://developer.mbed.org/users/4180_1/notebook/ulcd-144-g2-128-by-128-color-lcd/
fischk08 0:b35237c80489 5 // https://developer.mbed.org/handbook/LocalFileSystem
fischk08 0:b35237c80489 6 // Includes Files
fischk08 0:b35237c80489 7 #include "mbed.h"
fischk08 0:b35237c80489 8 #include <string>
fischk08 0:b35237c80489 9 #include "uLCD_4DGL.h"
fischk08 0:b35237c80489 10 #include "wave_player.h"
fischk08 0:b35237c80489 11 #include "MBed_Adafruit_GPS.h" // GPS Unit
fischk08 0:b35237c80489 12 #include "spdomparser.hpp" /// Parseing Weather
fischk08 0:b35237c80489 13 #include "spxmlnode.hpp" /// Parseing Weather
fischk08 0:b35237c80489 14 #include "spxmlhandle.hpp" /// Parseing Weather
fischk08 0:b35237c80489 15 #include "EthernetNetIf.h"
fischk08 0:b35237c80489 16 #include "HTTPClient.h"
fischk08 0:b35237c80489 17 #include <string>
fischk08 2:714d63ea1eef 18 #include "NTPClient.h"
fischk08 3:d62bb9ee0aa0 19 #include "NixieTube.h"
fischk08 3:d62bb9ee0aa0 20 Serial pc(USBTX, USBRX);
fischk08 3:d62bb9ee0aa0 21 NixieTube Ntube(p24,p23,p22,p21);
fischk08 2:714d63ea1eef 22 /////////////////////////
fischk08 0:b35237c80489 23 EthernetNetIf eth;
fischk08 0:b35237c80489 24 HTTPClient http;
fischk08 0:b35237c80489 25 HTTPResult result;
fischk08 3:d62bb9ee0aa0 26 ////////////////////////
fischk08 3:d62bb9ee0aa0 27 NTPClient ntp; // Used for Clock
fischk08 3:d62bb9ee0aa0 28 ////////////////////////////////
fischk08 3:d62bb9ee0aa0 29 LocalFileSystem local("local"); // Create the local filesystem under the name "local"
fischk08 3:d62bb9ee0aa0 30 AnalogOut DACout(p18); // DAC for producing sound
fischk08 3:d62bb9ee0aa0 31 wave_player waver(&DACout); // wavplayer object
fischk08 3:d62bb9ee0aa0 32 Serial * gps_Serial; // GPS Interface
fischk08 3:d62bb9ee0aa0 33 uLCD_4DGL uLCD(p13,p14,p15); // serial tx, serial rx, reset pin;
fischk08 4:9651e706b36f 34 AnalogIn photocell(p19);
fischk08 4:9651e706b36f 35 Serial bt(p28, p27);
fischk08 4:9651e706b36f 36
fischk08 4:9651e706b36f 37 // Bluetooth
fischk08 4:9651e706b36f 38
fischk08 0:b35237c80489 39
fischk08 0:b35237c80489 40 //LCD Files Needs to be local to the ulcd screen
fischk08 0:b35237c80489 41
fischk08 0:b35237c80489 42 // Declarations
fischk08 3:d62bb9ee0aa0 43 bool completed = false; // Used for HTTP Request
fischk08 3:d62bb9ee0aa0 44 bool pressPlay = false; // Default on pause (song select mode)
fischk08 0:b35237c80489 45 int volume = 0;
fischk08 3:d62bb9ee0aa0 46 volatile float lattitude = 33.7490;
fischk08 3:d62bb9ee0aa0 47 volatile float longitude = -84.3880;
fischk08 2:714d63ea1eef 48 bool gps_setup();
fischk08 4:9651e706b36f 49 void display_screen_debug(),request_callback(),parseWeather(),system_setup(),display_weather(int),sound_alarm(float),ethernet_setup();
fischk08 2:714d63ea1eef 50
fischk08 2:714d63ea1eef 51 // Used for obtaining weather Information
fischk08 3:d62bb9ee0aa0 52 void request_callback(HTTPResult r){
fischk08 0:b35237c80489 53 result = r;
fischk08 0:b35237c80489 54 completed = true;
fischk08 0:b35237c80489 55 }
fischk08 0:b35237c80489 56
fischk08 3:d62bb9ee0aa0 57 void display_weather(int code){
fischk08 3:d62bb9ee0aa0 58 uLCD.flush_media();
fischk08 3:d62bb9ee0aa0 59 uLCD.cls();
fischk08 3:d62bb9ee0aa0 60 uLCD.media_init();
fischk08 3:d62bb9ee0aa0 61 uLCD.set_sector_address(0x0000,(code*65));
fischk08 3:d62bb9ee0aa0 62 uLCD.display_control(3);
fischk08 3:d62bb9ee0aa0 63 uLCD.display_image(0,0);
fischk08 3:d62bb9ee0aa0 64 }
fischk08 3:d62bb9ee0aa0 65
fischk08 3:d62bb9ee0aa0 66 void parseWeather(SP_XmlElementNode *node, string loc){
fischk08 0:b35237c80489 67 //extracts current weather XML data fields for LCD
fischk08 0:b35237c80489 68 SP_XmlHandle handle(node);
fischk08 0:b35237c80489 69 SP_XmlElementNode * condition = handle.getChild( "item" ).getChild("yweather:condition").toElement();
fischk08 0:b35237c80489 70 //display Weather conditions
fischk08 0:b35237c80489 71 // Print the name of the city
fischk08 3:d62bb9ee0aa0 72 pc.printf("%s:", loc);
fischk08 0:b35237c80489 73 //Print the weather conditions
fischk08 3:d62bb9ee0aa0 74 pc.printf("%s",condition->getAttrValue("text"));
fischk08 0:b35237c80489 75 //Print the termperature (in degrees Celcius)
fischk08 3:d62bb9ee0aa0 76 pc.printf("%sF",condition->getAttrValue("temp"));
fischk08 3:d62bb9ee0aa0 77 display_weather(atoi(condition->getAttrValue("code")));
fischk08 0:b35237c80489 78 }
fischk08 1:a6fbd8341289 79
fischk08 3:d62bb9ee0aa0 80 int weather(){
fischk08 3:d62bb9ee0aa0 81 retryweather:
fischk08 1:a6fbd8341289 82 completed = false;
fischk08 1:a6fbd8341289 83 SP_XmlDomParser parser;
fischk08 1:a6fbd8341289 84 HTTPStream stream;
fischk08 1:a6fbd8341289 85 char buffer3 [200];
fischk08 1:a6fbd8341289 86 wait(1);
fischk08 1:a6fbd8341289 87 pc.printf("%.6f:",lattitude);
fischk08 1:a6fbd8341289 88 pc.printf("%.6f:",longitude);
fischk08 1:a6fbd8341289 89 int n2;
fischk08 1:a6fbd8341289 90 char post[] = "https://query.yahooapis.com/v1/public/yql?q=select+item.condition+from+weather.forecast+where+woeid+in+(SELECT+woeid+FROM+geo.places+WHERE+text='(";
fischk08 1:a6fbd8341289 91 char msg[10] = ")')";
fischk08 1:a6fbd8341289 92 n2 = sprintf(buffer3, "%.6f,%.6f", lattitude, longitude);
fischk08 1:a6fbd8341289 93 char* temppostRequest = strcat(post,buffer3);
fischk08 1:a6fbd8341289 94 char* postRequest = strcat(temppostRequest,msg);
fischk08 1:a6fbd8341289 95 pc.printf(post);
fischk08 1:a6fbd8341289 96 pc.printf(msg);
fischk08 1:a6fbd8341289 97 pc.printf(temppostRequest);
fischk08 1:a6fbd8341289 98 pc.printf("Reading\r\n");
fischk08 1:a6fbd8341289 99 pc.printf(postRequest);
fischk08 1:a6fbd8341289 100 wait(1);
fischk08 1:a6fbd8341289 101 //n=sprintf(buffer, "https://query.yahooapis.com/v1/public/yql?q=select%20item.condition%20from%20weather.forecast%20where%20woeid%20in%20%28SELECT%20woeid%20FROM%20geo.places%20WHERE%20text%3D%22(%.6f%2C%.6f)%22)", lat, log);
fischk08 1:a6fbd8341289 102 char BigBuf[512 + 1] = {0};
fischk08 1:a6fbd8341289 103 wait(1);
fischk08 1:a6fbd8341289 104 stream.readNext((byte*)BigBuf, 512); //Point to buffer for the first read
fischk08 1:a6fbd8341289 105 //Yahoo! weather API for selected city - get XML document for parsing
fischk08 1:a6fbd8341289 106 //HTTPResult r = http.get("https://query.yahooapis.com/v1/public/yql?q=select+item.condition+from+weather.forecast+where+woeid+in+(SELECT+woeid+FROM+geo.places+WHERE+text='(33.7490,-84.3880)')", &stream, request_callback);
fischk08 1:a6fbd8341289 107 HTTPResult r = http.get(postRequest, &stream, request_callback); // This is wehre the http query for the local weather occurs
fischk08 1:a6fbd8341289 108 while (!completed) {
fischk08 1:a6fbd8341289 109 Net::poll(); //Polls the Networking stack
fischk08 1:a6fbd8341289 110 if (stream.readable()) {
fischk08 1:a6fbd8341289 111 BigBuf[stream.readLen()] = 0; //Transform this buffer in a zero-terminated char* string
fischk08 1:a6fbd8341289 112 parser.append( BigBuf, strlen(BigBuf)); // stream current buffer data to the XML parser
fischk08 1:a6fbd8341289 113 stream.readNext((byte*)BigBuf, 512); //Buffer has been read, now we can put more data in it
fischk08 1:a6fbd8341289 114 }
fischk08 1:a6fbd8341289 115 }
fischk08 1:a6fbd8341289 116 if (result == HTTP_OK) {
fischk08 1:a6fbd8341289 117 pc.printf("Weather complete");
fischk08 1:a6fbd8341289 118 } else {
fischk08 1:a6fbd8341289 119 pc. printf("Weather Error %d", result);
fischk08 3:d62bb9ee0aa0 120 goto retryweather;
fischk08 1:a6fbd8341289 121 return -1;
fischk08 1:a6fbd8341289 122 }
fischk08 1:a6fbd8341289 123
fischk08 1:a6fbd8341289 124 SP_XmlHandle rootHandle( parser.getDocument()->getRootElement() );
fischk08 1:a6fbd8341289 125 SP_XmlElementNode * child2 = rootHandle.getChild( "results" ).getChild( "channel" ).toElement();
fischk08 1:a6fbd8341289 126
fischk08 1:a6fbd8341289 127 //pc.printf(BigBuf);
fischk08 1:a6fbd8341289 128
fischk08 1:a6fbd8341289 129 if ( child2 ) {
fischk08 1:a6fbd8341289 130 parseWeather(child2, "Atlanta"); //parses XML "current-conditions" info
fischk08 1:a6fbd8341289 131 }
fischk08 1:a6fbd8341289 132
fischk08 1:a6fbd8341289 133 if ( NULL != parser.getError() ) {
fischk08 1:a6fbd8341289 134 pc.printf( "\n error: %s\n", parser.getError() );
fischk08 1:a6fbd8341289 135 }
fischk08 1:a6fbd8341289 136 return 0;
fischk08 1:a6fbd8341289 137 }
fischk08 2:714d63ea1eef 138 // Need to update so that if it failes to get a GPS fix after say 10 tries it defaults lat and longitude values and time is found thought another method.
fischk08 2:714d63ea1eef 139 bool gps_setup(){
fischk08 2:714d63ea1eef 140 Timer refresh_Timer; //sets up a timer for use in loop; how often do we print GPS info?
fischk08 2:714d63ea1eef 141 const int refresh_Time = 2000; //refresh time in ms
fischk08 2:714d63ea1eef 142 bool gps_off = true;
fischk08 2:714d63ea1eef 143 char c;
fischk08 2:714d63ea1eef 144 gps_Serial = new Serial(p9,p10); //serial object for use w/ GPS tx rx
fischk08 2:714d63ea1eef 145 Adafruit_GPS myGPS(gps_Serial); //object of Adafruit's GPS class
fischk08 2:714d63ea1eef 146 myGPS.begin(9600); //sets baud rate for GPS communication; note this may be changed via Adafruit_GPS::sendCommand(char *)
fischk08 2:714d63ea1eef 147 myGPS.sendCommand(PMTK_SET_NMEA_OUTPUT_RMCGGA); //these commands are defined in MBed_Adafruit_GPS.h; a link is provided there for command creation
fischk08 2:714d63ea1eef 148 myGPS.sendCommand(PMTK_SET_NMEA_UPDATE_1HZ);
fischk08 2:714d63ea1eef 149 myGPS.sendCommand(PGCMD_ANTENNA);
fischk08 2:714d63ea1eef 150 refresh_Timer.start(); //starts the clock on the timer
fischk08 3:d62bb9ee0aa0 151 uLCD.text_width(1); //4X size text
fischk08 3:d62bb9ee0aa0 152 uLCD.text_height(1);
fischk08 2:714d63ea1eef 153 uLCD.printf("Starting Up\n");
fischk08 2:714d63ea1eef 154 while(gps_off){
fischk08 2:714d63ea1eef 155 uLCD.cls();
fischk08 2:714d63ea1eef 156 c = myGPS.read(); //queries the GPS
fischk08 2:714d63ea1eef 157 if (c) { uLCD.printf("%c", c); } //this line will echo the GPS data if not paused
fischk08 2:714d63ea1eef 158 //check if we recieved a new message from GPS, if so, attempt to parse it,
fischk08 2:714d63ea1eef 159 if ( myGPS.newNMEAreceived() ) {
fischk08 2:714d63ea1eef 160 if ( !myGPS.parse(myGPS.lastNMEA()) ) {
fischk08 2:714d63ea1eef 161 continue;
fischk08 2:714d63ea1eef 162 }
fischk08 2:714d63ea1eef 163 }
fischk08 2:714d63ea1eef 164 //check if enough time has passed to warrant printing GPS info to screen
fischk08 2:714d63ea1eef 165 //note if refresh_Time is too low or pc.baud is too low, GPS data may be lost during printing
fischk08 2:714d63ea1eef 166 if (refresh_Timer.read_ms() >= refresh_Time) {
fischk08 2:714d63ea1eef 167 refresh_Timer.reset();
fischk08 2:714d63ea1eef 168 uLCD.printf("Fix: %d\n", (int) myGPS.fix);
fischk08 2:714d63ea1eef 169 uLCD.printf("Quality: %d\n", (int) myGPS.fixquality);
fischk08 2:714d63ea1eef 170 if (myGPS.fix) {
fischk08 2:714d63ea1eef 171 uLCD.printf("Time: %d:%d:%d.%u\n", myGPS.hour, myGPS.minute, myGPS.seconds, myGPS.milliseconds);
fischk08 2:714d63ea1eef 172 uLCD.printf("Date: %d/%d/20%d\n", myGPS.day, myGPS.month, myGPS.year);
fischk08 2:714d63ea1eef 173 uLCD.printf("Location: %5.2f%c, %5.2f%c\n", myGPS.latitude, myGPS.lat, myGPS.longitude, myGPS.lon);
fischk08 2:714d63ea1eef 174 uLCD.printf("Speed: %5.2f knots\n", myGPS.speed);
fischk08 2:714d63ea1eef 175 uLCD.printf("Angle: %5.2f\n", myGPS.angle);
fischk08 2:714d63ea1eef 176 uLCD.printf("Altitude: %5.2f\n", myGPS.altitude);
fischk08 2:714d63ea1eef 177 uLCD.printf("Satellites: %d\n", myGPS.satellites);
fischk08 2:714d63ea1eef 178 gps_off = false;
fischk08 3:d62bb9ee0aa0 179 lattitude = myGPS.latitude;
fischk08 3:d62bb9ee0aa0 180 longitude = myGPS.longitude;
fischk08 2:714d63ea1eef 181 }
fischk08 2:714d63ea1eef 182 }
fischk08 1:a6fbd8341289 183 }
fischk08 2:714d63ea1eef 184 return true;
fischk08 2:714d63ea1eef 185 }
fischk08 2:714d63ea1eef 186
fischk08 4:9651e706b36f 187 void sound_alarm() {
fischk08 2:714d63ea1eef 188 pressPlay = true;
fischk08 2:714d63ea1eef 189 FILE *wave_file = fopen("/local/alarm.wav","r");
fischk08 4:9651e706b36f 190 waver.play(wave_file); //play(FILE *wavefile,bool *pressPlay, int *volume);
fischk08 2:714d63ea1eef 191 pressPlay = false;
fischk08 4:9651e706b36f 192 wait(10);
fischk08 2:714d63ea1eef 193 fclose(wave_file);
fischk08 3:d62bb9ee0aa0 194 }
fischk08 3:d62bb9ee0aa0 195
fischk08 1:a6fbd8341289 196
fischk08 3:d62bb9ee0aa0 197 int main() {
fischk08 4:9651e706b36f 198 unsigned char rx;
fischk08 4:9651e706b36f 199 bt.baud(9600);
fischk08 4:9651e706b36f 200 char alarm[6] = "02:14";
fischk08 3:d62bb9ee0aa0 201 pc.baud(9600);
fischk08 3:d62bb9ee0aa0 202 pc.printf("\r\nintializing hardware,...v15\r\n");
fischk08 3:d62bb9ee0aa0 203 eth.init(); //Use DHCP
fischk08 4:9651e706b36f 204 int retry = 0;
fischk08 3:d62bb9ee0aa0 205 sendGETRequest:
fischk08 3:d62bb9ee0aa0 206 EthernetErr ethErr = eth.setup(60000);
fischk08 4:9651e706b36f 207
fischk08 3:d62bb9ee0aa0 208 if (ethErr) {
fischk08 3:d62bb9ee0aa0 209 pc.printf("Error in setup trying again %d",retry);
fischk08 3:d62bb9ee0aa0 210 retry++;
fischk08 3:d62bb9ee0aa0 211 goto sendGETRequest;
fischk08 2:714d63ea1eef 212 }
fischk08 3:d62bb9ee0aa0 213 pc.printf("net ok testing weather\r\n");
fischk08 3:d62bb9ee0aa0 214 weather();
fischk08 3:d62bb9ee0aa0 215 weather();
fischk08 3:d62bb9ee0aa0 216 time_t ctTime;
fischk08 3:d62bb9ee0aa0 217 ctTime = time(NULL);
fischk08 3:d62bb9ee0aa0 218 Host server(IpAddr(), 123, "0.us.pool.ntp.org");
fischk08 3:d62bb9ee0aa0 219 ntp.setTime(server);
fischk08 3:d62bb9ee0aa0 220 char hString[3];
fischk08 3:d62bb9ee0aa0 221 char mString[3];
fischk08 4:9651e706b36f 222 char alarmCompare[5];
fischk08 3:d62bb9ee0aa0 223 int offset = 5;
fischk08 3:d62bb9ee0aa0 224 int old_min2 = 0;
fischk08 3:d62bb9ee0aa0 225 int reset = 0;
fischk08 4:9651e706b36f 226 char ch;
fischk08 4:9651e706b36f 227
fischk08 3:d62bb9ee0aa0 228 while(1) {
fischk08 4:9651e706b36f 229 int x = 0;
fischk08 4:9651e706b36f 230 if(bt.readable())
fischk08 4:9651e706b36f 231 {
fischk08 4:9651e706b36f 232 //pc.printf("device readable");
fischk08 4:9651e706b36f 233 ch=bt.getc();
fischk08 4:9651e706b36f 234 //alarm[x] = ch;
fischk08 4:9651e706b36f 235 pc.printf("%c",ch);
fischk08 4:9651e706b36f 236 bt.printf("%c",ch);
fischk08 4:9651e706b36f 237 //x++
fischk08 4:9651e706b36f 238 }
fischk08 4:9651e706b36f 239
fischk08 4:9651e706b36f 240 //pc.printf("%f", photocell.read() );
fischk08 4:9651e706b36f 241 if (photocell < .3) {
fischk08 4:9651e706b36f 242
fischk08 4:9651e706b36f 243 Ntube.set_dim(0.2);
fischk08 4:9651e706b36f 244 } else {
fischk08 4:9651e706b36f 245 Ntube.set_dim(0.5);
fischk08 4:9651e706b36f 246 }
fischk08 4:9651e706b36f 247
fischk08 3:d62bb9ee0aa0 248 ctTime = time(NULL) - 3600 * offset;
fischk08 3:d62bb9ee0aa0 249 strftime(hString, 3, "%I", localtime(&ctTime));
fischk08 3:d62bb9ee0aa0 250 strftime(mString, 3, "%M", localtime(&ctTime));
fischk08 4:9651e706b36f 251
fischk08 4:9651e706b36f 252 strftime(alarmCompare, 6, "%I:%M", localtime(&ctTime));
fischk08 4:9651e706b36f 253
fischk08 4:9651e706b36f 254 if (strcmp(alarm, alarmCompare) == 0) {
fischk08 4:9651e706b36f 255 pc.printf("ALARM\r\n");
fischk08 4:9651e706b36f 256 sound_alarm();
fischk08 4:9651e706b36f 257 wait(10);
fischk08 4:9651e706b36f 258 }
fischk08 3:d62bb9ee0aa0 259
fischk08 3:d62bb9ee0aa0 260 int hour = std::atoi(hString);
fischk08 3:d62bb9ee0aa0 261 int min = std::atoi(mString);
fischk08 3:d62bb9ee0aa0 262
fischk08 3:d62bb9ee0aa0 263 int hour1 = hour / 10;
fischk08 3:d62bb9ee0aa0 264 int hour2 = hour % 10;
fischk08 3:d62bb9ee0aa0 265
fischk08 3:d62bb9ee0aa0 266 int min1 = min / 10;
fischk08 3:d62bb9ee0aa0 267 int min2 = min % 10;
fischk08 3:d62bb9ee0aa0 268
fischk08 3:d62bb9ee0aa0 269 if (min2!=old_min2) {
fischk08 3:d62bb9ee0aa0 270 old_min2=min2;
fischk08 3:d62bb9ee0aa0 271 pc. printf("\n %d, %d, %d, %d\r\n", hour1, hour2, min1, min2);
fischk08 3:d62bb9ee0aa0 272 Ntube.update_all_nixie_tube(hour1, hour2, min1, min2);
fischk08 3:d62bb9ee0aa0 273 reset++;
fischk08 3:d62bb9ee0aa0 274
fischk08 3:d62bb9ee0aa0 275 }
fischk08 3:d62bb9ee0aa0 276 if (reset>14){
fischk08 3:d62bb9ee0aa0 277 reset = 0;
fischk08 3:d62bb9ee0aa0 278 weather();
fischk08 3:d62bb9ee0aa0 279 weather();
fischk08 3:d62bb9ee0aa0 280 }
fischk08 4:9651e706b36f 281 wait(0.3);
fischk08 3:d62bb9ee0aa0 282 }
fischk08 3:d62bb9ee0aa0 283 }