converting float to string

Hello Guys

I need put the temperature and umidity value in the a display OLED. But the function to do it accept only strings and the temperarute/umidity are float both.

How could I change the floats in strings?

Following the code.

#include <NTPClient.h>
#include <ESP8266WiFi.h>
#include <WiFiUdp.h>

#include <ESP8266WiFi.h>
#include <SSD1306.h>

#include <DHT.h>

#include "floatToString.h"

SSD1306 display(0x3c, D7, D5);

const char *ssid = "WIFI BASILIO";
const char *password = "34239755";
int16_t utc = -3;

#define DHTPIN 5
DHT dht(DHTPIN, DHT11);

WiFiUDP ntpUDP;

// You can specify the time server pool and the offset (in seconds, can be
// changed later with setTimeOffset() ). Additionaly you can specify the
// update interval (in milliseconds, can be changed using setUpdateInterval() ).

NTPClient timeClient(ntpUDP, "a.st1.ntp.br", utc*3600, 60000);

void setup(){

display.init();
display.flipScreenVertically();
display.setFont(ArialMT_Plain_10);

Serial.begin(115200);

WiFi.begin(ssid, password);

while ( WiFi.status() != WL_CONNECTED ) {
delay ( 500 );
Serial.print ( "." );
}

timeClient.begin();
}

void loop() {

timeClient.update();
Serial.println(timeClient.getFormattedTime());

float h = dht.readHumidity();
float t = dht.readTemperature();

String hum;
String temp;

floatToString(hum,h);

display.clear();
display.display();
display.setTextAlignment(TEXT_ALIGN_LEFT);
display.setFont(ArialMT_Plain_10);
display.drawString(0, 10, timeClient.getFormattedTime());
display.drawString(0, 20, dht.readHumidity()); // < Don't accepct the humidity value because is a float
display.drawString(0, 30, dht.readTempearute()); // < Don't accepct the temperatute value because is a float
display.display();

delay(1000);

}

Thank you!

dtostrf

@rsbasilio - how can you ever expect to write a computer program if you can't or won't follow the simple instructions in the thread locked at the top of the forum page that explains how to post a programming question ?

read this before posting a programming question

The dtostrf() function is not covered in the Language Reference (that I know of) so here is a link to a dtostrf function reference.

The function seems to take a String (big s).

 void drawString(int16_t x, int16_t y, String text);

// Draws a String with a maximum width at the given location.

Say, float x = 3.142;

We want --

//char stringOne [6] = {0x33, 0x2E, 0x31, 0x34, 0x32, 0x00}; 
//The above array contains ASCII codes for the symbols of x; null-character is automatically added

String stringOne = String(x, 3); // 3-digit accuracy after decimal point ; array char stringOne[6]; is automatically created.

Now, we can check --

Serial.print(stringOne); //shows: 3.142
Serial.write(stringOne[0]); //shows: 3
Serial.println();
Serial.print(stringOne[0], HEX); // shows: 0x33 --> ASCII code of 3

and so on....

String stringOne = String(x, 3); // 3-digit accuracy after decimal point ; array char stringOne[6]; is automatically created.You really haven't got the hang of classes yet, have you @GolemMostafa?

You haven't created a char array called "stringOne", you have created an instance of the String class called "stringOne".

The way we don't see the main() function in the Arduino IDE; this is the same way we don't see here the char stringOne[6]; array. Things are to be taken from conceptual point of view, and not by one-to-one correspondence. I am not the right person to go on debate with you and others on programming language mechanics!

Edit: The word correspondence is added.

TolpuddleSartre:
dtostrf

...or printf and the %f format...

krupski:
...or printf and the %f format...

Does that work in the IDE as standard ?

krupski:
...or printf and the %f format...

UKHeliBob:
Does that work in the IDE as standard ?

No.