Keep losing wifi connection Arduino Uno WIfi rev2

Hello,

i’m in the process of building a project to water my garden. I have:

  • Arduino Uno wifi rev2
  • Relay (8 ports) with valves attached
  • Two temp/humid sensors

The code is uploading sensordata and weatherforecast (meteoserver) every hour at 5 minutes past the hour to a database on my homeserver. This part works (as long as my wifi isnt dropping).

Since a few days, my arduino board is losing its wifi connection with my wifinetwork after ‘a bit’. Sometimes 10 seconds, another time its after 30 seconds or a few minutes.

The connection starts with wifi status 3. When its connection is about to drop the wifistatus changes from 3 to 255. Not long after that the ipadress is 0.0.0.0.

In github there is a post that suggests to introduce a delay in the loop. I have a delay (1000) in the loop.

I can’t seem to figger out what i am doing wrong. The board is updated to 1.2.1 and the libraries i use to their latest versions. Is my board faulty or am i doing something wroing in my code (newbie here)?

Many thanks for any help!

Scoobie

My code:

#include <NTPClient.h>
#include <ArduinoHttpClient.h>
#include <SPI.h>
#include <WiFiNINA.h>
#include "arduino_secrets.h" 
#include <Wire.h>
#include <WiFiUdp.h>
#include "DFRobot_SHT20.h"
extern "C" {
#include "utility/twi.h" 
}
#define TCAADDR 0x70   

char ssid[] = SECRET_SSID; 
char pass[] = SECRET_PASS;

int waterdruk =1;
String apiKeyValue = "aapjessleutel";
const char serverName[] = "homeserver.nl";  // server name
float zover =0;

int pinArray[] = {2,3,4,5,6,7}; // define the pins that will connect to the relays

//pin2 = schuur1
//pin3 = schuur2
//pin4 = schuur3
//pin5 = schuur4
//pin6 = zweet1
//pin7 = pomp1

int port = 80;
int getDay();
int getHours();
int getMinutes();
int getSeconds();

DFRobot_SHT20 sht20[2];            

WiFiClient wifi;
HttpClient client = HttpClient(wifi, serverName, port);
WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP, "nl.pool.ntp.org", 7200, 600000); // 7200 is tijdsverschil, 600k is updatefrequentie in ms

int status = WL_IDLE_STATUS;
void setup() {
    for (int i = 0; i < (sizeof(pinArray)/sizeof(int)); i++){
      pinMode(pinArray[i],OUTPUT);
      digitalWrite(pinArray[i], HIGH);    // initialise all relays to HIGH (= off)
    }
    Serial.begin(115200);
      while (status != WL_CONNECTED) {
        Serial.print("Attempting to connect to WPA SSID: ");
        Serial.println(ssid);
        status = WiFi.begin(ssid, pass);  
        delay(10000);
       }
     Serial.print("You're connected to the network");
    
     Wire.begin();
     timeClient.begin();
 }

void loop() {
    timeClient.update();
    delay(1000);
    Serial.println(WiFi.status());
    Serial.println(WiFi.localIP());

    //sensor1: schuur
    Wire.beginTransmission(TCAADDR);
    Wire.write(1 << 1);
    Wire.endTransmission(); 
    sht20[1].initSHT20();
    delay(100);
    float humd = sht20[1].readHumidity();
    float temp = sht20[1].readTemperature();

    //sensor2: tuin
    Wire.beginTransmission(TCAADDR);
    Wire.write(1 << 2);
    Wire.endTransmission(); 
    sht20[2].initSHT20();
    delay(100);
    float humd2 = sht20[2].readHumidity();
    float temp2 = sht20[2].readTemperature();

    if (timeClient.getMinutes() == 5) { 

      Serial.println("making POST request sensordata");
      String httpRequestData = "api_key=" + apiKeyValue + "&humtemp_schuur_humid=" + humd + "&humtemp_schuur_temp=" + temp + "&humtemp_tuin_humid=" + humd2 + "&humtemp_tuin_temp=" + temp2 + "&waterdruk=" + waterdruk + "";
      String contentType = "application/x-www-form-urlencoded";
      client.post("/post-esp-data.php", contentType, httpRequestData);
      int statusCode = client.responseStatusCode();
      Serial.print("Status code sensor datadump: ");
      Serial.println(statusCode);
      String response = client.responseBody();
      Serial.print("Response sensor datadump: ");
      Serial.println(response);
      Serial.println("Wachttijd: 1 minuut");
      delay(60000);
      Serial.println("making POST request meteo");
      String httpRequestData2 = "api_key=" + apiKeyValue + "";
      client.post("/meteo.php", contentType, httpRequestData2);
      Serial.print("Status code meteo: ");
      Serial.println(statusCode);
      String response2 = client.responseBody();
      Serial.print("Response meteo: ");
      Serial.println(response2);
      Serial.println("Wachttijd: 1 minuut");
      delay(60000);
    }

 
}

Does the problem still occur if you disconnect the relay module?

Hi Pert,

i disconnected the realay module. The problem stays exactly the same. I did however realize that i was´t complete in my inventory as mentioned in my first post. In addition of the mentioned goodies:

The two temp humid sensors are SHT20 sen0227. I had to install a multiplexer (TCA9548A I2C) to give them seperate adresses. When i disconnect the power from the multiplexer, it seems that the problem has gone. Giving it power makes the problem reappear.

So i think it has to be the multiplexer.

Any clues?

Thanks !!

I think i have found my problem!

I had a memory leak:

I moved this code (and the one form the second sensor):

//sensor1: schuur
   Wire.beginTransmission(TCAADDR);
   Wire.write(1 << 1);
   Wire.endTransmission();
   sht20[1].initSHT20();
   delay(100);

to the void setup part. Now everything is running smoothly!

Thanks!