I'm trying to get my ESP32 internet clock to show temperature and humidity data from a dht11 sensor on a MAX7219 display but my code seems to have some issues. After verifying the code there are no errors, however instead of showing time,temperature and humidity my display only reads two letters "rk." What am I doing wrong? Thanks.
#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;
int interval = 1000;
unsigned long updateTimer;
unsigned long changeDisplayTimer;
unsigned long changeDisplayPeriod = 3000;
boolean printTime = true;
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.begin(4);
Display.setIntensity(0);
Display.displayClear();
dht.begin();
}
void loop() {
// check if more time than stored in variable "changeDisplayPeriod"
// has passed by since last time this period was over
if ( TimePeriodIsOver(changeDisplayTimer, changeDisplayPeriod) ) {
// if more time REALLY has passed by
printTime = !printTime; // invert the value of flag "printTime"
}
if (printTime == true) {
obtainTime();
}
else { // which means printTime == false)
printHumTEmp();
}
}
void obtainTime() {
if ( TimePeriodIsOver(updateTimer, 1000) ) {
Formatted_date = "does not work";//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);
}
}
void printHumTEmp() {
if ( TimePeriodIsOver(updateTimer, 1000) ) {
// your code reading in
// and printing humidity and temperature
}
}
// easy to use helper-function for non-blocking timing
boolean TimePeriodIsOver (unsigned long &startOfPeriod, unsigned long TimePeriod) {
unsigned long currentMillis = millis();
if ( currentMillis - startOfPeriod >= TimePeriod ) {
// more time than TimePeriod has elapsed since last time if-condition was true
startOfPeriod = currentMillis; // a new period starts right here so set new starttime
return true;
}
else return false; // actual TimePeriod is NOT yet over
}