diff --git a/platformio.ini b/platformio.ini index a372945..1c6f819 100644 --- a/platformio.ini +++ b/platformio.ini @@ -15,4 +15,5 @@ framework = arduino lib_deps = hideakitai/ArtNet @ ^0.8.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 diff --git a/src/main.cpp b/src/main.cpp index 7019a79..f73da4e 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,8 +1,17 @@ #include -// #include - +#include #include #include "ESPDMX.h" +#include "SPI.h" + +#ifdef ESP32 +#include +#include +#elif defined(ESP8266) +#include +#include +#endif + #include #include #include @@ -10,6 +19,15 @@ Preferences config; 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); ArtnetWiFi artnet; @@ -18,7 +36,11 @@ uint8_t data[size]; void setup() { + // Serial console Serial.begin(9600); + Serial.print("Start DMX-Interface"); + delay(1000); + Serial.println("..."); config.begin("dmx", false); @@ -35,22 +57,82 @@ void setup() const IPAddress gateway(192, 168, 1, 1); const IPAddress subnet(255, 255, 255, 0); - // WiFi stuff - // WiFi.begin(ssid, pwd); - WiFi.softAP(ssid, pwd); - WiFi.softAPConfig(ip, gateway, subnet); - // WiFi.config(ip, gateway, subnet); - // while (WiFi.status() != WL_CONNECTED) { - // Serial.print("."); + + // Initialize Interface connection type - to be changed to Raffaels(TM) enum + ethTypes ethType; + ethType = ETH; + + switch (ethType) + { + 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); - //} - // Serial.print("WiFi connected, IP = "); - // Serial.println(WiFi.localIP()); + // Initialize Art-Net + Serial.println("Initialize Art-Net..."); artnet.begin(); // Initialize DMX ports + Serial.println("Initialize DMX..."); dmx.init(); // if Artnet packet comes to this universe, this function is called