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 11DHT 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 5MD_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}- 2why do you think that there are issues? ... nobody will waste time trying to figure out what problems you are seeing ... please describe what you expect to happens, what actually happens and what errors you get ... add all of that to the question ...do not write a commentjsotola– jsotola2023-08-23 06:22:14 +00:00CommentedAug 23, 2023 at 6:22
- my code seems to have some issues could mean anything. Describe the issues. We are not mind-readers.2023-08-24 09:24:00 +00:00CommentedAug 24, 2023 at 9:24
- To try to reproduce whatever these issues are we would have to download all those libraries you used, for which you have not provided links. It is not uncommon for libraries to be supplied on different sites, and be of different versions. If there is an error message please copy and paste it.2023-08-24 09:25:33 +00:00CommentedAug 24, 2023 at 9:25
- Move
unsigned long currentMillis = millis();to the end of thesetup()function, and move the rest ofTimePeriodIsOver()(actually only two lines of codes needed) to theloop(), and reset thechangeDisplayTimerif time is over (you never did that in your code). Write simple straightforward code, and only when it is work, then try to re-factor into function if necessary.hcheung– hcheung2023-08-24 11:09:58 +00:00CommentedAug 24, 2023 at 11:09 - The 'rk' which you mentioned in the title comes from the end of the string "does not work". This statement extracts it:
hour = Formatted_date.substring(11, 13);. The following statement reads past the end of the string so will return invalid data:minute = Formatted_date.substring(14, 16);. This comment in the program:// your code reading in and printing humidity and temperatureis to inform you where you should write your code to complete this assignment.6v6gt– 6v6gt2023-08-28 02:30:22 +00:00CommentedAug 28, 2023 at 2:30