I want print data from a DHT11 (temperature and humidity) on a MAX7219 LED display but it only shows 0. Serial monitor, on the other hand, shows both humidity and temperature which means that my circuit works just fine and there's something wrong with code. Could anyone please point out what needs to be fixed to make it work? Thanks. enter image description here enter image description here Here is the code
#include <WiFi.h>
#include <NTPClient.h>
#include <DHT.h>
#include <WiFiUdp.h>
#include <MD_Parola.h>
#include <MD_MAX72xx.h>
#include <SPI.h>
#define HARDWARE_TYPE MD_MAX72XX::FC16_HW
//#define HARDWARE_TYPE MD_MAX72XX::GENERIC_H
#define MAX_DEVICES 4
#define CLK_PIN 5
#define DATA_PIN 7
#define CS_PIN 6
MD_Parola Display = MD_Parola(HARDWARE_TYPE, DATA_PIN, CLK_PIN, CS_PIN, MAX_DEVICES);
WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP);
const char* ssid = "6666";
const char* password = "6666";
String Time, hour, minute;
String Formatted_date;
long currentMillis = 0;
long previousMillis = 0;
int interval = 1000;
#define DHTPIN A2 /*Signal pin for DHT11 Sensor*/
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);
void setup() {
Serial.begin(115200);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print("Connecting.");
}
Serial.println("");
Serial.println("WiFi connected.");
timeClient.begin();
timeClient.setTimeOffset(0);
Display.begin();
Display.setIntensity(0);
Display.displayClear();
dht.begin();
Serial.println("Status\tHumidity (%)\tTemperature (C)");
}
void loop()
{
obtainTime();
}
void obtainTime() {
while(!timeClient.update()) {
timeClient.forceUpdate();
}
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);
delay(3000);
// The DHT11 returns at most one measurement every 3s
float h = dht.readHumidity();
//Read the moisture content in %.
float t = dht.readTemperature();
//Read the temperature in degrees Celsius
Serial.println(t);
Serial.println(h);
Display.setTextAlignment(PA_CENTER);
Display.print(t);
delay(5000);
Display.displayClear();
}
}
1 Answer 1
It turned out that MD_Parola can't handle floats directly. I fixed the problem by converting the floats t and h into strings using dtostrf()
float h = dht.readHumidity();
float t = dht.readTemperature();
char temp_result[6];
dtostrf(t,2,1,temp_result);
char hum_result[6];
dtostrf(h,2,0,hum_result);
Serial.println(t);
Serial.println(h);
Display.setTextAlignment(PA_CENTER);
//Display.print(temp_result);
Display.print((String)temp_result+" "+"C");
delay(5000);
//Display.print(hum_result);
Display.print((String)hum_result+" "+"%");
delay(5000);
t
is a float. Try temporarily changing this:Display.print(t);
toDisplay.print(42);
to see if the displayed temperature is something other zero. Also look at the Parola library to see what data type theprint()
method can handle. Maybe also try a minimal Parola "Hello World" type sketch to see if a simple sketch behaves as expected. It would be nice if you formatted your code in the IDE before posting so it would be easier to read.t
to a "c string" using dtostrf() , that is to a null terminated character array, then used that in the print() method for parola and it works. The conclusion is that parola can't handle a float datatype directly. I guess that you can now write your own answer to the original question to close the issue.