ESP32 Ausgang über Ethernet ansteuern

Hallo zusammen,

ich würde gerne über einen MQTT Server über Ethernet Ausgänge von einem ESP32 ansteuern.
Hat jemand so was schonmal gemacht? Ich habe leider keine bestehenden Projekte gefunden.

Vielen Dank

Im englischen Teil des Forum müssen die Beiträge und Diskussionen in englischer Sprache verfasst werden.
Deswegen wurde diese Diskussion in den deutschen Teil des Forums verschoben.

mfg ein Moderator.

Was willst du ansteuern? Du beschreibst nur mit was du es machen willst, nicht das Ziel.

Edit: Ach, ich hab das Komma in deinem Satz übersehen.

Ja, das wurde bestimmt schon oft gemacht.

Da gibts sicher was

Hmm... Welchen ESP32 mit Ethernet hast du da?

Beispiele wie man eine Led blinken lässt sollte jede Arduino MQTT Library mitbringen.

Ich habe den hier ESP32-S3-ETH von Waveshare

/*
 Web client
 
 This sketch connects to a test website (httpbin.org)
 and try to do a GET request, the output is printed
 on Serial
 
 by Renzo Mischianti <www.mischianti.org>
 
 https://www.mischianti.org
 
 */
 
#include <SPI.h>
#include <Ethernet.h>
 
// if you don't want to use DNS (and reduce your sketch size)
// use the numeric IP instead of the name for the server:
//IPAddress server(74,125,232,128);  // numeric IP for Google (no DNS)
//char server[] = "www.google.com";    // name address for Google (using DNS)
char server[] = "httpbin.org";    // name address for Google (using DNS)
 
// Enter a MAC address for your controller below.
// Newer Ethernet shields have a MAC address printed on a sticker on the shield
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
 
// Set the static IP address to use if the DHCP fails to assign
#define MYIPADDR 10,0,0,162
#define MYIPMASK 255,255,255,0
#define MYDNS 10,0,0,1
#define MYGW 10,0,0,1
 
// Initialize the Ethernet client library
// with the IP address and port of the server
// that you want to connect to (port 80 is default for HTTP):
EthernetClient client;
 
// Variables to measure the speed
unsigned long beginMicros, endMicros;
unsigned long byteCount = 0;
bool printWebData = true;  // set to false for better speed measurement
 
void setup() {
    Serial.begin(115200);
    delay(1000);
    Serial.println("Begin Ethernet");
 
    // You can use Ethernet.init(pin) to configure the CS pin
    //Ethernet.init(10);  // Most Arduino shields
    //Ethernet.init(5);   // MKR ETH Shield
    //Ethernet.init(0);   // Teensy 2.0
    //Ethernet.init(20);  // Teensy++ 2.0
    //Ethernet.init(15);  // ESP8266 with Adafruit FeatherWing Ethernet
    Ethernet.init(49);  // ESP32 with Adafruit FeatherWing Ethernet
 
    IPAddress ip(MYIPADDR);
    IPAddress dns(MYDNS);
    IPAddress gw(MYGW);
    IPAddress sn(MYIPMASK);
    Ethernet.begin(mac, ip, dns, gw, sn);
    Serial.println("STATIC OK!");
    
    delay(5000);
 
 
    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");
  // if you get a connection, report back via serial:
  beginMicros = micros();
}
 
void loop() {
  // if there are incoming bytes available
  // from the server, read them and print them:
  int len = client.available();
  if (len > 0) {
    byte buffer[80];
    if (len > 80) len = 80;
    client.read(buffer, len);
    if (printWebData) {
      Serial.write(buffer, len); // show in the serial monitor (slows some boards)
    }
    byteCount = byteCount + len;
  }

 
    // do nothing forevermore:
    while (true) {
      delay(1);
    }
  }

Das ist bis jetzt mein Programm. Ich schaffe es nichtmal den ESP anzupingen :frowning:

Waveshare hat hier eine Wiki Seite zu dem Board:

Wie man den W5500 ans laufen bekommt kann ich mir erst heute Abend anschauen...

das wäre das erste das es zu lösen gibt.
mach mal ein IP config von deinem PC und poste das in code Tags.

Bist Du Dir sicher, dass Dein Heimnetz einen 10.0.0.x Adressbereich hat?

Gruß Tommy

Das hat mir schon mal extrem weiter geholfen jetzt kann ich ihn an pingen :slight_smile:

Ja :+1:

Kannst du einmal den Code posten?

Ok... es scheint die Ethernet.h verwendet zu werden.

Den EthernetClient scheinst du ja ans laufen bekommen zu haben.

Die nächsten Schritte dürfen diese sein:

  • Verbindung mit dem MQTT herstellen
  • Auf Topic subscriben
  • Die Payload parsen

Dann würde ich mit jetzt den PubSubClient anschauen:

Danke das erste Suchergebnis hat mir schon mal weitergeholfen im Bezug auf die MQTT Kommunikation

Ja das ist aktuell mein Code:

/**
 ******************************************************************************
 * @file     ETH_StaticIP.ino
 * @brief    Initialize W5500 Ethernet module with static IP configuration and 
 *           print the assigned IP address to the serial monitor.
 * @version  V1.1
 * @date     2025-07-03
 * @license  MIT
 * @copyright Copyright (c) 2025, Waveshare Electronics CO.,LTD
 *
 * Experiment Objective:
 * Demonstrate how to configure the W5500 Ethernet module with a static IP 
 * address and verify network initialization by printing the IP to the console.
 *
 * Hardware Resources:
 * 1. ESP32-S3-ETH
 *
 * Experiment Phenomenon:
 * 1. The W5500 module is initialized via SPI with user-defined pin assignments.
 * 2. The module is configured with a static IP address.
 * 3. The assigned IP address is printed via the serial port after successful setup.
 *
 * Notes:
 * - Ensure W5500 wiring matches the defined pin numbers.
 * - Modify the static IP settings according to your local network environment.
 *
 ******************************************************************************
 *
 * Development Platform: Arduino IDE
 * Support Forum: service.waveshare.com
 * Company Website: www.waveshare.com
 *
 ******************************************************************************
 */

#include <SPI.h>
#include <Ethernet.h>
#include <PubSubClient.h>

// W5500 Pin Definitions
#define W5500_CS     14  // Chip Select pin
#define W5500_RST     9  // Reset pin (optional, not used here)
#define W5500_INT    10  // Interrupt pin (optional, not used here)
#define W5500_MISO   12  // MISO pin
#define W5500_MOSI   11  // MOSI pin
#define W5500_SCK    13  // Clock pin

// MAC address (customize if required)
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };

// Static IP Configuration
IPAddress ip(10, 0, 0, 162);         // Static IP address
IPAddress dns(10, 0, 0, 1);          // DNS server
IPAddress gateway(10, 0, 0, 1);      // Gateway address
IPAddress subnet(255, 255, 255, 0);     // Subnet mask

EthernetClient client;

// MQTT Broker
const char *mqtt_broker = "";
const char *topic = "emqx/esp32";
const char *mqtt_username = "";
const char *mqtt_password = "";
const int mqtt_port = ;

void setup() {
  Serial.begin(115200);  // Start serial communication
  while (!Serial) {
    ; // Wait until serial port is ready
  }

  // Initialize SPI with custom pin configuration
  SPI.begin(W5500_SCK, W5500_MISO, W5500_MOSI, W5500_CS);

  // Initialize Ethernet with static IP settings
  Ethernet.init(W5500_CS);
  Ethernet.begin(mac, ip, dns, gateway, subnet);

  // Verify if IP address is properly assigned
  if (Ethernet.localIP() == IPAddress(0, 0, 0, 0)) {
    Serial.println("Failed to configure Ethernet with static IP");
    while (true); // Halt on failure
  }

  // Print assigned IP address
  Serial.print("IP Address: ");
  Serial.println(Ethernet.localIP());
}

