Results 1 to 5 of 5

Thread: Problems sending commands via LAN (UDP sockets)

  1. #1
    New Member
    Join Date
    Jun 2014
    Location
    Portugalete
    Posts
    3

    Problems sending commands via LAN (UDP sockets)

    Hi everybody,

    I have been testing Link2Fs Multi v6f for Experts. Whenever I send and receive data via Serial port everything works fine. I have been performing some tests connecting arduinos with Ethernet Shield in an Ethernet network. For that, I configure cards in Link2FS to work in LAN (using UDP Protocol).

    I receive perfectly data from Link2FS in my card via UDP sockets. The problem I found is that whenever I try to send data to link2FS via UDP sockets it doesn´t seem to work. “From Card” appears empty.
    Here I put a basic and reduced example of my source code:

    #include // needed for Arduino versions later than 0018
    #include
    #include // UDP library

    #define AP1_pushbutton 2
    #define AP1_button_light 5

    //BUTTONS
    int stateAP1_pushbutton=1,previousStateAP1_pushbutton=1;

    byte mac[] = { 0x90, 0xA2, 0xDA, 0x0F, 0x83, 0x67 }; //MAC Arduino
    IPAddress ipLocal(192, 168, 1, 3); //IP local
    IPAddress ipRemote (192, 168, 1, 2); //IP remote

    unsigned int port = 65222; // UDP port
    char caracter,code,value;

    // An EthernetUDP instance to let us send and receive packets over UDP
    EthernetUDP Udp;

    char packetBuffer[UDP_TX_PACKET_MAX_SIZE]; //buffer to hold incoming packet,


    void setup()
    {
    pinMode(53,OUTPUT); //In the Mega, pin53 as OUTPUT to activate SPI interface
    pinMode(AP1_pushbutton,INPUT_PULLUP); //Activate pullup resistors
    pinMode(AP1_button_light,OUTPUT);

    // start the Ethernet and UDP:
    Ethernet.begin(mac,ipLocal);
    Udp.begin(port);
    }

    void loop()
    {
    BUTTONS(); //go and check the buttons

    //receive data
    int packetSize = Udp.parsePacket();
    if(packetSize)
    {
    while ( Udp.available() )
    {
    caracter=Udp.read();
    if (caracter=='#')
    {
    HASH(); //The first identifier is "#"
    }
    } //end while
    }
    }//end loop

    void HASH()
    {
    // The first identifier was "#"
    code=Udp.read();
    switch (code)
    {
    case 'b': //AP1 button light
    value=Udp.read();
    if (value=='0')
    {
    digitalWrite(AP1_button_light,LOW);
    }
    else if (value=='1')
    {
    digitalWrite(AP1_button_light,HIGH);
    }
    break;
    } //end switch

    } //end HASH()


    void BUTTONS()
    {
    stateAP1_pushbutton=digitalRead(AP1_pushbutton); //read the actual state of the button
    if ( previousStateAP1_pushbutton && !stateAP1_pushbutton ) //if previous state is HIGH (OFF) and actual state is LOW (ON)
    {
    Udp.beginPacket(ipRemote,port);
    Udp.write("#a1");
    Udp.endPacket();
    }
    previousStateAP1_pushbutton=stateAP1_pushbutton;
    } //END BUTTONS

    Do I send it in a correct format? For example, for the Udp.write(“#a1”). I have tried it by adding a final ‘\0’ to the string to be sent and doesn´t work. If I send data between two arduinos via UDP sockets it works fine.
    Could anybody help me?

    Many thanks!
    Attached Images Attached Images

  2. #2
    500+ This must be a daytime job
    Join Date
    Jan 2009
    Location
    Tampa
    Posts
    538

    Re: Problems sending commands via LAN (UDP sockets)

    Did you ever get this figured out?

  3. #3
    500+ This must be a daytime job
    Join Date
    Jan 2009
    Location
    Tampa
    Posts
    538

    Re: Problems sending commands via LAN (UDP sockets)

    Hi 4Dagaci, Not sure if you are still working on this, if so I got it working. Let me know if you still need help.

    Buddy

  4. #4
    New Member
    Join Date
    Jun 2014
    Location
    Portugalete
    Posts
    3

    Re: Problems sending commands via LAN (UDP sockets)

    Hi Buddym.Yes, I would be so pleased if you could give me a hand with this.

  5. #5
    500+ This must be a daytime job
    Join Date
    Jan 2009
    Location
    Tampa
    Posts
    538

    Re: Problems sending commands via LAN (UDP sockets)

    Here's my working code:

    #include "TM1638.h"
    #include "TM1640.h"

    #include "SPI.h"
    #include "Ethernet.h"
    #include "EthernetUdp.h"



    #define AP1_pushbutton 2
    #define AP1_button_light 5

    TM1640 module(6, 7);



    //BUTTONS
    int stateAP1_pushbutton=1,previousStateAP1_pushbutton=1;
    byte mac[] = { 0x90, 0xA2, 0xDA, 0x0F, 0x83, 0x67 }; //MAC Arduino
    IPAddress ipLocal(192, 168, 1, 119); //IP local
    IPAddress ipRemote (192, 168, 1, 16); //IP remote
    unsigned int port = 65222; // UDP port
    char caracter,code,value;
    // An EthernetUDP instance to let us send and receive packets over UDP
    EthernetUDP Udp;
    char packetBuffer[UDP_TX_PACKET_MAX_SIZE]; //buffer to hold incoming packet,

    void setup()
    {
    pinMode(53,OUTPUT); //In the Mega, pin53 as OUTPUT to activate SPI interface
    pinMode(AP1_pushbutton,INPUT_PULLUP); //Activate pullup resistors
    pinMode(AP1_button_light,OUTPUT);
    module.setDisplayToString("--");
    // start the Ethernet and UDP:
    Ethernet.begin(mac,ipLocal);
    Udp.begin(port);
    }
    void loop()
    {
    BUTTONS(); //go and check the buttons
    //receive data
    int packetSize = Udp.parsePacket();
    if(packetSize)
    {
    while ( Udp.available() )
    {
    caracter=Udp.read();
    if (caracter=='=')
    {
    HASH(); //The first identifier is "#"
    }
    } //end while
    }
    }//end loop
    void HASH()
    {
    // The first identifier was "#"
    code=Udp.read();
    switch (code)
    {
    case 'a': //AP1 button light
    value=Udp.read();
    if (value=='0')
    {
    digitalWrite(AP1_button_light,LOW);
    module.setDisplayToString("LO");
    }
    else if (value=='1')
    {
    digitalWrite(AP1_button_light,HIGH);
    module.setDisplayToString("HI");
    }
    break;
    } //end switch
    } //end HASH()

    void BUTTONS()
    {
    stateAP1_pushbutton=digitalRead(AP1_pushbutton); //read the actual state of the button
    if ( previousStateAP1_pushbutton && !stateAP1_pushbutton ) //if previous state is HIGH (OFF) and actual state is LOW (ON)
    {
    Udp.beginPacket(ipRemote,port);
    Udp.write("B01");
    Udp.endPacket();
    }
    previousStateAP1_pushbutton=stateAP1_pushbutton;
    } //END BUTTONS
    Last edited by Jim NZ; 02-07-2015 at 03:22 AM. Reason: Fixed the "include"s