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

udp Send not working in udpListener #97

Closed
LegendMicro opened this issue Aug 22, 2013 · 1 comment
Closed

udp Send not working in udpListener #97

LegendMicro opened this issue Aug 22, 2013 · 1 comment

Comments

@LegendMicro
Copy link

Hi All,

I am using the new Microduino ENC28J60 Ethernet Module (Arduino compatible).

I am using the udpListener sketch and want to echo a message back to the sender when a UDP packet arrives.

I am receiving messages OK, but the udpSend in the callback method will not work.

Can anyone help.

Thanks

Here is the Code:

// Demonstrates usage of the new udpServer feature.
//You can register the same function to multiple ports, and multiple functions to the same port.
//
// 2013-4-7 Brian Lee <[email protected]>

#include <EtherCard.h>
#include <IPAddress.h>

#define STATIC 1  // set to 1 to disable DHCP (adjust myip/gwip values below)

#if STATIC
// ethernet interface ip address
static byte myip[] = { 192,168,0,201 };
// gateway ip address
static byte gwip[] = { 192,168,0,1 };

static byte ipDestination[] = {192, 168, 0, 9};

unsigned int portMy = 8888; 
unsigned int portDestination = 9000; 


#endif

// ethernet mac address - must be unique on your network
static byte mymac[] = { 0x70,0x69,0x69,0x2D,0x30,0x31 };

byte Ethernet::buffer[500]; // tcp/ip send and receive buffer

char msg[] = {"Hello World"};

//callback that prints received packets to the serial port
void udpSerialPrint(word port, byte ip[4], const char *data, word len) {
  IPAddress src(ip[0], ip[1], ip[2], ip[3]);
  Serial.println(src);
  Serial.println(port);
  Serial.println(data);
  Serial.println(len);

  //I Added this to echo the packet     <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
  ether.sendUdp(msg, sizeof msg, portMy, ipDestination, portDestination);
  Serial.println("UDP Sent !!");  

}

void setup(){
  Serial.begin(9600);
  Serial.println("\n[backSoon]");

  if (ether.begin(sizeof Ethernet::buffer, mymac) == 0)
    Serial.println( "Failed to access Ethernet controller");
#if STATIC
  ether.staticSetup(myip, gwip);
    Serial.println("Serial Started on FixedIP");
#else
  if (!ether.dhcpSetup())
    Serial.println("DHCP failed");
#endif

  ether.printIp("IP:  ", ether.myip);
  ether.printIp("GW:  ", ether.gwip);
  ether.printIp("DNS: ", ether.dnsip);

  //register udpSerialPrint() to port 1337
  ether.udpServerListenOnPort(&udpSerialPrint, portMy);

  //register udpSerialPrint() to port 42.
  //ether.udpServerListenOnPort(&udpSerialPrint, 42);
}

void loop(){
  //this must be called for ethercard functions to work.
  ether.packetLoop(ether.packetReceive());
}

(EDIT: backticks added -jcw)

@solarkennedy
Copy link
Contributor

First, did you know you can format your code between four backticks to make your post much more readable?

Your code works for me. Use wireshark to be sure about your source and destination. (although wireshark tries to decode it incorrectly as some ipv6 cisco packet)

I can send a packet like this:

echo bla | nc -u -w 1 192.168.1.201 8888

And I get it reflected to the ipDestination:

$ nc -l 9000 -u
Hello World

@jcw jcw closed this as completed Feb 12, 2017
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

3 participants