Community
cancel
Showing results for 
Search instead for 
Did you mean: 
Highlighted
Community Manager
1,704 Views

Ethernet udp broadcast not working

Hi everybody.

We're working with 2 Galileo and we want to send some UDP packets to the broadcast address.

We managed to send/receive from a board to another one and viceversa with this sketches:

# include

# include

byte mac[6] = { 0x98, 0x4F, 0xEE, 0x00, 0xF2, 0xA1 };

IPAddress ip(192, 168, 0, 90);

EthernetUDP Udp;

IPAddress to(192, 168, 0, 91);

void setup() {

Serial.begin(9600);

Ethernet.begin(mac,ip);

system("ifup eth0");

Udp.begin(8888);

system("ifconfig -a eth0 > /dev/ttyGS0");

}

void loop() {

Udp.beginPacket(to, 8888);

Udp.write("hello\n");

Udp.endPacket();

delay(2000);

}

# include

# include

byte mac[6] = { 0x98, 0x4F, 0xEE, 0x01, 0x09, 0xAD };

IPAddress ip(192, 168, 0, 91);

EthernetUDP Udp;

char packetBuffer[UDP_TX_PACKET_MAX_SIZE];

void setup() {

Serial.begin(9600);

Ethernet.begin(mac,ip);

system("ifup eth0");

Udp.begin(8888);

system("ifconfig -a eth0 > /dev/ttyGS0");

}

void loop() {

int packetSize = Udp.parsePacket();

if(packetSize) {

Serial.print("Received packet of size ");

Serial.println(packetSize);

// read the packet into packetBufffer

Udp.read(packetBuffer,UDP_TX_PACKET_MAX_SIZE);

Serial.println("Contents:");

Serial.println(packetBuffer);

}

}

When we tried to set 255.255.255.255 OR 192.168.0.255 as destination, it just doesn't work.

The strange thing is: if we output the return values of beginPacket() and endPacket(), the first call returns 0 and the second -1.

From the Ethernet lib documentation the endPacket() function should return 0 when there is an error, but weirdly it gives us -1 instead.

Any ideas or solutions?

0 Kudos
15 Replies
Highlighted
Honored Contributor I
7 Views

Hi Gorghino,

I've tried to use your sending sketch (the first one in your post) and it seems it does not work with

SPI flash image (upgraded)

eglibc based yocto (current one, 20160606)

Edit:

Using the SPI, the Serial Monitor shows:

==============================================================

eth0 Link encap:Ethernet HWaddr 98:4F:EE:01:0E:EA UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:0 errors:0 dropped:0 overruns:0 frame:0 TX packets:3 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:0 (0.0 B) TX bytes:966 (966.0 B) Interrupt:41 Base address:0x8000

==============================================================

i.e. the eth0 does not seem to be Up&Running...

Using the eglibc based yocto there is no output...

What image are you using?

Fernando.

0 Kudos
Highlighted
Community Manager
7 Views

Hello Gorghino,

 

 

I don't have much experience in networking but I believe the issue is on the IP addresses you are setting. You should set the IP addresses to be on the same network, otherwise they won't find each other. I'm not sure what you mean when you mention that you try to set 255.255.255.255 or 192.168.0.255 as destination. Are those the IP addresses of each board? If so, what I mentioned before makes sense as these addresses would not find each other.

 

 

-Peter.
0 Kudos
Highlighted
Honored Contributor I
7 Views

Hi Peter,

Both, 255.255.255.255 and 192.168.0.255 are two possible broadcast addresses (roughly speaking...) for the IPs in the Gorghino's sketches. At leas one of them should be usable... at least from the point of view of Linux. I'm not so sure about Ethernet and UDPEthernet (EthernetUdp) libraries... I've not used them, and I did not see any example.

Fernando.

0 Kudos
Highlighted
Community Manager
7 Views

Hi Fernando,

 

we're using the stock image. Where can we check the image version?

 

We upgraded them to the 1.0.4 version with the tool.

The ethernet interface is up:

eth0 Link encap:Ethernet HWaddr 98:4F:EE:01:09:AD

inet addr:192.168.0.90 Bcast:192.168.0.255 Mask:255.255.255.0

UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1

RX packets:0 errors:0 dropped:0 overruns:0 frame:0

TX packets:0 errors:0 dropped:0 overruns:0 carrier:0

collisions:0 txqueuelen:1000

RX bytes:0 (0.0 B) TX bytes:0 (0.0 B)

Interrupt:41 Base address:0x8000

0 Kudos
Highlighted
Honored Contributor I
7 Views

Hi Gorghino,

 

we're using the stock image. Where can we check the image version?

 

We upgraded them to the 1.0.4 version with the tool.

Hmmm... if by "stock image" you mean the one at firmware then we are using the same one, since you upgraded the firmware to the 1.0.4 version. If you are using SD card for boot then please tell me the image you are using.

You can check the firmware version with the software updater tool provided by Intel, but if you are using an Arduino IDE version greater than 1.5.3 (which is an old one, actually) then the most likely is that you are using the correct one, which is version 1.0.4.

Maybe our differences are given by different Intel Galileo card versions... I'm using Intel Galileo, aka Intel Galileo Gen1. Are you using Intel Galileo Gen2?

Fernando.

0 Kudos
Highlighted
Community Manager
7 Views

Hi FGT,

 

We're not using a SD card and the firmware is 1.0.4.

 

We're currently using the Intel Arduino libraries 1.6.7 and We have two Galileo Gen1.
0 Kudos
Highlighted
Honored Contributor I
7 Views

Gorghino wrote:

Hi FGT,

 

We're not using a SD card and the firmware is 1.0.4.

 

