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.