Arduino MKR1010 WiFinina/MQTT weird connecting problem.

Hello everyone,

My first post in this forum. I have a couple of Arduino MKR 1010 and I'm having problems connecting to WiFi... or actually the total lack of connection thereof but here is thing, If I connect the Arduino to my computer and while the computer is ON and USB is connected, it will connect to WiFi... I mean is insanely slow to connect, somewhere between 15-20 seconds but it will connect almost every time. I turn off the computer, even though the USB HUB has power, and it will not connect... ever!

Any ideas?

Thank you so much in advance!

Here the logs:

20:26:38.287 -> Attempting to connect to WPA SSID: Pepeslatvia
20:26:50.579 -> Attempting to connect to WPA SSID: Pepeslatvia
20:27:03.777 -> Dns configured.You're connected to the networkSSID: Pepeslatvia
20:27:03.777 -> BSSID: 3E:94:ED:38:7F:A4
20:27:03.777 -> signal strength (RSSI):-33
20:27:03.777 -> Encryption Type:4
20:27:03.777 ->
20:27:03.777 -> IP Address: 192.168.1.124
20:27:03.777 -> 192.168.1.124
20:27:03.777 -> MAC address: 30:AE:A4:BA:82:C8
20:27:08.782 -> Attempting to connect to the MQTT broker: 192.168.1.8
20:27:09.124 -> Sending message to topic: IoT/SEVT/LWT
20:27:09.124 -> Connected
20:27:09.124 -> You're connected to the MQTT broker!
20:27:09.124 ->
20:27:09.124 -> Subscribing to topic: IoT/SEVT/Scene1r
20:27:09.124 ->
20:27:09.294 -> Subscribing to topic: IoT/SEVT/Scene2r
20:27:09.294 ->
20:27:09.328 -> Subscribing to topic: IoT/SEVT/Scene3r
20:27:09.328 ->
20:27:09.328 -> Subscribing to topic: IoT/SEVT/Scene4r
20:27:09.328 ->
20:27:09.328 -> Subscribing to topic: IoT/SEVT/Scene5r
20:27:09.328 ->
20:27:09.328 -> Subscribing to topic: IoT/SEVT/Scene6r
20:27:09.328 ->
20:27:09.364 -> Subscribing to topic: IoT/SEVT/Scene7r
20:27:09.364 ->
20:27:09.364 -> Subscribing to topic: IoT/SEVT/Scene8r
20:27:09.364 ->
20:27:09.364 -> Subscribing to topic: IoT/SEVT/Scene9r
20:27:09.364 ->

Segments of the sketch:

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; // the Wifi radio's status
WiFiClient wifiClient;
MqttClient mqttClient(wifiClient);
.
.
.
.
void setup() {
//Initialize serial and wait for port to open:
Serial.begin(9600);
Serial1.begin(9600);

// NEXTION
nexInit();
delay(1000);

bt0.attachPop(bt0PopCallback, &bt0);
bt1.attachPop(bt1PopCallback, &bt1);
bt2.attachPop(bt2PopCallback, &bt2);
bt3.attachPop(bt3PopCallback, &bt3);
bt4.attachPop(bt4PopCallback, &bt4);
bt5.attachPop(bt5PopCallback, &bt5);
bt6.attachPop(bt6PopCallback, &bt6);
bt7.attachPop(bt7PopCallback, &bt7);
bt8.attachPop(bt8PopCallback, &bt8);

// These lines will not let the Arduino to even boot!!! WTH!!!
//while (!Serial) {
// ; // wait for serial port to connect. Needed for native USB port only
//}

// check for the WiFi module:
if (WiFi.status() == WL_NO_MODULE) {
Serial.println("Communication with WiFi module failed!");
// don't continue
while (true);
}

String fv = WiFi.firmwareVersion();
if (fv < "1.0.0") {
Serial.println("Please upgrade the firmware");
}

WiFi.setHostname("ArduinoMKR1010-1");

// attempt to connect to Wifi network:
while (status != WL_CONNECTED) {
Serial.print("Attempting to connect to WPA SSID: ");
Serial.println(ssid);
// Connect to WPA/WPA2 network:
status = WiFi.begin(ssid, pass);
Serial1.print("t0.txt=");
Serial1.print(""");
Serial1.print("Initializing...");
Serial1.print(""");
Serial1.write(0xff);
Serial1.write(0xff);
Serial1.write(0xff);
// wait 10 seconds for connection:
delay(10000);
}

// print your WiFi's IP address:
WiFi.setDNS(dns);
Serial.print("Dns configured.");

// you're connected now, so print out the data:
Serial.print("You're connected to the network");
printCurrentNet();
printWifiData();
Serial1.print("t0.txt=");
Serial1.print(""");
Serial1.print(ssid);
Serial1.print(""");
Serial1.write(0xff);
Serial1.write(0xff);
Serial1.write(0xff);
delay(5000);

// You can provide a unique client ID, if not set the library uses Arduin-millis()
// Each client must have a unique client ID
mqttClient.setId("IoT-MKR-1010-1");

// set a will message, used by the broker when the connection dies unexpectantly
// you must know the size of the message before hand, and it must be set before connecting
String willPayload = "oh no!";
bool willRetain = true;
int willQos = 1;

mqttClient.beginWill(willTopic, willPayload.length(), willRetain, willQos);
mqttClient.print(willPayload);
mqttClient.endWill();

Serial.print("Attempting to connect to the MQTT broker: ");
Serial.println(broker);
Serial1.print("t0.txt=");
Serial1.print(""");
Serial1.print("Connecting MQTT");
Serial1.print(""");
Serial1.write(0xff);
Serial1.write(0xff);
Serial1.write(0xff);

if (!mqttClient.connect(broker, port)) {
Serial.print("MQTT connection failed! Error code = ");
Serial.println(mqttClient.connectError());
Serial.println(broker);
Serial1.print("t0.txt=");
Serial1.print(""");
Serial1.print("MQTT Error");
Serial1.print(""");
Serial1.write(0xff);
Serial1.write(0xff);
Serial1.write(0xff);
while (1);
}

//set the message receive callback
mqttClient.onMessage(onMqttMessage);
int subscribeQos = 1;

////////// LWT Startup Message Publish //////////
/////////////////////////////////////////////////
String payload = "Connected";
Serial.print("Sending message to topic: ");
Serial.println(willTopic);
Serial.println(payload);
Serial.println("You're connected to the MQTT broker!");
Serial.println();
Serial1.print("t0.txt=");
Serial1.print(""");
Serial1.print("MQTT Connected");
Serial1.print(""");
Serial1.write(0xff);
Serial1.write(0xff);
Serial1.write(0xff);
// send message, the Print interface can be used to set the message contents
// in this case we know the size ahead of time, so the message payload can be streamed
bool retained = false;
int qos = 1;
bool dup = false;

mqttClient.beginMessage(willTopic, payload.length(), true, 1);
mqttClient.print(payload);

Serial.print("Subscribing to topic: ");
Serial.println(Scene1rpub);
Serial.println();
mqttClient.subscribe(Scene1rpub, subscribeQos);

Serial.print("Subscribing to topic: ");
Serial.println(Scene2rpub);
Serial.println();
mqttClient.subscribe(Scene2rpub, subscribeQos);

Serial.print("Subscribing to topic: ");
Serial.println(Scene3rpub);
Serial.println();
mqttClient.subscribe(Scene3rpub, subscribeQos);

Serial.print("Subscribing to topic: ");
Serial.println(Scene4rpub);
Serial.println();
mqttClient.subscribe(Scene4rpub, subscribeQos);

Serial.print("Subscribing to topic: ");
Serial.println(Scene5rpub);
Serial.println();
mqttClient.subscribe(Scene5rpub, subscribeQos);

Serial.print("Subscribing to topic: ");
Serial.println(Scene6rpub);
Serial.println();
mqttClient.subscribe(Scene6rpub, subscribeQos);

Serial.print("Subscribing to topic: ");
Serial.println(Scene7rpub);
Serial.println();
mqttClient.subscribe(Scene7rpub, subscribeQos);

Serial.print("Subscribing to topic: ");
Serial.println(Scene8rpub);
Serial.println();
mqttClient.subscribe(Scene8rpub, subscribeQos);

Serial.print("Subscribing to topic: ");
Serial.println(Scene9rpub);
Serial.println();
mqttClient.subscribe(Scene9rpub, subscribeQos);

Serial1.print("t0.txt=");
Serial1.print(""");
Serial1.print("MQTT Ready");
Serial1.print(""");
Serial1.write(0xff);
Serial1.write(0xff);
Serial1.write(0xff);
}

To put your code in a code box, use the </> icon in the far left of the post tool bar and paste your code between the two bracket sets that appear.

To go back and put your code in a code box, in the bottom right of your post, select "more" and click modify. When the modify post opens, high light your code and click the </> in the far left of the post tool bar. This will put you code in code brackets. Then save the changes.