void loop() {
  delay(1000); // Wait for 1 second
  client.setServer(mqtt_broker, mqtt_port);
client.setCallback(callback);
while (!client.connected()) {
    String client_id = "esp32-client-";
    client_id += String(WiFi.macAddress());
    Serial.printf("The client %s connects to the public MQTT broker\n", client_id.c_str());
    if (client.connect(client_id.c_str(), mqtt_username, mqtt_password)) {
        Serial.println("Public EMQX MQTT broker connected");
    } else {
        Serial.print("failed with state ");
        Serial.print(client.state());
        delay(2000);
    }
}
}

Ich muss jetzt nochmal rumprobieren mal schauen ob ich das mit der MQTT Kommunikation zum laufen bringe.
Danke schonmal allen für die Hilfe :slight_smile:

Hab da mal schnell was zusammen kopiert:
(Du musst deine MQTT Credentials noch eintragen)


#include <SPI.h>
#include <Ethernet.h>
#include <PubSubClient.h>

// W5500 Pin Definitions
#define W5500_CS 14    // Chip Select pin
#define W5500_RST 9    // Reset pin (optional, not used here)
#define W5500_INT 10   // Interrupt pin (optional, not used here)
#define W5500_MISO 12  // MISO pin
#define W5500_MOSI 11  // MOSI pin
#define W5500_SCK 13   // Clock pin

// MAC address (customize if required)
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };

// Static IP Configuration
IPAddress ip(10, 0, 0, 162);         // Static IP address
IPAddress dns(10, 0, 0, 1);          // DNS server
IPAddress gateway(10, 0, 0, 1);      // Gateway address
IPAddress subnet(255, 255, 255, 0);  // Subnet mask

EthernetClient ethClient;
PubSubClient client(ethClient);

// MQTT Broker
const char *mqtt_broker = "";
const char *topic = "emqx/esp32";
const char *mqtt_username = "";
const char *mqtt_password = "";
const int mqtt_port = 1883;

void callback(char *topic, byte *payload, unsigned int length) {
  Serial.print("Message arrived [");
  Serial.print(topic);
  Serial.print("] ");
  for (int i = 0; i < length; i++) {
    Serial.print((char)payload[i]);
  }
  Serial.println();

  // Als schneller test... Steuerung der LED mit dem LSB des 1. Zeichens der Payload
  // Sende A um die LED anzuschalten
  // Sende B um die LED auszuschalten
  // Oder wars anders rum?!?
  if (length > 0) {
    bool on = payload[0] & 1;
    digitalWrite(LED_BUILTIN, on);
  }
}

void reconnect() {
  // Loop until we're reconnected
  while (!client.connected()) {
    Serial.print("Attempting MQTT connection...");
    // Attempt to connect
    if (client.connect("arduinoClient")) {
      Serial.println("connected");
      // Once connected, publish an announcement...
      client.publish("outTopic", "hello world");
      // ... and resubscribe
      client.subscribe(topic);
    } else {
      Serial.print("failed, rc=");
      Serial.print(client.state());
      Serial.println(" try again in 5 seconds");
      // Wait 5 seconds before retrying
      delay(5000);
    }
  }
}

void setup() {
  Serial.begin(115200);  // Start serial communication
  while (!Serial) {
    ;  // Wait until serial port is ready
  }

  pinMode(LED_BUILTIN, OUTPUT);

  // Initialize SPI with custom pin configuration
  SPI.begin(W5500_SCK, W5500_MISO, W5500_MOSI, W5500_CS);


  client.setServer(mqtt_broker, mqtt_port);
  client.setCallback(callback);

  // Initialize Ethernet with static IP settings
  Ethernet.init(W5500_CS);
  Ethernet.begin(mac, ip, dns, gateway, subnet);

  // Verify if IP address is properly assigned
  if (Ethernet.localIP() == IPAddress(0, 0, 0, 0)) {
    Serial.println("Failed to configure Ethernet with static IP");
    while (true)
      ;  // Halt on failure
  }

  // Print assigned IP address
  Serial.print("IP Address: ");
  Serial.println(Ethernet.localIP());
}

void loop() {
  if (!client.connected()) {
    reconnect();
  }
  client.loop();
}

Es kompiliert, aber ich kanns mangels Hardware nicht testen