Add ethernet/wifi switch

This commit is contained in:
Patrick Schwarz 2024-11-02 11:59:10 +01:00
parent 13055b4b32
commit 3ce1df2136
2 changed files with 96 additions and 13 deletions

View file

@ -15,4 +15,5 @@ framework = arduino
lib_deps = lib_deps =
hideakitai/ArtNet @ ^0.8.0 hideakitai/ArtNet @ ^0.8.0
bblanchon/ArduinoJson @ ^7.2.0 bblanchon/ArduinoJson @ ^7.2.0
me-no-dev/ESP Async WebServer me-no-dev/ESP Async WebServer@^1.2.4
arduino-libraries/Ethernet @ ^2.0.2

View file

@ -1,8 +1,17 @@
#include <ArtnetWiFi.h> #include <ArtnetWiFi.h>
// #include <ArtnetEther.h> #include <ArtnetEther.h>
#include <ArduinoJson.h> #include <ArduinoJson.h>
#include "ESPDMX.h" #include "ESPDMX.h"
#include "SPI.h"
#ifdef ESP32
#include <WiFi.h>
#include <AsyncTCP.h>
#elif defined(ESP8266)
#include <ESP8266WiFi.h>
#include <ESPAsyncTCP.h>
#endif
#include <ESPAsyncWebServer.h> #include <ESPAsyncWebServer.h>
#include <SPIFFS.h> #include <SPIFFS.h>
#include <Preferences.h> #include <Preferences.h>
@ -10,6 +19,15 @@
Preferences config; Preferences config;
DMXESPSerial dmx; DMXESPSerial dmx;
// Ethernet stuff
#define SCK 39
#define SS 33
#define MOSI 35
#define MISO 37
#define SPI_FREQ 32000000
byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
enum ethTypes {AP = 1, STA = 2, ETH = 3}; // to be changed to Raffaels(TM) enum
AsyncWebServer server(80); AsyncWebServer server(80);
ArtnetWiFi artnet; ArtnetWiFi artnet;
@ -18,7 +36,11 @@ uint8_t data[size];
void setup() void setup()
{ {
// Serial console
Serial.begin(9600); Serial.begin(9600);
Serial.print("Start DMX-Interface");
delay(1000);
Serial.println("...");
config.begin("dmx", false); config.begin("dmx", false);
@ -35,22 +57,82 @@ void setup()
const IPAddress gateway(192, 168, 1, 1); const IPAddress gateway(192, 168, 1, 1);
const IPAddress subnet(255, 255, 255, 0); const IPAddress subnet(255, 255, 255, 0);
// WiFi stuff
// WiFi.begin(ssid, pwd); // Initialize Interface connection type - to be changed to Raffaels(TM) enum
WiFi.softAP(ssid, pwd); ethTypes ethType;
WiFi.softAPConfig(ip, gateway, subnet); ethType = ETH;
// WiFi.config(ip, gateway, subnet);
// while (WiFi.status() != WL_CONNECTED) { switch (ethType)
// Serial.print("."); {
case STA:
Serial.println("Initialize as WiFi-STA");
WiFi.begin(ssid, pwd);
WiFi.config(ip, gateway, subnet);
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(500);
}
Serial.print("WiFi connected, IP = ");
Serial.println(WiFi.localIP());
break;
case ETH:
Serial.println("Initialize as ETH");
WiFi.mode(WIFI_STA); // Trotzdem wegen WebServer
SPI.begin(SCK, MISO, MOSI, SS);
SPI.setFrequency(SPI_FREQ);
Ethernet.init(SS);
delay(1000);
if (Ethernet.begin(mac)) { // Dynamic IP setup
Serial.println("DHCP OK!");
}else{
Serial.println("Failed to configure Ethernet using DHCP");
// Check for Ethernet hardware present
if (Ethernet.hardwareStatus() == EthernetNoHardware) {
Serial.println("Ethernet shield was not found. Sorry, can't run without hardware. :(");
while (true) {
delay(1); // do nothing, no point running without Ethernet hardware
}
}
if (Ethernet.linkStatus() == LinkOFF) {
Serial.println("Ethernet cable is not connected.");
}
Ethernet.begin(mac, ip, gateway, gateway, subnet);
Serial.println("STATIC OK!");
}
Serial.print("Local IP : ");
Serial.println(Ethernet.localIP());
Serial.print("Subnet Mask : ");
Serial.println(Ethernet.subnetMask());
Serial.print("Gateway IP : ");
Serial.println(Ethernet.gatewayIP());
Serial.print("DNS Server : ");
Serial.println(Ethernet.dnsServerIP());
Serial.println("Ethernet Successfully Initialized");
break;
default:
Serial.println("Initialize as WiFi-AP");
WiFi.softAP(ssid, pwd);
WiFi.softAPConfig(ip, gateway, subnet);
Serial.print("WiFi AP enabled, IP = ");
Serial.println(WiFi.localIP());
break;
}
delay(500); delay(500);
//}
// Serial.print("WiFi connected, IP = ");
// Serial.println(WiFi.localIP());
// Initialize Art-Net // Initialize Art-Net
Serial.println("Initialize Art-Net...");
artnet.begin(); artnet.begin();
// Initialize DMX ports // Initialize DMX ports
Serial.println("Initialize DMX...");
dmx.init(); dmx.init();
// if Artnet packet comes to this universe, this function is called // if Artnet packet comes to this universe, this function is called