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_SSID “MYSSID”
#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(ip);

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

// print the received signal strength:
long rssi = WiFi.RSSI();
Serial.print(“signal strength (RSSI):”);
Serial.println(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:
Serial.begin(9600);
while (!Serial);

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

// wait 10 seconds for connection:
delay(10000);
}

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

Serial.println("----------------------------------------");
printData();
Serial.println("----------------------------------------");
}

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

delay(10000);

//Serial.println("----------------------------------------");

}

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

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

if (client.connect(targetIP , TARGETPORT))
{
connected = true;
Serial.println(“Connected to server”);
}
else
{
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
Serial.println(final);
// 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(written);
Serial.print(” bytes to socket.”);
// Check the amount written
if (written = 0)
{
// This doesn’t seem to have any impact.
client.stop();
}
}
}

// Fake read test
void ReadData()
{
return;
if (ClientConnected())
{
int bytesread;
byte buffer[512];
if ((bytesread = client.read(buffer, 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);

ParseData(fullTransmission);
}
}
}
}

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);
}
else
{
IOValue = data.toInt();

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

}
}
Serial.println(“Exiting ParseValue”);
}

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

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

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

ParseValue(IOData);

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: