2 Replies Latest reply on Feb 20, 2014 3:15 PM by X_Y

    UDP with local loopback

    X_Y

      I'm trying to get Arduino sketch communicate with the linux system with UDP. I take the example from python docs https://wiki.python.org/moin/UdpCommunication, have the following python script running:

       

      import socket

       

      UDP_IP = "127.0.0.1"

      UDP_PORT = 5005

       

      sock = socket.socket(socket.AF_INET, # Internet

         socket.SOCK_DGRAM) # UDP

      sock.bind((UDP_IP, UDP_PORT))

       

      while True:

         data, addr = sock.recvfrom(1024) # buffer size is 1024 bytes

         print "received message:", data


      Tried it with a python script for sending UPD packages and it works.

      However, My arduino sketch fails to work:


      #include <SPI.h>    

      #include <Ethernet.h>

      #include <EthernetUdp.h>    

       

      byte mac[] = { 

        0x12, 0x13, 0x56, 0xEA, 0xF6, 0x3D };

      IPAddress ip(127, 0, 0, 1);

       

      unsigned int localPort = 5005;   

       

      char packetBuffer[UDP_TX_PACKET_MAX_SIZE];

      char  ReplyBuffer[] = "acknowledged";

       

      EthernetUDP Udp;


      void setup() {

           Ethernet.begin(mac,ip);

           Udp.begin(localPort);

      }

      void loop() {

           Udp.beginPacket(ip, localPort);

           Udp.write(ReplyBuffer);

           Udp.endPacket();

           delay(2000);

      }

       

      I'm using Putty connecting to Galileo with a serial port, and nothing shows up when the arduino sketch is running.

       

      I'm not having the Ethernet cable connected since I consider it's just communicating internally, it should just use local loopback to do the work.

       

      Am I correct in the setups? How to get it to work?

        • 1. Re: UDP with local loopback
          Clayton Hofrock

          I believe the Arduino Ethernet library is hard coded to use the Linux interface eth0. In order to use the loop back, you would need to use the Linux interface l0.

           

          Re: Adding another Ethernet port to Intel Galileo board  In this thread there is a comment pointing to an #ifndef in the Ethernet.cpp file which defines which interface the Arduino sketch uses. You should be able to change the interface to use the loopback interface "l0". I would think that would work, but I have not tried it.

          • 2. Re: UDP with local loopback
            X_Y

            Thanks Clay!

            I tried to replace ARDUINO_ETH to "lo" and it proved to be working. The other link you added is very helpful.

             

            One thing I noticed is that the line `Udp.begin(localPort);` needs to use a different port number than the target port in `Udp.beginPacket(ip, targetPort);`, when both sending and receiving are in the same device. It's probably a very stupid error but it took me quite some time to figure out.

             

            And adding another ip address to the eth0 interface seems to be a really good idea(As mentioned by Marclll in the linked post). This way I don't need to change the defined network interface in Arduino library, while still achieving the goal. I'm currently going this way.