Initialisation depot
This commit is contained in:
9
src/DCC.cpp
Normal file
9
src/DCC.cpp
Normal file
@@ -0,0 +1,9 @@
|
||||
#include "DCC.h"
|
||||
|
||||
void setupDCC() {
|
||||
// Initialize DCC output pins and timers
|
||||
}
|
||||
|
||||
void sendDCCPacket(const uint8_t* packet, size_t length) {
|
||||
// Generate DCC signal using LM18200
|
||||
}
|
||||
5
src/DCC.h
Normal file
5
src/DCC.h
Normal file
@@ -0,0 +1,5 @@
|
||||
#pragma once
|
||||
#include <Arduino.h>
|
||||
|
||||
void setupDCC();
|
||||
void sendDCCPacket(const uint8_t* packet, size_t length);
|
||||
61
src/DisplayHelper.cpp
Normal file
61
src/DisplayHelper.cpp
Normal file
@@ -0,0 +1,61 @@
|
||||
#include "DisplayHelper.h"
|
||||
#include <Wire.h>
|
||||
#include <Adafruit_SSD1306.h>
|
||||
#include "main.h"
|
||||
|
||||
#define SCREEN_WIDTH 128
|
||||
#define SCREEN_HEIGHT 64
|
||||
#define OLED_RESET -1
|
||||
#define SCREEN_ADDRESS 0x3C // If not work please scan the bus
|
||||
#define OLED_SDA 14 // D6
|
||||
#define OLED_SCL 12 // D5
|
||||
|
||||
// Adafruit_SSD1306 *display;
|
||||
|
||||
|
||||
|
||||
|
||||
// Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
|
||||
|
||||
void displayInit() {
|
||||
display = new Adafruit_SSD1306(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
|
||||
Wire.begin(OLED_SDA, OLED_SCL);
|
||||
display->begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS);
|
||||
display->clearDisplay();
|
||||
display->setTextSize(1);
|
||||
display->setTextColor(SSD1306_WHITE);
|
||||
display->setCursor(0, 0);
|
||||
display->println("DCC Cmd Station");
|
||||
display->display();
|
||||
}
|
||||
|
||||
void displayShowIP(const String& ip) {
|
||||
display->clearDisplay();
|
||||
display->setCursor(0, 0);
|
||||
display->println("DCC Cmd Station");
|
||||
display->setCursor(0, 16);
|
||||
display->println("WiFi Connected!");
|
||||
display->setCursor(0, 32);
|
||||
display->print("IP: ");
|
||||
display->println(ip);
|
||||
display->display();
|
||||
delay(3000); // Show IP for 5 seconds before clearing
|
||||
display->clearDisplay();
|
||||
display->setCursor(0, 0);
|
||||
display->println("DCC Cmd Station");
|
||||
display->setCursor(0, 16);
|
||||
display->print("IP: ");
|
||||
display->println(ip);
|
||||
display->display();
|
||||
}
|
||||
|
||||
void displayShowPortal() {
|
||||
display->clearDisplay();
|
||||
display->setCursor(0, 0);
|
||||
display->println("DCC Cmd Station");
|
||||
display->setCursor(0, 16);
|
||||
display->println("Please Connect to");
|
||||
display->setCursor(0, 32);
|
||||
display->println("DCC-CommandStation");
|
||||
display->display();
|
||||
}
|
||||
9
src/DisplayHelper.h
Normal file
9
src/DisplayHelper.h
Normal file
@@ -0,0 +1,9 @@
|
||||
#pragma once
|
||||
#include <Arduino.h>
|
||||
#include <Adafruit_SSD1306.h>
|
||||
|
||||
// extern Adafruit_SSD1306 display;
|
||||
|
||||
void displayInit();
|
||||
void displayShowIP(const String& ip);
|
||||
void displayShowPortal();
|
||||
97
src/WiFiManagerHelper.cpp
Normal file
97
src/WiFiManagerHelper.cpp
Normal file
@@ -0,0 +1,97 @@
|
||||
|
||||
#include "WiFiManagerHelper.h"
|
||||
|
||||
|
||||
#include <ESP8266WiFi.h>
|
||||
#include <WiFiManager.h>
|
||||
#include <EEPROM.h>
|
||||
#include "DisplayHelper.h"
|
||||
#include "main.h"
|
||||
|
||||
#define EEPROM_SIZE 96
|
||||
#define SSID_ADDR 0
|
||||
#define PASS_ADDR 32
|
||||
#define MAX_CRED_LEN 32
|
||||
|
||||
|
||||
// Save WiFi credentials to EEPROM
|
||||
void saveCredentialsToEEPROM(const char* ssid, const char* pass) {
|
||||
EEPROM.begin(EEPROM_SIZE);
|
||||
for (int i = 0; i < MAX_CRED_LEN; i++) {
|
||||
EEPROM.write(SSID_ADDR + i, i < strlen(ssid) ? ssid[i] : 0);
|
||||
EEPROM.write(PASS_ADDR + i, i < strlen(pass) ? pass[i] : 0);
|
||||
}
|
||||
EEPROM.commit();
|
||||
EEPROM.end();
|
||||
}
|
||||
|
||||
// Load WiFi credentials from EEPROM
|
||||
void loadCredentialsFromEEPROM(char* ssid, char* pass) {
|
||||
EEPROM.begin(EEPROM_SIZE);
|
||||
for (int i = 0; i < MAX_CRED_LEN; i++) {
|
||||
ssid[i] = EEPROM.read(SSID_ADDR + i);
|
||||
pass[i] = EEPROM.read(PASS_ADDR + i);
|
||||
}
|
||||
ssid[MAX_CRED_LEN - 1] = 0;
|
||||
pass[MAX_CRED_LEN - 1] = 0;
|
||||
EEPROM.end();
|
||||
}
|
||||
|
||||
// Callback for WiFiManager to save credentials
|
||||
void saveWifiCallback() {
|
||||
Serial.println("[WiFiManager] Settings saved.");
|
||||
WiFiManager wifiManager;
|
||||
const char* ssid = WiFi.SSID().c_str();
|
||||
const char* pass = WiFi.psk().c_str();
|
||||
saveCredentialsToEEPROM(ssid, pass);
|
||||
}
|
||||
|
||||
|
||||
void setupWiFiManager() {
|
||||
char ssid[MAX_CRED_LEN] = {0};
|
||||
char pass[MAX_CRED_LEN] = {0};
|
||||
|
||||
loadCredentialsFromEEPROM(ssid, pass);
|
||||
// displayInit();
|
||||
|
||||
if (strlen(ssid) > 0) {
|
||||
Serial.print("[WiFiManager] Trying stored credentials: ");
|
||||
display->setCursor(0, 16);
|
||||
display->print("Connecting to Wifi ");
|
||||
// display->println(ssid);
|
||||
display->display();
|
||||
Serial.println(ssid);
|
||||
WiFi.begin(ssid, pass);
|
||||
unsigned long start = millis();
|
||||
while (WiFi.status() != WL_CONNECTED && millis() - start < 10000) {
|
||||
delay(500);
|
||||
Serial.print(".");
|
||||
}
|
||||
Serial.println();
|
||||
if (WiFi.status() == WL_CONNECTED) {
|
||||
Serial.print("[WiFiManager] Connected with stored credentials! IP: ");
|
||||
Serial.println(WiFi.localIP());
|
||||
displayShowIP(WiFi.localIP().toString());
|
||||
return;
|
||||
} else {
|
||||
Serial.println("[WiFiManager] Failed to connect with stored credentials.");
|
||||
}
|
||||
}
|
||||
|
||||
WiFiManager wifiManager;
|
||||
wifiManager.setSaveConfigCallback(saveWifiCallback);
|
||||
displayShowPortal();
|
||||
// Uncomment to reset saved settings for testing
|
||||
// wifiManager.resetSettings();
|
||||
if (!wifiManager.autoConnect("DCC-CommandStation")) {
|
||||
Serial.println("[WiFiManager] Failed to connect and hit timeout. Restarting...");
|
||||
delay(3000);
|
||||
ESP.restart();
|
||||
delay(5000);
|
||||
}
|
||||
// Save credentials after successful connection
|
||||
saveCredentialsToEEPROM(WiFi.SSID().c_str(), WiFi.psk().c_str());
|
||||
Serial.print("[WiFiManager] Connected! IP address: ");
|
||||
Serial.println(WiFi.localIP());
|
||||
// displayShowIP(WiFi.localIP().toString());
|
||||
}
|
||||
4
src/WiFiManagerHelper.h
Normal file
4
src/WiFiManagerHelper.h
Normal file
@@ -0,0 +1,4 @@
|
||||
#pragma once
|
||||
#include <Arduino.h>
|
||||
|
||||
void setupWiFiManager();
|
||||
13
src/XpressNet.cpp
Normal file
13
src/XpressNet.cpp
Normal file
@@ -0,0 +1,13 @@
|
||||
#include "XpressNet.h"
|
||||
|
||||
void setupXpressNet() {
|
||||
// Initialize XpressNet communication (e.g., Serial, pins)
|
||||
}
|
||||
|
||||
void handleXpressNet() {
|
||||
// Poll and process XpressNet messages
|
||||
}
|
||||
|
||||
void sendXpressNetMessage(const uint8_t* data, size_t length) {
|
||||
// Send a message to the XpressNet bus
|
||||
}
|
||||
6
src/XpressNet.h
Normal file
6
src/XpressNet.h
Normal file
@@ -0,0 +1,6 @@
|
||||
#pragma once
|
||||
#include <Arduino.h>
|
||||
|
||||
void setupXpressNet();
|
||||
void handleXpressNet();
|
||||
void sendXpressNetMessage(const uint8_t* data, size_t length);
|
||||
72
src/main.cpp
Normal file
72
src/main.cpp
Normal file
@@ -0,0 +1,72 @@
|
||||
#include <Arduino.h>
|
||||
#include <Adafruit_SSD1306.h>
|
||||
#include <Wire.h>
|
||||
#include "DisplayHelper.h"
|
||||
#include "WiFiManagerHelper.h"
|
||||
#include "main.h"
|
||||
#include <ESP8266WebServer.h>
|
||||
|
||||
#define SCREEN_WIDTH 128 // OLED display width, in pixels
|
||||
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
|
||||
#define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin)
|
||||
#define SCREEN_ADDRESS 0x3C // If not work please scan the bus
|
||||
#define OLED_SDA 14 // D6
|
||||
#define OLED_SCL 12 // D5
|
||||
|
||||
// Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
|
||||
Adafruit_SSD1306 *display;
|
||||
|
||||
// Mode for command station operation
|
||||
// Remove conflicting definition, use extern from main.h
|
||||
// Mode currentMode = MODE_OFF;
|
||||
|
||||
// Create web server instance on port 80
|
||||
ESP8266WebServer server(80);
|
||||
|
||||
// HTML content for welcome page
|
||||
const char* welcome_html = R"HTML(
|
||||
<!DOCTYPE html>
|
||||
<html lang=\"en\">
|
||||
<head>
|
||||
<meta charset=\"UTF-8\">
|
||||
<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">
|
||||
<title>Welcome to ESP8266 DCC Command Station</title>
|
||||
<style>
|
||||
body { font-family: Arial, sans-serif; background: #f4f4f4; color: #333; margin: 0; padding: 0; }
|
||||
.container { max-width: 600px; margin: 40px auto; background: #fff; border-radius: 8px; box-shadow: 0 2px 8px rgba(0,0,0,0.1); padding: 32px; }
|
||||
h1 { color: #0077cc; }
|
||||
p { font-size: 1.2em; }
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<div class=\"container\">
|
||||
<h1>Welcome!</h1>
|
||||
<p>This is your ESP8266 DCC Command Station.</p>
|
||||
<p>Configure WiFi, manage XpressNet, and control your layout from here.</p>
|
||||
<p>For documentation, see <a href=\"/xpressnet.md\">XpressNet Commands</a> and <a href=\"/README.md\">Hardware Setup</a>.</p>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
||||
)HTML";
|
||||
|
||||
void handleWelcome() {
|
||||
server.send(200, "text/html", welcome_html);
|
||||
}
|
||||
|
||||
void setup() {
|
||||
Serial.begin(115200);
|
||||
delay(100);
|
||||
Serial.println("[DEBUG] Serial started at 115200 baud");
|
||||
displayInit();
|
||||
Serial.println("[DEBUG] OLED display initialized");
|
||||
setupWiFiManager();
|
||||
server.on("/", handleWelcome);
|
||||
server.begin();
|
||||
Serial.println("[DEBUG] Web server started");
|
||||
}
|
||||
|
||||
void loop() {
|
||||
server.handleClient();
|
||||
delay(1000);
|
||||
}
|
||||
|
||||
33
src/main.h
Normal file
33
src/main.h
Normal file
@@ -0,0 +1,33 @@
|
||||
// expose display and other shared resources
|
||||
#pragma once
|
||||
|
||||
#include <Adafruit_SSD1306.h>
|
||||
|
||||
extern Adafruit_SSD1306 *display;
|
||||
extern int currentMode;
|
||||
|
||||
// static const uint8_t D0 = 16;
|
||||
// static const uint8_t D1 = 5;
|
||||
// static const uint8_t D2 = 4;
|
||||
// static const uint8_t D3 = 0;
|
||||
// static const uint8_t D4 = 2;
|
||||
// static const uint8_t D5 = 14;
|
||||
// static const uint8_t D6 = 12;
|
||||
// static const uint8_t D7 = 13;
|
||||
// static const uint8_t D8 = 15;
|
||||
// static const uint8_t D9 = 3;
|
||||
// static const uint8_t D10 = 1;
|
||||
|
||||
// LM18200 pin definitions
|
||||
#define PWM_PIN 5 // D1
|
||||
#define DIR_PIN 4 // D2
|
||||
#define BRAKE_PIN 0 // D3
|
||||
#define MARKLIN_RELAY_PIN 2 // D4 // Relay to swith setup (28V) to reverse polarity for Marklin relays
|
||||
|
||||
// Mode for command station operation
|
||||
enum Mode {
|
||||
MODE_OFF, // No operation
|
||||
MODE_DCC, // DCC command station mode
|
||||
MODE_ANA, // Analog command station mode (PWM)
|
||||
MODE_MAR // Märklin command station mode (PWM with setup for Marklin relays)
|
||||
};
|
||||
Reference in New Issue
Block a user