Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

trying to send broadcast SSDP message #51

Open
fatmehosseini opened this issue Jun 8, 2023 · 2 comments
Open

trying to send broadcast SSDP message #51

fatmehosseini opened this issue Jun 8, 2023 · 2 comments

Comments

@fatmehosseini
Copy link

fatmehosseini commented Jun 8, 2023

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
Copy link
Member

JAndrassy commented Jun 8, 2023

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
Copy link
Author

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants