Struggle to run Arduino Uno Wifi R2 for more than 5 minutes.... :|

I've built a soil moisture and temperature monitoring station based on Arduino Uno Wifi Rev2, five soil moisture sensors and three DS18B20 temperature sensors.

I have very little experience in programming but somehow I've managed to build a sketch to read those measurements, count moving averages and push it to mqtt broker over WiFi.

The problem is.... that communication dies after couple of minutes.

This is what I see in the serial console:
https://pastebin.com/dcUPxkU4
Usually some garbage chars show up there.
No other lines are displayed until I reboot the device.

Here's my sketch. I know it's very primitive, but should work right?
https://pastebin.com/4CWNiw78

I've even mounted two additional step-down voltage converters (one for 9V -> 3.3V for moisture sensors, second for 9V -> 5V for dallas sensors) just to be sure Adruino is not overloaded by the sensors. I've measured all voltages and everything seems fine...... but it still crashes.

Any suggestions appreciated.
Best Regards,

My suggestion is that you post your output and code here on the forum rather than on Pastebin. Some forum members won't or can't access content hosted outside the forum, so you reduce your chances of getting help by sharing it that way.

You can learn how to do that properly here:

Output:

22:01:24.853 -> Attempting to connect to WPA SSID: XXXXXXXXXXXX
22:01:24.866 -> You're connected to the network!
22:01:24.912 -> 
22:01:24.912 -> Attempting to connect to the MQTT broker: XXXXXXXXXXXXXX
22:01:24.959 -> You're connected to the MQTT broker!
22:01:25.006 -> 
22:01:32.129 -> Sending message to topic: arduino/soilwatch1: 0 (Real value: 0)
22:01:32.176 -> Sending message to topic: arduino/soilwatch2: 1 (Real value: 1)
22:01:32.269 -> Sending message to topic: arduino/soilwatch3: 0 (Real value: 0)
22:01:32.316 -> Sending message to topic: arduino/soilwatch4: 0 (Real value: 0)
22:01:32.362 -> Sending message to topic: arduino/soilwatch5: -1 (Real value: -1)
22:01:32.456 -> Sending message to topic: arduino/soilwatch6: 43 (Real value: 43)
22:01:42.091 -> Sending message to topic: arduino/soilwatch1: 0 (Real value: 0)
22:01:42.137 -> Sending message to topic: arduino/soilwatch2: 1 (Real value: 0)
22:01:42.232 -> Sending message to topic: arduino/soilwatch3: 0 (Real value: -2)
22:01:42.280 -> Sending message to topic: arduino/soilwatch4: 1 (Real value: 1)
22:01:42.374 -> Sending message to topic: arduino/soilwatch5: 0 (Real value: 0)
22:01:42.421 -> Sending message to topic: arduino/soilwatch6: 42 (Real value: 40)
22:01:42.608 -> Sending message to topic: arduino/temperature1: 26.50 (Real value: 26.50)
22:01:42.702 -> Sending message to topic: arduino/temperature2: 25.80 (Real value: 25.75)
22:01:42.795 -> Sending message to topic: arduino/temperature3: 27.00 (Real value: 27.00)
22:01:52.061 -> Sending message to topic: arduino/soilwatch1: 0 (Real value: 0)
22:01:52.108 -> Sending message to topic: arduino/soilwatch2: 0 (Real value: -1)
22:01:52.199 -> Sending message to topic: arduino/soilwatch3: 0 (Real value: 0)
22:01:52.246 -> Sending message to topic: arduino/soilwatch4: 0 (Real value: 0)
22:01:52.292 -> Sending message to topic: arduino/soilwatch5: 0 (Real value: 0)
22:01:52.385 -> Sending message to topic: arduino/soilwatch6: 42 (Real value: 42)
22:02:02.024 -> Sending message to topic: arduino/soilwatch1: 0 (Real value: 0)
22:02:02.070 -> Sending message to topic: arduino/soilwatch2: 0 (Real value: 1)
22:02:02.165 -> Sending message to topic: arduino/soilwatch3: 0 (Real value: 0)
22:02:02.212 -> Sending message to topic: arduino/soilwatch4: 0 (Real value: -1)
22:02:02.258 -> Sending message to topic: arduino/soilwatch5: 0 (Real value: 1)
22:02:02.351 -> Sending message to topic: arduino/soilwatch6: 41 (Real value: 40)
22:02:02.537 -> Sending message to topic: arduino/temperature1: 26.50 (Real value: 26.50)
22:02:02.630 -> Sending message to topic: arduino/temperature2: 25.90 (Real value: 26.00)
22:02:02.723 -> Sending message to topic: arduino/temperature3: 26.90 (Real value: 26.75)
22:02:11.985 -> Sending message to topic: arduino/soilwatch1: 0 (Real value: 0)
22:02:12.033 -> Sending message to topic: arduino/soilwatch2: 0 (Real value: 1)
22:02:12.127 -> Sending message to topic: arduino/soilwatch3: 0 (Real value: 0)
22:02:12.174 -> Sending message to topic: arduino/soilwatch4: 0 (Real value: 0)
22:02:12.221 -> Sending message to topic: arduino/soilwatch5: 0 (Real value: 0)
22:02:12.315 -> Sending message to topic: arduino/soilwatch6: 41 (Real value: 42)
22:02:21.923 -> Sending message to topic: arduino/soilwatch1: 0 (Real value: 1)
22:02:22.015 -> Sending message to topic: arduino/soilwatch2: 0 (Real value: 0)
22:02:22.062 -> Sending message to topic: arduino/soilwatch3: 0 (Real value: 0)
22:02:22.155 -> Sending message to topic: arduino/soilwatch4: 0 (Real value: 0)
22:02:22.202 -> Sending message to topic: arduino/soilwatch5: 0 (Real value: 0)
22:02:22.296 -> Sending message to topic: arduino/soilwatch6: 42 (Real value: 42)
22:02:22.483 -> Sending message to topic: arduino/temperature1: 26.50 (Real value: 26.50)
22:02:22.529 -> Sending message to topi⸮

Code:

#include <ArduinoMqttClient.h>
#include <WiFiNINA.h>
#include "arduino_secrets.h"
#include <movingAvg.h>
#include <OneWire.h> 
#include <DallasTemperature.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)

char mqttuser[] = MQTT_USER;
char mqttpass[] = MQTT_PASS;

WiFiClient wifiClient;
MqttClient mqttClient(wifiClient);

const char broker[] = "192.168.203.233";
int        port     = 1883;
const char topic1[] = "arduino/soilwatch1";
const char topic2[] = "arduino/soilwatch2";
const char topic3[] = "arduino/soilwatch3";
const char topic4[] = "arduino/soilwatch4";
const char topic5[] = "arduino/soilwatch5";
const char topic6[] = "arduino/soilwatch6";
const char ttopic1[] = "arduino/temperature1";
const char ttopic2[] = "arduino/temperature2";
const char ttopic3[] = "arduino/temperature3";


const int analogIn1 = A0;   // Analog input pin that the sensor output is attached to (white wire)
int minADC1 = 16;           // replace with min ADC value read in air
int maxADC1 = 587;          // replace with max ADC value read fully submerged in water
const int analogIn2 = A1;   // Analog input pin that the sensor output is attached to (white wire)
int minADC2 = 20;           // replace with min ADC value read in air
int maxADC2 = 588;          // replace with max ADC value read fully submerged in water
const int analogIn3 = A2;   // Analog input pin that the sensor output is attached to (white wire)
int minADC3 = 15;           // replace with min ADC value read in air
int maxADC3 = 595;          // replace with max ADC value read fully submerged in water
const int analogIn4 = A3;   // Analog input pin that the sensor output is attached to (white wire)
int minADC4 = 18;           // replace with min ADC value read in air
int maxADC4 = 615;          // replace with max ADC value read fully submerged in water
const int analogIn5 = A4;   // Analog input pin that the sensor output is attached to (white wire)
int minADC5 = 28;           // replace with min ADC value read in air
int maxADC5 = 622;          // replace with max ADC value read fully submerged in water
const int analogIn6 = A5;   // Analog input pin that the sensor output is attached to (white wire)
int minADC6 = 28;           // replace with min ADC value read in air
int maxADC6 = 622;          // replace with max ADC value read fully submerged in water

int moistureValue1, moistureValue2, moistureValue3, moistureValue4, moistureValue5, moistureValue6;
int mappedValue1, mappedValue2, mappedValue3, mappedValue4, mappedValue5, mappedValue6; 

//set interval for sending messages (milliseconds)
const long interval = 10000;
const long interval2 = 20000;
unsigned long previousMillis = 0;
unsigned long previousMillis2 = 0;
movingAvg avgValue1(12);
movingAvg avgValue2(12);
movingAvg avgValue3(12);
movingAvg avgValue4(12);
movingAvg avgValue5(12);
movingAvg avgValue6(12);
movingAvg avgTemp1(12);
movingAvg avgTemp2(12);
movingAvg avgTemp3(12);

// Data wire is plugged into pin 2 on the Arduino 
#define ONE_WIRE_BUS 2 
// Setup a oneWire instance to communicate with any OneWire devices  
// (not just Maxim/Dallas temperature ICs) 
OneWire oneWire(ONE_WIRE_BUS); 
// Pass our oneWire reference to Dallas Temperature. 
DallasTemperature sensors(&oneWire);

