Nano 33 iot won't connect to wifi after adding code for displaying sensor data

If I use the example code, the nano 33 iot connects to my wifi network without any issue. As soon as I add the code for reading the sensor and displaying the readings, it no longer wants to connect. Similar code is working on my UNO R3 with a ethernet shield without any problem. What wold be causing this, I did update the wifinina to the latest firmware (1.4.8 I believe). All I did was comment out the lines for printing out the analog lines and insert the code for printing out the sensor readings. Connecting to the network takes place further up in the code.

Here is the original code along with what I placed into the code, my added code begins at "begin my code":

/*
  WiFi Web Server

 A simple web server that shows the value of the analog input pins.

 This example is written for a network using WPA encryption. For
 WEP or WPA, change the WiFi.begin() call accordingly.

 Circuit:
 * Analog inputs attached to pins A0 through A5 (optional)

 created 13 July 2010
 by dlf (Metodo2 srl)
 modified 31 May 2012
 by Tom Igoe

 */

#include <SPI.h>
#include <WiFiNINA.h>
#include "SHT31.h"

SHT31 sht31 = SHT31();


#include "arduino_secrets.h" 
///////please enter your sensitive data in the Secret tab/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)
int keyIndex = 0;                 // your network key index number (needed only for WEP)

int status = WL_IDLE_STATUS;

WiFiServer server(80);

void setup() {
  //Initialize serial and wait for port to open:
  Serial.begin(9600);
  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 < WIFI_FIRMWARE_LATEST_VERSION) {
    Serial.println("Please upgrade the firmware");
  }

  // attempt to connect to WiFi network:
  while (status != WL_CONNECTED) {
    Serial.print("Attempting to connect to SSID: ");
    Serial.println(ssid);
    // Connect to WPA/WPA2 network. Change this line if using open or WEP network:
    status = WiFi.begin(ssid, pass);

    // wait 10 seconds for connection:
    delay(10000);
  }
  server.begin();
  // you're connected now, so print out the status:
  printWifiStatus();
}


void loop() {
  float temp = sht31.getTemperature();
  float humidity = sht31.getHumidity();
  float f;
  f = (temp * (9.0/5.0) + 32);
  // listen for incoming clients
  WiFiClient client = server.available();
  if (client) {
    Serial.println("new client");
    // an HTTP request ends with a blank line
    boolean currentLineIsBlank = true;
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();
        Serial.write(c);
        // if you've gotten to the end of the line (received a newline
        // character) and the line is blank, the HTTP request has ended,
        // so you can send a reply
        if (c == '\n' && currentLineIsBlank) {
          // send a standard HTTP response header
          client.println("HTTP/1.1 200 OK");
          client.println("Content-Type: text/html");
          client.println("Connection: close");  // the connection will be closed after completion of the response
          client.println("Refresh: 5");  // refresh the page automatically every 5 sec
          client.println();
          client.println("<!DOCTYPE HTML>");
          client.println("<html>");
          // output the value of each analog input pin
//          for (int analogChannel = 0; analogChannel < 6; analogChannel++) {
//            int sensorReading = analogRead(analogChannel);
//            client.print("analog input ");
//            client.print(analogChannel);
//            client.print(" is ");
//            client.print(sensorReading);
//            client.println("<br />");
//          }
begin my code
          client.println(temp);
          client.println(" C");
          Serial.println(temp);
          Serial.println(" C");
          client.println("<br />");
          client.println(f);
          client.println(" F");
          client.println("<TABLE>");
          client.println("<TR>");
          client.println("<TD>location1</TD><TD>location2</TD><TD>location3</TD>");
          client.println("</TR>");
          client.println("<TR>");
          client.println("<TD>"); client.println(humidity); client.println("</TD>");
          client.println("</TR>");
          client.println("</TABLE>");
          client.println("</html>");
          break;
        }
        if (c == '\n') {
          // you're starting a new line
          currentLineIsBlank = true;
        } else if (c != '\r') {
          // you've gotten a character on the current line
          currentLineIsBlank = false;
        }
      }
    }
    // give the web browser time to receive the data
    delay(1);

    // close the connection:
    client.stop();
    Serial.println("client disconnected");
  }
}


void printWifiStatus() {
  // print the SSID of the network you're attached to:
  Serial.print("SSID: ");
  Serial.println(WiFi.SSID());

  // print your board's IP address:
  IPAddress ip = WiFi.localIP();
  Serial.print("IP Address: ");
  Serial.println(ip);

  // print the received signal strength:
  long rssi = WiFi.RSSI();
  Serial.print("signal strength (RSSI):");
  Serial.print(rssi);
  Serial.println(" dBm");
}

If I comment out my code and uncomment the original it connects just fine. What would be causing this.

Ok, so looking at my code with the UNO and the Nano 33 IoT I found that I didn't use the "sht31.begin()" line in the setup section, would this cause the situation I am seeing

Tried adding the initialization code and that didn't have any affect.

Welcome to the forum.

Can you please modify your original post to ensure the source code is in a single box? It should look like this.

// Your example source code

You need three ' at the beginning and end in the edit window? When you click on this icon </> you get.

```
type or paste code here
```

This ensures we can get all the source code. Right now if you copy the code in cannot be compiled.

I also recommend you try the Tools -> Auto Format in the Arduino IDE. That will make it easier to read the code and to spot errors like missing or wrongly placed brackets.

I also checked to make sure it was not an issue with the router by doing a test with another router.

I started from scratch with the wifi web server code and placed my code back in. It is working fine now. Now to do some clean up and get the rest of the pieces in place.

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.