/* WeatherBot 3000 2017 Displaying date, time, weather and special events like birthdays on an 8z8 led display. Using tab anim.h for all icons */ #include #include //#include #include #include #include "Adafruit_LEDBackpack.h" #include "anim.h" #include RTCZero rtc; Adafruit_8x8matrix matrix = Adafruit_8x8matrix(); #define RBUFFSIZE 450 char responseBuffer[RBUFFSIZE] = {'1'}; char ssid[] = “Network Name”; // your network SSID (name) char pass[] = “Password”; // your network password int keyIndex = 0; // your network key Index number (needed only for WEP) char *az = "1"; char *ahval = "1"; char *azval = "1"; char *mainDescrip = "1"; int status = WL_IDLE_STATUS; int calls = 0; int wCalls = 0; int rbindex = 0; int firstTimeCounter = 0; const int GMT = -5; //change this to adapt it to your time zone //char *currentHumidity = 0; //char *currentTemp = 0; char* icon[] = {"Sunny", "Cloudy", "Rainy", "Stormy", "Night", "Hazy"}; char* titleMarquee[] = {"WeatherBot 3000"}; int ic = 0; byte marker = 0; byte weatherIcon = 0; byte dateCounter = 0; char *prevTemp = "32"; char *prevHumid = "32"; byte badBuffer = 0; /// change dates here if needed byte bMonth = 9; // Birthday byte bDay = 3; // byte aMonth = 9; // Anniversay byte aDay = 2; byte cMonth = 12; // Holiday byte cDay = 25; byte vMonth = 2; // Valentines Day byte vDay = 14; // Initialize the WiFi client library WiFiClient client; // server address: char server[] = "api.openweathermap.org"; //IPAddress server(64,131,82,241); unsigned long lastConnectionTime = 0; // last time you connected to the server, in milliseconds unsigned long postingInterval = 600000l; // delay between updates, in milliseconds void setup() { matrix.begin(0x70); // pass in the address matrix.setBrightness(3); // check for the presence of the shield: if (WiFi.status() == WL_NO_SHIELD) { // don't continue: while (true); } // attempt to connect to WiFi network: while ( status != WL_CONNECTED) { // Connect to WPA/WPA2 network. Change this line if using open or WEP network: status = WiFi.begin(ssid, pass); title(); } // you're connected now, so print out the status: rtc.begin(); unsigned long epoch; int numberOfTries = 0, maxTries = 6; do { epoch = WiFi.getTime(); numberOfTries++; } while ((epoch == 0) || (numberOfTries > maxTries)); if (numberOfTries > maxTries) { while (1); } else { rtc.setEpoch(epoch); client.stop(); Serial.println(); } } void loop() { dateCounter++; if (dateCounter >= 2) { displayDate(); dateCounter = 0; } displayTime(); displayWIcons(); displayWeather(); specialEvents(); while (client.available()) { char c = client.read(); if (c == '[') marker = 1; if (marker == 1) { if (rbindex < RBUFFSIZE) { responseBuffer[rbindex] = c; rbindex++; } } } // if enough time has passed since your last connection, // then connect again and send data: if (millis() - lastConnectionTime > postingInterval || firstTimeCounter <= 2) { firstTimeCounter++; httpRequest(); getWeather(); marker = 0; } } // this method makes a HTTP connection to the server: void httpRequest() { calls++; client.stop(); rbindex = 0; // if there's a successful connection: if (client.connect(server, 80)) { // send the HTTP PUT request: client.println("GET /data/2.5/weather?id=4487042,us&units=imperial&APPID=05907f1eaa33a09802c4e35c8f3c549a HTTP/1.1"); client.println("Host: api.openweathermap.org.com"); client.println("Connection: close"); client.println(); // note the time that the connection was made: lastConnectionTime = millis(); } else { // if you couldn't make a connection: } } void getWeather() { // attempt to get "main" before temp wCalls++; // sometimes cannot connect to server..use previous values if (strlen(responseBuffer) <= 50) { badBuffer = 1; } else badBuffer = 0; /* if(strlen(responseBuffer) >= 51){ prevTemp = azval; prevHumid = ahval; }*/ // Main weather IE: clear, cloudy etc.. az = strstr(responseBuffer, "main"); az += strlen("main':'"); mainDescrip = strtok(az, "\""); // Get temp azval = strtok(NULL, "]"); azval = strtok(NULL, "{"); azval = strtok(NULL, ":"); azval = strtok(NULL, ","); if (badBuffer == 1 || strcmp("0", azval) == 0 || strcmp("", azval) == 0 || strcmp(" ", azval) == 0) azval = prevTemp; if (badBuffer == 0) prevTemp = azval; // get humidity ahval = strtok(NULL, ":"); ahval = strtok(NULL, ":"); ahval = strtok(NULL, ","); if (badBuffer == 1 || strcmp("0", ahval) == 0 || strcmp("", ahval) == 0 || strcmp(" ", ahval) == 0) ahval = prevHumid; if (badBuffer == 0) prevHumid = ahval; if (rtc.getHours() >= 1 && rtc.getHours() <= 10) { weatherIcon = 4; } else if (strcmp("Clear", mainDescrip) == 0) { //Sunny weatherIcon = 0; } else if (strcmp("Sunny", mainDescrip) == 0) { weatherIcon = 0; } else if (strcmp("Haze", mainDescrip) == 0) { weatherIcon = 5; } else if (strcmp("Clouds", mainDescrip) == 0) { weatherIcon = 1; } else if (strcmp("Cloudy", mainDescrip) == 0) { weatherIcon = 1; } else if (strcmp("Rain", mainDescrip) == 0) { weatherIcon = 2; } else if (strcmp("Stormy", mainDescrip) == 0) { weatherIcon = 3; } else if (strcmp("Storm", mainDescrip) == 0) { weatherIcon = 3; } else {; weatherIcon = 1; } int h = atoi(ahval); } void displayWeather() { matrix.setTextSize(1); matrix.setTextWrap(false); // we dont want text to wrap so it scrolls nicely matrix.setTextColor(LED_ON); for (int8_t x = 5; x >= -30; x--) { matrix.clear(); matrix.setCursor(x, 0); matrix.print(atoi(azval)); matrix.drawBitmap(x + 9, 0, degrees_bmp, 8, 8, LED_ON); matrix.print(" F"); matrix.writeDisplay(); delay(100); } matrix.setTextSize(1); matrix.setTextWrap(false); // we dont want text to wrap so it scrolls nicely matrix.setTextColor(LED_ON); for (int8_t x = 0; x >= -50; x--) { matrix.clear(); matrix.setCursor(x, 0); matrix.print(F(" ")); matrix.print(atoi(ahval)); matrix.print(F("% H")); matrix.writeDisplay(); delay(100); } } // special day functions below void happyBirthday() { matrix.setTextSize(1); matrix.setTextWrap(false); // we dont want text to wrap so it scrolls nicely matrix.setTextColor(LED_ON); for (int8_t x = 5; x >= -98; x--) { matrix.clear(); matrix.setCursor(x, 0); matrix.print(F("Happy Birthday!")); matrix.drawBitmap(x + 89, 0, cake_bmp, 8, 8, LED_ON); matrix.writeDisplay(); delay(100); } } void happyAnniversary() { matrix.setTextSize(1); matrix.setTextWrap(false); // we dont want text to wrap so it scrolls nicely matrix.setTextColor(LED_ON); for (int8_t x = 5; x >= -106; x--) { matrix.clear(); matrix.setCursor(x, 0); matrix.print(F("Happy Anniversary!")); matrix.writeDisplay(); delay(100); } } void merryChristmas() { matrix.setTextSize(1); matrix.setTextWrap(false); // we dont want text to wrap so it scrolls nicely matrix.setTextColor(LED_ON); for (int8_t x = 5; x >= -105; x--) { matrix.clear(); matrix.setCursor(x, 0); matrix.print(F("Merry Christmas")); matrix.drawBitmap(x + 91, 0, tree_bmp, 8, 8, LED_ON); matrix.writeDisplay(); delay(100); } } void valentines() { //hearts for valentines day matrix.setTextSize(1); matrix.setTextWrap(false); // we dont want text to wrap so it scrolls nicely matrix.setTextColor(LED_ON); for (int8_t x = 5; x >= -115; x--) { matrix.clear(); matrix.setCursor(x, 0); matrix.print(F("Happy Valentines Day")); matrix.writeDisplay(); delay(100); } matrix.clear(); matrix.drawBitmap(0, 0, heartSmall_bmp, 8, 8, LED_ON); matrix.writeDisplay(); delay(400); matrix.clear(); matrix.drawBitmap(0, 0, heart_bmp, 8, 8, LED_ON); matrix.writeDisplay(); delay(400); matrix.clear(); matrix.drawBitmap(0, 0, heartSmall_bmp, 8, 8, LED_ON); matrix.writeDisplay(); delay(400); matrix.clear(); matrix.drawBitmap(0, 0, heart_bmp, 8, 8, LED_ON); matrix.writeDisplay(); delay(400); matrix.clear(); matrix.drawBitmap(0, 0, heartSmall_bmp, 8, 8, LED_ON); matrix.writeDisplay(); delay(400); matrix.clear(); matrix.drawBitmap(0, 0, heart_bmp, 8, 8, LED_ON); matrix.writeDisplay(); delay(400); } void displayDate() { matrix.setTextSize(1); matrix.setTextWrap(false); // we dont want text to wrap so it scrolls nicely matrix.setTextColor(LED_ON); for (int8_t x = 5; x >= -30; x--) { matrix.clear(); matrix.setCursor(x, 0); matrix.print(rtc.getMonth()); matrix.print("/"); if (rtc.getHours() >= 0 && rtc.getHours() <= abs(GMT) - 1) { //corecting day to compensate for GMT matrix.print(rtc.getDay() - 1); } else matrix.print(rtc.getDay()); matrix.writeDisplay(); delay(100); } } void displayTime() { matrix.setTextSize(1); matrix.setTextWrap(false); // we dont want text to wrap so it scrolls nicely matrix.setTextColor(LED_ON); int hour = rtc.getHours() + GMT; for (int8_t x = 5; x >= -30; x--) { matrix.clear(); matrix.setCursor(x, 0); if (rtc.getHours() == 0) { hour = 12 + GMT; } if (rtc.getHours() == abs(GMT)) { hour = 12; } if (rtc.getHours() >= 1 && rtc.getHours() <= abs(GMT) - 1) hour = hour + 12; if (hour >= 13) // 12 hour clock not 24 hour = hour - 12; matrix.print(hour); matrix.print(":"); if (rtc.getMinutes() <= 9) { matrix.print(F("0")); } matrix.print(rtc.getMinutes()); matrix.writeDisplay(); delay(100); } } void displayWIcons() { for (int8_t x = 5; x >= -20; x--) { matrix.clear(); if (weatherIcon == 0) { matrix.drawBitmap(x, 0, sun_bmp, 8, 8, LED_ON); } else if (weatherIcon == 1) { matrix.drawBitmap(x, 0, cloud_bmp, 8, 8, LED_ON); } else if (weatherIcon == 2) { matrix.drawBitmap(x, 0, rain_bmp, 8, 8, LED_ON); } else if (weatherIcon == 3) { matrix.drawBitmap(x, 0, storm_bmp, 8, 8, LED_ON); } else if (weatherIcon == 4) { matrix.drawBitmap(x, 0, moon_bmp, 8, 8, LED_ON); } matrix.writeDisplay(); delay(100); if ( x == 0) { delay(100); matrix.setBrightness(5); delay(100); matrix.setBrightness(2); delay(100); matrix.setBrightness(5); delay(100); matrix.setBrightness(2); delay(100); matrix.setBrightness(5); delay(100); matrix.setBrightness(2); delay(100); } } } void specialEvents() { int day = 0; if (rtc.getHours() >= 0 && rtc.getHours() <= abs(GMT) - 1) { //corecting day to compensate for GMT day = rtc.getDay()-1; } else day = rtc.getDay(); if (rtc.getMonth() == vMonth && day == vDay) { valentines(); } if (rtc.getMonth() == bMonth && day == bDay) { happyBirthday(); } if (rtc.getMonth() == aMonth && day == aDay) { happyAnniversary(); } if (rtc.getMonth() == cMonth && day == cDay) { merryChristmas(); } } void title() { matrix.setTextSize(1); matrix.setTextWrap(false); matrix.setTextColor(LED_ON); for (int8_t x = 12; x >= -90; x--) { matrix.clear(); matrix.setCursor(x, 0); matrix.print(titleMarquee[0]); matrix.writeDisplay(); delay(100); } matrix.clear(); matrix.drawBitmap(0, 0, wifi1_bmp, 8, 8, LED_ON); matrix.writeDisplay(); delay(300); matrix.clear(); matrix.drawBitmap(0, 0, wifi2_bmp, 8, 8, LED_ON); matrix.writeDisplay(); delay(300); matrix.clear(); matrix.drawBitmap(0, 0, wifi3_bmp, 8, 8, LED_ON); matrix.writeDisplay(); delay(300); matrix.clear(); matrix.drawBitmap(0, 0, wifi_bmp, 8, 8, LED_ON); matrix.writeDisplay(); delay(300); matrix.clear(); matrix.drawBitmap(0, 0, wifi1_bmp, 8, 8, LED_ON); matrix.writeDisplay(); delay(300); matrix.clear(); matrix.drawBitmap(0, 0, wifi2_bmp, 8, 8, LED_ON); matrix.writeDisplay(); delay(300); matrix.clear(); matrix.drawBitmap(0, 0, wifi3_bmp, 8, 8, LED_ON); matrix.writeDisplay(); delay(300); matrix.clear(); matrix.drawBitmap(0, 0, wifi_bmp, 8, 8, LED_ON); matrix.writeDisplay(); delay(500); }