Has anyone seen an example of multiple microcontrollers talking to each other using sockets compatible with Mbed 6? I have two Nucleo F767ZI connected to my router with ethernet cords currently with the data just going one way using the 6.13.0 library and am in mbed studio. I assume my mistake in is connecting with the right port or misuse of SocketAddress.
The code on the transmitting controller is
#include “mbed.h”
#include “EthernetInterface.h”
DigitalOut led1(LED1);
DigitalOut led2(LED2);
// Static IP network variables
static const char* mbedIP = “192.168.1.45”; //IP
static const char* mbedMask = “255.255.255.0”; // Mask
static const char* mbedGateway = “192.168.1.254”; //Gateway
static const char* recvIP = “192.168.1.38”;
const int PORT = 4466; //Port used for UDP communication
EthernetInterface eth; //Ethernet interface
int main()
{
eth.set_network(mbedIP, mbedMask, mbedGateway); printf("UDP Socket example\n"); SocketAddress sockAddr(recvIP, NSAPI_IPv4, PORT); if (eth.connect() != 0) { printf("Unable to connect to the Ethernet.\r\n"); return -1; } eth.get_ip_address(&sockAddr); printf("IP address is: %s\n", sockAddr.get_ip_address() ? sockAddr.get_ip_address() : "No IP"); UDPSocket td_sock; td_sock.open(ð); const char out_buffer[] = "very important data"; while (true) { int code = td_sock.sendto(sockAddr, out_buffer, sizeof(out_buffer)); //int code = td_sock.send(out_buffer, sizeof(out_buffer)); printf("%d \r\n", code); led2 = !led2; ThisThread::sleep_for(2000ms); }
}
the serial monitor then reads
UDPSocket example
192.168.1.45
20
20
20
On the receiving microcontroller it has
/*
Copyright (c) 2006-2020 Arm Limited and affiliates.
SPDX-License-Identifier: Apache-2.0
*/
#include “mbed.h”
#include “EthernetInterface.h”
DigitalOut led1(LED1);
DigitalOut led2(LED2);
// Static IP network variables
static const char* mbedIP = “192.168.1.45”; //IP
static const char* mbedMask = “255.255.255.0”; // Mask
static const char* mbedGateway = “192.168.1.254”; //Gateway
static const char* recvIP = “192.168.1.38”;
const int PORT = 4466; //Port used for UDP communication
EthernetInterface eth; //Ethernet interface
int main()
{
int code; eth.set_network(recvIP, mbedMask, mbedGateway); printf("UDP Socket example\n"); SocketAddress sockAddr(mbedIP, NSAPI_IPv4, PORT); if (eth.connect() != 0) { printf("Unable to connect to the Ethernet.\r\n"); return -1; } eth.get_ip_address(&sockAddr); printf("IP address is: %s\n", sockAddr.get_ip_address() ? sockAddr.get_ip_address() : "No IP"); UDPSocket td_sock; td_sock.open(ð); code = td_sock.getpeername(&sockAddr); td_sock.set_timeout(3000); printf("%d\r\n",code); while (true) { char in_buff[20]; //code = td_sock.recvfrom(&sockAddr, in_buff, sizeof(in_buff)); code = td_sock.recv(&in_buff, sizeof(in_buff)); printf("%d\r\n",code); led2 = !led2; //ThisThread::sleep_for(500ms); }
}
This prints UDP socket example
-3004
-3001
-3001
-3001
I have tried with and without the nsapi version. I have also tried without the timeout and it sticks forever. I assume the in_buff size doesn’t matter but I have tried making it 50 and 254 as well.
I have also checked the buffer with
printf(“%u, %u, %u,\r\n”,in_buff[0],in_buff[1], in_buff[2]);
and they all remain 0