MKR WiFiClient connect fails

I have this code that seems to not work. It never connects to the server, but if I ping the board, it connects and starts sending data. If I stop the server, it reports sending 0 bytes, but never indicates that the connection to the server has failed. What am I doing incorrectly with the connection to the server?

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

#define SECRET_PASS “MyWiFiPassword”

#define TARGETPORT 44444

WiFiClient client;

int status = WL_IDLE_STATUS;

char ssid = SECRET_SSID; // your network SSID (name)
char pass = SECRET_PASS; // your network password (use for WPA, or use as key for WEP)

// Local Network
IPAddress targetIP(192,168,0,21);

int port = TARGETPORT;

String readData;

IPAddress remoteIp;

void printData() {
Serial.println(“Board Information:”);
// print your board’s IP address:
IPAddress ip = WiFi.localIP();
Serial.print("IP Address: ");

Serial.println(“Network Information:”);
Serial.print("SSID: ");

// print the received signal strength:
long rssi = WiFi.RSSI();
Serial.print(“signal strength (RSSI):”);


void setup() {

pinMode(0, INPUT);
pinMode(1, INPUT);
pinMode(2, INPUT);
pinMode(3, INPUT);
pinMode(4, INPUT);
pinMode(5, INPUT);
pinMode(6, INPUT);
pinMode(7, INPUT);
pinMode(8, INPUT);
pinMode(9, OUTPUT);
pinMode(10, OUTPUT);
pinMode(11, OUTPUT);
pinMode(12, OUTPUT);
//pinMode(13, INPUT); // RX
//pinMode(14, INPUT); // TX
// Actually Analog input pins
pinMode(15, INPUT);
pinMode(16, INPUT);
pinMode(17, INPUT);
pinMode(18, INPUT);
pinMode(18, INPUT);
pinMode(20, INPUT);
pinMode(21, INPUT);

// put your setup code here, to run once:
//Initialize serial and wait for port to open:
while (!Serial);

// attempt to connect to Wifi network:
while (WiFi.status() != WL_CONNECTED) {
Serial.print("Attempting to connect to network: ");
// Connect to WPA/WPA2 network:
status = WiFi.begin(ssid, pass);

// wait 10 seconds for connection:

// you’re connected now, so print out the data:
Serial.println(“You’re connected to the network”);


void loop() {
// put your main code here, to run repeatedly:
// check the network connection once every 10 seconds:
//Serial.println(“Processing Write Data”);
//Serial.println(“Processing Read Data”);




// Check to see if the client is connected to a server.
bool ClientConnected()
bool connected = false;

if (client.connected())
connected = true;
Serial.println(“Attempting to connect to server”);

if (client.connect(targetIP , TARGETPORT))
connected = true;
Serial.println(“Connected to server”);
Serial.println(“Connect returned false”);
Serial.println(“Leaving ClientConnected()”);

return connected;

// Fake write test
void WriteData()
Serial.println(“In WriteData()”);
// See if we are connected to the server.
if (ClientConnected())

// Say we are here
Serial.println(“Processing Write Data”);
// Declare final string.
String final = “”;
// Fill in the data
for (int x = 0; x < 22; x++)
final += String(“D”) + String(x) + String("=") + String(digitalRead(x)) + String("#");
// Add the terminator
final += String("^");
// Show the output
// Convert to a byte buffer
byte buffer [final.length()];
final.getBytes(buffer, 512);
// write it to the server.
int written = client.write(buffer, final.length());
Serial.print(“Wrote “);
Serial.print(” bytes to socket.”);
// Check the amount written
if (written = 0)
// This doesn’t seem to have any impact.

// Fake read test
void ReadData()
if (ClientConnected())
int bytesread;
byte buffer[512];
if ((bytesread =, sizeof(buffer))) > 0)
byte *data = new byte[bytesread];

memcpy(buffer, data, bytesread);

readData += String(*data);
int index;
if ((index = readData.indexOf(’^’)) > 0)
String fullTransmission = readData.substring(0, index);
readData.remove(0, index + 1);


void ParseValue(String data)
Serial.println(“Entering ParseValue”);
String IOPoint = “”;
int IOValue = 0;
int index = data.indexOf(’=’);
while (index > 0)
if (IOPoint.length() == 0)
IOPoint = data.substring(0, index);
data = data.substring(index + 1);
IOValue = data.toInt();

Serial.print("IOPoint “);
Serial.print(” = ");

Serial.println(“Exiting ParseValue”);

void ParseData(String data)
Serial.print(“Parsing “);
Serial.println(” from server”);

int index = data.indexOf("#");

while (index > 0)
String IOData = data.substring(0, index);


data = data.substring(index + 1);

index = data.indexOf("#");

Welcome to the forum.

Could you please read the "How to post? It’s at the beginning of each sub forum.

Please look especially at how to use code tags and modify your post. It should look like this. Makes it easier to copy the code and to read the post.

Your example code

There is only one sketch. What is on the other side? Do you have two Arduinos? One as server and one as client? If yes, could you post the sketch from the other side?

One thing I noted is a delay() in your code. It is generally not a good idea to use delay and communication stacks do not like that kind of thing. There are other means to execute functions in an interval. Have a look at the BlinkWithoutDelay example.

File -> Examples -> 02.Digital -> BlinkWithoutDelay

Additionally, you might want to investigate removing your WiFi code from setup and create a construct that tests the WiFi connection and restarts it when necessary. I like placing my communication code into a function with a state machine and call that every time I run through loop().

The readData function has a return at the beginning. I guess you left that from debugging.

What is a Level 2 programmer? :slight_smile: