I have the Nano 33 IoT, and I'm using the WiFiNINA library. Here is a very simple program that simply tries to detect incoming TCP connections. When a connection is made, I want the program to say "Got new client."
#include <WiFiNINA.h>
char ssid[] = ""; // replace with your network SSID
char pass[] = ""; // replace with your network password
WiFiServer server(52345); // you can choose a different port if needed
void setup() {
Serial.begin(9600);
while(!Serial) {}
if (WiFi.status() == WL_NO_MODULE) {
Serial.println("WiFi module not found!");
while (true);
}
Serial.print("Connecting to WiFi");
while (WiFi.begin(ssid, pass) != WL_CONNECTED) {
delay(1000);
Serial.print(".");
}
Serial.println("\nConnected.");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
server.begin();
Serial.println("TCP server started.");
}
WiFiClient client;
void loop() {
if (!client) {
client = server.accept();
if (client) {
Serial.println("Got new client");
}
}
if (client && !client.connected()) {
Serial.println("Client disconnected.");
client.stop();
client = WiFiClient();
}
}
Unfortunately, when I make a connection, no valid WiFiClient is returned by the server.accept() call. Here is a python program I am using to make the connection.
import socket
import time
ip = '....' # ip address of the Nano 33 IoT
port = 52345
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
def main():
try:
sock.connect((ip, port))
print("Connected!")
time.sleep(10)
finally:
print("Disconnecting!")
sock.close()
main()
The python program reports that the connection and disconnection are made correctly. Furthermore, if I send a byte down the socket sock.sendall(b'.'), the Nano detects the connection, but not the disconnect.