I am working on a project where I read sensor data on a NUCLEO-L432KC and send it to a NUCLEO-F767ZI using TCP sockets in Mbed 6.15. The only way I can get it to work is to close the socket and reopen after each packet. When I am sending the packet to a YAT terminal on my PC it works to leave the socket open and is much faster.
The client device code is
#include “mbed.h”
#include “ICM20948.h”
#include “ESP8266Interface.h”
using namespace std::chrono;
ESP8266Interface wifi(PA_9, PA_10); //(TX, RX)
ICM20948 imu(PB_4, PA_7); //(SDA, SCL)
Timer t;
// Static IP network variables
static const char* mbedIP = “192.168.1.253”; //IP setting with ESP8266 not supported
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”; //my other mbed
//static const char* recvIP = “192.168.1.212”; //my laptop
const int PORT = 80; //Port used for UDP communication
int main()
{
DigitalOut led(LED1); int code; //wifi stuff //code = wifi.set_network(mbedIP, mbedMask, mbedGateway); //printf("IP set %d\r\n", code); printf("TCP Socket example\n"); //SocketAddress sockAddr(recvIP, NSAPI_IPv4, PORT); // wrong SocketAddress sockAddr; SocketAddress remoteSockAddr(recvIP, PORT); printf("Running\n"); wifi.connect("*myrouter*", "*mypassword*", NSAPI_SECURITY_WPA2); printf("%d\r\n", code); wifi.get_ip_address(&sockAddr); printf("IP address is: %s\n", sockAddr.get_ip_address() ? sockAddr.get_ip_address() : "No IP"); //IMU stuff printf("hello IMU!\r\n"); led = 1; code = imu.whoAmI(); printf("%d \r\n", code); led = !led; imu.init(); led = !led; printf("IMU initialized\r\n"); //socket stuff TCPSocket td_sock; td_sock.open(&wifi); td_sock.connect(remoteSockAddr); int send_counter = 0; t.start(); while (true) { //led = !led; float ax, ay, az, gx, gy, gz; float ax_data[10], ay_data[10], az_data[10]; float ax_delta[10], ay_delta[10], az_delta[10]; unsigned int time = duration_cast<microseconds>(t.elapsed_time()).count(); unsigned int time_tot = time + time_tot; //printf("%d\r\n", time); t.reset(); imu.getAccGyro(&ax, &ay, &az, &gx, &gy, &gz); //shift data down a position for (int i = 9; i > 0; i--) { ax_data[i] = ax_data[i-1]; ay_data[i] = ay_data[i-1]; az_data[i] = az_data[i-1]; } //add new first position ax_data[0] = ax; ay_data[0] = ay; az_data[0] = az; //find diffence in all previous stored time stamps and integrate over communication window for (int i = 1; i < 10; i++){ ax_delta[i-1] = ax_delta[i-1] + abs(ax - ax_data[i]); ay_delta[i-1] = ay_delta[i-1] + abs(ay - ay_data[i]); az_delta[i-1] = az_delta[i-1] + abs(az - az_data[i]); } send_counter ++; if (send_counter > 99){ char out_buffer[64]; out_buffer[0] = (time_tot >> 24); out_buffer[1] = ((time_tot >> 16) & 0xFF); out_buffer[2] = ((time_tot >> 8) & 0xFF); out_buffer[3] = (time_tot & 0xFF); for (int i = 0; i < 10; i++){ unsigned int _ax_delta[10], _ay_delta[10], _az_delta[10]; _ax_delta[i] = static_cast<int>(ax_delta[i] * 100.0f); _ay_delta[i] = static_cast<int>(ay_delta[i] * 100.0f); _az_delta[i] = static_cast<int>(ay_delta[i] * 100.0f); ax_delta[i] = 0; ay_delta[i] = 0; az_delta[i] = 0; out_buffer[(i*6) + 4] = (_ax_delta[i] >> 8); out_buffer[(i*6) + 5] = (_ax_delta[i] & 0xFF); out_buffer[(i*6) + 6] = (_ay_delta[i] >> 8); out_buffer[(i*6) + 7] = (_ay_delta[i] & 0xFF); out_buffer[(i*6) + 8] = (_az_delta[i] >> 8); out_buffer[(i*6) + 9] = (_az_delta[i] & 0xFF); } code = td_sock.sendto(remoteSockAddr, out_buffer, sizeof(out_buffer)); td_sock.close(); td_sock.open(&wifi); td_sock.connect(remoteSockAddr); printf("%d \r\n", code); led = !led; time_tot = 0; send_counter = 0; } }
}
The server device code is
#include “mbed.h”
#include “EthernetInterface.h”
DigitalOut led1(LED1);
DigitalOut led2(LED2);
#define IP “192.168.1.38”
#define GATEWAY “192.168.1.254”
#define NETMASK “255.255.255.0”
#define PORT 80
EthernetInterface* net;
TCPSocket server;
TCPSocket* clientSocket;
SocketAddress clientAddress;
char rxBuf[64];
int main(void)
{
printf("Starting\r\n"); net = new EthernetInterface; net->set_network(IP, NETMASK, GATEWAY); if (!net) { printf("Error! No network inteface found.\n"); return 0; } //net->set_network (IP, NETMASK, GATEWAY); // include to use static IP address nsapi_size_or_error_t r = net->connect(); if (r != 0) { printf("Error! net->connect() returned: %d\n", r); return r; } // Show the network address SocketAddress ip; SocketAddress netmask; SocketAddress gateway; net->get_ip_address(&ip); net->get_netmask(&netmask); net->get_gateway(&gateway); ip.set_port(PORT); const char* ipAddr = ip.get_ip_address(); const char* netmaskAddr = netmask.get_ip_address(); const char* gatewayAddr = gateway.get_ip_address(); printf("IP address: %s\r\n", ipAddr ? ipAddr : "None"); printf("Netmask: %s\r\n", netmaskAddr ? netmaskAddr : "None"); printf("Gateway: %s\r\n\r\n", gatewayAddr ? gatewayAddr : "None"); while (true) { printf("===================\r\n"); nsapi_error_t error = 0; server.open(net); server.bind(ip); server.listen(5); clientSocket = server.accept(&error); if (error != 0) { printf("Connection failed!\r\n"); } else { //clientSocket->set_timeout(200); clientSocket->getpeername(&clientAddress); printf("Client with IP address %s connected.\r\n\r\n", clientAddress.get_ip_address()); error = clientSocket->recv(rxBuf, sizeof(rxBuf)); printf("%d, %d, %d, %d\r\n", rxBuf[0], rxBuf[1], rxBuf[2], rxBuf[3]); } clientSocket->close(); }
}
Is there a better way to do this and does anyone have advice about the best next step to add more than one client device running at the same time?