DeviceAddress TempSensor1 = { 0x28, 0x67, 0x6C, 0x78, 0x4D, 0x20, 0x01, 0x62 };
DeviceAddress TempSensor2 = { 0x28, 0x39, 0x9E, 0x68, 0x4D, 0x20, 0x01, 0x42 };
DeviceAddress TempSensor3 = { 0x28, 0xF2, 0x01, 0xDA, 0x4D, 0x20, 0x01, 0x6B };

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
  }

  // attempt to connect to Wifi network:
  Serial.print("Attempting to connect to WPA SSID: ");
  Serial.println(ssid);
  while (WiFi.begin(ssid, pass) != WL_CONNECTED) {
    // failed, retry
    Serial.print(".");
    delay(5000);
  }

  Serial.println("You're connected to the network!");
  Serial.println();

  Serial.print("Attempting to connect to the MQTT broker: ");
  Serial.println(broker);

  mqttClient.setUsernamePassword(mqttuser, mqttpass);
  mqttClient.setId("arduino");

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

  Serial.println("You're connected to the MQTT broker!");
  Serial.println();

  avgValue1.begin();
  avgValue2.begin();
  avgValue3.begin();
  avgValue4.begin();
  avgValue5.begin();
  avgValue6.begin();
  avgTemp1.begin();
  avgTemp2.begin();
  avgTemp2.begin();

  // start oneWire instance
  sensors.begin();
  sensors.setResolution(TempSensor1, 10);
  sensors.setResolution(TempSensor2, 10);
  sensors.setResolution(TempSensor3, 10);
  
}

void wifiConnectionCheck() {
  if (WiFi.status() != WL_CONNECTED) {  
    Serial.print("Wifi Connection lost (Status=");
    Serial.print(WiFi.status());
    Serial.println(")");
    WiFi.end();   
    WiFi.disconnect();
    Serial.print("Attempting to reconnect to WPA SSID: ");
    Serial.println(ssid);
    while (WiFi.begin(ssid, pass) != WL_CONNECTED) {
      // failed, retry
      Serial.print(".");
      delay(5000);
    }
    Serial.println("You're connected to the network!");
  }
}

void mqttConnectionCheck() {
 if (!mqttClient.connected()) {
    Serial.println("MQTT connection lost");
    if (!mqttClient.connect(broker, port)) {
      Serial.print("MQTT connection failed! Error code = ");
      Serial.println(mqttClient.connectError());
      while (1);
    }
    Serial.println("You're connected to the MQTT broker!");
  }
}

