Compare commits
22 Commits
d3d74938d3
...
master
Author | SHA1 | Date | |
---|---|---|---|
987719caa7 | |||
877d2b1207 | |||
3f20df7571 | |||
fd3c54d517 | |||
3d43fa7a84 | |||
d1b2e6d365 | |||
0d5265d1ad | |||
9c25cb47cd | |||
1c6d4eaaba | |||
f835aa0122 | |||
20a6714bef | |||
56ed7e4b19 | |||
b8c903534f | |||
982321141b | |||
ee2581b3ef | |||
1107b563d4 | |||
5e78e8e369 | |||
ef6327e2eb | |||
d6f5d2a8ce | |||
7b67633bc9 | |||
dea19d89d8 | |||
3299c3d66a |
9
.vscode/settings.json
vendored
Normal file
9
.vscode/settings.json
vendored
Normal file
@ -0,0 +1,9 @@
|
||||
{
|
||||
"files.associations": {
|
||||
"*.page-template": "vue",
|
||||
"*.layout-template": "vue",
|
||||
"*.vue": "vue",
|
||||
"*.tcc": "cpp",
|
||||
"functional": "cpp"
|
||||
}
|
||||
}
|
23
src/bitmaps.h
Normal file
23
src/bitmaps.h
Normal file
@ -0,0 +1,23 @@
|
||||
#include <avr/pgmspace.h>
|
||||
|
||||
const unsigned char bitmap_nozzle[] PROGMEM = {
|
||||
0b00111100,
|
||||
0b00111100,
|
||||
0b00111100,
|
||||
0b11111111,
|
||||
0b11111111,
|
||||
0b01111110,
|
||||
0b00111100,
|
||||
0b00011000
|
||||
};
|
||||
|
||||
const unsigned char bitmap_bed[] PROGMEM = {
|
||||
0b00100010,
|
||||
0b01000100,
|
||||
0b00100010,
|
||||
0b01000100,
|
||||
0b11111111,
|
||||
0b11111111,
|
||||
0b01100110,
|
||||
0b00000000
|
||||
};
|
@ -1,7 +1,3 @@
|
||||
// WiFi settings
|
||||
#define WIFI_SSID "Your_SSID"
|
||||
#define WIFI_PASSWORD "Your_Password"
|
||||
|
||||
// OctoPrint settings
|
||||
#define OCTOPRINT_HOST "your.octoprint.local"
|
||||
#define OCTOPRINT_PORT 9000
|
||||
@ -15,7 +11,7 @@
|
||||
#define FRAMERATE 8
|
||||
|
||||
// Buttons and buzzer
|
||||
#define PIN_BTN_L 12 // D6
|
||||
#define PIN_BTN_L 15 // D6
|
||||
#define PIN_BTN_M 13 // D7
|
||||
#define PIN_BTN_R 15 // D8
|
||||
#define PIN_BTN_R 12 // D8
|
||||
#define PIN_BUZZER 0 // D3
|
150
src/functions.h
150
src/functions.h
@ -4,6 +4,9 @@
|
||||
#include <Adafruit_SSD1306.h>
|
||||
#include <ESP8266WiFi.h>
|
||||
|
||||
void playTune(int tuneNumber);
|
||||
void beep(int buzz);
|
||||
|
||||
unsigned long middle_long_press_started = 0;
|
||||
unsigned long right_long_press_started = 0;
|
||||
bool is_display_off = false;
|
||||
@ -35,79 +38,94 @@ void initSystems() {
|
||||
display.display();
|
||||
}
|
||||
|
||||
// Connect to WiFi
|
||||
void connectToWifi() {
|
||||
|
||||
Serial.println("Connecting to WiFi...");
|
||||
display.clearDisplay();
|
||||
display.setCursor(0, 0);
|
||||
display.print("Connecting to WiFi...");
|
||||
display.display();
|
||||
|
||||
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
|
||||
|
||||
int attemptCount = 0;
|
||||
while (WiFi.status() != WL_CONNECTED && attemptCount < 20) { // Timeout after 20 attempts
|
||||
delay(500);
|
||||
Serial.print(".");
|
||||
display.print(".");
|
||||
display.display();
|
||||
attemptCount++;
|
||||
}
|
||||
|
||||
// Check if connected
|
||||
if (WiFi.status() == WL_CONNECTED) {
|
||||
Serial.println("\nConnected to WiFi!");
|
||||
Serial.print("IP Address: ");
|
||||
Serial.println(WiFi.localIP());
|
||||
|
||||
// Display connection success and IP address
|
||||
display.clearDisplay();
|
||||
display.setCursor(0, 0);
|
||||
display.print("WiFi Connected!");
|
||||
display.setCursor(0, 10);
|
||||
display.print("IP: ");
|
||||
display.print(WiFi.localIP());
|
||||
display.display();
|
||||
} else {
|
||||
Serial.println("\nFailed to connect to WiFi.");
|
||||
display.clearDisplay();
|
||||
display.setCursor(0, 0);
|
||||
display.print("WiFi Connection");
|
||||
display.setCursor(0, 10);
|
||||
display.print("Failed");
|
||||
display.display();
|
||||
}
|
||||
delay(1000);
|
||||
}
|
||||
|
||||
void commonButtonHandler() {
|
||||
|
||||
unsigned long currentMillis = millis();
|
||||
bool middleLeftPressed = (digitalRead(PIN_BTN_M) == HIGH) && (digitalRead(PIN_BTN_L) == HIGH);
|
||||
static unsigned long leftPressStart = 0;
|
||||
static unsigned long middlePressStart = 0;
|
||||
static unsigned long rightPressStart = 0;
|
||||
|
||||
static bool leftBeeped = false;
|
||||
static bool middleBeeped = false;
|
||||
static bool rightBeeped = false;
|
||||
|
||||
bool leftPressed = (digitalRead(PIN_BTN_L) == HIGH);
|
||||
bool middlePressed = (digitalRead(PIN_BTN_M) == HIGH);
|
||||
bool rightPressed = (digitalRead(PIN_BTN_R) == HIGH);
|
||||
|
||||
// Handle middle + left button long press for reset
|
||||
if (middleLeftPressed) {
|
||||
if ((currentMillis - middle_long_press_started) > 2000) {
|
||||
ESP.restart(); // Restart if the buttons are pressed for more than 2 seconds
|
||||
// Short press detection
|
||||
if (leftPressed) {
|
||||
if ((currentMillis - leftPressStart > 50)) { // Debounce delay
|
||||
if (!leftBeeped) {
|
||||
beep(1000); // Play beep sound
|
||||
leftBeeped = true; // Set beeped state
|
||||
// Handle left short press action here
|
||||
}
|
||||
}
|
||||
} else {
|
||||
middle_long_press_started = currentMillis; // Reset timer if the buttons are not pressed
|
||||
leftPressStart = currentMillis; // Reset the timer if button is not pressed
|
||||
leftBeeped = false; // Reset beeped state
|
||||
}
|
||||
|
||||
if (rightPressed) {
|
||||
if ((currentMillis - right_long_press_started) > 2000) {
|
||||
if (!is_display_off) {
|
||||
display.ssd1306_command(SSD1306_DISPLAYOFF); // Turn off display
|
||||
is_display_off = true;
|
||||
}
|
||||
} else if (is_display_off) {
|
||||
display.ssd1306_command(SSD1306_DISPLAYON); // Turn on display
|
||||
is_display_off = false;
|
||||
if (middlePressed) {
|
||||
if ((currentMillis - middlePressStart > 50)) { // Debounce delay
|
||||
if (!middleBeeped) {
|
||||
beep(1000); // Play beep sound
|
||||
middleBeeped = true; // Set beeped state
|
||||
// Handle middle short press action here
|
||||
}
|
||||
}
|
||||
} else {
|
||||
middlePressStart = currentMillis; // Reset the timer if button is not pressed
|
||||
middleBeeped = false; // Reset beeped state
|
||||
}
|
||||
|
||||
if (rightPressed) {
|
||||
if ((currentMillis - rightPressStart > 50)) { // Debounce delay
|
||||
if (!rightBeeped) {
|
||||
beep(1000); // Play beep sound
|
||||
rightBeeped = true; // Set beeped state
|
||||
if (is_display_off) {
|
||||
display.ssd1306_command(SSD1306_DISPLAYON); // Turn on display
|
||||
is_display_off = false;
|
||||
beep(1300);
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
rightPressStart = currentMillis; // Reset the timer if button is not pressed
|
||||
rightBeeped = false; // Reset beeped state
|
||||
}
|
||||
|
||||
// Long press detection
|
||||
if (leftPressed && (currentMillis - leftPressStart > 2000)) {
|
||||
if (!leftBeeped) {
|
||||
beep(1000); // Play beep sound
|
||||
leftBeeped = true; // Set beeped state
|
||||
// Handle left long press action here
|
||||
}
|
||||
}
|
||||
|
||||
if (middlePressed && (currentMillis - middlePressStart > 2000)) {
|
||||
if (!middleBeeped) {
|
||||
beep(1000); // Play beep sound
|
||||
middleBeeped = true; // Set beeped state
|
||||
// Handle middle long press action here
|
||||
}
|
||||
}
|
||||
|
||||
if (rightPressed && (currentMillis - rightPressStart > 2000)) {
|
||||
if (!is_display_off) {
|
||||
beep(1300);
|
||||
display.ssd1306_command(SSD1306_DISPLAYOFF); // Turn off display
|
||||
is_display_off = true;
|
||||
beep(1000);
|
||||
}
|
||||
}
|
||||
|
||||
// Combination of Left and Middle long press
|
||||
if (leftPressed && middlePressed &&
|
||||
(currentMillis - leftPressStart > 2000) && (currentMillis - middlePressStart > 2000)) {
|
||||
beep(1000); // Play beep sound
|
||||
ESP.restart();
|
||||
}
|
||||
} else {
|
||||
right_long_press_started = currentMillis;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -3,18 +3,20 @@
|
||||
#include <Wire.h>
|
||||
#include <Adafruit_GFX.h>
|
||||
#include <Adafruit_SSD1306.h>
|
||||
#include "netman.h"
|
||||
#include "functions.h"
|
||||
#include "bitmaps.h"
|
||||
#include "octoprint.h"
|
||||
|
||||
void setup() {
|
||||
|
||||
initSystems();
|
||||
netman netman(display);
|
||||
playTune(1);
|
||||
connectToWifi();
|
||||
netman.start();
|
||||
displayOctoPrintVersion(display);
|
||||
|
||||
}
|
||||
|
||||
void loop() {
|
||||
commonButtonHandler();
|
||||
fetchPrintingStatus(display);
|
||||
}
|
||||
|
333
src/netman.h
Normal file
333
src/netman.h
Normal file
@ -0,0 +1,333 @@
|
||||
#include <ESP8266WiFi.h>
|
||||
#include <WiFiClient.h>
|
||||
#include <ESP8266WebServer.h>
|
||||
#include <Adafruit_SSD1306.h>
|
||||
#include <LittleFS.h>
|
||||
#include <DNSServer.h>
|
||||
#include <map>
|
||||
|
||||
// Constants for the HTML pages and config file
|
||||
static const String beginHtml = "<!DOCTYPE html><html lang='en'><head><meta charset='UTF-8'><meta name='viewport' content='width=device-width, initial-scale=1.0'><title>SmartCube Configure</title><style>body{font-family:Arial,sans-serif;background-color:#f4f4f9;color:#333;display:flex;align-items:center;justify-content:center;height:100vh;margin:0}.container{width:100%;max-width:300px;padding:20px;background:#fff;border-radius:10px;box-shadow:0 4px 8px rgba(0,0,0,0.2);box-sizing:border-box}h2{margin-top:0;text-align:center;color:#0073e6}label{display:block;margin-bottom:5px;font-weight:bold}input[type='text'],input[type='password']{width:100%;padding:8px;margin-bottom:15px;border:1px solid #ccc;border-radius:4px;box-sizing:border-box}button{width:100%;padding:10px;background-color:#0073e6;color:white;border:none;border-radius:4px;cursor:pointer;font-size:16px}button:hover{background-color:#005bb5}table{width:100%;margin-top:20px}td{padding:5px;text-align:left}</style></head><body><div class='container'><h2>Configure AP</h2><table><tbody><tr><td><label for='ssid'>SSID</label></td><td><input id='ssid' type='text' placeholder='Enter SSID'/></td></tr><tr><td><label for='pass'>Password</label></td><td><input id='pass' type='password' placeholder='Enter Password'/></td></tr><tr><td colspan='2'><button onclick=\"location.href = '/add?ssid=' + encodeURIComponent(document.getElementById('ssid').value) + '&pass=' + encodeURIComponent(document.getElementById('pass').value);\">Add Network</button></td></tr></tbody></table><br/><table><tbody>";
|
||||
static const String endHtml = "</tbody></table></body></html>";
|
||||
static const String configFile = "/netman";
|
||||
|
||||
// Timeout for WiFi connection attempts
|
||||
static const unsigned long connectionTimeout = 15000; // 15 seconds
|
||||
|
||||
struct netman {
|
||||
public:
|
||||
netman(Adafruit_SSD1306& display);
|
||||
netman(String ssid, String pass, bool hidden, Adafruit_SSD1306& display);
|
||||
bool start();
|
||||
void reset();
|
||||
void addSsid(String ssid, String password);
|
||||
void removeSsid(String ssid, String password);
|
||||
|
||||
private:
|
||||
Adafruit_SSD1306& display;
|
||||
std::unique_ptr<ESP8266WebServer> server;
|
||||
std::map<String, String> _ssids;
|
||||
String _ssid, _pass;
|
||||
bool _hidden;
|
||||
|
||||
void init(String ssid, String pass, bool hidden);
|
||||
bool tryConnectToSsid(const char* ssid, const char* pass);
|
||||
bool tryConnect();
|
||||
void createAP();
|
||||
bool redirectToIp();
|
||||
void readConfig();
|
||||
void writeConfig();
|
||||
void handleRoot();
|
||||
void handleAdd();
|
||||
void handleRemove();
|
||||
void handleSelect();
|
||||
};
|
||||
|
||||
// Initialization
|
||||
void netman::init(String ssid, String pass, bool hidden) {
|
||||
// Ensure password meets minimum length
|
||||
if (pass != "" && pass.length() < 8) {
|
||||
display.clearDisplay();
|
||||
display.setCursor(0, 0);
|
||||
display.println("Password too short");
|
||||
display.display();
|
||||
pass = "8characters";
|
||||
}
|
||||
|
||||
// Get the last 4 characters of the MAC address
|
||||
String macAddress = WiFi.macAddress();
|
||||
String macSuffix = macAddress.substring(macAddress.length() - 5);
|
||||
macSuffix.replace(":", "");
|
||||
|
||||
// Set default SSID if none provided
|
||||
_ssid = ssid.isEmpty() ? "SmartCube_" + macSuffix : ssid;
|
||||
_pass = pass;
|
||||
_hidden = hidden;
|
||||
|
||||
|
||||
// Initialize LittleFS with error handling
|
||||
if (!LittleFS.begin()) {
|
||||
display.clearDisplay();
|
||||
display.setCursor(0, 0);
|
||||
display.println("FS init failed");
|
||||
display.display();
|
||||
}
|
||||
}
|
||||
|
||||
// Constructors
|
||||
netman::netman(Adafruit_SSD1306& display) : display(display) {
|
||||
init("", "", false);
|
||||
}
|
||||
|
||||
netman::netman(String ssid, String pass, bool hidden, Adafruit_SSD1306& display) : display(display) {
|
||||
init(ssid, pass, hidden);
|
||||
}
|
||||
|
||||
// Attempt to start and connect or create AP
|
||||
bool netman::start() {
|
||||
if (_pass == "8characters") {
|
||||
display.clearDisplay();
|
||||
display.setCursor(0, 0);
|
||||
display.println("Using default pass:");
|
||||
display.println("8characters");
|
||||
display.display();
|
||||
}
|
||||
return tryConnect() || (createAP(), false);
|
||||
}
|
||||
|
||||
// Attempt connection to each saved SSID in order
|
||||
bool netman::tryConnect() {
|
||||
readConfig();
|
||||
for (auto const& item : _ssids) {
|
||||
if (tryConnectToSsid(item.first.c_str(), item.second.c_str())) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
// Attempt to connect to a specific SSID with timeout, dot animation, and IP display
|
||||
bool netman::tryConnectToSsid(const char* ssid, const char* pass) {
|
||||
WiFi.begin(ssid, pass);
|
||||
unsigned long start = millis();
|
||||
|
||||
// Clear display and set initial message
|
||||
display.clearDisplay();
|
||||
display.setCursor(0, 0);
|
||||
display.println("Connecting to WiFi:");
|
||||
display.setCursor(0, 11);
|
||||
display.println(String(ssid));
|
||||
display.display();
|
||||
|
||||
int dotCount = 0;
|
||||
while (millis() - start < connectionTimeout) {
|
||||
delay(500);
|
||||
|
||||
// Check WiFi connection status
|
||||
if (WiFi.status() == WL_CONNECTED) {
|
||||
// Success message with IP address
|
||||
display.clearDisplay();
|
||||
display.setCursor(0, 0);
|
||||
display.println("Connected!");
|
||||
display.setCursor(0, 12);
|
||||
display.print("IP: ");
|
||||
display.println(WiFi.localIP());
|
||||
display.display();
|
||||
delay(500);
|
||||
return true;
|
||||
}
|
||||
|
||||
// Animate by adding dots
|
||||
display.setCursor(0, 20);
|
||||
for (int i = 0; i < dotCount; i++) {
|
||||
display.print(".");
|
||||
}
|
||||
display.display();
|
||||
dotCount = (dotCount + 1);
|
||||
}
|
||||
|
||||
// Connection failed
|
||||
display.clearDisplay();
|
||||
display.setCursor(0, 0);
|
||||
display.println("Connection failed.");
|
||||
display.display();
|
||||
WiFi.disconnect();
|
||||
return false;
|
||||
}
|
||||
|
||||
// Setup Access Point with DNS and HTTP server
|
||||
void netman::createAP() {
|
||||
WiFi.softAPdisconnect(true);
|
||||
WiFi.mode(WIFI_AP);
|
||||
WiFi.softAP(_ssid.c_str(), _pass.c_str(), 1, _hidden);
|
||||
|
||||
display.clearDisplay();
|
||||
display.setCursor(0, 0);
|
||||
display.println("AccessPoint created");
|
||||
display.setCursor(0, 14);
|
||||
display.println("SSID:");
|
||||
display.setCursor(0, 24);
|
||||
display.setTextSize(1);
|
||||
display.println(_ssid.c_str());
|
||||
display.setTextSize(1);
|
||||
display.setCursor(0, 40);
|
||||
display.println("Config portal:");
|
||||
display.setCursor(0, 50);
|
||||
display.print("http://");
|
||||
display.println(WiFi.softAPIP().toString());
|
||||
display.display();
|
||||
|
||||
server.reset(new ESP8266WebServer(80));
|
||||
DNSServer dnsServer;
|
||||
dnsServer.start(53, "*", WiFi.softAPIP());
|
||||
|
||||
server->on("/", std::bind(&netman::handleRoot, this));
|
||||
server->on("/add", std::bind(&netman::handleAdd, this));
|
||||
server->on("/remove", std::bind(&netman::handleRemove, this));
|
||||
server->on("/select", std::bind(&netman::handleSelect, this));
|
||||
|
||||
server->begin();
|
||||
|
||||
while (true) {
|
||||
dnsServer.processNextRequest();
|
||||
server->handleClient();
|
||||
delay(10);
|
||||
}
|
||||
}
|
||||
|
||||
// Redirect to AP IP if not accessed directly
|
||||
bool netman::redirectToIp() {
|
||||
if (server->hostHeader() == WiFi.softAPIP().toString()) {
|
||||
return false;
|
||||
}
|
||||
server->sendHeader("Location", "http://" + WiFi.softAPIP().toString(), true);
|
||||
server->send(302, "text/plain", "");
|
||||
server->client().stop();
|
||||
return true;
|
||||
}
|
||||
|
||||
// Modify the addSsid function to take parameters from the `select` page
|
||||
void netman::addSsid(String ssid, String password) {
|
||||
_ssids[ssid] = password;
|
||||
writeConfig();
|
||||
|
||||
// Attempt to connect to the selected network
|
||||
if (tryConnectToSsid(ssid.c_str(), password.c_str())) {
|
||||
// Redirect to the main page on success
|
||||
server->sendHeader("Location", "/", true);
|
||||
server->send(302, "text/plain", "");
|
||||
} else {
|
||||
// Show error message if connection failed
|
||||
server->send(200, "text/html", "<html><body><h2>Connection failed. Please try again.</h2></body></html>");
|
||||
}
|
||||
}
|
||||
|
||||
// Remove SSID from config
|
||||
void netman::removeSsid(String ssid, String password) {
|
||||
if (_ssids.count(ssid) && _ssids[ssid] == password) {
|
||||
_ssids.erase(ssid);
|
||||
writeConfig();
|
||||
}
|
||||
}
|
||||
|
||||
// Handle file-based config loading
|
||||
void netman::readConfig() {
|
||||
_ssids.clear();
|
||||
File file = LittleFS.open(configFile, "r");
|
||||
if (!file) {
|
||||
display.clearDisplay();
|
||||
display.setCursor(0, 0);
|
||||
display.println("Config not found");
|
||||
display.display();
|
||||
return;
|
||||
}
|
||||
|
||||
while (file.available()) {
|
||||
String ssid = file.readStringUntil('\n');
|
||||
ssid.trim();
|
||||
String pass = file.readStringUntil('\n');
|
||||
pass.trim();
|
||||
_ssids[ssid] = pass;
|
||||
}
|
||||
file.close();
|
||||
}
|
||||
|
||||
// Handle file-based config saving
|
||||
void netman::writeConfig() {
|
||||
File file = LittleFS.open(configFile, "w");
|
||||
for (const auto& item : _ssids) {
|
||||
file.println(item.first);
|
||||
file.println(item.second);
|
||||
}
|
||||
file.close();
|
||||
}
|
||||
|
||||
// Reset configuration
|
||||
void netman::reset() {
|
||||
LittleFS.remove(configFile);
|
||||
_ssids.clear();
|
||||
}
|
||||
|
||||
String urlEncode(const String &str) {
|
||||
String encoded = "";
|
||||
char c;
|
||||
for (size_t i = 0; i < str.length(); i++) {
|
||||
c = str.charAt(i);
|
||||
if (isalnum(c)) {
|
||||
encoded += c;
|
||||
} else {
|
||||
encoded += '%';
|
||||
char buf[3];
|
||||
snprintf(buf, sizeof(buf), "%02X", c);
|
||||
encoded += buf;
|
||||
}
|
||||
}
|
||||
return encoded;
|
||||
}
|
||||
|
||||
void netman::handleRoot() {
|
||||
if (redirectToIp()) return;
|
||||
|
||||
// Scan for available networks
|
||||
int n = WiFi.scanNetworks();
|
||||
String result = beginHtml;
|
||||
|
||||
// Add stored SSIDs to the page
|
||||
result += "<h3>Saved Networks</h3>";
|
||||
for (const auto& item : _ssids) {
|
||||
result += "<tr><td><button onclick=\"location.href='/remove?ssid=' + escape('" + item.first + "') + '&pass=' + escape('" + item.second + "') \">×</button></td><td>" + item.first + "</td><td>-</td><td>" + item.second + "</td></tr>";
|
||||
}
|
||||
|
||||
// Display available WiFi networks
|
||||
result += "</tbody></table><h3>Available Networks</h3><table><tbody>";
|
||||
for (int i = 0; i < n; i++) {
|
||||
// Get SSID and signal strength
|
||||
String ssid = WiFi.SSID(i);
|
||||
int rssi = WiFi.RSSI(i);
|
||||
bool openNetwork = (WiFi.encryptionType(i) == ENC_TYPE_NONE);
|
||||
|
||||
// Show network with button to select
|
||||
result += "<tr><td><button onclick=\"location.href='/select?ssid=" + urlEncode(ssid) + "'\">" + ssid + "</button></td><td>" + (openNetwork ? "(Open)" : "(Secured)") + "</td><td>" + String(rssi) + " dBm</td></tr>";
|
||||
}
|
||||
result += endHtml;
|
||||
server->send(200, "text/html", result);
|
||||
}
|
||||
|
||||
void netman::handleAdd() {
|
||||
server->send(200, "text/html", "The ESP will now reboot.");
|
||||
addSsid(server->arg("ssid"), server->arg("pass"));
|
||||
delay(500);
|
||||
ESP.restart();
|
||||
}
|
||||
|
||||
void netman::handleRemove() {
|
||||
removeSsid(server->arg("ssid"), server->arg("pass"));
|
||||
handleRoot();
|
||||
}
|
||||
|
||||
// Add SSID to config and save
|
||||
void netman::handleSelect() {
|
||||
String ssid = server->arg("ssid");
|
||||
String selectPage = "<!DOCTYPE html><html lang='en'><head><meta charset='UTF-8'><meta name='viewport' content='width=device-width, initial-scale=1.0'><title>Connect to " + ssid + "</title><style>body{font-family:Arial,sans-serif;background-color:#f4f4f9;color:#333;display:flex;align-items:center;justify-content:center;height:100vh;margin:0}.container{width:100%;max-width:300px;padding:20px;background:#fff;border-radius:10px;box-shadow:0 4px 8px rgba(0,0,0,0.2);box-sizing:border-box}h2{margin-top:0;text-align:center;color:#0073e6}label{display:block;margin-bottom:5px;font-weight:bold}input[type='password']{width:100%;padding:8px;margin-bottom:15px;border:1px solid #ccc;border-radius:4px;box-sizing:border-box}button{width:100%;padding:10px;background-color:#0073e6;color:white;border:none;border-radius:4px;cursor:pointer;font-size:16px}button:hover{background-color:#005bb5}</style></head><body><div class='container'><h2>Connect to " + ssid + "</h2><form action='/add' method='get'><input type='hidden' name='ssid' value='" + ssid + "'><label for='pass'>Password:</label><input id='pass' type='password' name='pass' placeholder='Enter Password'><button type='submit'>Connect</button></form></div></body></html>";
|
||||
server->send(200, "text/html", selectPage);
|
||||
}
|
208
src/octoprint.h
208
src/octoprint.h
@ -7,56 +7,192 @@
|
||||
#include "tunes.h"
|
||||
|
||||
void playTune(int tuneNumber);
|
||||
|
||||
WiFiClient client;
|
||||
|
||||
String printerOperational;
|
||||
String printerPaused;
|
||||
String printerPrinting;
|
||||
String printerReady;
|
||||
String printerText;
|
||||
String printerHotend;
|
||||
String printerTarget;
|
||||
String payload;
|
||||
|
||||
OctoprintApi api(client, OCTOPRINT_HOST, OCTOPRINT_PORT, OCTOPRINT_API_KEY);
|
||||
OctoprintApi octoprint(client, (const char*)OCTOPRINT_HOST, OCTOPRINT_PORT, OCTOPRINT_API_KEY);
|
||||
unsigned long octoprint_mtbs = OCTOPRINT_API_REFRESH_INTERVAL;
|
||||
unsigned long octoprint_lasttime = 0;
|
||||
|
||||
bool displayOctoPrintVersion(Adafruit_SSD1306& display) {
|
||||
const int maxRetries = 3; // Maximum number of attempts
|
||||
int attempts = 0;
|
||||
|
||||
// Initial display of connecting message with host and port
|
||||
display.clearDisplay();
|
||||
display.setCursor(0, 0);
|
||||
display.setTextSize(1);
|
||||
display.setTextColor(WHITE);
|
||||
display.print("Fetching OctoPrint Version...");
|
||||
display.display(); // Show loading message
|
||||
display.print("Connecting to");
|
||||
display.setCursor(0, 10);
|
||||
display.print("OctoPrint");
|
||||
display.setCursor(0, 25);
|
||||
display.print("Host: ");
|
||||
display.print(OCTOPRINT_HOST);
|
||||
display.setCursor(0, 35);
|
||||
display.print("Port: ");
|
||||
display.print(OCTOPRINT_PORT);
|
||||
display.display();
|
||||
|
||||
// Attempt to fetch the OctoPrint version
|
||||
if (api.getOctoprintVersion()) {
|
||||
delay(500); // Delay to allow message display before attempting connection
|
||||
|
||||
// Attempt to connect up to maxRetries times
|
||||
while (attempts < maxRetries) {
|
||||
if (octoprint.getOctoprintVersion()) {
|
||||
// Connection successful; display version information
|
||||
display.clearDisplay();
|
||||
display.setCursor(0, 0);
|
||||
display.setTextSize(1);
|
||||
display.setTextColor(WHITE);
|
||||
display.print("OctoPrint Version:");
|
||||
display.setCursor(0, 10);
|
||||
display.setTextSize(2);
|
||||
display.print(octoprint.octoprintVer.octoprintServer);
|
||||
display.display();
|
||||
playTune(2); // Success tune
|
||||
return true; // Exit function, indicating success
|
||||
}
|
||||
|
||||
attempts++; // Increment attempt count
|
||||
|
||||
// If the connection attempt fails, clear the display and show retry status
|
||||
display.clearDisplay();
|
||||
display.setCursor(0, 0);
|
||||
display.setTextSize(1);
|
||||
display.setTextColor(WHITE);
|
||||
|
||||
display.print("OctoPrint Version:");
|
||||
display.print("Retry ");
|
||||
display.print(attempts);
|
||||
display.print(" of ");
|
||||
display.print(maxRetries);
|
||||
display.setCursor(0, 10);
|
||||
display.setTextSize(2); // Larger text for version
|
||||
display.print(api.octoprintVer.octoprintServer);
|
||||
|
||||
display.display(); // Update the display
|
||||
playTune(2);
|
||||
return true; // Indicate success
|
||||
} else {
|
||||
display.clearDisplay();
|
||||
display.setCursor(0, 0);
|
||||
display.setTextSize(1);
|
||||
display.setTextColor(WHITE);
|
||||
display.print("Failed to get");
|
||||
display.setCursor(0, 10);
|
||||
display.print("OctoPrint version.");
|
||||
display.print("Failed to connect.");
|
||||
display.display();
|
||||
|
||||
// Optionally, add a short delay to show the error
|
||||
playTune(3);
|
||||
return false; // Indicate failure
|
||||
delay(5000); // Wait before retrying
|
||||
}
|
||||
delay(1000);
|
||||
|
||||
// If all attempts fail, display final failure message
|
||||
display.clearDisplay();
|
||||
display.setCursor(0, 0);
|
||||
display.setTextSize(1);
|
||||
display.setTextColor(WHITE);
|
||||
display.print("Failed to get");
|
||||
display.setCursor(0, 10);
|
||||
display.print("OctoPrint version.");
|
||||
display.display();
|
||||
playTune(3); // Failure tune
|
||||
return false;
|
||||
}
|
||||
|
||||
int scrollPos = 0; // Global variable to keep track of the scroll position
|
||||
int scrollDirection = 1; // 1 for left, -1 for right
|
||||
unsigned long lastScrollTime = 0; // To control the scroll speed
|
||||
const int scrollDelay = 100; // Delay in milliseconds between scroll updates
|
||||
|
||||
bool fetchPrintingStatus(Adafruit_SSD1306& display) {
|
||||
// Fetch printer statistics and update the display every octoprint_mtbs milliseconds
|
||||
if ((millis() - octoprint_lasttime > octoprint_mtbs || octoprint_lasttime == 0) && !is_display_off) {
|
||||
if (octoprint.getPrinterStatistics() && octoprint.getPrintJob()) {
|
||||
// Update last fetch time
|
||||
octoprint_lasttime = millis();
|
||||
} else {
|
||||
Serial.println("Failed to fetch printer statistics.");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// Regardless of whether the fetch was successful, continue with the display updates
|
||||
display.clearDisplay();
|
||||
display.setCursor(0, 0);
|
||||
display.setTextSize(1);
|
||||
display.setTextColor(WHITE);
|
||||
|
||||
// Display lines
|
||||
display.println(octoprint.printerStats.printerState);
|
||||
display.drawLine(0, 10, 127, 10, WHITE);
|
||||
display.drawLine(63, 10, 63, 22, WHITE);
|
||||
display.drawLine(0, 22, 127, 22, WHITE);
|
||||
|
||||
// Display nozzle and bed temperature
|
||||
display.drawBitmap(10, 13, bitmap_nozzle, 8, 8, WHITE);
|
||||
display.setCursor(21, 13);
|
||||
display.print(octoprint.printerStats.printerTool0TempActual);
|
||||
display.drawBitmap(74, 13, bitmap_bed, 8, 8, WHITE);
|
||||
display.setCursor(85, 13);
|
||||
display.print(octoprint.printerStats.printerBedTempActual);
|
||||
|
||||
// Estimated print time in HH:MM:SS format
|
||||
int estrunHours = octoprint.printJob.estimatedPrintTime / 3600;
|
||||
int estsecsRemaining = octoprint.printJob.estimatedPrintTime % 3600;
|
||||
int estrunMinutes = estsecsRemaining / 60;
|
||||
int estrunSeconds = estsecsRemaining % 60;
|
||||
char estbuf[31];
|
||||
sprintf(estbuf, "%02d:%02d:%02d", estrunHours, estrunMinutes, estrunSeconds);
|
||||
display.setCursor(80, 0);
|
||||
display.println(estbuf);
|
||||
|
||||
// Display and scroll the file name
|
||||
const char* fileName = octoprint.printJob.jobFileName.c_str(); // Convert to const char*
|
||||
int fileNameWidth = strlen(fileName) * 6; // Approximate width of the file name in pixels
|
||||
int displayWidth = 127;
|
||||
|
||||
// Clear the area for the file name to avoid overlap
|
||||
display.fillRect(0, 26, displayWidth, 11, BLACK);
|
||||
|
||||
// Check if it’s time to scroll based on the delay
|
||||
if(fileNameWidth > displayWidth) {
|
||||
if (millis() - lastScrollTime > scrollDelay) {
|
||||
// Update scroll position based on the current direction
|
||||
scrollPos += scrollDirection;
|
||||
lastScrollTime = millis();
|
||||
|
||||
// Reverse direction if the text reaches either edge
|
||||
if (scrollPos > 0) { // Reached the left edge
|
||||
scrollDirection = -1; // Start moving left
|
||||
} else if (scrollPos < -fileNameWidth + displayWidth) { // Reached the right edge
|
||||
scrollDirection = 1; // Start moving right
|
||||
}
|
||||
}
|
||||
}
|
||||
// Draw the file name with current scroll position
|
||||
display.setCursor(scrollPos, 27);
|
||||
display.print(fileName);
|
||||
|
||||
// Clear the area for "Time left" display before printing
|
||||
display.fillRect(0, 35, displayWidth, 9, BLACK); // Clear area for "Time left"
|
||||
// Print time left in HH:MM:SS
|
||||
int runHours = octoprint.printJob.progressPrintTimeLeft / 3600;
|
||||
int secsRemaining = octoprint.printJob.progressPrintTimeLeft % 3600;
|
||||
int runMinutes = secsRemaining / 60;
|
||||
int runSeconds = secsRemaining % 60;
|
||||
char buf[31];
|
||||
sprintf(buf, "Time left: %02d:%02d:%02d", runHours, runMinutes, runSeconds);
|
||||
display.setCursor(0, 42);
|
||||
display.println(buf);
|
||||
|
||||
// Progress bar and percentage
|
||||
const float temp_percent = floor(octoprint.printJob.progressCompletion * 100) / 100;
|
||||
int barY = 52; // Position just above the bottom of a 64-pixel-high display
|
||||
int barWidth = 127; // Full width of the display
|
||||
int barHeight = 12; // Set the height of the progress bar to 12 pixels
|
||||
int filledWidth = (temp_percent / 100.0) * barWidth;
|
||||
|
||||
// Draw the progress bar
|
||||
display.drawRect(0, barY, barWidth, barHeight, WHITE); // Outline of the bar
|
||||
display.fillRect(0, barY, filledWidth, barHeight, WHITE); // Filled portion
|
||||
|
||||
// Display centered percentage text in the progress bar
|
||||
char percentText[6];
|
||||
sprintf(percentText, "%.0f%%", temp_percent);
|
||||
int textWidth = strlen(percentText) * 6; // Approximate text width
|
||||
int textX = (barWidth - textWidth) / 2; // Center the text horizontally
|
||||
int textY = barY + (barHeight - 8) / 2; // Center the text vertically within the bar
|
||||
|
||||
// Clear the area for text and print centered percentage
|
||||
display.fillRect(textX - 1, textY -1, textWidth + 2, 10, BLACK);
|
||||
display.setCursor(textX, textY);
|
||||
display.print(percentText);
|
||||
display.drawLine(0, 38, 127, 38, WHITE);
|
||||
|
||||
display.display();
|
||||
|
||||
return true; // Return true to indicate the display was updated
|
||||
}
|
@ -44,3 +44,9 @@ void playTune(int tuneIndex) {
|
||||
noTone(PIN_BUZZER); // Stop the tone
|
||||
}
|
||||
}
|
||||
|
||||
void beep(int buzz) {
|
||||
tone(PIN_BUZZER, buzz, 100);
|
||||
delay(100);
|
||||
noTone(PIN_BUZZER);
|
||||
}
|
Reference in New Issue
Block a user