ESP32 - Serial1 Midi port causes crash

As in the topic. I was just trying to make a simple midi-dual-merger and then i thought that the ESP32 has 3 hwSerial ports, every time i call midi.begin() on UART1 the esp crashes. Anybody know why ?
the code.(sorry for the mess and commented stuff.)

#include <WiFi.h>
#include <ESPmDNS.h>
#include <WiFiUdp.h>
#include <ArduinoOTA.h>
#include <MIDI.h>

#define LED 2
#define DEBUG_SERIAL 0
#define RXD1 12
#define TXD1 14

// This example shows how to create two instances of the library to create a merger.
// There are two MIDI couples of IO, A and B, each using thru and merging with the
// input from the other node. The result is the following:
// A out = A in + B in
// B out = B in + A in

struct Serial1MIDISettings : public midi::DefaultSettings
{
static const long BaudRate = 31250;
static const int8_t TxPin = TXD1;
static const int8_t RxPin = RXD1;
};

MIDI_CREATE_INSTANCE(HardwareSerial, Serial,     midiA);
MIDI_CREATE_CUSTOM_INSTANCE(HardwareSerial, Serial1, midiB, Serial1MIDISettings);
MIDI_CREATE_INSTANCE(HardwareSerial, Serial2,    midiC);

WiFiServer server(80);

const char* ssid = "LAB";
const char* password = "Page22Air";

void setup() {
  pinMode(LED, OUTPUT);
  digitalWrite(LED, LOW);
  if (DEBUG_SERIAL) {
    Serial.begin(250000);
    Serial.println();
    Serial.println("Starting Midi-Merger");
  }

  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    if (DEBUG_SERIAL) Serial.print(".");
    delay(500);
  }

  if (WiFi.status() != WL_CONNECTED) {
    if (DEBUG_SERIAL) Serial.println("Connection Failed! Rebooting...");
    delay(5000);
    ESP.restart();
  }
  ArduinoOTA.setHostname("MiDiTriple");
  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()
    //Serial.println("Start updating " + type);
  })
  .onEnd([]() {
    //Serial.println("\nEnd");
  })
  .onProgress([](unsigned int progress, unsigned int total) {
    //Serial.printf("Progress: %u%%\r", (progress / (total / 100)));
  })
  .onError([](ota_error_t error) {
    //Serial.printf("Error[%u]: ", error);
    if (error == OTA_AUTH_ERROR) ;// Serial.println("Auth Failed");
    else if (error == OTA_BEGIN_ERROR); // Serial.println("Begin Failed");
    else if (error == OTA_CONNECT_ERROR); // Serial.println("Connect Failed");
    else if (error == OTA_RECEIVE_ERROR); // Serial.println("Receive Failed");
    else if (error == OTA_END_ERROR); // Serial.println("End Failed");
  });

  ArduinoOTA.begin();
  if (DEBUG_SERIAL) {
    Serial.println();
    Serial.println("Ready");
    Serial.print("IP address: ");
    Serial.println(WiFi.localIP());
  }

  if (!MDNS.begin("esp32")) {
    if (DEBUG_SERIAL) Serial.println("Error setting up MDNS responder!");
    while (1) {
      delay(1000);
    }
  }
  if (DEBUG_SERIAL) Serial.println("mDNS responder started");

  // Start TCP (HTTP) server
  server.begin();
  if (DEBUG_SERIAL) Serial.println("TCP server started");

  // Add service to MDNS-SD
  //MDNS.addService("http", "tcp", 80);
  //delay(1000);

  // Initiate MIDI communications, listen to all channels
  //Serial1.begin(31250,SERIAL_8N1,RXD1,TXD1);
  midiA.begin(MIDI_CHANNEL_OMNI);
  // midiB.begin(MIDI_CHANNEL_OMNI);   // This line -------------------------
  midiC.begin(MIDI_CHANNEL_OMNI);
  digitalWrite(LED, HIGH);
  if (DEBUG_SERIAL) Serial.println("Midi started");
  delay(1000);
}

