Need example using LAN line ethernet UDP on wt32-eth01

I have seen several examples for UDP WebServices from khoih-prog but I don't use the WiFi on the wt32-eth01. I need an example of using UDP broadcast message from my wt32-eth01to other devices on my network using the wired ethernet cable.

I tried:
#include <Ethernet.h>
#include <EthernetUdp.h>
Ethernet.begin(mac, ip); // but it locks up at this line.
maybe I needed to change the ip address first.

I don't have a shield.

Should I #include "AsyncUDP.h" ?
and define:
AsyncUDP udp;

You can adapt from these examples

  1. UdpSendReceive
  2. UdpNTPClient

of WebServer_WT32_ETH01 Library

The syntax for WT32-ETH01 to start LAN8720 connection is as follows (Just a little bit different from W5x00 from the beginning)

#include <WebServer_WT32_ETH01.h>

...
void setup()
{
   ...
   // To be called before ETH.begin()
  WT32_ETH01_onEvent();

  ETH.begin(ETH_PHY_ADDR, ETH_PHY_POWER);

  // Static IP, leave without this line to get IP via DHCP
  //bool config(IPAddress local_ip, IPAddress gateway, IPAddress subnet, IPAddress dns1 = 0, IPAddress dns2 = 0);
  ETH.config(myIP, myGW, mySN, myDNS);

  WT32_ETH01_waitForConnect();

  Serial.println(F("\nStarting connection to server..."));
  // if you get a connection, report back via serial:
  Udp.begin(localPort);

  Serial.print(F("Listening on port "));
  Serial.println(localPort);
  ...
}

Here is my code that I implemented from your suggestions. I broadcasted a message from my linux computer and wt32-eth01 never saw it. I confirmed with wireshark. I was unsure of myDNS IP address. What should DNS be?

I started with UDPSendReceiveString example.
I only see my mac address printed during the loop.

See code below:

#include <Ethernet.h>
//#include <EthernetUdp.h>
#include <WebServer_WT32_ETH01.h>

// The IP address will be dependent on your local network:
IPAddress myIP(192, 168, 86, 177);
IPAddress myGW(192, 168, 86, 1);
IPAddress mySN(255, 255, 255, 0);
IPAddress myDNS(8, 8, 8, 8);

uint64_t chipid;
unsigned int localPort = 65000; // local port to listen on

// buffers for receiving and sending data
char packetBuffer[UDP_TX_PACKET_MAX_SIZE]; // buffer to hold incoming packet,
char ReplyBuffer[] = "acknowledged"; // a string to send back

// An EthernetUDP instance to let us send and receive packets over UDP
EthernetUDP Udp;

void setup() {
chipid=ESP.getEfuseMac();//The chip ID is essentially its MAC address(length: 6 bytes).
WT32_ETH01_onEvent();
ETH.begin(ETH_PHY_ADDR, ETH_PHY_POWER);

// Static IP, leave without this line to get IP via DHCP
ETH.config(myIP, myGW, mySN, myDNS);

WT32_ETH01_waitForConnect();

// Open serial communications and wait for port to open:
Serial.begin(115200);
Serial.printf("ESP32 Chip ID = %04X\n",(uint16_t)(chipid>>32));//print High 2 bytes
Serial.printf("%08X\n",(uint32_t)chipid);//print Low 4bytes.

// start UDP
Udp.begin(localPort);
Serial.println("setup done");
Serial.println(localPort);
}

void loop() {
// if there's data available, read a packet
Serial.printf("ESP32 Chip ID = %04X\n",(uint16_t)(chipid>>32));//print High 2 bytes
Serial.printf("%08X\n",(uint32_t)chipid);//print Low 4bytes.

int packetSize = Udp.parsePacket();
if (packetSize) {
Serial.print("Received packet of size ");
Serial.println(packetSize);
Serial.print("From ");
IPAddress remote = Udp.remoteIP();
for (int i=0; i < 4; i++) {
Serial.print(remote[i], DEC);
if (i < 3) {
Serial.print(".");
}
}
Serial.print(", port ");
Serial.println(Udp.remotePort());

// read the packet into packetBufffer
Udp.read(packetBuffer, UDP_TX_PACKET_MAX_SIZE);
Serial.println("Contents:");
Serial.println(packetBuffer);

// send a reply to the IP address and port that sent us the packet we received
Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
Udp.write(ReplyBuffer);
Udp.endPacket();

}
delay(1000);
}

First, it's good you spend some time to rewrite your code, but sorry you still don't get and use the correct code for WT32_ETH01.

No more Ethernet-library-related code, such as

#include <Ethernet.h>
...
// An EthernetUDP instance to let us send and receive packets over UDP
EthernetUDP Udp;

but use Udp instance from ESP32 WiFiUDP class

// A UDP instance to let us send and receive packets over UDP
WiFiUDP Udp;

I suggest you try to use the above-mentioned examples first, to study more carefully what are the differences before moving on to modify your code.

PS:

You have to use code tag to format your code. Read the Forum help.

I am confused why I have to use a class named WiFi when I am using a LAN line ethernet.

Whenever you're confused, it's time to treat it as a simple exercise / basic research for you.

Read ESP32 core's Ethernet library.

If you still don't understand why, then it's really difficult to move forward with WT32_ETH01. Otherwise, you'd be much better in the future.

Sorry, hopefully someone will have time to explain in much more details for you.

Good Luck,

@ khoih-prog: It's a bad excuse and not polite to other people who searching for help. Telling them what they should do and what they will be in future or have been in the past, is just arrogant behavior.

If you don't have time to explain, you could drop the act and don't reply at all.

1 Like

@ KathyControls: It cold be that network communication on the ESP32 core is always initiated over Wifi. But the Wifi stack enables one to hook in for switching to Ethernet communication. The chain of events given in this sample could represent such hook.

Please be aware, what I am telling here is only a logical conclusion of myself. It is not proofed, since I am as confused as you.

Oh, and DNS address is typically the same as your gateway address since on most simple private home networks it's the router which does that layer 3 network stuff.

It would help a lot if someone could confirm (or not), whether the denotation "Wifi" means network communication over the air and the denotation "Ethernet" means network communication over the wire / cable.

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