I am not surprised. As stated before i have a triple hwUART port working on a MIDI-merger, all 3 ports work, though also for UART0 i had to choose different pins, since the CH340g was disturbing the signal on the RX-pin.
I did however had to make a small modification to SerialMIDI.h
void begin()
{
// Initialise the Serial port
#if defined (ARDUINO_ARCH_ESP32) || defined(ESP32)
// easiest is to manually call begin() for an ESP32 and then include the RX & TX pins
// Serial1 default pins can not be used, but i use rx=5 & tx=18
// Serial defaultpins are connected to USB, RX can not be used, may as well just use rx=15 & tx=4
// Serial2 default pins are fine to use, but may as well be declared rx=16 & tx=17
#elif defined(AVR_CAKE)
mSerial. template open<Settings::BaudRate>();
#else
mSerial.begin(Settings::BaudRate);
#endif
}
Basically preventing begin() being called on the selected Serial object if the board is an ESP32.
So that can be done manually before hand and not be re-done when the MIDI library call is made.
#include <WiFi.h>
#include <WiFiClient.h>
#include <WebServer.h>
#include <ESPmDNS.h>
#include <WiFiUdp.h>
#include <ArduinoOTA.h>
#include <MIDI.h>
#include <PatchDNSServer.h>
#define LED 2
#define DEBUG_SERIAL 0
#define DNS_PORT 53
#define FORMAT SERIAL_8N1
#define BAUD 31250
#define RXD2 16
#define TXD2 17
#define RXD0 15
#define TXD0 4
#define RXD1 5
#define TXD1 18
MIDI_CREATE_INSTANCE(HardwareSerial, Serial, midiA);
MIDI_CREATE_INSTANCE(HardwareSerial, Serial1, midiB);
MIDI_CREATE_INSTANCE(HardwareSerial, Serial2, midiC);
decltype(&midiA) ports[] = {&midiA, &midiB, &midiC };
decltype(&Serial) uarts[] = {&Serial, &Serial1, &Serial2};
int RXpins[] = {RXD0, RXD1, RXD2};
int TXpins[] = {TXD0, TXD1, TXD2};
uint8_t nrPorts = 3;
WebServer server(80);
DNSServer dnsServer;
const char* ssid = "LAB";
const char* password = "Page22Air";
const char *apname = "Midi-Merger";
const char *appass = "Evolution";
const char // like this these lines are statically declared and const, so we can't change them at all
*pageheader = "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\"\"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">",
*htmlhead = "<html><head><title>Midi-Merger</title><meta http-equiv=\"Content-Type\" content=\"text/html;charset=utf-8\" ></head>",
*bodystyle = "<body style=\"color: wheat; background-color: teal; font-size: 12pt; font-family: sans-serif;\">",
*accessIP = "http://192.168.4.1",
*htmlclose = "</body></html>";
String webIP = "/";
void setup() {
pinMode(LED, OUTPUT);
digitalWrite(LED, LOW);
WiFiSetup();
OTAUpdateSetup();
for (uint8_t i = 0; i < nrPorts; i++) {
uarts[i]->begin(BAUD, FORMAT, RXpins[i], TXpins[i]); // call Serial begin
ports[i]->begin(MIDI_CHANNEL_OMNI); // call midi begin
}
}
void loop() {
ArduinoOTA.handle();
server.handleClient();
dnsServer.processNextRequest();
MiDiHandle();
Blink();
}
void Blink() {
if ((millis() % 1000) / 500) {
digitalWrite(LED, LOW);
}
else {
digitalWrite(LED, HIGH);
}
}
void MiDiHandle() {
for (uint8_t in = 0; in < nrPorts; in++) {
if (ports[in]->read()) {
for (uint8_t out = 0; out < nrPorts; out++) {
if (ports[in]->getType() == midi::Clock) {
ports[out]->sendRealTime(midi::Clock);
}
else if (ports[in]->getType() == midi::Start) {
ports[out]->sendRealTime(midi::Start);
}
else if (ports[in]->getType() == midi::Stop) {
ports[out]->sendRealTime(midi::Stop);
}
else if (ports[in]->getType() == midi::Continue) {
ports[out]->sendRealTime(midi::Continue);
}
else {
ports[out]->send(ports[in]->getType(),
ports[in]->getData1(),
ports[in]->getData2(),
ports[in]->getChannel());
}
}
}
}
}
void handleRoot() {
String s = "";
s += pageheader;
s += htmlhead;
s += bodystyle;
s += "<h1>Welcome to the Triple Midi Merger</h1>";
s += "<pre>This unit has 3 input and 3 output ports, and you can<br>";
s += "configure how the data from the input ports gets relayed to<br>";
s += "the indivdual outputs.<br>";
s += "By default, all input gets relayed to all outputs</pre>";
s += htmlclose;
yield();
server.send(200, "text/html", s); //Send web page
}
void OTAUpdateSetup() {
ArduinoOTA.setHostname("MiDiMerger");
ArduinoOTA.setPassword("admin");
ArduinoOTA
.onStart([]() {
String type;
if (ArduinoOTA.getCommand() == U_FLASH)
type = "sketch";
else // U_SPIFFS
type = "filesystem";
// NOTE: if updating SPIFFS this would be the place to unmount SPIFFS using SPIFFS.end()
if (DEBUG_SERIAL) Serial.println("Start updating " + type);
})
.onEnd([]() {
if (DEBUG_SERIAL) Serial.println("\nEnd");
})
.onProgress([](unsigned int progress, unsigned int total) {
if (DEBUG_SERIAL) Serial.printf("Progress: %u%%\r", (progress / (total / 100)));
})
.onError([](ota_error_t error) {
if (DEBUG_SERIAL) Serial.printf("Error[%u]: ", error);
if (error == OTA_AUTH_ERROR) if (DEBUG_SERIAL) Serial.println("Auth Failed");
else if (error == OTA_BEGIN_ERROR) if (DEBUG_SERIAL) Serial.println("Begin Failed");
else if (error == OTA_CONNECT_ERROR) if (DEBUG_SERIAL) Serial.println("Connect Failed");
else if (error == OTA_RECEIVE_ERROR) if (DEBUG_SERIAL) Serial.println("Receive Failed");
else if (error == OTA_END_ERROR) if (DEBUG_SERIAL) Serial.println("End Failed");
});
ArduinoOTA.begin();
}
void WiFiSetup() {
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
}
if (WiFi.status() != WL_CONNECTED) {
delay(5000);
ESP.restart();
}
if (!MDNS.begin("Evolution")) {
while (1) {
delay(1000);
}
}
dnsServer.start(DNS_PORT, "lab.com", IPAddress(192, 168, 4, 1));
server.on("/", handleRoot);
server.begin();
// Add service to MDNS-SD
MDNS.addService("http", "tcp", 80);
}