Hi. Im having trouble being able to ping/connect to other devices on the same local network using the Arduino Nano 33 IoT. I am able to successfully ping the arduino FROM the windows laptop but unable to ping the windows laptop FROM the arduino. I am also able to successfully ping the windows laptop from a macbook on the same network (and vice versa). I also tried disabling the firewall and testing pinging the windows laptop from the arduino - to no avail. All 3 devices are on the same subnet.
Here is my code for the nano (built using PIO):
#include <WiFiNINA.h>
#include <WebSocketsClient.h>
#include <Arduino_JSON.h>
#include <Wire.h>
#include <Arduino_LSM6DS3.h>
const char *ssid = ""; // Removed for security in the forum post
const char *password = ""; // Removed for security in the forum post
const char *server = "20.1.1.251"; // Slightly changed the first digit for security purposes
const uint16_t port = 3000;
WebSocketsClient webSocket;
IPAddress ip;
void connectToWiFi()
{
Serial.begin(9600);
while (!Serial)
{
}
Serial.print("Connecting to WiFi...");
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED)
{
delay(1000);
Serial.print(".");
}
Serial.print("IP Address: ");
Serial.println(WiFi.localIP());
Serial.print("Attempting to connect to WS: ");
Serial.print(server);
Serial.print(":");
Serial.println(port);
}
void webSocketEvent(WStype_t type, uint8_t *payload, size_t length)
{
switch (type)
{
case WStype_CONNECTED:
Serial.println("[WebSocket] Connected to server");
webSocket.sendTXT("Arduino"); // Identify as Arduino client
break;
case WStype_DISCONNECTED:
Serial.println("[WebSocket] Disconnected");
break;
case WStype_TEXT:
Serial.print("[WebSocket] Text received: ");
Serial.println((char *)payload);
break;
case WStype_BIN:
Serial.println("Binary message received");
break;
case WStype_PING:
Serial.println("PING!");
break;
case WStype_PONG:
Serial.println("PONG!");
break;
default:
Serial.print("Unknown event type: ");
Serial.print(type);
break;
}
}
void setup()
{
connectToWiFi();
if (!IMU.begin())
{
Serial.println("Failed to initialize IMU!");
while (1)
{
}
}
Serial.println("IMU initialized");
// Ping the server
if (WiFi.hostByName(server, ip))
{
Serial.print("Server IP: ");
Serial.println(ip);
if (WiFi.ping(ip) >= 0)
{
Serial.println("Ping successful");
}
else
{
Serial.println("Ping failed");
}
}
else
{
Serial.println("Failed to resolve server IP");
}
webSocket.begin(server, port, "/");
webSocket.onEvent(webSocketEvent);
// Connect to the WebSocket server
webSocket.setReconnectInterval(1000); // try to reconnect every 5s
}
String getMeasurements()
{
if (!IMU.accelerationAvailable() || !IMU.gyroscopeAvailable())
return "";
// Read IMU data
float accX, accY, accZ;
float gyroX, gyroY, gyroZ;
// Read accelerometer data
IMU.readAcceleration(accX, accY, accZ);
// Read gyroscope data
IMU.readGyroscope(gyroX, gyroY, gyroZ);
// Create JSON object
JSONVar imuData;
imuData["acceleration"]["x"] = accX;
imuData["acceleration"]["y"] = accY;
imuData["acceleration"]["z"] = accZ;
imuData["gyroscope"]["x"] = gyroX;
imuData["gyroscope"]["y"] = gyroY;
imuData["gyroscope"]["z"] = gyroZ;
// Convert JSON object to string
String jsonString = JSON.stringify(imuData);
return jsonString;
}
void loop()
{
webSocket.loop();
String measurements = getMeasurements();
if (measurements != "")
{
String jsonString = JSON.stringify(measurements);
webSocket.sendTXT(jsonString);
}
}
here is the output from the serial output of the arduino:
---- Opened the serial port /dev/tty.usbmodem13201 ----
Connecting to WiFi...IP Address: 20.1.1.170
Attempting to connect to WS: 20.1.1.251:3000
IMU initialized
Server IP: 20.1.1.251
Ping failed
[WebSocket] Disconnected
[WebSocket] Disconnected
[WebSocket] Disconnected
[WebSocket] Disconnected
---- Closed the serial port /dev/tty.usbmodem1320 ----
This is really confusing me. I have tripple checked the IPs but here is the below:
Windows laptop ip: "20.1.1.251"; // Slightly changed the first digit for forum
Macbook laptop ip: "20.1.1.18"; // Slightly changed the first digit for forum
Arduino nano 33 IoT ip: "20.1.1.170"; // Slightly changed the first digit for forum z
What could be the reason for this?