Strange results with a code used to add a missing zero to date/time

Hi,
I'm really a newbie ( I just entered this fascinating world less than three months ago) and probably this fact and my old age (72) are causing some problems during my experience with Arduino.
The problem
After many trials and errors I was able to arrange a sketch which manages a RTC DS 3231 clock, an OLED 1306 display, a DHT 11 sensor for air humidity and temperature and a soil humidity capacitive sensor ( the final goal is to monitor a big planter ); I quickly realized that both in serial monitor and on the OLED screen some data were written without the usual 0 when the hour, minute , second is less than 10.
I searched for theis problem in the forum but the solutions that I found were too difficult to me.
So I simply wrote an if cycle saying
if ((dt.hour)<10) {
Serial.print("0"); Serial.print(dt.hour); Serial.print(":");
}
else
{Serial.print(dt.hour); Serial.print(":");}
The same was made for minutes and seconds and with the proper modifications also for the 1306 OLED screen
At a first glance it seemed to work but at a deeper insight I realized I was getting some strange results

a) n the serial monitor the seconds are sometimes wrong ( for example 18...24 ...20...30...36 )

b) in the OLED the interval at which the seconds are displayed shows great variations
I'm afraid the problem is caused by the code I wrote to add a 0 but it could be also caused by the delays I added in some points of the sketch ( and I don't know if they are really useful but they were in the original sketches I used to create mine)
I'd be really grateful if you could suggest athe correct way of solving this problem hoping it will be understandable by a newbie as I am
Thanks in advance for your help
Here is the code

#include <DS3231.h>
#include <SPI.h>
#include<Wire.h>
#include "DHT.h"
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <splash.h>

#define OLED_RESET 4
Adafruit_SSD1306 display(OLED_RESET);

 DS3231 clock;
 RTCDateTime dt;

#define DHTPIN A0     // pin connected to DTH11

#define DHTTYPE DHT11   // DHT 11 

// Initialize DHT sensor for normal 16mhz Arduino
DHT dht(DHTPIN, DHTTYPE);


const int AirValue = 830;   // A1 reading when in open air
const int WaterValue = 565;  // A1 reading when immersed in water
int soilMoistureValue = 0;
int soilmoisturepercent = 0;
int sensorReading = analogRead(A1);

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
  }

  int soilMoistureValue = analogRead(A1);
//Initialize DS3231
Serial.println("Initialize DS3231");
clock.begin();

// Set sketch compiling time
  clock.setDateTime(__DATE__, __TIME__);

  dht.begin();

}
void loop() {

 dt = clock.getDateTime();
  if ((dt.hour)<10) {
    Serial.print("0"); Serial.print(dt.hour); Serial.print(":");
     }
     else
     {Serial.print(dt.hour);   Serial.print(":");}
    if ((dt.minute)<10) {
     Serial.print("0"); Serial.print(dt.minute); Serial.print(":");  
    }
    else
    {Serial.print(dt.minute); Serial.print(":");}
    if ((dt.second)<10) {
     Serial.print("0"); Serial.print(dt.second);Serial.print(" ");
    }
     else {
      Serial.print(dt.second); Serial.print(" ");
     }
  Serial.print(dt.year);   Serial.print("-");
  Serial.print(dt.month);  Serial.print("-");
  Serial.print(dt.day);    Serial.print(" ");
       
  delay(2000);

  // Reading temperature or humidity takes about 250 milliseconds!
  // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
  float h = dht.readHumidity();
  // Read temperature as Celsius
  float t = dht.readTemperature();

 
 int soilMoistureValue = analogRead(A1);
 
  // Check if any reads failed and exit early (to try again).
  if (isnan(h) || isnan(t))  {
    Serial.println("Failed to read from DHT sensor!");
    return;
  } 
soilMoistureValue = analogRead(A1);  //put Sensor insert into soil
Serial.print( "A1 reading is  ");
Serial.println(soilMoistureValue);
soilmoisturepercent = map(soilMoistureValue, AirValue, WaterValue, 0, 100);

 Serial.print("Humidity soil is ");
if(soilmoisturepercent >=100)
{ Serial.println("100%");
   }
  else if(soilmoisturepercent <=0)
  {
    Serial.println("0%");
     }
    else if(soilmoisturepercent >0 && soilmoisturepercent<100)
    {
      Serial.print(soilmoisturepercent);
      Serial.println("%");
         }
  Serial.print("Air_humid: "); 
  Serial.print(h);
  Serial.print(" %\t");
  Serial.print("Temp: "); 
  Serial.print(t);
  Serial.print(" *C ");
  delay(2000);
 
  display.begin(SSD1306_SWITCHCAPVCC, 0x3C); 
  display.clearDisplay();  
  display.setTextSize(1); 
  display.setTextColor(WHITE); 

  display.setCursor(0,0);
  if ((dt.hour)<10) {
    display.print("0"); display.print(dt.hour); display.print(":");}
    else {
  display.print(dt.hour);}
  
  display.setCursor(16,0);
   if ((dt.minute)<10) {
     display.print("0"); display.print(dt.minute); display.print(":");  
    }
    else {
  display.print(dt.minute); display.print(":");}
  display.setCursor(32,0);
  if ((dt.second)<10) {
     display.print("0"); display.print(dt.second);Serial.print(" ");
    }
     else {
    display.print(dt.second);
     }
 
  display.setCursor(60,0);
  display.print(dt.day);
  display.setCursor(78,0);
  display.print(dt.month);
  display.setCursor(90,0);
  display.print(dt.year);
  display.setCursor(0,8); 
  display.print("Air_humid is ");
  display.print(h);
  display.println(" %");
  display.setCursor(0,16);
  display.print("Temp is: "); 
  display.print(t);
  display.println(" C ");
  display.setCursor(0,24);
  display.print("A1 is ");
  display.println(soilMoistureValue);
   display.setCursor(60,24);
  display.print("H_soil = ");
      if(soilmoisturepercent >=100)
  { display.println("100%");
   }
    else if(soilmoisturepercent <=0)
  {
    display.println("0%");
     }
    else if(soilmoisturepercent >0 && soilmoisturepercent<100)
    {
      display.print(soilmoisturepercent);
     display.println("%");
         }
  display.display();
 display.clearDisplay();
   delay(1000);
  display.clearDisplay();
   

  }

You may want to consider a string formatting function, e.g.,

char timeString[9];
sprintf(timeString, "%02i:%02i:%02i", 1, 2, 3);
Serial.println(timeString);  // Prints "01:02:03".

For more information, see the sprintf and snprintf documentation.

[edit]

And the delays are certainly not helping when displaying a clock that needs to be updated regularly. If you want to reduce the number of reads from the input devices, maybe have a look at this article, where it is explained how to do something at regular intervals without having to use delay.

Hi,
thanks for your really ultrafast answer.
I understand that the code you suggest is going to substitute the if cycles but I really don't know wherr to inserti in my sketch for proper working...I told that I am a newbie for what attains Arduino and old for what attains the age and probably the brain
Thanks again for your help

I hadn't read the second part of your post Blink without delay. It's really interesting and I'm giung to try
Thx a lot

Okay, so you could replace the following,

with this:

  char timeString[9];
  sprintf(timeString, "%02i:%02i:%02i", dt.hour, dt.minute, dt.second);

And this piece of code,

with this:

  char dateString[11];
  sprintf(dateString, "%04i-%02i-%02i", dt.year, dt.month, dt.day);

Then, when you want to print the time and date to serial, you could do the following:

  Serial.print(timeString);
  Serial.print(' ');
  Serial.println(dateString);

And if you want to print it to the display, this should work:

  display.setCursor(0, 0);
  display.print(timeString);
  display.setCursor(60, 0);
  display.print(dateString);

There are quite some other issues with the code, but if you get these suggestions working first, we have a cleaner sketch to work on.

Some other issues we can address later:

  • soilMoistureValue is declared multiple times.
  • sensorReading is not used.
  • The extra check for a percentage being between 0 and 100 can be done with a constrain() call.
  • display.begin() and some other display functions should be moved to setup().
  • There are too many delays.
  • And then some more.

Notice how the last two lines of your 'if' part are the same as the last two lines of your 'else' part. That means that those two lines happen either way and don't need to be part of the 'if/else'. Those 11 lines can be reduced to these 4:

  if ((dt.hour)<10) 
    Serial.print('0'); 
  Serial.print(dt.hour);   
  Serial.print(":");

Hi Jfjlaros,
I followed your suggestions and it works just fine.
Really you deserve a big hug for your help.
Now I'm going to "clean the code" according to your suggestions
Thanks again

Hi,
thx for your suggestion.
I'm learning every day and it's really a long process

You are welcome.

Feel free to post the updated code in this thread if you want us to have a look at it some more.

Hi,

  1. I deleted unnecessary soilMoistureValue and sensorReading
  2. I reduced the delays to 100 instead of 2000 just leaving the (1000) delay for the OLED screen
  3. when I moved the display.begin() to setup it seems to have resolved the issue of strange values on the display
  4. strange values are still present in the serial monitor but I think that I just can suppress the infos coming from the RTC on hour minute second since they are already displayed on the serial monitor every time new values from the sensors are received
  5. I tried to use the constrain function for the values less than 0% or more than 100% according to what is suggested in constrain() - Arduino Reference but in my hands it gave errors so I was forced to use this cumbersome coding.

However here is the revised version of the code thanks to your help

#include <DS3231.h>
#include <SPI.h>
#include<Wire.h>
#include "DHT.h"
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <splash.h>

#define OLED_RESET 4
Adafruit_SSD1306 display(OLED_RESET);

 DS3231 clock;
 RTCDateTime dt;

#define DHTPIN A0     // 

#define DHTTYPE DHT11   // DHT 11 

// Initialize DHT sensor for normal 16mhz Arduino
DHT dht(DHTPIN, DHTTYPE);


const int AirValue = 830;   
const int WaterValue = 565;  
int soilMoistureValue = 0;
int soilmoisturepercent = 0;

 

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
  }
  display.begin(SSD1306_SWITCHCAPVCC, 0x3C);  
  display.clearDisplay();  
  display.setTextSize(1);  
  display.setTextColor(WHITE);
  
//Initialize DS3231
Serial.println("Initialize DS3231");
clock.begin();

// Set sketch compiling time
  clock.setDateTime(__DATE__, __TIME__);

  dht.begin();

}
void loop() {

 dt = clock.getDateTime();
 char timeString[9];
  sprintf(timeString, "%02i:%02i:%02i", dt.hour, dt.minute, dt.second);
 char dateString[11];
  sprintf(dateString, "%04i-%02i-%02i", dt.year, dt.month, dt.day);                                                        

delay(100);



  // Reading temperature or humidity takes about 250 milliseconds!
  // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
  float h = dht.readHumidity();
  // Read temperature as Celsius
  float t = dht.readTemperature();

 
 int soilMoistureValue = analogRead(A1);
 
  // Check if any reads failed and exit early (to try again).
  if (isnan(h) || isnan(t))  {
    Serial.println("Failed to read from DHT sensor!");
    return;
  } 

Serial.print( "A1 reading is  ");
Serial.println(soilMoistureValue);
soilmoisturepercent = map(soilMoistureValue, AirValue, WaterValue, 0, 100);

 Serial.print("Humidity soil is ");
if(soilmoisturepercent >=100)
{ Serial.println("100%");
   }
  else if(soilmoisturepercent <=0)
  {
    Serial.println("0%");
     }
    else if(soilmoisturepercent >0 && soilmoisturepercent<100)
    {
      Serial.print(soilmoisturepercent);
      Serial.println("%");
         }
  Serial.print("Air_humid: "); 
  Serial.print(h);
  Serial.print(" %\t");
  Serial.print("Temp: "); 
  Serial.print(t);
  Serial.print(" *C ");
  Serial.print(timeString);
  Serial.print(' ');
  Serial.println(dateString);
  
  delay(100);
 


  display.setCursor(0, 0);
  display.print(timeString);
  display.setCursor(60, 0);
  display.print(dateString);
  display.setCursor(0,8); 
  display.print("Air_humid is ");
  display.print(h);
  display.println(" %");
  display.setCursor(0,16);
  display.print("Temp is: "); 
  display.print(t);
  display.println(" C ");
  display.setCursor(0,24);
  display.print("A1 is ");
  display.println(soilMoistureValue);
   display.setCursor(60,24);
  display.print("H_soil = ");
      if(soilmoisturepercent >=100)
  { display.println("100%");
   }
    else if(soilmoisturepercent <=0)
  {
    display.println("0%");
     }
    else if(soilmoisturepercent >0 && soilmoisturepercent<100)
    {
      display.print(soilmoisturepercent);
     display.println("%");
         }
   display.display();
   display.clearDisplay();
   delay(1000);
   display.clearDisplay();
   

  }

That looks like a big improvement.

Perhaps you can specify this and show some examples?

Perhaps the following examples will help with understanding the constrain function:

Serial.println(constrain(-1, 0, 100));   // Prints "0".
Serial.println(constrain(50, 0, 100));   // Prints "50".
Serial.println(constrain(101, 0, 100));  // Prints "100".

We can feed the output of the map function to the constrain function in a similar way. The following lines,

could be replaced with these ones:

soilmoisturepercent = constrain(
  map(soilMoistureValue, AirValue, WaterValue, 0, 100), 0, 100);

Serial.print("Humidity soil is ");
Serial.print(soilmoisturepercent);
Serial.println("%");

Of course you can split the fist line if you find it more readable:

int mappedMoistureValue = map(soilMoistureValue, AirValue, WaterValue, 0, 100);
soilmoisturepercent = constrain(mappedMoistureValue, 0, 100);

Thanks for your help
I'm going to try what you are suggesting tomorrow morning since now I have to go for an unexpect problem
Have a nice night

Hi
Yes I agree with you that "looks like a big improv
I followed your suggestions and constrain works just fine.
You asked me to specify the"strange" values I'm getting on the serial monitor and I'm enclosing two screenshots.

It's not a major problem but I just liked to understand the possible reasons of this seemingly random behaviour : I doubt it can be related to the delays even if I shortened them to 100 milliseconds.

I take advantage of your courtesy and competence to submit another problem:
I'm enclosing the code that manages to use a datalogger (with the modifications you suggested for my other code) and it works fine, but if I try to add the part of the code related to the SD card and datalogger to the code that manages the OLED screen, I notice that only a single data acquisition appears on the SD and on the OLED screen and the system is apparently blocked. I don't understand where the mistake is.
Very grateful if you could clarify this issue

#include <SD.h>
#include <DS3231.h>
#include <SPI.h>
#include<Wire.h>
#include "DHT.h"

 DS3231 clock;
 RTCDateTime dt;

#define DHTPIN A0     // pin cui è connesso DHT11(D2 su Nano 33)

#define DHTTYPE DHT11   // DHT 11 

// Initialize DHT sensor for normal 16mhz Arduino
DHT dht(DHTPIN, DHTTYPE);


const int AirValue = 830;   // A1 reading when in open air
const int WaterValue = 565;  // A1 reading when immersed in water
int soilMoistureValue = 0;
int soilmoisturepercent = 0;
int sensorReading = analogRead(A1);

