Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ RemoteSystemsTempFiles
.vscode
.clang_complete
.gcc-flags.json
*.bin
Original file line number Diff line number Diff line change
@@ -0,0 +1,351 @@
/**The MIT License (MIT)

Copyright (c) 2018 by Daniel Eichhorn - ThingPulse

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

See more at https://thingpulse.com
*/

#include <Arduino.h>

#if defined(ESP8266)
#include <ESP8266WiFi.h>
#include <coredecls.h> // settimeofday_cb()
#else
#include <WiFi.h>
#endif
#include <ESPHTTPClient.h>
#include <JsonListener.h>

// time
#include <time.h> // time() ctime()
#include <sys/time.h> // struct timeval

#include "SSD1306Wire.h"
#include "OLEDDisplayUi.h"
#include "Wire.h"
#include "VisualCrossing.h"
#include "WeatherStationFonts.h"
#include "WeatherStationImages.h"


/***************************
* Begin Settings
**************************/

// WIFI
const char* WIFI_SSID = "yourssid";
const char* WIFI_PWD = "yourpassw0rd";

// Timezone
// Enter a POSIX timezone string. A current list can be found here:
// https://github.com/nayarsystems/posix_tz_db/blob/master/zones.csv
#define TZ "CET-1CEST,M3.5.0,M10.5.0/3"

// Setup
const int UPDATE_INTERVAL_SECS = 20 * 60; // Update every 20 minutes

// Display Settings
const int I2C_DISPLAY_ADDRESS = 0x3c;
#if defined(ESP8266)
const int SDA_PIN = D3;
const int SDC_PIN = D4;
#else
const int SDA_PIN = 5; //D3;
const int SDC_PIN = 4; //D4;
#endif


// VisualCrossing Settings
// Sign up here to get an API key:
// https://www.visualcrossing.com/sign-up
String VISUAL_CROSSING_KEY = "XXX";

// Enter a plaintext location.
// Go to https://www.visualcrossing.com/weather-data to test
String VISUAL_CROSSING_LOCATION = "Zurich,Switzerland";

// Pick a language code from this list:
// ar (Arabic), bg (Bulgiarian), cs (Czech), da (Danish), de (German),el (Greek Modern),
// en (English), es (Spanish), fa (Farsi), fi (Finnish), fr (French), he (Hebrew),
// hu, (Hungarian), it (Italian), ja (Japanese), ko (Korean), nl (Dutch), pl (Polish),
// pt (Portuguese), ru (Russian), sk (Slovakian), sr (Serbian), sv (Swedish),
// tr (Turkish), uk (Ukranian), vi (Vietnamese) and zh (Chinese).
String VISUAL_CROSSING_LANGUAGE = "de";
const uint8_t MAX_FORECASTS = 4;

const boolean IS_METRIC = true;
const boolean IS_24HOUR = true;
enum dateOrders {DMY, MDY, YMD};
const dateOrders dateOrder = DMY;

// Adjust according to your language
const String WDAY_NAMES[] = {"SUN", "MON", "TUE", "WED", "THU", "FRI", "SAT"};
const String MONTH_NAMES[] = {"JAN", "FEB", "MAR", "APR", "MAY", "JUN", "JUL", "AUG", "SEP", "OCT", "NOV", "DEC"};

/***************************
* End Settings
**************************/
// Initialize the oled display for address 0x3c
// sda-pin=14 and sdc-pin=12
SSD1306Wire display(I2C_DISPLAY_ADDRESS, SDA_PIN, SDC_PIN);
OLEDDisplayUi ui( &display );

VisualCrossingData currentWeather;
VisualCrossingData forecasts[MAX_FORECASTS];
VisualCrossing forecastClient;

time_t now;

// flag changed in the ticker function every 10 minutes
bool readyForWeatherUpdate = false;

String lastUpdate = "--";

long timeSinceLastWUpdate = 0;

//declaring prototypes
void drawProgress(OLEDDisplay *display, int percentage, String label);
void updateData(OLEDDisplay *display);
void drawDateTime(OLEDDisplay *display, OLEDDisplayUiState* state, int16_t x, int16_t y);
void drawCurrentWeather(OLEDDisplay *display, OLEDDisplayUiState* state, int16_t x, int16_t y);
void drawForecast(OLEDDisplay *display, OLEDDisplayUiState* state, int16_t x, int16_t y);
void drawForecastDetails(OLEDDisplay *display, int x, int y, int dayIndex);
void drawHeaderOverlay(OLEDDisplay *display, OLEDDisplayUiState* state);
void setReadyForWeatherUpdate();


