Merge branch 'DMX-5-switch-connection' into 8-ethernet

This commit is contained in:
Hendrik Rauh 2024-11-15 17:49:36 +01:00 committed by GitHub
commit 50c37dc80a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
14 changed files with 649 additions and 221 deletions

View file

@ -1,94 +1,71 @@
// - - - - -
// ESPDMX - A Arduino library for sending and receiving DMX using the builtin serial hardware port.
// ESPDMX.cpp: Library implementation file
//
// Copyright (C) 2015 Rick <ricardogg95@gmail.com>
// This work is licensed under a GNU style license.
//
// Last change: Marcel Seerig <https://github.com/mseerig>
//
// Documentation and samples are available at https://github.com/Rickgg/ESP-Dmx
// - - - - -
/* ----- LIBRARIES ----- */
#include <Arduino.h>
#include "ESPDMX.h"
#define DMXSPEED 250000
#define DMXFORMAT SERIAL_8N2
#define BREAKSPEED 83333
#define BREAKFORMAT SERIAL_8N1
#define SERIALPORT Serial0
#define DMXCHANNELS 512
bool dmxStarted = false;
int sendPin = 33;
int receivePin = -1;
// DMX value array and size. Entry 0 will hold startbyte, so we need 512+1 elements
uint8_t dmxDataStore[DMXCHANNELS + 1] = {};
// std::vector<uint8_t[DMXCHANNELS + 1]> dmxDataStores(MAX_IDS);
// uint8_t dmxDataStores[MAX_IDS][DMXCHANNELS + 1];
// Set up the DMX-Protocol
void DMXESPSerial::init()
void DMXESPSerial::init(int pinSend = 19, int pinRecv = -1)
{
SERIALPORT.begin(DMXSPEED, DMXFORMAT, receivePin, sendPin);
sendPin = pinSend;
recvPin = pinRecv;
SERIALPORT.begin(DMXSPEED, DMXFORMAT, recvPin, sendPin);
pinMode(sendPin, OUTPUT);
dmxStarted = true;
}
// Function to read DMX data
uint8_t DMXESPSerial::read(int Channel)
uint8_t DMXESPSerial::read(int channel)
{
if (dmxStarted == false)
init();
if (Channel < 1)
Channel = 1;
if (Channel > DMXCHANNELS)
Channel = DMXCHANNELS;
return (dmxDataStore[Channel]);
if (channel < 1)
channel = 1;
if (channel > DMXCHANNELS)
channel = DMXCHANNELS;
return (dmxDataStore[channel]);
}
// Function to send DMX data
void DMXESPSerial::write(int Channel, uint8_t value)
void DMXESPSerial::write(int channel, uint8_t value)
{
if (dmxStarted == false)
init();
if (Channel < 1)
Channel = 1;
if (Channel > DMXCHANNELS)
Channel = DMXCHANNELS;
if (channel < 1)
channel = 1;
if (channel > DMXCHANNELS)
channel = DMXCHANNELS;
if (value < 0)
value = 0;
if (value > 255)
value = 255;
dmxDataStore[Channel] = value;
dmxDataStore[channel] = value;
}
void DMXESPSerial::end()
{
SERIALPORT.end();
dmxStarted = false;
}
// Function to update the DMX bus
void DMXESPSerial::update()
{
if (dmxStarted == false)
init();
// Send break
digitalWrite(sendPin, HIGH);
SERIALPORT.begin(BREAKSPEED, BREAKFORMAT, receivePin, sendPin);
SERIALPORT.begin(BREAKSPEED, BREAKFORMAT, recvPin, sendPin);
SERIALPORT.write(0);
SERIALPORT.flush();
delay(1);
SERIALPORT.end();
// send data
SERIALPORT.begin(DMXSPEED, DMXFORMAT, receivePin, sendPin);
SERIALPORT.begin(DMXSPEED, DMXFORMAT, recvPin, sendPin);
digitalWrite(sendPin, LOW);
SERIALPORT.write(dmxDataStore, DMXCHANNELS);
SERIALPORT.flush();

View file

@ -1,29 +1,28 @@
// - - - - -
// ESPDMX - A Arduino library for sending and receiving DMX using the builtin serial hardware port.
// ESPDMX.cpp: Library implementation file
//
// Copyright (C) 2015 Rick <ricardogg95@gmail.com>
// This work is licensed under a GNU style license.
//
// Last change: Marcel Seerig <https://github.com/mseerig>
//
// Documentation and samples are available at https://github.com/Rickgg/ESP-Dmx
// - - - - -
#include <inttypes.h>
#ifndef ESPDMX_h
#define ESPDMX_h
// ---- Methods ----
#define DMXSPEED 250000
#define DMXFORMAT SERIAL_8N2
#define BREAKSPEED 83333
#define BREAKFORMAT SERIAL_8N1
#define SERIALPORT Serial0
#define DMXCHANNELS 512
class DMXESPSerial {
public:
void init();
uint8_t read(int Channel);
void write(int channel, uint8_t value);void update();
void end();
class DMXESPSerial
{
public:
int sendPin;
int recvPin;
bool dmxStarted;
uint8_t dmxDataStore[DMXCHANNELS + 1];
void init(int pinSend, int pinRecv);
uint8_t read(int Channel);
void write(int channel, uint8_t value);
void update();
void end();
};
#endif

View file

@ -10,17 +10,17 @@
//#include "w5500/esp32_w5500.h"
//#include <ESPAsyncWebServer.h>
#include <ArtnetWiFi.h>
#include <ArduinoJson.h>
#include "ESPDMX.h"
#include "SPI.h"
#include <SPIFFS.h>
#include <Preferences.h>
#include "routes/config.h"
Preferences config;
DMXESPSerial dmx;
DMXESPSerial dmx1;
DMXESPSerial dmx2;
// Button
#define PIN_LED 7
@ -96,9 +96,16 @@ void setup()
delay(1000);
Serial.println("...");
config.begin("dmx", false);
config.begin("dmx", true);
uint8_t universe = config.getUChar("universe", 1);
uint8_t universe1 = config.getUChar("universe-1", 1);
uint8_t universe2 = config.getUChar("universe-2", 1);
Direction direction1 = static_cast<Direction>(config.getUInt("direction-1", 0));
Direction direction2 = static_cast<Direction>(config.getUInt("direction-2", 1));
Connection connection = static_cast<Connection>(config.getUInt("connection", WiFiAP));
IpMethod ipMethod = static_cast<IpMethod>(config.getUInt("ip-method"), Static);
WiFi.macAddress(mac);
char hostname[30];
@ -111,84 +118,113 @@ void setup()
IPAddress defaultIp(2, mac[3], mac[4], mac[5]);
IPAddress ip = config.getUInt("ip", defaultIp);
IPAddress defaultSubnet(255, 255, 255, 0);
IPAddress subnet = config.getUInt("subnet", defaultSubnet);
IPAddress defaultGateway(192, 168, 1, 1);
IPAddress gateway = config.getUInt("gateway", defaultGateway);
IPAddress cidr = config.getUChar("cidr", 24);
config.end();
// TODO: \/ Herleiten \/ @psxde
const IPAddress gateway(2, 0, 0, 1);
const IPAddress subnet(255, 0, 0, 0);
// wait for serial monitor
delay(5000);
// TODO: Initialize Interface connection type - to be changed to Raffaels(TM) enum
ethType = TYP_ETH;
// Button
pinMode(PIN_BUTTON,INPUT_PULLUP);
if(digitalRead(PIN_BUTTON) == LOW){
ledBlink(100);
delay(2000);
Serial.println("Start AP-Mode");
ethType = TYP_AP;
}
switch (ethType)
switch (connection)
{
case TYP_STA:
Serial.println("Initialize as WiFi-STA");
case WiFiSta:
Serial.println("Initialize as WiFi-Station");
WiFi.begin(ssid, pwd);
WiFi.setHostname(hostname);
WiFi.config(ip, gateway, subnet);
while (WiFi.status() != WL_CONNECTED) {
if (ipMethod == Static)
{
WiFi.config(ip, gateway, subnet);
}
while (WiFi.status() != WL_CONNECTED)
{
Serial.print(".");
delay(500);
}
Serial.println("");
Serial.print("WiFi connected, IP = ");
Serial.println(WiFi.localIP());
Serial.print("MAC address: ");
Serial.println(WiFi.macAddress());
break;
case TYP_ETH:{
Serial.println("Initialize as ETH");
ESP32_W5500_onEvent();
if (ETH.begin( ETH_MISO, ETH_MOSI, ETH_SCK, ETH_SS, ETH_INT, ETH_SPI_CLOCK_MHZ, ETH_SPI_HOST, mac )) { // Dynamic IP setup
}else{
Serial.println("Failed to configure Ethernet");
}
ETH.setHostname(hostname);
//ESP32_W5500_waitForConnect();
uint8_t timeout = 5; // in s
Serial.print("Wait for connect");
while (!ESP32_W5500_eth_connected && timeout > 0) {
delay(1000);
timeout--;
Serial.print(".");
}
Serial.println();
if (ESP32_W5500_eth_connected) {
Serial.println("DHCP OK!");
} else {
Serial.println("Set static IP");
ETH.config(ip, gateway, subnet);
}
Serial.print("Local IP : ");
Serial.println(ETH.localIP());
Serial.print("Subnet Mask : ");
Serial.println(ETH.subnetMask());
Serial.print("Gateway IP : ");
Serial.println(ETH.gatewayIP());
Serial.print("DNS Server : ");
Serial.println(ETH.dnsIP());
Serial.print("MAC address : ");
Serial.println(ETH.macAddress());
Serial.println("Ethernet Successfully Initialized");
case WiFiAP:
Serial.println("Initialize as WiFi-Access-Point");
WiFi.softAP(ssid, pwd);
WiFi.softAPConfig(ip, gateway, subnet);
Serial.print("WiFi AP enabled, IP = ");
Serial.println(WiFi.softAPIP());
break;
case Ethernet:
// TODO: Initialize Interface connection type - to be changed to Raffaels(TM) enum
ethType = TYP_ETH;
// Button
pinMode(PIN_BUTTON,INPUT_PULLUP);
if(digitalRead(PIN_BUTTON) == LOW){
ledBlink(100);
delay(2000);
Serial.println("Start AP-Mode");
ethType = TYP_AP;
}
switch (ethType)
{
case TYP_STA:
Serial.println("Initialize as WiFi-STA");
WiFi.begin(ssid, pwd);
WiFi.setHostname(hostname);
WiFi.config(ip, gateway, subnet);
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(500);
}
Serial.print("WiFi connected, IP = ");
Serial.println(WiFi.localIP());
Serial.print("MAC address: ");
Serial.println(WiFi.macAddress());
break;
case TYP_ETH:{
Serial.println("Initialize as ETH");
ESP32_W5500_onEvent();
if (ETH.begin( ETH_MISO, ETH_MOSI, ETH_SCK, ETH_SS, ETH_INT, ETH_SPI_CLOCK_MHZ, ETH_SPI_HOST, mac )) { // Dynamic IP setup
}else{
Serial.println("Failed to configure Ethernet");
}
ETH.setHostname(hostname);
//ESP32_W5500_waitForConnect();
uint8_t timeout = 5; // in s
Serial.print("Wait for connect");
while (!ESP32_W5500_eth_connected && timeout > 0) {
delay(1000);
timeout--;
Serial.print(".");
}
Serial.println();
if (ESP32_W5500_eth_connected) {
Serial.println("DHCP OK!");
} else {
Serial.println("Set static IP");
ETH.config(ip, gateway, subnet);
}
Serial.print("Local IP : ");
Serial.println(ETH.localIP());
Serial.print("Subnet Mask : ");
Serial.println(ETH.subnetMask());
Serial.print("Gateway IP : ");
Serial.println(ETH.gatewayIP());
Serial.print("DNS Server : ");
Serial.println(ETH.dnsIP());
Serial.print("MAC address : ");
Serial.println(ETH.macAddress());
Serial.println("Ethernet Successfully Initialized");
break;
}
default:
Serial.println("Initialize as WiFi-AP");
@ -202,12 +238,11 @@ void setup()
break;
}
delay(500);
}
// Initialize DMX ports
Serial.println("Initialize DMX...");
dmx.init();
dmx1.init(19, -1);
// Initialize Art-Net
Serial.println("Initialize Art-Net...");
@ -218,12 +253,13 @@ void setup()
{
for (size_t i = 0; i < size; ++i)
{
dmx.write((i + 1), data[i]);
dmx1.write((i + 1), data[i]);
}
dmx.update();
dmx1.update();
});
// if Artnet packet comes, this function is called to every universe
artnet.subscribeArtDmx([&](const uint8_t *data, uint16_t size, const ArtDmxMetadata &metadata, const ArtNetRemoteInfo &remote) {});
@ -235,28 +271,24 @@ void setup()
server.serveStatic("/", SPIFFS, "/").setDefaultFile("index.html");
server.on("/config", HTTP_GET, [&, defaultIp, ssid, pwd, universe](AsyncWebServerRequest *request)
server.on("/config", HTTP_GET, [](AsyncWebServerRequest *request)
{ onGetConfig(request); });
server.on("/config", HTTP_DELETE, [](AsyncWebServerRequest *request)
{
JsonDocument doc;
config.begin("dmx", false);
config.clear();
config.end();
// respond with default config
onGetConfig(request);
doc["ssid"] = ssid;
doc["pwd"] = pwd;
doc["ip"] = defaultIp;
doc["universe"] = universe;
String jsonString;
serializeJson(doc, jsonString);
request->send(200, "application/json", jsonString); });
ESP.restart(); });
server.onRequestBody([](AsyncWebServerRequest *request, uint8_t *data, size_t len, size_t index, size_t total)
{
if (request->url() == "/config" && request->method() == HTTP_PUT) {
Serial.printf("[REQUEST]\t%s\r\n", (const char *)data);
StaticJsonDocument<256> doc;
deserializeJson(doc, data);
request->send(200);
onPutConfig(request, data, len, index, total);
ESP.restart();
} });
delay(1000);

163
src/routes/config.cpp Normal file
View file

@ -0,0 +1,163 @@
#include "config.h"
#include <stdexcept>
#include <ArduinoJson.h>
Preferences config;
#pragma region Utility
uint32_t parseIp(String str)
{
const int size = 4;
String ipStrings[size];
uint8_t ipIndex = 0;
for (int i = 0; i < str.length(); i++)
{
if (str[i] == '.')
{
ipIndex++;
continue;
}
ipStrings[ipIndex] += str[i];
}
String ip = "";
for (int i = 0; i < size; i++)
{
String paddedString = ipStrings[i];
while (paddedString.length() < 3)
{
paddedString = "0" + paddedString;
}
ip.concat(paddedString);
}
Serial.println("ip string: " + ip);
return atoi(ip.c_str());
}
IpMethod parseIpMethod(uint8_t ipMethod)
{
if (ipMethod > 0 || ipMethod < IP_METHOD_SIZE)
{
return static_cast<IpMethod>(ipMethod);
}
throw ::std::invalid_argument("Invalid IP method value" + ipMethod);
}
Connection parseConnection(uint8_t connection)
{
if (connection > 0 || connection < CONNECTION_SIZE)
{
return static_cast<Connection>(connection);
}
throw ::std::invalid_argument("Invalid connection value: " + connection);
}
Direction parseDirection(uint8_t direction)
{
if (direction > 0 || direction < DIRECTION_SIZE)
{
return static_cast<Direction>(direction);
}
throw ::std::invalid_argument("Invalid direction value: " + direction);
}
#pragma endregion
void onGetConfig(AsyncWebServerRequest *request)
{
config.begin("dmx", true);
IPAddress defaultIp(192, 168, 1, 201);
IPAddress ip = config.getUInt("ip", defaultIp);
IPAddress defaultSubnet(255, 255, 255, 0);
IPAddress subnet = config.getUInt("subnet", defaultSubnet);
IPAddress defaultGateway(192, 168, 1, 1);
IPAddress gateway = config.getUInt("gateway", defaultGateway);
JsonDocument doc;
doc["connection"] = config.getUInt("connection", WiFiSta);
doc["ssid"] = config.getString("ssid", "artnet");
doc["password"] = config.getString("password", "mbgmbgmbg");
doc["ip-method"] = config.getUInt("ip-method"), Static;
doc["ip"] = ip.toString();
doc["subnet"] = subnet.toString();
doc["gateway"] = gateway.toString();
doc["universe-1"] = config.getUInt("universe-1", 1);
doc["direction-1"] = config.getUInt("direction-1", Output);
doc["universe-2"] = config.getUInt("universe-2", 1);
doc["direction-2"] = config.getUInt("direction-2", Input);
config.end();
String jsonString;
serializeJson(doc, jsonString);
request->send(200, "application/json", jsonString);
}
void onPutConfig(AsyncWebServerRequest *request, uint8_t *data, size_t len, size_t index, size_t total)
{
Serial.printf("[REQUEST]\t%s\r\n", (const char *)data);
JsonDocument doc;
deserializeJson(doc, data);
try
{
config.begin("dmx", false);
IpMethod ipMethod = parseIpMethod(doc["ip-method"].as<uint8_t>());
config.putUInt("ip-method", ipMethod);
if (ipMethod == Static)
{
IPAddress ipAddress;
ipAddress.fromString(doc["ip"].as<String>());
config.putUInt("ip", ipAddress);
IPAddress subnet;
subnet.fromString(doc["subnet"].as<String>());
config.putUInt("subnet", subnet);
IPAddress gateway;
gateway.fromString(doc["gateway"].as<String>());
config.putUInt("gateway", gateway);
}
Connection connection = parseConnection(doc["connection"].as<uint8_t>());
config.putUInt("connection", connection);
if (connection == WiFiSta || connection == WiFiAP)
{
config.putString("ssid", doc["ssid"].as<String>());
config.putString("password", doc["password"].as<String>());
}
Direction direction1 = parseDirection(doc["direction-1"].as<uint8_t>());
config.putUInt("direction-1", direction1);
Direction direction2 = parseDirection(doc["direction-2"].as<uint8_t>());
config.putUInt("direction-2", direction2);
config.putUInt("universe-1", doc["universe-1"]);
config.putUInt("universe-2", doc["universe-2"]);
config.end();
request->send(200);
}
catch (::std::invalid_argument &e)
{
config.end();
request->send(400, "text/plain", e.what());
}
}

38
src/routes/config.h Normal file
View file

@ -0,0 +1,38 @@
#pragma once
#include <ESPAsyncWebServer.h>
#include <ESPDMX.h>
#include <Preferences.h>
// #ifndef CONFIG_h
// #define CONFIG_h
extern Preferences config;
enum IpMethod
{
Static,
DHCP
};
const uint8_t IP_METHOD_SIZE = 2;
enum Connection
{
WiFiSta,
WiFiAP,
Ethernet
};
const uint8_t CONNECTION_SIZE = 3;
enum Direction
{
Output,
Input
};
const uint8_t DIRECTION_SIZE = 2;
void onGetConfig(AsyncWebServerRequest *request);
void onPutConfig(AsyncWebServerRequest *request, uint8_t *data, size_t len, size_t index, size_t total);
// #endif