File SensData;

void setup() {

 
  Serial.begin(9600);
  int soilMoistureValue = analogRead(A0);

  // SD Card Initialization
  if (SD.begin())
  {
    Serial.println("SD card is ready to use.");
  } else
  {
    Serial.println("SD card initialization failed");
    return;
  }

//Initialize DS3231
Serial.println("Initialize DS3231");
clock.begin();

// Set sketch compiling time
  clock.setDateTime(__DATE__, __TIME__);

  dht.begin();
}
void loop() {

 dt = clock.getDateTime();
 
 char timeString[9];
  sprintf(timeString, "%02i:%02i:%02i", dt.hour, dt.minute, dt.second);
 char dateString[11];
  sprintf(dateString, "%04i-%02i-%02i", dt.year, dt.month, dt.day);                                                        

 
  delay(1000);



  // Reading temperature or humidity takes about 250 milliseconds!
  // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
  int h = dht.readHumidity();
  // Read temperature as Celsius
  int t = dht.readTemperature();

 
 int soilMoistureValue = analogRead(A1);
 
  // Check if any reads failed and exit early (to try again).
  if (isnan(h) || isnan(t))  {
    Serial.println("Failed to read from DHT sensor!");
    return;
  } 
 soilmoisturepercent = constrain(
  map(soilMoistureValue, AirValue, WaterValue, 0, 100), 0, 100);


 Serial.print("Humidity soil is ");
 Serial.print(soilmoisturepercent);
 Serial.println("%");
    
  Serial.print("Air_humid: "); 
  Serial.print(h);
  Serial.print(" %\t");
  Serial.print("Temp: "); 
  Serial.print(t);
  Serial.print(" *C ");
  Serial.print(timeString);
  Serial.print(' ');
  Serial.println(dateString);
  delay(10000);

   // Create/Open file 
 SensData = SD.open("SensData.txt", FILE_WRITE);
  
  // if the file opened okay, write to it:

 if (SensData) {
    Serial.println("Writing to file...");
    // Write to file
  SensData.print(timeString);;
  SensData.print(' ');
  SensData.print(dateString);
  
  SensData.print("Air temp: ");
  SensData.print(t ); 
  SensData.println("%");
  SensData.print("Air humid: ");
  SensData.print(h );
  SensData.println("%");
  SensData.print("Soil Humid  ");
  SensData.print(soilmoisturepercent);
  SensData.println("%");
  SensData.close();
  Serial.println("Done.");
  }
    // if the file didn't open, print an error:
  else {
    Serial.println("error opening SensData.txt");
  }
}

  
  

One more detail.
In the datalogger code i elongated the interval between the writings on SD card to 10 seconds and the "strange" values on the serial monitor recorded with the other code just disappeared.
Probably they are related to some conflict when the interval is too short
Here you have the screenshot of the datalogger

Hmm, I am not sure and I do not have the hardware to perform any tests.

Could there be a conflict between the SD and the OLED screen? Do they perhaps use the same pins?

I am just guessing here.

Hi,thx for the time you're spending helping me.
For what attains your question, the only info I have is that the OLED 1306 and the RTC DS3231 both use the I2C protocol with SDA/SCL but for datalogger it is a shield I bought from Ali Express with no documentation; I just plugged it over Uno and it worked.
So I can't answer your right question
I bought a datalogger working with Uno because I was not able to arrange the same scheme (DS3231 RTC, OLED 1306, DTH 11 hum & temp air sensor and soil humidity sensor) on a Nano 33.
My original project in fact was to monitor a planter by using a WiFi connection that is always on because it is used by a videosurveillance system.
But when I tried to add the WiFi to the code controlling sensors and OLED I got errors since I was not able to write on the "client" the same info I was able to write on the serial monitor and the OLED screen.
I'm enclosing the code ( that is amply commented BUT has not yet been modified according to your suggestions for the missing zero and the use the constrain)

