Networking-for-Arduino / EthernetENC

Ethernet library for ENC28J60. This is a modern version of the UIPEthernet library. EthernetENC library is compatible with all Arduino architectures with Arduino SPI library with transactions support. Only include EthernetENC.h instead of Ethernet.h
126 stars 28 forks source link

trying to send broadcast SSDP message #51

Open fatmehosseini opened 1 year ago

fatmehosseini commented 1 year ago

Hi, I am trying to send broadcast SSDP, based on this example https://github.com/arduino-libraries/Ethernet/blob/master/examples/UDPSendReceiveString/UDPSendReceiveString.ino by changing the IP to 239:255:255:250 and the port to 1900. But I don't receive any message on Wire Shark. This is my code:

#include <EthernetENC.h>
#include <EthernetUdp.h>

#define UDP_TX_PACKET_MAX_SIZE 24

byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
IPAddress ip(239, 255, 255, 250);

unsigned int localPort = 1900;      // local port to listen on
char packetBuffer[UDP_TX_PACKET_MAX_SIZE];  // buffer to hold incoming packet,
char ReplyBuffer[] = "acknowledged";        // a string to send back
EthernetUDP Udp;

void setup() {
    Serial.begin(115200);
    Serial.println("START");
  Ethernet.init(25);  // Most Arduino shields
  Ethernet.begin(mac);
  if (Ethernet.hardwareStatus() == EthernetNoHardware) {
    Serial.println("Ethernet shield was not found.  Sorry, can't run without hardware. :(");
    while (true) {
      delay(1); // do nothing, no point running without Ethernet hardware
    }
  }
  if (Ethernet.linkStatus() == LinkOFF) {
    Serial.println("Ethernet cable is not connected.");
  }

  // start UDP
  Udp.begin(localPort);
  Serial.printf("local IP is %s \n", Ethernet.localIP().toString());
}

void loop() {
  // if there's data available, read a packet
  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 packetBuffer
    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(ip, localPort);
    Udp.write(ReplyBuffer);
    Udp.endPacket();
  delay(10);
}  
JAndrassy commented 1 year ago

the example primarily shows receiving of UDP packets (and the echoing them). it will only send a packet when it received one. for some reason the designers of the UDP API at Arduino expected one would always listen to UDP packets too, while there are many scenarios when packets are only sent. so just skip the parsePacket part and only send a packet to any address and port.

my WiFiEspAT library has an UdpSender example. it is not fully applicable for Ethernet or EthernetENC library which always require Udp.begin but the loop() function should work. https://github.com/JAndrassy/WiFiEspAT/blob/master/examples/Basic/UdpSender/UdpSender.ino

void loop() {

  Udp.beginPacket(reciverIP, receiverPort);
  Udp.print("Arduino millis ");
  Udp.print(millis());
  Udp.endPacket();

  delay(5000);
}
fatmehosseini commented 1 year ago

My problem solved, I was trying to send the message on 239, 255, 255, 250 IP which is a multicast IP and in your library multicast sending is disabled. I changed the IP to broadcast IP and my message are received on Wire-shark now. By the way, is there any way that I could enable multi-casting in this library?