ESP32-EVB ethernet looses connection when switching AC load

Started by MaxWimmreuter, May 28, 2020, 08:29:21 PM

Previous topic - Next topic

MaxWimmreuter

Hello,

I am using an ESP32-EVB for switching a 230V 30W solenoid valve with MQTT over a wired Ethernet connection.

Every time I turn on the relays, the ethernet disconnects. Also the ethernet LEDs turn off.
Disconnecting the AC Power form the relays does not fix it, only after a reset ethernet is working again.

If there is no AC load connected to the output of the relays (no current flowing through the relays), it works without any problem.
Using the blue power supply in the picture or USB power makes no difference.

My setup:

Left cable is the 230V AC input, right cable the solenoid


Code running on the ESP32-EVB:
#include <ETH.h>
#include <MQTT.h>

static bool eth_connected = false;
bool networkConnected = false;

//MQTT topics
char* STcommand = "Home/garten/command";
char* OTonline = "Home/garten/online/state";

//MQTT Server IP Address
const char* server = "192.168.178.3";

long unsigned int wait = 0;


//MQTT + Ethernet client
MQTTClient client;
WiFiClient net;

//print ethernet status
void WiFiEvent(WiFiEvent_t event)
{
  switch (event) {
    case SYSTEM_EVENT_ETH_START:
      Serial.println("ETH Started");
      //set eth hostname here
      ETH.setHostname("Garten");
      break;
    case SYSTEM_EVENT_ETH_CONNECTED:
      Serial.println("ETH Connected");
      break;
    case SYSTEM_EVENT_ETH_GOT_IP:
      Serial.print("ETH MAC: ");
      Serial.print(ETH.macAddress());
      Serial.print(", IPv4: ");
      Serial.print(ETH.localIP());
      if (ETH.fullDuplex()) {
        Serial.print(", FULL_DUPLEX");
      }
      Serial.print(", ");
      Serial.print(ETH.linkSpeed());
      Serial.println("Mbps");
      eth_connected = true;
      break;
    case SYSTEM_EVENT_ETH_DISCONNECTED:
      Serial.println("ETH Disconnected");
      eth_connected = false;
      break;
    case SYSTEM_EVENT_ETH_STOP:
      Serial.println("ETH Stopped");
      eth_connected = false;
      break;
    default:
      break;
  }
}

void setup()
{
  pinMode(32, OUTPUT);
  pinMode(33, OUTPUT);

  digitalWrite(32, LOW);
  digitalWrite(33, LOW);
 
  Serial.begin(115200);
  WiFi.onEvent(WiFiEvent);
  ETH.begin();

  delay(5000);
 
  client.begin(server, net);
  client.onMessage(messageReceived);


}


void loop()
{
  //print MQTT connection state every second
  if ( wait + 1000 < millis()) {
    Serial.print("Client Connected: ");
    Serial.println(client.connected());
    wait = millis();
  }

  //reconnect after ethernet or MQTT disconnect
  if (eth_connected && !networkConnected) {
    Serial.print("MQTT connect success: ");
    Serial.println(client.connect("garten", false));
    Serial.print("MQTT subscribed: ");
    Serial.println(client.subscribe(STcommand));
    client.publish(OTonline, "1");
    networkConnected = true;
  }
  if (!eth_connected || !client.connected()) {
    networkConnected = false;
  }

  //MQTT loop
  delay(10);
  client.loop();
}

//switching relays based on MQTT message
void messageReceived(String &topic, String &payload){
  if(topic == STcommand) {
    if (payload == "ON" || payload == "1"){
      Serial.println("ON");
      digitalWrite(32, HIGH);
      digitalWrite(33, HIGH);
    } else if (payload == "OFF" || payload == "0"){
      Serial.println("OFF");
      digitalWrite(32, LOW);
      digitalWrite(33, LOW);
    }
  }
}

Is there any way to make this work?

Thanks, Max

LubOlimex

Sounds like the typical problem with electromechanical relays when switching big loads. Sparking and so on. And this is combined with the poor behavior of the Ethernet chip after ESP32-EVB board reset, which sometimes doesn't start (an issue that persist only in ESP32-EVB of all Olimex-made ESP32 boards and we are currently unable to solve it without major changes in the hardware specifications of the board). The Ethernet chip behavior after restart had been improved since revision J. Also if you encounter issues, insert a small delay before initializing the Ethernet.

Distance between relays and solenoid might play a role. Try increasing the distance as a test.

Try adding RC snubber, read here: https://electronics.stackexchange.com/questions/42131/how-to-design-an-rc-snubber-for-a-solenoid-relay-driving-an-inductive-load

Check more for more ideas about typical problems with such relays: https://delcon.fi/typical-problems-relating-electromechanical-relays-and-optocouplers

Technical support and documentation manager at Olimex

SejnyM

SSR relay solve problem. No contacts = no bouncing/arcing

MaxWimmreuter

Thank you for your suggestions.

I never experienced any issues with ethernet port after a reset, also in a "normal" enviornment everything worked without any porblems.
The solenoid and EPS32-EVB are about 2 Meters apart. Should be enough

I just ordered a basic RC snubber module, since I can't mesaure the coil inductance for calculating the exact values.

A SSR might also work, but I want to keep the dual pole switching for safety reasons.

Next week I will also try one of those external relay boards with optocupplers.