/** * Gets temperature data from Yahoo RSS feed and displays it in a meter * as well as numerically. * * Copyright (C) 2012-2013 Mithat Konar */ // Constants: // change the URL to get the desired location and units // (see http://developer.yahoo.com/weather/). final String URL = "http://weather.yahooapis.com/forecastrss?w=12781882&u=f"; // path in XML data to current conditions: final String ELEMENT_CONDITION="channel/item/yweather:condition"; // attribute name in element above for current temperature: final String ATTRIBUTE_CONDITION_TEMPERATURE = "temp"; // path in XML data to element that holds units of measurement: final String ELEMENT_UNITS = "channel/yweather:units"; // attribute name in element above that holds units for temperature: final String ATTRIBUTE_UNIT_TEMPERATURE = "temperature"; // misc. final int BORDER=10; // Functions: void setup() { size(200, 100); smooth(); try { XML weather = loadXML(URL); // instantiate an XML object from URL XML condition = weather.getChild(ELEMENT_CONDITION); // pull current cond. XML units = weather.getChild(ELEMENT_UNITS); // pull units data background(#296783); int temperature = condition.getInt(ATTRIBUTE_CONDITION_TEMPERATURE); drawDigitalTemp(temperature, units.getString(ATTRIBUTE_UNIT_TEMPERATURE), width/2, height/2); drawMeter(temperature, -40, 120, 10, height-2*BORDER, width-10-BORDER, BORDER); } catch (Exception e) { println("Problem loading " + URL); println(e); // Turn the canvas red with a big 'X' in it to indicate error. background(#ff0000); strokeWeight(5); line(BORDER, BORDER, width-BORDER, height-BORDER); line(BORDER, height-BORDER, width-BORDER, BORDER); } println("Exiting app."); } /** * Render a temperature numerically at a given canvas location. * * @param temperature the temperature to be drawn (int) * @param units the units of measure used for temperatre (String) * @param x the horizontal location of the display (int) * @param y the vertical location of the display (int) * * @return void */ void drawDigitalTemp(int temperature, String units, int x, int y) { PFont font; font = loadFont("Monospaced.bold-48.vlw"); textFont(font, 48); textAlign(CENTER, CENTER); fill(#ffffff); text(temperature + "\u00b0" + units, x, y); } /** * Render a value on a vertical meter. * * @param val the value to be drawn (float) * @param minimum val's value that corresponds to no deflection (float) * @param minimum val's value that corresponds to maximum deflection (float) * @param w the width of the meter (int) * @param h the height of the meter (int) * @param x the horizontal location of the meter (int) * @param y the vertical location of the meter (int) * * @return void */ void drawMeter(float val, float minimum, float maximum, int w, int h, int x, int y) { // calculate height of indicator in pixels float barHeight=map(val, minimum, maximum, 0, h); // draw the indicator strokeWeight(1); stroke(#ff0000); fill(#e00000); rect(x, y+(h-barHeight), w, barHeight); // draw the frame noFill(); stroke(#1F4D62); rect(x, y, w, h); }