[SOLVED] ESP8266 - OLED 0.96 NTP Clock DHT22 - SSD1306 - compil error

SOLVED! scroll down for the code
ORIGINAL TITLE :
[ESP8266 - OLED 0.96 NTP Clock DHT22 - SSD1306 - no matching function for call]

Hello guys,

I’m beginning in the arduino topic, i’m trying to display with an OLED screen :

  • Date & Time (from a NTP server) ;
  • temperature ;
  • humidity ;

I’ve got an error with code compilation :

LINE : display.drawString(64, 50, temp);
ERROR MESSAGE : no matching function for call to ‘SSD1306Wire::drawString(int, int, float&)’

I’m requesting some help from experts … :slight_smile:

I do not know how to print on the Oled Screen the temperature and humidity.

I drop my code here :

// SKETCH DESCRIPTION : WEATHER STATION CLOCK - DHT22 - OLED 0,96
//
// Libraries needed:
//  Time.h & TimeLib.h:  https://github.com/PaulStoffregen/Time
//  Timezone.h: https://github.com/JChristensen/Timezone
//  SSD1306.h & SSD1306Wire.h:  https://github.com/squix78/esp8266-oled-ssd1306
//  NTPClient.h: https://github.com/arduino-libraries/NTPClient
//  ESP8266WiFi.h & WifiUDP.h: https://github.com/ekstrand/ESP8266wifi
//
// 128x64 OLED pinout:
// GND goes to ground
// Vin goes to 3.3V
// Data to I2C SDA >>> Pour NodeMcuV2 ou V3 : PIN D1 sur SCL | PIN D2 sur SDA
// Clk to I2C SCL  >>>  

#include <ESP8266WiFi.h>
#include <WifiUDP.h>
#include <String.h>
#include <Wire.h>
#include <SSD1306.h>
#include <SSD1306Wire.h>
#include <NTPClient.h>
#include <Time.h>
#include <TimeLib.h>
#include <Timezone.h>
#include <DHT.h>
#include <Adafruit_Sensor.h>


// Define NTP properties
#define NTP_OFFSET   60 * 60      // In seconds
#define NTP_INTERVAL 60 * 1000    // In miliseconds
#define NTP_ADDRESS  "ntp.unice.fr"  // change this to whatever pool is closest (see ntp.org)

#define DHTPIN D6     // Digital pin connected to the DHT sensor
#define DHTTYPE    DHT22     // DHT 22 (AM2302)
DHT dht(DHTPIN, DHTTYPE);

// Set up the NTP UDP client
WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP, NTP_ADDRESS, NTP_OFFSET, NTP_INTERVAL);

// Create a display object
SSD1306  display(0x3c, D2, D1); //0x3d for the Adafruit 1.3" OLED, 0x3C being the usual address of the OLED

 
const char* ssid = "WiFi";                       // insert your own ssid 
const char* password = "PWD";              // and password
String date;
String t;
const char * days[] = {"DIM", "LUN", "MAR", "MER", "JEU", "VEN", "SAM"} ;
const char * months[] = {"Janv", "Fevr", "Mars", "Avril", "Mai", "Juin", "Juil", "Aout", "Sept", "Oct", "Nov", "Dec"} ;
const char * ampm[] = {"AM", "PM"} ;
 
void setup () 
{
  Serial.begin(115200); // most ESP-01's use 115200 but this could vary

  dht.begin();
  
  timeClient.begin();   // Start the NTP UDP client

//  Wire.pins(0, 2);  // Start the OLED avec SCL sur D1 et SDA sur D2
//  Wire.begin(0, 2); // 0=sda, 2=scl
  display.init();
  display.flipScreenVertically();   

  // Connect to wifi

  Serial.println("");
  Serial.print("Connexion ...");
  Serial.print(ssid);
  display.drawString(20, 0, "|||||| -STARTED- ||||||");
  display.drawString(0, 12, "NTP_CLO_24H_FR.ino");
  display.drawString(0, 24, "Connexion au WiFi...");
//  display(0, 30, NTPadress);
  display.display();
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) 
  {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.print("Connexion au WiFi... ");
  Serial.print(WiFi.localIP());
  Serial.println("");
  display.drawString(0, 35, "Connecté !");
  display.display();
  delay(1000);
}