/* this code is the fusion of two different codes, both of them working when tested separately. The first one sends to serial monitor and to an  OLED the outputs of two sensors ( a DHT11 and a capacitive soil humidity sensor);
the second one is the example known as WiFiWebServerof the Arduino library WiFiNINA abd is used to send the outputs of six sensors connected to analog pin to a remote PC.
When I tried to combine these two codes, obviously adapting them the situations of my sensors I succedd in compiling and uploading the code without "formal errors" but I was not able to get the IP address of the webserver and to see the outputs on the OLED
  */

// as a first step I've included the different libraries for RTC, DHT, OLED and WiFi

#include <DS3231.h>
#include <SPI.h>
#include <Wire.h>
#include "DHT.h"
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <splash.h>
#include <WiFiNINA.h>
#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) ; probably this line can be removed

//as a second step I've defined the objects WiFiserver, OLED, DS3231 clock, humidity and temperature sensor TYPE and PIN, calibration values for capacitive humidity soil sensor

int status = WL_IDLE_STATUS;
WiFiServer server(80);

#define OLED_RESET 4
Adafruit_SSD1306 display(OLED_RESET);

 DS3231 clock;
 RTCDateTime dt;

#define DHTPIN 2     // pin connected to DHT11(D2 on Nano 33)
#define DHTTYPE DHT11   // DHT 11 
// Initialize DHT sensor for normal 16mhz Arduino
DHT dht(DHTPIN, DHTTYPE);

const int AirValue = 830;   //this is the value I got from the soil humidity sensor when in air
const int WaterValue = 565;  //this is the value I got from the soil humidity sensor when in water
//int soilMoistureValue = 0; I commented this line because it caused a redefinition error since in line 81 the same variable is assigned to analogRead(A0)
int soilmoisturepercent = 0;
int sensorReading = analogRead(A0);

// as a third step I'm trying to connect Nano33 to my WiFi network
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 I don't know if I'm using a native USB or not in my system
  }

  // 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 Network named: ") //;the code stops at this level and just prints this info but nothing else
    Serial.println(ssid);  // print the network name (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:
//printWifi.Status(); I had to comment this line because it always gives the same error 'printWiFistatus' not declared in this scope; in the original example from WiFiNINA there was NO problem
}
 void Clock_and_Sensors() // since when I tried to write the following code lines before the void loop I got a lot of errors I tried to create a function just dedicated to the sensors and clock and it seems to work even if I don't know if it's correct
 { Serial.begin(9600);
  int soilMoistureValue = analogRead(A0);
//Initialize DS3231
Serial.println("Initialize DS3231");
clock.begin();

// Set sketch compiling time
 clock.setDateTime(__DATE__, __TIME__);// in this way when I upload the code the time is set according to this time

  dht.begin();

 }