void loop() {
  ArduinoOTA.handle();
  /*if (midiA.read()) {
    // Thru on A has already pushed the input message to out A.
    // Forward the message to out B as well.
    midiB.send(midiA.getType(),
               midiA.getData1(),
               midiA.getData2(),
               midiA.getChannel());

    midiC.send(midiA.getType(),
               midiA.getData1(),
               midiA.getData2(),
               midiA.getChannel());
    }
    if (midiB.read()) {
    // Thru on B has already pushed the input message to out B.
    // Forward the message to out A as well.
    midiA.send(midiB.getType(),
               midiB.getData1(),
               midiB.getData2(),
               midiB.getChannel());*/

  /* midiC.send(midiB.getType(),
              midiB.getData1(),
              midiB.getData2(),
              midiB.getChannel());
    }if (midiC.read()) {
    // Thru on A has already pushed the input message to out A.
    // Forward the message to out B as well.
    midiB.send(midiC.getType(),
              midiC.getData1(),
              midiC.getData2(),
              midiC.getChannel());

    midiA.send(midiC.getType(),
              midiC.getData1(),
              midiC.getData2(),
              midiC.getChannel());
    }*/
  HandleClient();

  if ((millis() % 1000) / 500) {
    digitalWrite(LED, LOW);
  }
  else {
    digitalWrite(LED, HIGH);
  }
}

void HandleClient() {

  WiFiClient client = server.available();
  if (!client) {
    return;
  }
  digitalWrite(LED, LOW);
  if (DEBUG_SERIAL) {
    Serial.println("");

    Serial.println("New client");
  }

  // Wait for data from client to become available
  uint32_t timeout = millis();
  while ((client.connected() && !client.available()) && (millis() - timeout < 200))  {
    delay(1);
  }

  // Read the first line of HTTP request
  String req = client.readStringUntil('\r');

  // First line of HTTP request looks like "GET /path HTTP/1.1"
  // Retrieve the "/path" part by finding the spaces
  int addr_start = req.indexOf(' ');
  int addr_end = req.indexOf(' ', addr_start + 1);
  if (addr_start == -1 || addr_end == -1) {
    if (DEBUG_SERIAL) {
      Serial.print("Invalid request: ");
      Serial.println(req);
    }
    return;
  }
  req = req.substring(addr_start + 1, addr_end);
  if (DEBUG_SERIAL) {
    Serial.print("Request: ");
    Serial.println(req);
  }
  String s;
  if (req == "/")
  {
    IPAddress ip = WiFi.localIP();
    String ipStr = String(ip[0]) + '.' + String(ip[1]) + '.' + String(ip[2]) + '.' + String(ip[3]);
    s = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n<!DOCTYPE HTML>\r\n<html>Hello from ESP32 at ";
    s += ipStr;
    s += "</html>\r\n\r\n";
    //Serial.println("Sending 200");
  }
  else
  {
    s = "HTTP/1.1 404 Not Found\r\n\r\n";
    //Serial.println("Sending 404");
  }
  client.print(s);

  client.stop();
  if (DEBUG_SERIAL) Serial.println("Done with client");
  delay(200);
  digitalWrite(LED, HIGH);
}

I've been trying to reassign the GPIO pins, cause i thought that may be the issue, but still no luck.

The 'natural' serial1 pins of the ESP32 are GPIO_16 and GPIO_17. GPIO_NUM12 and GPIO_14 could cause issues depending upon the load at boot and programing. When I use the ESP32's serial ports I do the thing like this

#include <HardwareSerial.h>
HardwareSerial GPSSerial ( 1 );
// pin 2=RX, pin 15=TX
HardwareSerial LIDARSerial ( 2 );
// pin 26=RX, pin 25=TX

voided setup()
{
  LIDARSerial.begin ( SerialDataBits, SERIAL_8N1, 26, 25 );
  GPSSerial.begin ( GPS_DataBits, SERIAL_8N1, 2, 15 ); // begin GPS hardware serial


}



when defining my own pins for serial to use.

I started out without specifying other pins, so just the default pins. The other pins are an attempt to fix something.
The issue only happens when i call begin on a midi object which uses Serial1 as the transport. Calling Serial.begin() directly has no issues.

Those are the pins for Serial2 ? Serial1 is normally on 9 & 10 which is also connected to the flash.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.