void loop() 
{
  //read temperature and humidity
  float temp = dht.readTemperature();
  float humi = dht.readHumidity();
  if (isnan(humi) || isnan(temp)) {
    Serial.println("Failed to read from DHT sensor!");
  }


  
  if (WiFi.status() == WL_CONNECTED) //Check WiFi connection status
  {   
    date = "";  // clear the variables
    t = "";


    
    // update the NTP client and get the UNIX UTC timestamp 
    timeClient.update();
    unsigned long epochTime =  timeClient.getEpochTime();

    // convert received time stamp to time_t object
    time_t local, utc;
    utc = epochTime;

    // Then convert the UTC UNIX timestamp to local time
    TimeChangeRule usEDT = {"EDT", Second, Sun, Mar, 2, 60};  //UTC -+ change this as needed
    TimeChangeRule usEST = {"EST", First, Sun, Nov, 2, 0};   //UTC -+ change this as needed
    Timezone usEastern(usEDT, usEST);
    local = usEastern.toLocal(utc);

    // now format the Time variables into strings with proper names for month, day etc
    date += days[weekday(local)-1];
    date += ", ";
    date += day(local);
    date += " "; 
    date += months[month(local)-1];
    date += " ";
    date += year(local);

    // format the time to 24-hour format
    t += hour(local);
    t += ":";
    if(minute(local) < 10) // add a zero if minute is under 10
    t += "0";
    t += minute(local);
    t += ":";
    if(second(local) < 10) // add a zero if minute is under 10
    t += "0";
    t += second(local);

    // Display the date and time
    Serial.println("");
    Serial.print(" ");    
    Serial.print("Local date: ");
    Serial.print(date);
//    Serial.println("");
    Serial.print(" - ");
    Serial.print("Local time: ");
    Serial.print(t);
    Serial.print(" - ");
//    Serial.println("");
    Serial.print("temp: "); 
    Serial.print(temp);
    Serial.print("°C");    
    Serial.print(" - ");
    Serial.print("humi: "); 
    Serial.print(humi);
    Serial.print("%");             

    // print the date and time on the OLED
    display.clear();
    display.setTextAlignment(TEXT_ALIGN_CENTER);
    display.setFont(ArialMT_Plain_24);
    display.drawStringMaxWidth(64, 10, 128, t);
    display.setFont(ArialMT_Plain_10);
    display.drawStringMaxWidth(64, 38, 128, date);
    display.drawString(64, 50, temp);                                    !!!! ERROR !!!!
    
    
    
    display.display();

    
  }
  else // attempt to connect to wifi again if disconnected
  {
    display.clear();
    display.drawString(0, 10, "Reconnexion au WiFi");
    display.display();
    WiFi.begin(ssid, password);
    display.drawString(0, 24, "Connecté !");
    display.display();
    delay(1000);
  }
    
  delay(1000);    //Send a request to update every 10 sec (= 10,000 ms)
}

Please read the sticky post at the top of the forum to learn how to post your code and error messages inside code tags. It will help people help you.

The error is due to the fact that your 'temp' variable is of type 'float', not a string. I don't know that library, but maybe there is a different call like drawFloat() or something. If not, you will have to convert your float to a string first.

Note that you declare a global variable 'temp' which is of type String, but you declare another variable 'temp' inside loop of type float which hides the global declaration.

Hello blh64 !

Thank you for your answer.

I'm still looking for a solution to print the TEMP and HUMI variable with SSD1306.h library.

If someone can help me with this...

:slight_smile: Finally ! i found the solution on another site.

To print the variable TEMP the right syntaxe is :

display.drawString(0, 50, String(temp));

SO… HERE THE WORKING CODE : (Need some modifications but it works, that’s what we want !)

// SKETCH DESCRIPTION : TEMPERATURE & HUMIDITY + CLOCK [ESP8266 - NODEMCU]
// DEVICES : DHT22 - OLED 0,96
// AUTHOR : -3x0c3t-

// 128x64 OLED pinout:
// GND > ground | Vin > 3.3V | SDA > D1 | SCL > D2

// DHT SENSOR
// DATA > D6
 

#include <ESP8266WiFi.h>
#include <WifiUDP.h>
#include <String.h>
#include <Wire.h>
#include <SSD1306.h>
#include <SSD1306Wire.h>
#include <NTPClient.h>
#include <Time.h>
#include <TimeLib.h>
#include <Timezone.h>
#include <DHT.h>
#include <Adafruit_Sensor.h>


// Define NTP properties
#define NTP_OFFSET   60 * 60      // In seconds
#define NTP_INTERVAL 60 * 1000    // In miliseconds
#define NTP_ADDRESS  "ntp.unice.fr"  // change this to whatever pool is closest (see ntp.org)

#define DHTPIN D6     // Digital pin connected to the DHT sensor
#define DHTTYPE    DHT22     // DHT 22 (AM2302)
DHT dht(DHTPIN, DHTTYPE);

// Set up the NTP UDP client
WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP, NTP_ADDRESS, NTP_OFFSET, NTP_INTERVAL);