void loop() 
{

dt = clock.getDateTime();
Serial.print("Raw data: ");
Serial.print(dt.year); 
Serial.print("-");
Serial.print(dt.month);  Serial.print("-");
Serial.print(dt.day);    Serial.print(" ");
Serial.print(dt.hour);   Serial.print(":");
Serial.print(dt.minute); Serial.print(":");
Serial.print(dt.second); Serial.println("");

  delay(1000);

  // Reading temperature or humidity takes about 250 milliseconds!
  // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
  float h = dht.readHumidity();
  // Read temperature as Celsius
  float t = dht.readTemperature();

 
 int soilMoistureValue = analogRead(A0);
 
  // Check if any reads failed and exit early (to try again).
  if (isnan(h) || isnan(t))  {
    Serial.println("Failed to read from DHT sensor!");
    return;
  } 
soilMoistureValue = analogRead(A0);  //this line is almost identical to line 81 and I don't know if it's just pleonastic or it is needed
Serial.print( "A0 reading is  ");
Serial.println(soilMoistureValue);
soilmoisturepercent = map(soilMoistureValue, AirValue, WaterValue, 0, 100);

 Serial.print("Humidity soil is ");
if(soilmoisturepercent >=100)
{ Serial.println("100%");
   }
  else if(soilmoisturepercent <=0)
  {
    Serial.println("0%");
     }
    else if(soilmoisturepercent >0 && soilmoisturepercent<100)
    {
      Serial.print(soilmoisturepercent);
      Serial.println("%");
         }
  Serial.print("Humidity: "); 
  Serial.print(h);
  Serial.print(" %\t");
  Serial.print("Temp: "); 
  Serial.print(t);
  Serial.print(" *C ");
 
  display.begin(SSD1306_SWITCHCAPVCC, 0x3C);  //the following four lines are used to setup the OLED
  display.clearDisplay();  
  display.setTextSize(1); 
  display.setTextColor(WHITE); 

  display.setCursor(0,0);
  display.print(dt.hour);
  display.setCursor(16,0);
  display.print(dt.minute);
  display.setCursor(32,0);
  display.print(dt.second);
  display.setCursor(60,0);
  display.print(dt.day);
  display.setCursor(78,0);
  display.print(dt.month);
  display.setCursor(90,0);
  display.print(dt.year);
  display.setCursor(0,8); 
  display.print("Air_h is ");
  display.print(h);
  display.println(" %");
  display.setCursor(0,16);
  display.print("Temp is: "); 
  display.print(t);
  display.println(" C ");
  display.setCursor(0,24);
  display.print("A0 is ");
  display.println(soilMoistureValue);
   display.setCursor(58,24);
  display.print("H_soil = "); //the following eleven lines are used to eliminate negative values or values over 100%; I tried to use " constrain " but I got errors
      if(soilmoisturepercent >=100) 
  { display.println("100%");
   }
    else if(soilmoisturepercent <=0)
  {
    display.println("0%");
     }
    else if(soilmoisturepercent >0 && soilmoisturepercent<100)
    {
      display.print(soilmoisturepercent);
     display.println("%");
         }
  display.display();
 display.clearDisplay();
   delay(1000);
  display.clearDisplay();
  
   // listen for incoming clients;// the following lines are really difficult for me because they are related also to HTML that I don't know so I just copied then from the example and I tried to adjust the code to my needs
  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
          int soilMoistureValue = analogRead(A0);
                     client.print("analog input ");
       //     client.print(); I commented this line because it caused an error
            client.print(" is ");
            client.print(soilMoistureValue);
            Serial.print("Humidity: "); 
              client.print(h);
              client.print(" %\t");
              client.print("Temp: "); 
              client.print(t);
              client.print(" *C ");            
              client.println("<br />");
          }
          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 with my request I'm transgressing the rules of the forum and I need to post this request in another section plz let me know:
Thanks, as usual, for you patience
Bruno

Perhaps this should be a call to WiFi.status? E.g.,

Serial.println(WiFi.status());

I think you wanted to call your "printWifiStatus()" function but put an extra '.' in the name. OR the error message is right and you tried to call "printWiFistatus" but capitalized the 'F' in "Wifi" and didn't capitalize the 's' in "Status".

Hi John,
thx for yor correct comment.
I followed your advice and the error just disappeared.
In fact I put an extra dot.
Sincerely, as a newbie ( I entered the Arduino world just three months ago) I'm amazed how "tight " are the rules for writing a correct code.
I thought I was involved in a fairly difficult work as a head and and skull base surgeon, but, believe me, I realize I had larger margins to err than when it comes to write an Arduino code...
But in this field the outcomes of a mistake maybe are less dangerous....
However, I'm still getting an error and I upload the file with the translation from Italian to English
error_message.txt (1,5 KB)
I think that the fact that more than one library is found for DS3231.h is ininfluent, because when I test the code without the part for WiFi it works fine.
Thx again for your help and have a nice day
Bruno

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