|
| 1 | +/* |
| 2 | + Yahoo Weather Forecast parser |
| 3 | + |
| 4 | + http://developer.yahoo.com/weather/ |
| 5 | + This sketch demonstrate how to use the Linux command line tools |
| 6 | + to parse a simple XML file on the Arduino Yún. |
| 7 | + |
| 8 | + First thing download the XML file from the Yahoo Weather service |
| 9 | + than use "grep" and "cut" to extract the data you want. |
| 10 | + |
| 11 | + To find the location ID of your location, browse or search for your |
| 12 | + city from the Weather home page. The location ID is in the URL for |
| 13 | + the forecast page for that city. |
| 14 | + |
| 15 | + created 21 Jun 2013 |
| 16 | + by Federico Vanzati |
| 17 | + |
| 18 | + */ |
| 19 | + |
| 20 | +#include <Bridge.h> |
| 21 | + |
| 22 | +String locationID = "725003"; // Turin, Italy |
| 23 | + |
| 24 | +// table with keywords to search in the XML file |
| 25 | +// the third column is the tag to the field |
| 26 | +String forecast[10][3] = { |
| 27 | + "location", "2", "city", |
| 28 | + "condition", "6", "temperature", |
| 29 | + "condition", "2", "condition", |
| 30 | + "astronomy", "2", "sunrise", |
| 31 | + "astronomy", "4", "sunset", |
| 32 | + "atmosphere", "2", "humidity", |
| 33 | + "atmosphere", "6", "pressure", |
| 34 | + "wind", "6", "wind speed", |
| 35 | + "wind", "4", "wind direction", |
| 36 | + "wind", "2", "chill temperature" |
| 37 | +}; |
| 38 | + |
| 39 | + |
| 40 | +void setup() { |
| 41 | + Bridge.begin(); |
| 42 | + Serial.begin(9600); |
| 43 | + while(!Serial); |
| 44 | + |
| 45 | + Serial.println("Weather Forecast for your location: \n"); |
| 46 | +} |
| 47 | + |
| 48 | +void loop() { |
| 49 | + |
| 50 | + for(int i=0; i<10; i++) { |
| 51 | + |
| 52 | + // Compose the request |
| 53 | + |
| 54 | + // curl is a program that connect to an URL an download the content |
| 55 | + // is used to get the weather forecast from yahoo in XML format |
| 56 | + String command = "curl -s "; // -s is the silent option |
| 57 | + command += "http://weather.yahooapis.com/forecastrss"; // yahoo weather RSS service |
| 58 | + command += "?w="; // query for the location |
| 59 | + command += locationID; |
| 60 | + //command += "\\&u=c"; // ask for celsius degrees |
| 61 | + |
| 62 | + // add a new process |
| 63 | + // grep is used to extract a single line of content containig a search keyword form the XML |
| 64 | + command += " | "; // pipe a new process |
| 65 | + command += "grep "; |
| 66 | + command += forecast[i][0]; // word to search in the XML file |
| 67 | + |
| 68 | + // add a new process |
| 69 | + // cut is a program that split a text in different fields |
| 70 | + // when encouter the passed character delimiter |
| 71 | + command += " | "; // pipe a new process |
| 72 | + command += "cut "; |
| 73 | + command += "-d \\\" "; // -d parameter split the string every " char |
| 74 | + command += "-f "; // -f parameter is to return the 6th splitted element |
| 75 | + command += forecast[i][1]; // the field are already manually calculated and inserted in the forecast table |
| 76 | + |
| 77 | + |
| 78 | + Serial.print(forecast[i][2]); |
| 79 | + Serial.print("= "); |
| 80 | + |
| 81 | + // run the command |
| 82 | + Process wf; |
| 83 | + wf.runShellCommand(command); |
| 84 | + |
| 85 | + while(wf.available()>0) |
| 86 | + { |
| 87 | + Serial.print( (char)wf.read() ); |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + //do nothing forevermore |
| 92 | + while(1); |
| 93 | +} |
| 94 | + |
0 commit comments