// Create a display object
SSD1306  display(0x3c, D2, D1); //0x3d for the Adafruit 1.3" OLED, 0x3C being the usual address of the OLED

 
const char* ssid = "YOUR WiFi SSID";                                // insert your own ssid 
const char* password = "YOUR WiFi PASSWORD";              // and password
String date;
String t;
const char * days[] = {"DIM", "LUN", "MAR", "MER", "JEU", "VEN", "SAM"} ;
const char * months[] = {"Janv", "Fevr", "Mars", "Avril", "Mai", "Juin", "Juil", "Aout", "Sept", "Oct", "Nov", "Dec"} ;
const char * ampm[] = {"AM", "PM"} ;
 
void setup () 
{
  Serial.begin(115200); // most ESP-01's use 115200 but this could vary

  dht.begin();
  
  timeClient.begin();   // Start the NTP UDP client

//  Wire.pins(0, 2);  // Start the OLED avec SCL sur D1 et SDA sur D2
//  Wire.begin(0, 2); // 0=sda, 2=scl
  display.init();
  display.flipScreenVertically();   

  // Connect to wifi

  Serial.println("");
  Serial.print("Connexion ...");
  Serial.print(ssid);
  display.drawString(20, 0, "|||||| -3x0c3t- ||||||");
  display.drawString(0, 12, "WEATHER STATION CLOCK");
  display.drawString(0, 24, "Connexion au WiFi...");
//  display(0, 30, NTPadress);
  display.display();
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) 
  {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.print("Connexion au WiFi... ");
  Serial.print(WiFi.localIP());
  Serial.println("");
  display.drawString(0, 35, "Connecté !");
  display.display();
  delay(1000);
}

void loop() 
{
  //read temperature and humidity
  float temp = dht.readTemperature();
  float humi = dht.readHumidity();
  if (isnan(humi) || isnan(temp)) {
    Serial.println("Failed to read from DHT sensor!");
  }


  
  if (WiFi.status() == WL_CONNECTED) //Check WiFi connection status
  {   
    date = "";  // clear the variables
    t = "";


    
    // update the NTP client and get the UNIX UTC timestamp 
    timeClient.update();
    unsigned long epochTime =  timeClient.getEpochTime();

    // convert received time stamp to time_t object
    time_t local, utc;
    utc = epochTime;

    // Then convert the UTC UNIX timestamp to local time
    TimeChangeRule usEDT = {"EDT", Second, Sun, Mar, 2, 60};  //UTC -+ change this as needed
    TimeChangeRule usEST = {"EST", First, Sun, Nov, 2, 0};   //UTC -+ change this as needed
    Timezone usEastern(usEDT, usEST);
    local = usEastern.toLocal(utc);

    // now format the Time variables into strings with proper names for month, day etc
    date += days[weekday(local)-1];
    date += ", ";
    date += day(local);
    date += " "; 
    date += months[month(local)-1];
    date += " ";
    date += year(local);

    // format the time to 24-hour format
    t += hour(local);
    t += ":";
    if(minute(local) < 10) // add a zero if minute is under 10
    t += "0";
    t += minute(local);
    t += ":";
    if(second(local) < 10) // add a zero if minute is under 10
    t += "0";
    t += second(local);

    // Display the date and time
    Serial.println("");
    Serial.print(" ");    
    Serial.print("Local date: ");
    Serial.print(date);
//    Serial.println("");
    Serial.print(" - ");
    Serial.print("Local time: ");
    Serial.print(t);
    Serial.print(" - ");
//    Serial.println("");
    Serial.print("temp: "); 
    Serial.print(temp);
    Serial.print("°C");    
    Serial.print(" - ");
    Serial.print("humi: "); 
    Serial.print(humi);
    Serial.print("%");             

    // print the date and time on the OLED
    display.clear();
    display.setTextAlignment(TEXT_ALIGN_CENTER);
    display.setFont(ArialMT_Plain_24);
    display.drawStringMaxWidth(64, 0, 128, t);
    display.setFont(ArialMT_Plain_10);
    display.drawStringMaxWidth(64, 21, 128, date);
//    draw(64, 50, temp);
    display.setTextAlignment(TEXT_ALIGN_LEFT);
    display.drawString(0, 50, String(temp));
    display.drawString(30, 50, "C");        
    display.drawString(50, 50, String(humi));
    display.drawString(80, 50, "%");     
    
    display.display();

    
  }
  else // attempt to connect to wifi again if disconnected
  {
    display.clear();
    display.drawString(0, 10, "Reconnexion au WiFi");
    display.display();
    WiFi.begin(ssid, password);
    display.drawString(0, 24, "Connecté !");
    display.display();
    delay(1000);
  }
    
  delay(1000);    //Send a request to update every 1 sec (= 1,000 ms)
}
1 Like

String() is never the right answer. It will fragment your memory and can lead to very odd crashes. Use the dtostr() function instead

@blh64,

Thank you for your feedback.

How can i use this command in my sketch ?

3x0c3t:
@blh64,

Thank you for your feedback.

How can i use this command in my sketch ?

by googling the syntax for the command and implementing it.