/** The MIT License (MIT)
Copyright (c) 2018 David Payne
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.
*/
/**********************************************
* Edit Settings.h for personalization
***********************************************/
#include "Settings.h"
#define VERSION "2.2"
#define HOSTNAME "OctMon-"
#define CONFIG "/conf.txt"
/* Useful Constants */
#define SECS_PER_MIN (60UL)
#define SECS_PER_HOUR (3600UL)
#define SECS_PER_DAY (SECS_PER_HOUR * 24L)
/* Useful Macros for getting elapsed time */
#define numberOfSeconds(_time_) (_time_ % SECS_PER_MIN)
#define numberOfMinutes(_time_) ((_time_ / SECS_PER_MIN) % SECS_PER_MIN)
#define numberOfHours(_time_) (( _time_% SECS_PER_DAY) / SECS_PER_HOUR)
#define elapsedDays(_time_) ( _time_ / SECS_PER_DAY)
// Initialize the oled display for I2C_DISPLAY_ADDRESS
// SDA_PIN and SCL_PIN
#if defined(DISPLAY_SH1106)
SH1106Wire display(I2C_DISPLAY_ADDRESS, SDA_PIN, SCL_PIN);
#else
SSD1306Wire display(I2C_DISPLAY_ADDRESS, SDA_PIN, SCL_PIN); // this is the default
#endif
OLEDDisplayUi ui( &display );
void drawProgress(OLEDDisplay *display, int percentage, String label);
void drawOtaProgress(unsigned int, unsigned int);
void drawScreen1(OLEDDisplay *display, OLEDDisplayUiState* state, int16_t x, int16_t y);
void drawScreen2(OLEDDisplay *display, OLEDDisplayUiState* state, int16_t x, int16_t y);
void drawScreen3(OLEDDisplay *display, OLEDDisplayUiState* state, int16_t x, int16_t y);
void drawHeaderOverlay(OLEDDisplay *display, OLEDDisplayUiState* state);
void drawClock(OLEDDisplay *display, OLEDDisplayUiState* state, int16_t x, int16_t y);
void drawWeather(OLEDDisplay *display, OLEDDisplayUiState* state, int16_t x, int16_t y);
void drawClockHeaderOverlay(OLEDDisplay *display, OLEDDisplayUiState* state);
// Set the number of Frames supported
const int numberOfFrames = 3;
FrameCallback frames[numberOfFrames];
FrameCallback clockFrame[13];
boolean isClockOn = false;
OverlayCallback overlays[] = { drawHeaderOverlay };
OverlayCallback clockOverlay[] = { drawClockHeaderOverlay };
int numberOfOverlays = 1;
// Time
TimeClient timeClient(UtcOffset);
long lastEpoch = 0;
long firstEpoch = 0;
long displayOffEpoch = 0;
String lastMinute = "xx";
String lastSecond = "xx";
String lastReportStatus = "";
boolean displayOn = true;
// OctoPrint Client
OctoPrintClient printerClient(OctoPrintApiKey, OctoPrintServer, OctoPrintPort, OctoAuthUser, OctoAuthPass);
int printerCount = 0;
// Weather Client
OpenWeatherMapClient weatherClient(WeatherApiKey, CityIDs, 1, IS_METRIC);
// News Client
NewsApiClient newsClient(NEWS_API_KEY, NEWS_SOURCE);
int newsIndex = 0;
String NEWS_OPTIONS = ""
""
""
""
""
""
""
""
""
""
""
""
""
""
""
""
""
""
""
""
"";
//declairing prototypes
void configModeCallback (WiFiManager *myWiFiManager);
int8_t getWifiQuality();
ESP8266WebServer server(WEBSERVER_PORT);
String WEB_ACTIONS = " Home"
" Configure"
" Weather"
" News"
" Reset Settings"
" Forget WiFi"
" About";
String CHANGE_FORM = "
";
String WEATHER_FORM = ""
"";
String COLOR_THEMES = ""
""
""
""
""
""
""
""
""
""
""
""
""
""
""
""
""
""
""
""
""
""
"";
// Change the externalLight to the pin you wish to use if other than the Built-in LED
int externalLight = LED_BUILTIN; // LED_BUILTIN is is the built in LED on the Wemos
void setup() {
Serial.begin(115200);
SPIFFS.begin();
delay(10);
//New Line to clear from start garbage
Serial.println();
// Initialize digital pin for LED (little blue light on the Wemos D1 Mini)
pinMode(externalLight, OUTPUT);
readSettings();
// initialize dispaly
display.init();
if (INVERT_DISPLAY) {
display.flipScreenVertically(); // connections at top of OLED display
}
display.clear();
display.display();
//display.flipScreenVertically();
display.setFont(ArialMT_Plain_16);
display.setTextAlignment(TEXT_ALIGN_CENTER);
display.setContrast(255); // default is 255
display.drawString(64, 5, "Printer Monitor\nBy Qrome\nV" + String(VERSION));
display.display();
//WiFiManager
//Local intialization. Once its business is done, there is no need to keep it around
WiFiManager wifiManager;
// Uncomment for testing wifi manager
//wifiManager.resetSettings();
wifiManager.setAPCallback(configModeCallback);
String hostname(HOSTNAME);
hostname += String(ESP.getChipId(), HEX);
if (!wifiManager.autoConnect((const char *)hostname.c_str())) {// new addition
delay(3000);
WiFi.disconnect(true);
ESP.reset();
delay(5000);
}
// You can change the transition that is used
// SLIDE_LEFT, SLIDE_RIGHT, SLIDE_TOP, SLIDE_DOWN
ui.setFrameAnimation(SLIDE_LEFT);
ui.setTargetFPS(30);
ui.disableAllIndicators();
ui.setFrames(frames, (numberOfFrames));
frames[0] = drawScreen1;
frames[1] = drawScreen2;
frames[2] = drawScreen3;
clockFrame[0] = drawClock;
clockFrame[1] = drawWeather;
ui.setOverlays(overlays, numberOfOverlays);
// Inital UI takes care of initalising the display too.
ui.init();
if (INVERT_DISPLAY) {
display.flipScreenVertically(); //connections at top of OLED display
}
// print the received signal strength:
Serial.print("Signal Strength (RSSI): ");
Serial.print(getWifiQuality());
Serial.println("%");
if (ENABLE_OTA) {
ArduinoOTA.onStart([]() {
Serial.println("Start");
});
ArduinoOTA.onEnd([]() {
Serial.println("\nEnd");
});
ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {
Serial.printf("Progress: %u%%\r", (progress / (total / 100)));
});
ArduinoOTA.onError([](ota_error_t error) {
Serial.printf("Error[%u]: ", error);
if (error == OTA_AUTH_ERROR) Serial.println("Auth Failed");
else if (error == OTA_BEGIN_ERROR) Serial.println("Begin Failed");
else if (error == OTA_CONNECT_ERROR) Serial.println("Connect Failed");
else if (error == OTA_RECEIVE_ERROR) Serial.println("Receive Failed");
else if (error == OTA_END_ERROR) Serial.println("End Failed");
});
ArduinoOTA.setHostname((const char *)hostname.c_str());
if (OTA_Password != "") {
ArduinoOTA.setPassword(((const char *)OTA_Password.c_str()));
}
ArduinoOTA.begin();
}
if (WEBSERVER_ENABLED) {
server.on("/", displayPrinterStatus);
server.on("/systemreset", handleSystemReset);
server.on("/forgetwifi", handleWifiReset);
server.on("/updateconfig", handleUpdateConfig);
server.on("/updateweatherconfig", handleUpdateWeather);
server.on("/configure", handleConfigure);
server.on("/configureweather", handleWeatherConfigure);
server.on("/updatenewsconfig", handleUpdateNews);
server.on("/configurenews", handleNewsConfigure);
server.onNotFound(redirectHome);
// Start the server
server.begin();
Serial.println("Server started");
// Print the IP address
String webAddress = "http://" + WiFi.localIP().toString() + ":" + String(WEBSERVER_PORT) + "/";
Serial.println("Use this URL : " + webAddress);
display.clear();
display.setTextAlignment(TEXT_ALIGN_CENTER);
display.setFont(ArialMT_Plain_10);
display.drawString(64, 10, "Web Interface On");
display.drawString(64, 20, "You May Connect to IP");
display.setFont(ArialMT_Plain_16);
display.drawString(64, 30, WiFi.localIP().toString());
display.drawString(64, 46, "Port: " + String(WEBSERVER_PORT));
display.display();
} else {
Serial.println("Web Interface is Disabled");
display.clear();
display.setTextAlignment(TEXT_ALIGN_CENTER);
display.setFont(ArialMT_Plain_10);
display.drawString(64, 10, "Web Interface is Off");
display.drawString(64, 20, "Enable in Settings.h");
display.display();
}
flashLED(5, 500);
findMDNS(); //go find Octoprint Server by the hostname
}
void findMDNS() {
if (OctoPrintHostName == "") {
return; // nothing to do here
}
// We now query our network for 'web servers' service
// over tcp, and get the number of available devices
int n = MDNS.queryService("http", "tcp");
if (n == 0) {
Serial.println("no services found - make sure OctoPrint server is turned on");
return;
}
Serial.println("*** Looking for " + OctoPrintHostName + " over mDNS");
for (int i = 0; i < n; ++i) {
// Going through every available service,
// we're searching for the one whose hostname
// matches what we want, and then get its IP
Serial.println("Found: " + MDNS.hostname(i));
if (MDNS.hostname(i) == OctoPrintHostName) {
IPAddress serverIp = MDNS.IP(i);
OctoPrintServer = serverIp.toString();
OctoPrintPort = MDNS.port(i); // save the port
Serial.println("*** Found OctoPrint Server " + OctoPrintHostName + " http://" + OctoPrintServer + ":" + OctoPrintPort);
writeSettings(); // update the settings
}
}
}
//************************************************************
// Main Looop
//************************************************************
void loop() {
//Get Time Update
if((getMinutesFromLastRefresh() >= minutesBetweenDataRefresh) || lastEpoch == 0) {
getUpdateTime();
}
if (lastMinute != timeClient.getMinutes() && !printerClient.isPrinting()) {
// Check status every 60 seconds
digitalWrite(externalLight, LOW);
lastMinute = timeClient.getMinutes(); // reset the check value
printerClient.getPrinterJobResults();
digitalWrite(externalLight, HIGH);
} else if (printerClient.isPrinting()) {
if (lastSecond != timeClient.getSeconds() && timeClient.getSeconds().endsWith("0")) {
lastSecond = timeClient.getSeconds();
// every 10 seconds while printing get an update
digitalWrite(externalLight, LOW);
printerClient.getPrinterJobResults();
digitalWrite(externalLight, HIGH);
}
}
checkDisplay(); // Check to see if the printer is on or offline and change display.
ui.update();
if (WEBSERVER_ENABLED) {
server.handleClient();
}
if (ENABLE_OTA) {
ArduinoOTA.handle();
}
}
void getUpdateTime() {
digitalWrite(externalLight, LOW); // turn on the LED
Serial.println();
if (displayOn && DISPLAYWEATHER) {
Serial.println("Getting Weather Data...");
weatherClient.updateWeather();
}
if (displayOn && NEWS_ENABLED) {
Serial.println("Getting News Data for " + NEWS_SOURCE);
newsClient.updateNews();
}
Serial.println("Updating Time...");
//Update the Time
timeClient.updateTime();
lastEpoch = timeClient.getCurrentEpoch();
Serial.println("Local time: " + timeClient.getAmPmFormattedTime());
digitalWrite(externalLight, HIGH); // turn off the LED
}
void handleSystemReset() {
if (!server.authenticate(www_username, www_password)) {
return server.requestAuthentication();
}
Serial.println("Reset System Configuration");
if (SPIFFS.remove(CONFIG)) {
redirectHome();
ESP.restart();
}
}
void handleUpdateWeather() {
if (!server.authenticate(www_username, www_password)) {
return server.requestAuthentication();
}
DISPLAYWEATHER = server.hasArg("isWeatherEnabled");
WeatherApiKey = server.arg("openWeatherMapApiKey");
CityIDs[0] = server.arg("city1").toInt();
IS_METRIC = server.hasArg("metric");
writeSettings();
isClockOn = false; // this will force a check for the display
checkDisplay();
lastEpoch = 0;
redirectHome();
}
void handleUpdateConfig() {
if (!server.authenticate(www_username, www_password)) {
return server.requestAuthentication();
}
OctoPrintApiKey = server.arg("octoPrintApiKey");
OctoPrintHostName = server.arg("octoPrintHostName");
OctoPrintServer = server.arg("octoPrintAddress");
OctoPrintPort = server.arg("octoPrintPort").toInt();
OctoAuthUser = server.arg("octoUser");
OctoAuthPass = server.arg("octoPass");
DISPLAYCLOCK = server.hasArg("isClockEnabled");
IS_24HOUR = server.hasArg("is24hour");
minutesBetweenDataRefresh = server.arg("refresh").toInt();
themeColor = server.arg("theme");
UtcOffset = server.arg("utcoffset").toFloat();
String temp = server.arg("userid");
temp.toCharArray(www_username, sizeof(temp));
temp = server.arg("stationpassword");
temp.toCharArray(www_password, sizeof(temp));
writeSettings();
findMDNS();
printerClient.getPrinterJobResults();
checkDisplay();
lastEpoch = 0;
redirectHome();
}
void handleWifiReset() {
if (!server.authenticate(www_username, www_password)) {
return server.requestAuthentication();
}
//WiFiManager
//Local intialization. Once its business is done, there is no need to keep it around
redirectHome();
WiFiManager wifiManager;
wifiManager.resetSettings();
ESP.restart();
}
void handleWeatherConfigure() {
if (!server.authenticate(www_username, www_password)) {
return server.requestAuthentication();
}
digitalWrite(externalLight, LOW);
String html = "";
server.sendHeader("Cache-Control", "no-cache, no-store");
server.sendHeader("Pragma", "no-cache");
server.sendHeader("Expires", "-1");
server.setContentLength(CONTENT_LENGTH_UNKNOWN);
server.send(200, "text/html", "");
html = getHeader();
server.sendContent(html);
String form = WEATHER_FORM;
String isWeatherChecked = "";
if (DISPLAYWEATHER) {
isWeatherChecked = "checked='checked'";
}
form.replace("%IS_WEATHER_CHECKED%", isWeatherChecked);
form.replace("%WEATHERKEY%", WeatherApiKey);
form.replace("%CITYNAME1%", weatherClient.getCity(0));
form.replace("%CITY1%", String(CityIDs[0]));
String checked = "";
if (IS_METRIC) {
checked = "checked='checked'";
}
form.replace("%METRIC%", checked);
server.sendContent(form);
html = getFooter();
server.sendContent(html);
server.sendContent("");
server.client().stop();
digitalWrite(externalLight, HIGH);
}
void handleNewsConfigure() {
if (!server.authenticate(www_username, www_password)) {
return server.requestAuthentication();
}
digitalWrite(externalLight, LOW);
String html = "";
String NEWS_FORM1 = ""
"";
server.sendHeader("Cache-Control", "no-cache, no-store");
server.sendHeader("Pragma", "no-cache");
server.sendHeader("Expires", "-1");
server.setContentLength(CONTENT_LENGTH_UNKNOWN);
server.send(200, "text/html", "");
html = getHeader();
server.sendContent(html);
String form = NEWS_FORM1;
String isNewsDisplayedChecked = "";
if (NEWS_ENABLED) {
isNewsDisplayedChecked = "checked='checked'";
}
form.replace("%NEWSCHECKED%", isNewsDisplayedChecked);
form.replace("%NEWSKEY%", NEWS_API_KEY);
server.sendContent(form); //Send first Chunk of form
server.sendContent(NEWS_OPTIONS);
form = NEWS_FORM2;
form.replace("%NEWSSOURCE%", NEWS_SOURCE);
server.sendContent(form);
html = getFooter();
server.sendContent(html);
server.sendContent("");
server.client().stop();
digitalWrite(externalLight, HIGH);
}
void handleConfigure() {
if (!server.authenticate(www_username, www_password)) {
return server.requestAuthentication();
}
digitalWrite(externalLight, LOW);
String html = "";
server.sendHeader("Cache-Control", "no-cache, no-store");
server.sendHeader("Pragma", "no-cache");
server.sendHeader("Expires", "-1");
server.setContentLength(CONTENT_LENGTH_UNKNOWN);
server.send(200, "text/html", "");
html = getHeader();
server.sendContent(html);
String form = CHANGE_FORM;
form.replace("%OCTOKEY%", OctoPrintApiKey);
form.replace("%OCTOHOST%", OctoPrintHostName);
form.replace("%OCTOADDRESS%", OctoPrintServer);
form.replace("%OCTOPORT%", String(OctoPrintPort));
form.replace("%OCTOUSER%", OctoAuthUser);
form.replace("%OCTOPASS%", OctoAuthPass);
String isClockChecked = "";
if (DISPLAYCLOCK) {
isClockChecked = "checked='checked'";
}
form.replace("%IS_CLOCK_CHECKED%", isClockChecked);
String is24hourChecked = "";
if (IS_24HOUR) {
is24hourChecked = "checked='checked'";
}
form.replace("%IS_24HOUR_CHECKED%", is24hourChecked);
String options = "";
options.replace(">"+String(minutesBetweenDataRefresh)+"<", " selected>"+String(minutesBetweenDataRefresh)+"<");
form.replace("%OPTIONS%", options);
String themeOptions = COLOR_THEMES;
themeOptions.replace(">"+String(themeColor)+"<", " selected>"+String(themeColor)+"<");
form.replace("%THEME_OPTIONS%", themeOptions);
form.replace("%UTCOFFSET%", String(UtcOffset));
form.replace("%USERID%", String(www_username));
form.replace("%STATIONPASSWORD%", String(www_password));
server.sendContent(form);
html = getFooter();
server.sendContent(html);
server.sendContent("");
server.client().stop();
digitalWrite(externalLight, HIGH);
}
void handleUpdateNews() {
if (!server.authenticate(www_username, www_password)) {
return server.requestAuthentication();
}
NEWS_ENABLED = server.hasArg("displaynews");
NEWS_API_KEY = server.arg("newsApiKey");
NEWS_SOURCE = server.arg("newssource");
writeSettings();
isClockOn = false; // this will force a check for the display
checkDisplay();
if (NEWS_ENABLED)
{
newsClient.updateNews();
}
redirectHome();
}
void displayMessage(String message) {
digitalWrite(externalLight, LOW);
server.sendHeader("Cache-Control", "no-cache, no-store");
server.sendHeader("Pragma", "no-cache");
server.sendHeader("Expires", "-1");
server.setContentLength(CONTENT_LENGTH_UNKNOWN);
server.send(200, "text/html", "");
String html = getHeader();
server.sendContent(String(html));
server.sendContent(String(message));
html = getFooter();
server.sendContent(String(html));
server.sendContent("");
server.client().stop();
digitalWrite(externalLight, HIGH);
}
void redirectHome() {
// Send them back to the Root Directory
server.sendHeader("Location", String("/"), true);
server.sendHeader("Cache-Control", "no-cache, no-store");
server.sendHeader("Pragma", "no-cache");
server.sendHeader("Expires", "-1");
server.send(302, "text/plain", "");
server.client().stop();
}
String getHeader() {
return getHeader(false);
}
String getHeader(boolean refresh) {
String menu = WEB_ACTIONS;
String html = "";
html += "Printer Monitor";
html += "";
if (refresh) {
html += "";
}
html += "";
html += "";
html += "";
html += "";
html += "";
html += "
";
html += " ";
html += weatherClient.getHumidity(0) + "% Humidity ";
html += weatherClient.getWind(0) + " " + getSpeedSymbol() + " Wind ";
html += "
";
html += "
";
html += weatherClient.getCondition(0) + " (" + weatherClient.getDescription(0) + ") ";
html += weatherClient.getTempRounded(0) + getTempSymbol(true) + " ";
html += " Map It! ";
html += "
";
}
server.sendContent(html); // spit out what we got
html = ""; // fresh start
}
if (NEWS_ENABLED) {
if (NEWS_API_KEY == "" || NEWS_SOURCE == "") {
html += "