Arduino nano 33 IoT sending UDP packets

Hello, I'm trying to send UDP packets from my Arduino nano 33 IoT to an ubuntu VM, both are connected to the same wifi, from ubuntu VM I can ping Arduino.
When Arduino starts sending udp packets, they don't reach ubuntu VM, and I'm wondering why.
Here's Arduino sketch:

#include <WiFiNINA.h>
#include <WiFiUdp.h>

//please enter your sensitive data in the Secret tab
char ssid[] = SECRET_SSID;                // your network SSID (name)
char pass[] = SECRET_PASS;                // your network password (use for WPA, or use as key for WEP)

int status = WL_IDLE_STATUS;
WiFiUDP Udp;

// IP address to send UDP packets to
const unsigned int localPort = 2390;
IPAddress remoteIP(192, 168, 60, 128);  // Replace with your computer's IP
const unsigned int remotePort = 2391;  // Replace with the port you're listening on

char packetBuffer[255];  // buffer per contenere il pacchetto in arrivo
const char ReplyBuffer[] = "acknowledged";  // una stringa da inviare come risposta

// Struttura per rappresentare il dispositivo
struct Device {
  const char* name;
  const char* wallet_config;
  const char* wallet_credentials;
  int pool_handle;
  const char* role;
};

// Funzione per inizializzare la struttura Device
void initDevice(Device& device) {
  device.name = "Device";
  device.wallet_config = "{\"id\":\"device_wallet\"}";
  device.wallet_credentials = "{\"key\":\"device_wallet_key\"}";
  device.pool_handle = 0;  // Sostituisci con il valore effettivo di pool_['handle']
  device.role = "ENDORSER";
}

// Istanza globale della struttura Device
Device myDevice;

void setup() {
  Serial.begin(9600);
  while (!Serial) {
    ; // attendi che la porta seriale si connetta. Necessario solo per la porta USB nativa
  }

  // Inizializza la struttura Device
  initDevice(myDevice);

  // Stampa i dati del dispositivo
  Serial.println("Device Info:");
  Serial.print("Name: ");
  Serial.println(myDevice.name);
  Serial.print("Wallet Config: ");
  Serial.println(myDevice.wallet_config);
  Serial.print("Wallet Credentials: ");
  Serial.println(myDevice.wallet_credentials);
  Serial.print("Pool Handle: ");
  Serial.println(myDevice.pool_handle);
  Serial.print("Role: ");
  Serial.println(myDevice.role);

  // Controlla il modulo WiFi:
  if (WiFi.status() == WL_NO_MODULE) {
    Serial.println("Comunicazione con il modulo WiFi fallita!");
    while (true);
  }

  // Tentativo di connessione alla rete WiFi:
  while (status != WL_CONNECTED) {
    Serial.print("Tentativo di connessione alla rete SSID: ");
    Serial.println(ssid);
    status = WiFi.begin(ssid, pass);
    delay(10000);
  }

  Serial.println("Connesso alla rete WiFi");
  printWiFiStatus();
  
  // Avvia l'UDP:
  Serial.println("Avvio UDP");
  Udp.begin(localPort);
}

void loop() {
  // Se ci sono dati disponibili, leggi un pacchetto
  int packetSize = Udp.parsePacket();
  if (packetSize) {
    Serial.print("Ricevuto pacchetto di dimensione ");
    Serial.println(packetSize);
    Serial.print("Da ");
    IPAddress remoteIp = Udp.remoteIP();
    Serial.print(remoteIp);
    Serial.print(", porta ");
    Serial.println(Udp.remotePort());

    // Leggi il pacchetto nel packetBuffer
    int len = Udp.read(packetBuffer, 255);
    if (len > 0) {
      packetBuffer[len] = 0;
    }
    Serial.println("Contenuto:");
    Serial.println(packetBuffer);

    // Invia una risposta all'indirizzo IP e alla porta che ci ha inviato il pacchetto che abbiamo ricevuto
    Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
    Udp.write(ReplyBuffer);
    Udp.endPacket();
  }

  // Invia un messaggio ogni 5 secondi
  static unsigned long lastSendTime = 0;
  if (millis() - lastSendTime > 5000) {
    sendDeviceInfo();
    lastSendTime = millis();
  }
}

void printWiFiStatus() {
  Serial.print("SSID: ");
  Serial.println(WiFi.SSID());

  IPAddress ip = WiFi.localIP();
  Serial.print("Indirizzo IP: ");
  Serial.println(ip);

  long rssi = WiFi.RSSI();
  Serial.print("Potenza del segnale (RSSI):");
  Serial.print(rssi);
  Serial.println(" dBm");
}

void sendDeviceInfo() {
  char message[255];
  IPAddress localIP = WiFi.localIP();
  snprintf(message, sizeof(message),
    "From Arduino %d.%d.%d.%d: Device info - Name: %s, Pool Handle: %d",
    localIP[0], localIP[1], localIP[2], localIP[3], myDevice.name, myDevice.pool_handle);
  
  Udp.beginPacket(remoteIP, remotePort);
  Udp.write(message);
  Udp.endPacket();
  Serial.print("Message sento to:  ");
  Serial.print(remoteIP);
  Serial.print(":");
  Serial.print(remotePort);
  Serial.println(message);
}

serial monitoring correctly perform the connection to the wifi, and output this message every 5 seconds:

"Message sent to 192.168.60.128:2391From Arduino 192.168.78.138: Device info - Name: Device, Pool Handle: 0"

But using netcat or tcpdump, or Wireshark on ubuntu vm, there's no incoming connection from Arduino.

Can you print the subnet currently set on the Arduino??
hope it's not 255.255.255.0
Also I do believe Udp.write(byte) without size param just writes one byte..
should be more like Udp.write(buf,sizeof(buf));
But thinking you should still be seeing a byte..
probably the subnet, should be something like 255.255.0.0 or 192.168.78.xxx can't get to 192.168.60.xxx..

good luck.. ~q

many thanks, didn't think about the subnet. how do I set it from 255.255.255.0 to 255.255.0.0?

in the router that's assigning the IP addresses..
i'd be curious how it got assigned an address on a different subnet..
~q

actually, all devices connected to wifi have 192.168.78.xxx, only the ubuntu vm has the 192.168.60.xxx , how is it possible?

virtual machine on top of what?? and what is it's ip address??
~q

I modified the subnet mask of the NAT adapter on VM, now th ubuntu vm has 192.168.78.xxx as well, but still doesn't receive anything from Arduino.
It's an ubuntu vm on windows, using VMware.
Host ip is 192.168.78.238, and ubuntu vm is on ens33 : 192.168.78.128

Not sure don't use VM..
i asked google..

To allow incoming connections on VMware, you need to configure the ESXi firewall by accessing the host settings in the vSphere Client, navigating to "Configure" then "Firewall", and specifying which IP addresses or networks can connect to the host by editing the allowed IP addresses for specific services within the firewall rules; essentially, you are defining which ports and protocols can receive incoming traffic from specific sources.

does any of that ring a bell??

~q

I'm not using vSphere, I'm using Vmware workstation, hosted on pc with windows OS.

and I already disabled firewall on ubuntu vm

sorry, like i mentioned, don't use VM..
let's ask uncle google again..
To allow incoming connections to a VMware Workstation virtual machine hosted on a Windows PC, you need to configure the virtual machine's network adapter to use "Bridged" mode, which directly connects the virtual machine to your physical network, enabling incoming connections from other devices on the network.

better??

you definitely have to configure something to allow for incoming connections..

good luck.. ~q

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.