Chat freely about anything...

User avatar
By Dr.Faustus
#72921 Hallo to everybody,
i'm wornking on a smart thermostat with nodemcu and and dht11, the code is developed on the base of various sketches found on the web.
I want to print on the oled screen the variables of temperature and humidity given by dht.
a first sketch works fine:
Code: Select all/ Example sketch for testing OLED display

// We need to include Wire.h for I2C communication
#include <Wire.h>
#include "OLED.h"
#include "DHT.h"


// Declare OLED display
// display(SDA, SCL);
// SDA and SCL are the GPIO pins of ESP8266 that are connected to respective pins of display.
OLED display(4, 5);


#define DHTPIN 14     // PIN DEL SENSORE DHT
#define DHTTYPE DHT11   // DHT 11
// Initialize DHT sensor.
// Note that older versions of this library took an optional third parameter to
// tweak the timings for faster processors.  This parameter is no longer needed
// as the current DHT reading algorithm adjusts itself to work on faster procs.
DHT dht(DHTPIN, DHTTYPE);
static char T[5];
static char Hu[5];





void setup() {

  Serial.begin(9600);
  Serial.println("OLED test!");

  dht.begin();

  // Initialize display
  display.begin();

  // Test message

 

  delay(3*1000);

 
 
}




void loop() {
 
      // Wait a few seconds between measurements.
  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 (the default)
  float t = dht.readTemperature();
  // Read temperature as Fahrenheit (isFahrenheit = true)
  float f = dht.readTemperature(true);

  dtostrf(t,3, 2, T);
  dtostrf(h,3, 0, Hu);


 

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

  display.print(T,1);
  display.print(Hu,2);


 



 
}

I've used dtostrf function to convert float to char variable, in order to print them on the oled
it works fine for both t and h
One I put that in a more complex code, the output on the oled shows only T variable but not Hu (humidity).
I can't undestand why.
he code is
Code: Select all//Progetto di termostato "Smart".
//V0.3 29/12/2017
//sketch led adattato da quello pubblicato da STEM su internetofthinking.blogspot.it
//sketch sensore adattato da quello pubblicato da ladyada
//sketch oled adattato da

#include <ESP8266WiFi.h>
#include "DHT.h"
// We need to include Wire.h for I2C communication
#include <Wire.h>
#include "OLED.h"
 
const char* ssid = "***********";
const char* password = "****************";


// Declare OLED display
// display(SDA, SCL);
// SDA and SCL are the GPIO pins of ESP8266 that are connected to respective pins of display.
OLED display(4, 5);

 
int relay = 5; // GPIO5 impostiamo il pin  del relay
float temp = 17.0;//impostazione iniziale temperatura
int onoff = LOW; //impostiamo la variabile on/off

//dichiaro levariabili necessarie alla scrittura su oled
static char T[5];
static char Hu[3];
static char Temp[4];

WiFiServer server(80);


#define DHTPIN 14     // PIN DEL SENSORE DHT
#define DHTTYPE DHT11   // DHT 11
// Initialize DHT sensor.
// Note that older versions of this library took an optional third parameter to
// tweak the timings for faster processors.  This parameter is no longer needed
// as the current DHT reading algorithm adjusts itself to work on faster procs.
DHT dht(DHTPIN, DHTTYPE);

 
void setup() {
  Serial.begin(9600);
  delay(10);


  dht.begin();

  // Initialize display
  display.begin();
 



 
 
  pinMode(relay, OUTPUT);
  digitalWrite(relay, LOW);
 
  // Connect to WiFi network
  Serial.println();
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);
 
  WiFi.begin(ssid, password);
 
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.println("WiFi connected");
 
  // Start the server
  server.begin();
  Serial.println("Server started");
 
  // Print the IP address
  Serial.print("Use this URL to connect: ");
  Serial.print("http://");
  Serial.print(WiFi.localIP());
  Serial.println("/");
  display.print("Termostato smart",1);
  delay(500);
  display.print("Versione 0.3",2);
  delay(500);
  display.print("Created by AP",3);
  delay(500);
  display.print("Inizialise....",4);
  delay(2000);
  display.clear();
 
 
}
 
void loop() {
  // Wait a few seconds between measurements.
  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 (the default)
  float t = dht.readTemperature();
  // Read temperature as Fahrenheit (isFahrenheit = true)
  float f = dht.readTemperature(true);

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

 
  //converto le float t e h in stringhe T e H
  dtostrf(t,3, 2, T);
  dtostrf(h,3, 0, Hu);
  dtostrf(temp,2, 1, Temp);


 
   
  //Scrivere temperatura su display

  display.print("T:",1,0);
  display.print(T,1,3);
  display.print("gradi C",1,9);
  display.print("Umidita':",2,0);
  display.print(Hu,2,9);
  display.print("%",2,15);
  //display.print("Temp setted:",3,0);
  //display.print(Temp,3,12);
 
 

 
 
}
 

Someone could help me to find a reason for this?
Many thanks