UDP static IP example

UDP messages receiver and transmitter example using static IP feature with mbed-OS.

Useful to read as reference Networking and Ethernet Interface MBED idea : https://docs.mbed.com/docs/mbed-os-api-reference/en/latest/APIs/communication/network_sockets/

Another example of UDP communication, without static IP. IP assigned to devices by DHCP.

https://developer.mbed.org/questions/73552/Udp-Receive-with-nucleo_f767zi/

Also there is libraries for Nucleo F7 for Ethernet to use with mbed-os : https://os.mbed.com/users/DieterGraef/code/Nucleo_F746ZG_Ethernet/

Requirements to set-up it properly:

  • 1. Set IP of the development bord in mbedIP variable and also create variable with IP address of a receiving device
  • 2. Create EthernetInternet object
  • 3. Create UDPSocket based on Ethernet type structure
  • 4. Create SocketAddress object with parameters of receiving device details (IP, port) for transmitting function
  • 5. Create SocketAddress empty object for receiving function
  • 6. bind receiving socket to specific port, because only needed to specify port
  • 7. Start network with connect() function in main
  • 8. Run receiving or transmitting Thread, according to device funcionality
  • 9. Uncomment one of two set_network options, with mbedIP for transmitter and recvIP for receiver

Important to create socket at the same lace where it is running. Should be found way to set-up socket as globally visibal.

Also getting feedback on serial port about device status . If running shorter wait(<500) period, check if printf() is not stopping thread, more sensitive is receiving thread.

Working with mbed-os:

  • 5.5.2
  • 5.5.3

Partly working on 5.5.5 release. Can assign static IP, but communication only happens if ones was connected to DHCP service. After DHCP(Internet) can be disconnected and keeps running communication between two Nucleo boards.

Not working on 5.5.4 release.

UDP communication example

#include "mbed.h"
#include "lwip-interface/EthernetInterface.h"
 
const int PORT = 8151;

DigitalOut led1(LED1);
DigitalOut led2(LED2);
 
// Static IP network variables
static const char*          mbedIP       = "192.168.0.45";  //IP 
static const char*          mbedMask     = "255.255.255.0";  // Mask
static const char*          mbedGateway  = "192.168.0.3";    //Gateway

static const char* recvIP = "192.168.0.38";
  
//Creating Ethernet object
EthernetInterface eth;
 
void transmitter(){
        
        //Buffer for output messages
        const char out_buffer[] = "very important data";
 
        //address of destination ip address and port
        SocketAddress td_addr(recvIP, PORT);
        UDPSocket td_sock(&eth);
 
        // Loop to send data to Socket address "td_sock"
       while(true){
            int ret = td_sock.sendto(td_addr, &out_buffer, sizeof(out_buffer));
         if(ret < 0){
         led
         Thread::wait(500);
          }
}
 
int main() {

     Thread receive;
     Thread transmit;

    // Set-up static IP network for transmitting device
    eth.set_network(mbedIP, mbedMask, mbedGateway);// my device address

    // Set-up static IP network for receiving device
    //eth.set_network(recvIP, mbedMask, mbedGateway);// my device address

    // Start network
    eth.connect();
 

    transmit.start(transmitter);
    receive.start(receiver);

        while(true){
 
            led2 = !led2;
                              
            Thread::wait(500);          
           led1 = !led1;    
          }
    }

UDP Receiver code

#include "mbed.h"
#include "lwip-interface/EthernetInterface.h"
 
const int PORT = 8151;

DigitalOut led1(LED1);
DigitalOut led2(LED2);
 
// Static IP network variables
static const char*          mbedIP       = "192.168.0.45";  //IP 
static const char*          mbedMask     = "255.255.255.0";  // Mask
static const char*          mbedGateway  = "192.168.0.3";    //Gateway

static const char* recvIP = "192.168.0.38";
  
//Creating Ethernet object
EthernetInterface eth;
 
void receiver()
{
    SocketAddress rd_addr;
    UDPSocket rd_sock(&eth);
 
    //bind to specific receiving port
    int bind = rd_sock.bind(PORT);
    printf("bind return: %d\n", bind);
 
    //buffer for received data
    char buffer[256];
 
    while(true){
    printf("\nWait for packet...\n");
    
    int ret = rd_sock.recvfrom(&rd_addr ,buffer, sizeof(buffer));
    if(ret > 0){
    buffer[ret] = '\0';
    //printf("Packet from \"%s\": %s\n", rd_addr.get_ip_address(), buffer);
   led1 = !led1;
    } 
    else{
    printf("error %d\n", ret);
    }
    Thread::wait(500);
    }
}

int main() {

     Thread receive;
     Thread transmit;

    // Set-up static IP network for transmitting device
    eth.set_network(recvIP, mbedMask, mbedGateway);// my device address

    // Start network
    eth.connect();
 
    receive.start(receiver);

        while(true){
 
            led2 = !led2;
                              
            Thread::wait(500);          
           led2 = !led2;    
          }
    }


Please log in to post comments.