// Add frames
// this array keeps function pointers to all frames
// frames are the single views that slide from right to left
FrameCallback frames[] = { drawDateTime, drawCurrentWeather, drawForecast };
int numberOfFrames = 3;

OverlayCallback overlays[] = { drawHeaderOverlay };
int numberOfOverlays = 1;

void setup() {
Serial.begin(115200);
Serial.println();
Serial.println();

// initialize dispaly
display.init();
display.clear();
display.display();

//display.flipScreenVertically();
display.setFont(ArialMT_Plain_10);
display.setTextAlignment(TEXT_ALIGN_CENTER);
display.setContrast(255);

WiFi.begin(WIFI_SSID, WIFI_PWD);

int counter = 0;
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
display.clear();
display.drawString(64, 10, "Connecting to WiFi");
display.drawXbm(46, 30, 8, 8, counter % 3 == 0 ? activeSymbole : inactiveSymbole);
display.drawXbm(60, 30, 8, 8, counter % 3 == 1 ? activeSymbole : inactiveSymbole);
display.drawXbm(74, 30, 8, 8, counter % 3 == 2 ? activeSymbole : inactiveSymbole);
display.display();

counter++;
}
// Get time from network time service
configTzTime(TZ, "pool.ntp.org");

ui.setTargetFPS(30);

ui.setActiveSymbol(activeSymbole);
ui.setInactiveSymbol(inactiveSymbole);

// You can change this to
// TOP, LEFT, BOTTOM, RIGHT
ui.setIndicatorPosition(BOTTOM);

// Defines where the first frame is located in the bar.
ui.setIndicatorDirection(LEFT_RIGHT);

// You can change the transition that is used
// SLIDE_LEFT, SLIDE_RIGHT, SLIDE_TOP, SLIDE_DOWN
ui.setFrameAnimation(SLIDE_LEFT);

ui.setFrames(frames, numberOfFrames);

ui.setOverlays(overlays, numberOfOverlays);

// Inital UI takes care of initalising the display too.
ui.init();

Serial.println("");

updateData(&display);
}

void loop() {

if (millis() - timeSinceLastWUpdate > (1000L*UPDATE_INTERVAL_SECS)) {
setReadyForWeatherUpdate();
timeSinceLastWUpdate = millis();
}

if (readyForWeatherUpdate && ui.getUiState()->frameState == FIXED) {
updateData(&display);
}

int remainingTimeBudget = ui.update();

if (remainingTimeBudget > 0) {
// You can do some work here
// Don't do stuff if you are below your
// time budget.
delay(remainingTimeBudget);
}
}

void drawProgress(OLEDDisplay *display, int percentage, String label) {
display->clear();
display->setTextAlignment(TEXT_ALIGN_CENTER);
display->setFont(ArialMT_Plain_10);
display->drawString(64, 10, label);
display->drawProgressBar(2, 28, 124, 10, percentage);
display->display();
}

void updateData(OLEDDisplay *display) {
drawProgress(display, 10, "Updating time...");
drawProgress(display, 30, "Updating weather...");
drawProgress(display, 50, "Updating forecasts...");
forecastClient.setMetric(IS_METRIC);
forecastClient.setLanguage(VISUAL_CROSSING_LANGUAGE);
forecastClient.updateForecasts(forecasts, currentWeather, VISUAL_CROSSING_KEY, VISUAL_CROSSING_LOCATION, MAX_FORECASTS);

readyForWeatherUpdate = false;
drawProgress(display, 100, "Done...");
delay(1000);
}

void drawDateTime(OLEDDisplay *display, OLEDDisplayUiState* state, int16_t x, int16_t y) {
now = time(nullptr);
struct tm* timeInfo;
timeInfo = localtime(&now);
char buff[16];

display->setTextAlignment(TEXT_ALIGN_CENTER);
display->setFont(ArialMT_Plain_10);
String date = WDAY_NAMES[timeInfo->tm_wday];

if(dateOrder == DMY) sprintf_P(buff, PSTR("%s, %02d/%02d/%04d"), WDAY_NAMES[timeInfo->tm_wday].c_str(), timeInfo->tm_mday, timeInfo->tm_mon+1, timeInfo->tm_year + 1900);
else if (dateOrder == MDY) sprintf_P(buff, PSTR("%s, %d/%d/%04d"), WDAY_NAMES[timeInfo->tm_wday].c_str(), timeInfo->tm_mon+1, timeInfo->tm_mday, timeInfo->tm_year + 1900);
else if (dateOrder == YMD) sprintf_P(buff, PSTR("%s, %04d/%02d/%02d"), WDAY_NAMES[timeInfo->tm_wday].c_str(), timeInfo->tm_year + 1900, timeInfo->tm_mday, timeInfo->tm_mon+1);
display->drawString(64 + x, 5 + y, String(buff));
display->setFont(ArialMT_Plain_24);

if(IS_24HOUR) sprintf_P(buff, PSTR("%02d:%02d:%02d"), timeInfo->tm_hour, timeInfo->tm_min, timeInfo->tm_sec);
else {
uint8_t hour;
hour = timeInfo->tm_hour % 12;
if(hour == 0) hour = 12;
sprintf_P(buff, PSTR("%d:%02d:%02d"), hour, timeInfo->tm_min, timeInfo->tm_sec);
}
display->drawString(64 + x, 15 + y, String(buff));
display->setTextAlignment(TEXT_ALIGN_LEFT);
}

