22.5.09

RFID controlled weather feed

it works and it's so glorious.

The one thing is that it waits till the feed is brought in again before it tells you the weather feed, it isn't instantaneous. At the moment my feed comes in every five seconds which i thought was quite a lot. It would be nice if i could get it looping rather than just waiting. maybe loop(); will work. starting to think about how it's all going to look visually as well...


//weather for twelve cities!
// now waiting for button presses from lilypad
//trying to make it learn to shut up

// XML stuff
import simpleML.*;

// Serial stuff
// import the serial library:
import processing.serial.*;

Serial myPort; // the serial port you're using
String tagID = ""; // the string for the tag ID

XMLRequest aberdeen;
XMLRequest aberystwyth;
XMLRequest birmingham;
XMLRequest bristol;
XMLRequest dundee;
XMLRequest leeds;
XMLRequest liverpool;
XMLRequest london;
XMLRequest manchester;
XMLRequest newcastle;
XMLRequest oxford;
XMLRequest reading;



int startTime; //create timer
int counter = 0;


void setup() {
size(200,200);


// list all the serial ports:
println(Serial.list());

// based on the list of serial ports printed from the
// previous command, change the 0 to your port's number:
String portnum = Serial.list()[0];
// initialize the serial port:
myPort = new Serial(this, portnum, 9600);
// incoming string from reader will have 16 bytes:
myPort.buffer(16);

// create a font with the second font available to the system:
PFont myFont = createFont(PFont.list()[2], 24);
textFont(myFont);

//create and start up request

aberdeen = new XMLRequest(this,"http://weather.yahooapis.com/forecastrss?p=UKXX0001&u=c");
aberdeen.makeRequest();

aberystwyth = new XMLRequest(this,"http://weather.yahooapis.com/forecastrss?p=UKXX0203&u=c");
aberystwyth.makeRequest();

birmingham = new XMLRequest(this,"http://weather.yahooapis.com/forecastrss?p=UKXX0018&u=c");
birmingham.makeRequest();

bristol = new XMLRequest(this,"http://weather.yahooapis.com/forecastrss?p=UKXX0025&u=c");
bristol.makeRequest();

dundee = new XMLRequest(this,"http://weather.yahooapis.com/forecastrss?p=UKXX0045&u=c");
dundee.makeRequest();

leeds = new XMLRequest(this,"http://weather.yahooapis.com/forecastrss?p=UKXX0078&u=c");
leeds.makeRequest();

liverpool = new XMLRequest(this,"http://weather.yahooapis.com/forecastrss?p=UKXX0083&u=c");
liverpool.makeRequest();

london = new XMLRequest(this,"http://weather.yahooapis.com/forecastrss?p=UKXX0085&u=c");
london.makeRequest();

manchester = new XMLRequest(this,"http://weather.yahooapis.com/forecastrss?p=UKXX0092&u=c");
manchester.makeRequest();

newcastle = new XMLRequest(this,"http://weather.yahooapis.com/forecastrss?p=UKXX0098&u=c");
newcastle.makeRequest();

oxford = new XMLRequest(this,"http://weather.yahooapis.com/forecastrss?p=UKXX0106&u=c");
oxford.makeRequest();

reading = new XMLRequest(this,"http://weather.yahooapis.com/forecastrss?p=UKXX0117&u=c");
reading.makeRequest();
startTime = millis();//more timer stuff



}

void serialEvent(Serial myPort) {
// get the serial input buffer in a string:
String inputString = myPort.readString();
// filter out the tag ID from the string:
tagID = parseString(inputString);
}

String parseString(String thisString) {
String tagString = ""; // string to put the tag ID into

// first character of the input:
char firstChar = thisString.charAt(0);
// last character of the input:
char lastChar = thisString.charAt(thisString.length() -1);

// if the first char is STX (0x02) and the last char
// is ETX (0x03), then put the next ten bytes
// into the tag string:

if ((firstChar == 0x02) && (lastChar == 0x03)) {
tagString = thisString.substring(1, 11);



}
return tagString;
}

void draw() {
background(0);

//every 5 seconds, make new request
int now = millis();

if (now - startTime > 5000) {
aberdeen.makeRequest();
println("1");
}

if (now - startTime > 5000) {
aberystwyth.makeRequest();
println("2");
}

if (now - startTime > 5000) {
birmingham.makeRequest();
println("3");
}

if (now - startTime > 5000) {
bristol.makeRequest();
println("4");
}

if (now - startTime > 5000) {
dundee.makeRequest();
println("5");
}

if (now - startTime > 5000) {
leeds.makeRequest();
println("6");
}

if (now - startTime > 5000) {
liverpool.makeRequest();
println("7");
}

if (now - startTime > 5001) {
london.makeRequest();
println("8");
}

if (now - startTime > 5000) {
manchester.makeRequest();
println("9");
}

if (now - startTime > 5000) {
newcastle.makeRequest();
println("10");
}

if (now - startTime > 5000) {
oxford.makeRequest();
println("11");
}

if (now - startTime > 5000) {
reading.makeRequest();
println("12");
startTime = now;
}


}


//when request is complete
void netEvent(XMLRequest ml) {

String city = ml.getElementAttributeText("yweather:location", "city");

String temp = ml.getElementAttributeText("yweather:condition", "temp");


String weather = ml.getElementAttributeText("yweather:condition","text");



while(tagID.equals("2100764B24")) { // PARTLY CLOUDY --------------------------------------

if (weather.equals("Partly Cloudy")) {
println("it is partly cloudy in " + city);
break;
}

if (weather.equals("Partly Cloudy/Windy")) {
println("it is partly cloudy and a bit windy in " + city);
break;
}
else {
println("not a little bit cloudy " + city);
break;
}
}

while(tagID.equals("2100766B22")) { // CLOUDY --------------------------------------

if (weather.equals("Cloudy")) {
println("it's cloudy in " + city);
break;
}

if (weather.equals("Mostly Cloudy")) {
println("it's mostly cloudy in " + city);
break;
}

if (weather.equals("Mostly Cloudy/Windy")) {
println("it's mostly cloudy and a bit windy in " + city);
break;
}
else {
println("it's not cloudy in " + city);
break;
}
}

while(tagID.equals("210076572B")) { // LIGHT RAIN --------------------------------------

if (weather.equals("Light Rain")) {
println("it's raining lightly in " + city);

}

if (weather.equals("Light Rain/Wind")) {
println("it's raining lightly and a bit windy in " + city);
break;
}

if (weather.equals("Light Rain Shower")) {
println("there's a light rain shower in " + city);
break;
}

if (weather.equals("Showers in the Vicinity")) {
println("there's some rain showers near " + city);
break;
}
else {
println("it's not raining lightly in " + city);
break;
}

}

while(tagID.equals("21007643E3")) { // RAIN --------------------------------------

if (weather.equals("Rain")) {
println("it's raining in " + city);
break;
}

if (weather.equals("Rain/Wind")) {
println("it's raining and windy in " + city);
break;
}

if (weather.equals("Few Showers")) {
println("there's rain showers in " + city);
break;
}
else {
println("it's not raining in " + city);
break;
}

}
while(tagID.equals("21007683C0")) { // SUNNY --------------------------------------

if (weather.equals("Sunny")) {
println("it's all nice and sunny in " + city);
break;
}

if (weather.equals("Fair")) {
println("it's nice and fair in " + city);
break;
}

if (weather.equals("Moslty Sunny")) {
println("it's mostly sunny in " + city);
break;
}

else {
println("it's not sunny in " + city);
break;
}

}


}

No comments: