This code kills my MKR 1010's ability to connect to the computer - why?

I’ve been working with a few sketches and things have been working fine. But if I upload the following to my MKR 1010, it stops appearing under ports in the Arduino IDE.

#include <SPI.h>
#include <PubSubClient.h>
#include <WiFiNINA.h>
#include "arduino_secrets.h"

char ssid[] = SECRET_SSID;        // your network SSID (name)
char pass[] = SECRET_PASS;    // your network password (use for WPA, or use as key for WEP)
IPAddress server(192,168,1,14);
IPAddress ip = WiFi.localIP();

void callback(char* topic, byte* payload, unsigned int length) {
  Serial.print("Message arrived [");
  Serial.print("] ");
  for (int i=0;i<length;i++) {

WiFiClient wifiClient;
PubSubClient client(wifiClient);
int status = WL_IDLE_STATUS;  

void reconnect() {
  // Loop until we're reconnected
  while (!client.connected()) {
    Serial.print("Attempting MQTT connection...");
    // Attempt to connect
    if (client.connect("arduinoClient")) {
      // Once connected, publish an announcement...
      client.publish("outTopic","hello world");
      // ... and resubscribe
    } else {
      Serial.print("failed, rc=");
      Serial.println(" try again in 5 seconds");
      // Wait 5 seconds before retrying

void setup() {
  // put your setup code here, to run once:
  while (!Serial);

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

    // wait 10 seconds for connection:

  // you're connected now, so print out the data: - this is also for debugging and should be commented out when running at the smoker if not debugging
  Serial.print("You're connected to the network");

  //mqtt part

  client.setServer(server, 1883);

void loop() {
  // put your main code here, to run repeatedly:
if (!client.connected()) {


The only way to get the port back is to reset the Arduino to a state where nothing is one it - double-click reset button. What’s going on here?


Here’s the minimal code to reproduce the issue:

#include <WiFiNINA.h>
IPAddress ip = WiFi.localIP();

void setup() {}
void loop() {}

The problem goes away if you call WiFi.localIP() from inside a function. Actually this function will only return if you call it before WiFi.begin() returns successfully so it wouldn’t make any sense to call the function there even if it didn’t break the USB.

I notice you’re not using ip in your code, so you should be able to simply remove that line. If you do need ip, then you can declare ip as a global variable:

IPAddress ip;

and then define it in setup():

ip = WiFi.localIP();

Removing that line fixed it for me, thanks!

I was trying to merge example code with the Ethernet shield and Wifinina and ended up with that error. Thanks a ton.

For the purpose of learning - how did you know that would be what would kill the USB?

Removing that line fixed it for me, thanks!

You're welcome. I'm glad to hear it's working now.

For the purpose of learning - how did you know that would be what would kill the USB?

I just started methodically removing code until the problem went away. The most efficient way to do this is the bisection technique. If you remove half the code and the problem goes away, then you know it was caused by something in the half you removed. Conversely, if the problem persists you know it's caused by something in the remaining code. Continue halfing the problematic code until you get down to the smallest amount of code that will still cause the problem.

Producing a minimal demonstration of the problem is often extremely useful for troubleshooting. Even if it doesn't point you to the solution, it is a very nice thing to have when you request help from others. That way, they can focus immediately on the problematic code instead of getting distracted or discouraged by large amounts of code that is completely irrelevant to the issue at hand.

Great points! Thanks!