void drawCurrentWeather(OLEDDisplay *display, OLEDDisplayUiState* state, int16_t x, int16_t y) {
display->setFont(ArialMT_Plain_10);
display->setTextAlignment(TEXT_ALIGN_CENTER);
display->drawString(64 + x, 38 + y, currentWeather.conditions);

display->setFont(ArialMT_Plain_24);
display->setTextAlignment(TEXT_ALIGN_LEFT);
String temp = String(currentWeather.temp, (IS_METRIC ? 1 : 0)) + (IS_METRIC ? "°C" : "°F");
display->drawString(60 + x, 5 + y, temp);

display->setFont(Meteocons_Plain_36);
display->setTextAlignment(TEXT_ALIGN_CENTER);
display->drawString(32 + x, 0 + y, currentWeather.iconMeteoCon);
}

void drawForecast(OLEDDisplay *display, OLEDDisplayUiState* state, int16_t x, int16_t y) {
time_t observationTimestamp = forecasts[0].observationTime;
struct tm* obsTimeInfo;
obsTimeInfo = localtime(&observationTimestamp);
now = time(nullptr);
struct tm* curTimeInfo;
curTimeInfo = localtime(&now);
uint8_t start;

// After 1800, stop showing todays forecast
if (curTimeInfo->tm_mday == obsTimeInfo->tm_mday && curTimeInfo->tm_hour > 18) start = 1;
else start = 0;

drawForecastDetails(display, x, y, start);
drawForecastDetails(display, x + 44, y, start+1);
drawForecastDetails(display, x + 88, y, start+2);
}

void drawForecastDetails(OLEDDisplay *display, int x, int y, int dayIndex) {
time_t observationTimestamp = forecasts[dayIndex].observationTime;
struct tm* timeInfo;
timeInfo = localtime(&observationTimestamp);
display->setTextAlignment(TEXT_ALIGN_CENTER);
display->setFont(ArialMT_Plain_10);
display->drawString(x + 20, y, WDAY_NAMES[timeInfo->tm_wday]);

display->setFont(Meteocons_Plain_21);
display->drawString(x + 20, y + 12, forecasts[dayIndex].iconMeteoCon);
String temp = String(forecasts[dayIndex].tempMax, 0) + (IS_METRIC ? "°C" : "°F");
display->setFont(ArialMT_Plain_10);
display->drawString(x + 20, y + 34, temp);
display->setTextAlignment(TEXT_ALIGN_LEFT);
}

void drawHeaderOverlay(OLEDDisplay *display, OLEDDisplayUiState* state) {
now = time(nullptr);
struct tm* timeInfo;
timeInfo = localtime(&now);
char buff[14];

if(IS_24HOUR) sprintf_P(buff, PSTR("%02d:%02d"), timeInfo->tm_hour, timeInfo->tm_min);
else {
uint8_t hour;
hour = timeInfo->tm_hour % 12;
if(hour == 0) hour = 12;
sprintf_P(buff, PSTR("%d:%02d"), hour, timeInfo->tm_min);
}

display->setColor(WHITE);
display->setFont(ArialMT_Plain_10);
display->setTextAlignment(TEXT_ALIGN_LEFT);
display->drawString(0, 54, String(buff));
display->setTextAlignment(TEXT_ALIGN_RIGHT);
String temp = String(currentWeather.temp, (IS_METRIC ? 1 : 0)) + (IS_METRIC ? "°C" : "°F");
display->drawString(128, 54, temp);
display->drawHorizontalLine(0, 52, 128);
}

void setReadyForWeatherUpdate() {
Serial.println("Setting readyForUpdate to true");
readyForWeatherUpdate = true;
}
Loading