We're currently using the Intel Arduino libraries 1.6.7 and We have two Galileo Gen1.

Then there is something I can't explain, I'm using the same environment (firmware, IDE, and Galileo cards) but my results are different... I think I'll not be able to help here... maybe you can try via standard networking programming, which is a little beyond Intel Galileo... contact me via PM if you prefer...

Fernando.

0 Kudos
Highlighted
Honored Contributor I
7 Views

Just curious: can you elaborate on the application you are working in?

Fernando.

0 Kudos
Highlighted
Community Manager
7 Views

Hello Gorghino,

 

 

I agree with Fernando, could you please let us know some more details about your project?

 

 

-Peter.
0 Kudos
Highlighted
Community Manager
7 Views

We're working on a simple NDN protocol over IP for an university project.

 

We built our UDP packages and we send them to the two Galileo connected via ethernet.

 

We made a GO client that correctly send the packets to broadcast IP but Galileo EthernetUDP library finds 255.255.255.255 as remoteIP() and the send() to 255.255.255.255 fails on beginPacket() or endPacket() like said before.
0 Kudos
Highlighted
Community Manager
7 Views

I understand, let me try to investigate about this. If I find anything useful I will post it here. I'll try to get back to you as quickly as I can.

 

 

-Peter.
0 Kudos
Highlighted
Community Manager
7 Views

Check this blog too: http://www.inspirel.com/yami4/intel_galileo.html Inspirel - YAMI4 on Intel Galileo

0 Kudos
Highlighted
Community Manager
7 Views

Hello Gorghino,

 

 

When using the remoteIP(), it returns 255.255.255.255 and sending packets to that client address doesn't work. There is a workaround that returns the correct address of the client, but there are some changes needed in the Ethernet library for Galileo.

The location of EthernetUdp.cpp (which is the file that needs the modifications) is in a path that should look similar to "C:\Users\Username\AppData\Local\Arduino15\packages\Intel\hardware\i586\1.6.7+1.0\libraries\Ethernet\...".

 

 

These are the 4 lines that need to be added in the EthernetUDP::available() function:

 

 

int EthernetUDP::available() {

 

struct pollfd ufds;

 

int ret = 0;

 

extern int errno;

 

int timeout = 0; // milliseconds

 

struct sockaddr_in remoteIP; // struct to save the remote IP

 

socklen_t size; // size of sockaddr, needed for getting the IP from sockaddr, will save the size of the remote IP

 

if (_sock == -1)

 

return 0;

 

ufds.fd = _sock;

 

ufds.events = POLLIN;

 

ufds.revents = 0;

 

ret = poll(&ufds, 1, timeout);

 

if ( ret < 0 ){

 

trace_error("%s error on poll errno %d", __func__, errno);

 

return 0;

 

}

 

if( ret == 0)

 

return 0;

 

// only return available if bytes are present to be read

 

if(ret > 0 && ufds.revents&POLLIN){

 

size=sizeof(struct sockaddr);

 

int bytes = 0;

 

ret = ioctl(_sock, FIONREAD, &bytes);

 

recvfrom(_sock, NULL, 0, MSG_PEEK ,(struct sockaddr*) &remoteIP, &size); //call to recvfrom with MSG_PEEK to get the remote IP and don't destroy the message

 

_remoteIP=(byte *)inet_ntoa(remoteIP.sin_addr); //saving ip in _remoteIP library variable

 

if ( ret < 0){

 

trace_error("ioctl fail on socket!");

 

return 0;

 

}

 

if ( ret == 0 && bytes != 0){

 

return bytes;

 

}

 

}

 

return 0;

 

}

 

 

Could you please try this modification and let us know how it goes?

 

-Peter.
0 Kudos
Highlighted
Beginner
7 Views

These changes worked for me. I also added a line to get the remote port:

_remotePort = htons(remoteIP.sin_port);

Thanks,

Tim

0 Kudos
Highlighted
Community Manager
7 Views

Hello Gorghino,

 

 

We have an update for this case:

 

 

The fix is to update the reader sketch to use available. This still has to go through regression testing.

 

Please note that the full regression will need more time and this proposed changes to your sketch will make it work for one use case only.

 

 

{code:title=UDPReader|borderStyle=solid}

 

# include

 

# include

 

//cahnge mac to your device

 

byte mac[6] = { 0x00, 0x13, 0x20, 0xFF, 0x16, 0x56 };

 

IPAddress ip(192, 168, 0, 102);

 

IPAddress gw(192, 168, 0, 1);

 

IPAddress sn(255, 255, 255, 0);

 

 

EthernetUDP Udp;

 

char packetBuffer[UDP_TX_PACKET_MAX_SIZE];

 

 

void setup() {

 

Serial.begin(9600);

 

// while (!Serial) {

 

// delay(500);

 

// }

 

system("ifup eth0");

 

//alternate ways to begin

 

//Ethernet.begin(mac,ip);

 

//Ethernet.begin(mac);

 

Ethernet.begin(mac, ip, gw, gw, sn);

 

//set broadcast

 

system("ifconfig eth0 broadcast 192.168.0.255");

 

//ping writer

 

system("ping -c 5 192.168.0.101 > /dev/ttyGS0");

 

Udp.begin(8888);

 

system("ifconfig -a eth0 > /dev/ttyGS0");

 

Serial.println(Ethernet.localIP());

 

}

 

 

void loop() {

 

int packetSize = Udp.parsePacket();

 

if (Udp.available()) {

 

//if (packetSize > 0 ) {

 

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);

 

//this is needed so no weird charcters on next read

 

Udp.flush();

 

}

 

}

 

{code}

 

 

I hope this helps.

 

-Peter.
0 Kudos