void loop() {
  // call poll() regularly to allow the library to send MQTT keep alives which
  // avoids being disconnected by the broker
  mqttClient.poll();

  unsigned long currentMillis = millis();

  if (currentMillis - previousMillis >= interval) {
    // save the last time a message was sent
    previousMillis = currentMillis;

    // read moisture voltage values
    moistureValue1 = analogRead(analogIn1);
    moistureValue2 = analogRead(analogIn2);
    moistureValue3 = analogRead(analogIn3);
    moistureValue4 = analogRead(analogIn4);
    moistureValue5 = analogRead(analogIn5);
    moistureValue6 = analogRead(analogIn6);
    // map moisture voltage values to percents
    mappedValue1 = map(moistureValue1,minADC1,maxADC1, 0, 100);
    mappedValue2 = map(moistureValue2,minADC2,maxADC2, 0, 100);
    mappedValue3 = map(moistureValue3,minADC3,maxADC3, 0, 100);
    mappedValue4 = map(moistureValue4,minADC4,maxADC4, 0, 100);
    mappedValue5 = map(moistureValue5,minADC5,maxADC5, 0, 100);
    mappedValue6 = map(moistureValue6,minADC6,maxADC6, 0, 100);
    // calculate moving averages
    int averagedValue1 = avgValue1.reading(mappedValue1);
    int averagedValue2 = avgValue2.reading(mappedValue2);
    int averagedValue3 = avgValue3.reading(mappedValue3);
    int averagedValue4 = avgValue4.reading(mappedValue4);
    int averagedValue5 = avgValue5.reading(mappedValue5);
    int averagedValue6 = avgValue6.reading(mappedValue6);

    wifiConnectionCheck();
    mqttConnectionCheck();

    // Soil Moisture Sensor 1
    Serial.print("Sending message to topic: ");
    Serial.print(topic1);
    Serial.print(": ");
    Serial.print(averagedValue1);
    Serial.print(" (Real value: ");
    Serial.print(mappedValue1);
    Serial.println(")");
    // publish message
    mqttClient.beginMessage(topic1);
    mqttClient.print(averagedValue1);
    mqttClient.endMessage();

    // Soil Moisture Sensor 2
    Serial.print("Sending message to topic: ");
    Serial.print(topic2);
    Serial.print(": ");
    Serial.print(averagedValue2);
    Serial.print(" (Real value: ");
    Serial.print(mappedValue2);
    Serial.println(")");
    // publish message
    mqttClient.beginMessage(topic2);
    mqttClient.print(averagedValue2);
    mqttClient.endMessage();

    // Soil Moisture Sensor 3
    Serial.print("Sending message to topic: ");
    Serial.print(topic3);
    Serial.print(": ");
    Serial.print(averagedValue3);
    Serial.print(" (Real value: ");
    Serial.print(mappedValue3);
    Serial.println(")");
    // publish message
    mqttClient.beginMessage(topic3);
    mqttClient.print(averagedValue3);
    mqttClient.endMessage();

    // Soil Moisture Sensor 4
    Serial.print("Sending message to topic: ");
    Serial.print(topic4);
    Serial.print(": ");
    Serial.print(averagedValue4);
    Serial.print(" (Real value: ");
    Serial.print(mappedValue4);
    Serial.println(")");
    // publish message
    mqttClient.beginMessage(topic4);
    mqttClient.print(averagedValue4);
    mqttClient.endMessage();

    // Soil Moisture Sensor 5
    Serial.print("Sending message to topic: ");
    Serial.print(topic5);
    Serial.print(": ");
    Serial.print(averagedValue5);
    Serial.print(" (Real value: ");
    Serial.print(mappedValue5);
    Serial.println(")");
    // publish message
    mqttClient.beginMessage(topic5);
    mqttClient.print(averagedValue5);
    mqttClient.endMessage();

    // Soil Moisture Sensor 6
    Serial.print("Sending message to topic: ");
    Serial.print(topic6);
    Serial.print(": ");
    Serial.print(averagedValue6);
    Serial.print(" (Real value: ");
    Serial.print(mappedValue6);
    Serial.println(")");
    // publish message
    mqttClient.beginMessage(topic6);
    mqttClient.print(averagedValue6);
    mqttClient.endMessage();

  }

  unsigned long currentMillis2 = millis();

  if (currentMillis2 - previousMillis2 >= interval2) {
    // save the last time a message was sent
    previousMillis2 = currentMillis2;

    // DS18B20 sensors
    sensors.requestTemperatures();
    //delay(5000);
        
    // Soil Temperature Sensor 1
    float Temp1a = sensors.getTempC(TempSensor1); // get Temp
    int Temp1b = round(Temp1a*10); // multiply by 10, round, convert to integer
    int Temp1c = avgTemp1.reading(Temp1b); // calculate moving average
    float averagedTemp1 = (Temp1c/10.0);  // divide by 10, convert to float
    Serial.print("Sending message to topic: ");
    Serial.print("arduino/temperature1");
    Serial.print(": ");
    Serial.print(averagedTemp1);
    Serial.print(" (Real value: ");
    Serial.print(Temp1a);
    Serial.println(")");
    // publish message
    mqttClient.beginMessage(ttopic1);
    mqttClient.print(averagedTemp1);
    mqttClient.endMessage();

    // Soil Temperature Sensor 2
    float Temp2a = sensors.getTempC(TempSensor2); // get Temp
    int Temp2b = round(Temp2a*10); // multiply by 10, round, convert to integer
    int Temp2c = avgTemp2.reading(Temp2b); // calculate moving average
    float averagedTemp2 = (Temp2c/10.0);  // divide by 10, convert to float
    Serial.print("Sending message to topic: ");
    Serial.print("arduino/temperature2");
    Serial.print(": ");
    Serial.print(averagedTemp2);
    Serial.print(" (Real value: ");
    Serial.print(Temp2a);
    Serial.println(")");
    // publish message
    mqttClient.beginMessage(ttopic2);
    mqttClient.print(averagedTemp2);
    mqttClient.endMessage();

    // Soil Temperature Sensor 3
    float Temp3a = sensors.getTempC(TempSensor3); // get Temp
    int Temp3b = round(Temp3a*10); // multiply by 10, round, convert to integer
    int Temp3c = avgTemp3.reading(Temp3b); // calculate moving average
    float averagedTemp3 = (Temp3c/10.0);  // divide by 10, convert to float
    Serial.print("Sending message to topic: ");
    Serial.print("arduino/temperature3");
    Serial.print(": ");
    Serial.print(averagedTemp3);
    Serial.print(" (Real value: ");
    Serial.print(Temp3a);
    Serial.println(")");
    // publish message
    mqttClient.beginMessage(ttopic3);
    mqttClient.print(averagedTemp3);
    mqttClient.endMessage();
    
  }
}

@tomekwaw, your topic has been moved to a more suitable location on the forum.

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