#include /* * * Weather Clock * */ #include ; #include ; #include ; #include ; #define endl "\n"; // // Ethernet variables // byte arduinoMac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xEA }; byte arduinoIp[] = { 10, 0, 0, 4 }; byte serverIp[] = { 10, 0, 0, 2 }; Server server(80); Client fetchClient(serverIp, 80); // // Weather conditions // int8_t temperature = 99; int8_t windchill = 99; String currentCondition = String(30); String currentConditionLower = String(30); int8_t currentConditionImage = 0; String wind = String(15); int8_t humidity = 101; String forecastCondition = String(30); String forecastConditionLower = String(30); int8_t forecastConditionImage = 0; int8_t forecastHigh = 99; int8_t forecastLow = 99; int8_t forecastPOP = 0; int time = 0; int updateAfterTime = 700; int updateBeforeTime = 2200; boolean useWindchill = 0; Servo servoCondition; Servo servoTemperature; uint8_t servoConditionValue = 0; uint8_t servoTemperatureValue = 0; uint8_t oldServoConditionValue = 90; uint8_t oldServoTemperatureValue = 90; byte maxLength = 50; String inString = String(maxLength); int updatecount = 890; long previousMillis = 0; void setup() { // Start serial for debugging Serial.begin(57600); Serial.println("Up"); servoTemperature.attach(5); delay(1000); servoCondition.attach(6); servoCondition.write(90); servoTemperature.write(90); delay(1000); servoTemperature.detach(); servoCondition.detach(); // Delay until ethernet initalizes delay(5000); // Start ethernet Ethernet.begin(arduinoMac, arduinoIp); server.begin(); } void loop() { if (millis() - previousMillis > 1000) { previousMillis = millis(); // One second has elapsed, tick the clock. updatecount++; } if (updatecount >= 900) { fetchWeather(); } Client client = server.available(); if (client) { boolean current_line_is_blank = true; while (client.connected()) { if (client.available()) { char c = client.read(); if (inString.length() < maxLength) { inString.concat(c); } if (c == '\n' && current_line_is_blank) { if (inString.indexOf("?") != -1) { int Pos_a = inString.indexOf("a"); int Pos_b = inString.indexOf("b"); int Pos_c = inString.indexOf("c"); int End = inString.indexOf("H"); updateAfterTime = getInt(inString.substring((Pos_a+2), (Pos_b-1))); updateBeforeTime = getInt(inString.substring((Pos_b+2), (Pos_c-1))); useWindchill = getInt(inString.substring((Pos_c+2), (End-1))); updateClock(); } client << F("HTTP/1.1 200 OK") << endl; client << F("Content-Type: text/html") << endl; client << endl; client << F("Winnipeg Weather") << endl; client << F("

Winnipeg Weather

") << endl; client << F("

Current Conditions

") << endl; client << F("
") << endl; client << F("

"); client.print(currentCondition); client << F("

") << endl; client << F("Temperature: ") << temperature; if ((windchill != -1) && (windchill != 99) && (windchill != 0)) { client << F("° (") << windchill << F("° with wind)
") << endl; } else { client << "°
" << endl; } client << F("Wind: "); client.print(wind); client << F("
") << endl; client << F("Humidity: ") << humidity << F("%
") << endl; client << F("

Forecast

") << endl; client << F("
") << endl; client << F("

"); client.print(forecastCondition); client << F("

") << endl; client << F("Daytime high: ") << forecastHigh << F("°
") << endl; client << F("Nighttime low: ") << forecastLow << F("°
") << endl; client << F("Probability of precipitation: ") << forecastPOP << F("%
") << endl; client << F("

Settings

") << endl; client << F("
Only update the clock between and
"); client << F("Show windchill adjusted temperature Yes No

"); client << F("

Last updated: ") << time << F("

") << endl; client << F("") << endl; break; } if (c == '\n') { current_line_is_blank = true; } else if (c != '\r') { current_line_is_blank = false; } } } delay(50); // this may be why wireless isn't working - need to increase the timeout inString = ""; client.flush(); client.stop(); } } void fetchWeather() { if (fetchClient.connect()) { Serial << F("Reconnected"); fetchClient.println("GET http://www.seancarney.ca/backend/weather-clock/xml/canada-manitoba-winnipeg.php HTTP/1.0"); fetchClient.println(); delay(2000); updatecount = 0; windchill = 99; forecastHigh = 99; forecastLow = 99; forecastPOP = 0; while (fetchClient.available()) { char c = fetchClient.read(); if (inString.length() < maxLength) { inString.concat(c); } if (c == '\n') { Serial.println(inString); if (inString.startsWith("")) { temperature = getInt(inString.substring(13, inString.length()-15)); Serial.println(temperature); } if (inString.startsWith("")) windchill = getInt(inString.substring(11, inString.length()-13)); if (inString.startsWith("")) currentCondition = inString.substring(10, inString.length()-12); if (inString.startsWith("")) wind = inString.substring(6, inString.length()-8); if (inString.startsWith("")) humidity = getInt(inString.substring(10, inString.length()-12)); if (inString.startsWith("")) forecastCondition = inString.substring(10, inString.length()-12); if (inString.startsWith("")) forecastHigh = getInt(inString.substring(10, inString.length()-12)); if (inString.startsWith("")) forecastLow = getInt(inString.substring(9, inString.length()-11)); if (inString.startsWith("")) forecastPOP = getInt(inString.substring(5, inString.length()-7)); if (inString.startsWith("