Display data from DHT11 sensor / ESP32 internet clock

Hello. I've got this ESP32 internet clock and I'm thinking to add a DHT11 sensor it to show temperature and humidity. I've added a DHT11 library along with a few variables to my clock code but I'm not sure what to add to void loop() to make the ESP32 read DHT11 sensor's data and display it on an MAX7219 display every N seconds and switch back to time/clock. Any suggestion? Thanks.

Here is my code

#include <WiFi.h>
#include <NTPClient.h>
#include <WiFiUdp.h>
#include <MD_Parola.h>
#include <MD_MAX72xx.h>
#include <SPI.h>
#include <DHT.h>
#define DHTPIN 1
#define DHTTYPE DHT11   // DHT 11
DHT dht(DHTPIN, DHTTYPE);
float h;
float t;
#define HARDWARE_TYPE MD_MAX72XX::FC16_HW
//#define HARDWARE_TYPE MD_MAX72XX::GENERIC_H
#define MAX_DEVICES 4
#define CLK_PIN   18 
#define DATA_PIN  23 
#define CS_PIN    5 
MD_Parola Display = MD_Parola(HARDWARE_TYPE, DATA_PIN, CLK_PIN, CS_PIN, MAX_DEVICES);
WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP);
const char* ssid     = "12345";
const char* password = "12345";
String Time, hour, minute;
String Formatted_date;
long currentMillis = 0;
long previousMillis = 0;
int interval = 1000;
void setup() {
  Serial.begin(115200);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print("Connecting.");
    while(!timeClient.update()) {
    timeClient.forceUpdate();
  }
  }
  Serial.println("");
  Serial.println("WiFi connected.");
  timeClient.begin();
  timeClient.setTimeOffset(0);
  
  Display.begin();
  Display.setIntensity(0);
  Display.displayClear();
  dht.begin();
}

void loop()
 {
   obtainTime();
}
void obtainTime() {
  
  currentMillis = millis();
  if (currentMillis - previousMillis > interval)  {
previousMillis = millis();
Formatted_date = timeClient.getFormattedDate();
Serial.println(Formatted_date);
hour = Formatted_date.substring(11, 13);
minute = Formatted_date.substring(14, 16);

Time = hour + ":" + minute;
Serial.println(Time);
Display.setTextAlignment(PA_CENTER);
Display.print(Time);


}
} 

I haven't used the DHT11, but just by looking at the library examples, it looks like you can just use the output values from dht.readHumidity() and dht.readTemperature() (both of which output floats), and format it as you like, then pass it to the Display.print(); function you've already used in your code.

1 Like

a)
don't use an external library for NTP on an ESP32 (nor on the ESP8266).
NTP is part of the ESP core and there is an example in the IDE.

here I have a simplified variant of NTP for an ESP32
https://werner.rothschopf.net/microcontroller/202103_arduino_esp32_ntp_en.htm

b)
Press CTRL-T in the IDE to format the sketch in a better way.

1 Like

Thanks, I'll give it a shot

Thanks but this is not what I asked for :slight_smile:

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