Conversion of sensor data to string not working with Wemos D1 Mini

Hi,
I apologize in advance for my question because it could be a futile one but I'm a newbie.
After some months of training with Arduino now I'm trying to use a Wemos D1 Mini with a BME280 sensor and a OLED SSD1306 display both working through I2C protocol.

In my hands the Adafruit 1306 library doesn't work with Wemos D1 Mini so I had to use to SSD1306.h library that works nice with text but seems not to be able to get data from sensors if they are not converted to text/string. So I tried to use dtostrf function but in my code I'm enclosing it just gives a series of 0.00 values even if the BME 280 is working fine ( as I can verify from the serial monitor). I think that I've made some mistake in choosing the source and/or destination of the function or in the size of buffer. I would be very grateful if you could spend your time in helping me to solve this problem and to teach me how to use the Adafruit 1306 library with Wemos D1 Mini. Thanks in advance for your help

WEMOS_D1_MINI_BME_280_OLED_1306_NOT_WORKING.ino (1,5 KB)

rather than providing a link upload your code using code tags </>
try moving the dtostrf() function calls from setup() into loop() when the data is read

float source_1 = temperature;
char destination_1 [8];
float source_2 = humidity;
char destination_2 [8];
float source_3 = pressure;
char destination_3 [8];dtostrf(source_1,4,2,destination_1);
dtostrf(source_2,4,2,destination_2);
dtostrf(source_3,4,2,destination_3);
  display.drawString(0, 20, destination_1);
  display.drawString(0, 30, destination_2);
  display.drawString(0, 40, destination_3);
  display.display();
  
    delay(1000);

you don't required source_1 as temperature is a float etc etc

I'm pretty sure that library works with D1 mini for me.

Hi Paul,
thx for your prompt answer but still I have trouble with Adafruit_SSD1306.h.
Probably I'm making a mistake when I use these two lines of code
#define OLED_ADDR 0x3C
Adafruit_SSD1306 display(-1);
and in the setup
display.begin(SSD1306_SWITCHCAPVCC, OLED_ADDR);
I've copied them from an example found on the web but if I try to use with a very siimple sketch just asking for a line of text displayed they don't work and
if I use the code lines that worked with Arduino
#define OLED_RESET 4
Adafruit_SSD1306 display(OLED_RESET);
and in the setup or in the loop
display.begin(SSD1306_SWITCHCAPVCC, 0x3C) I'm not able to see anything on the Oled display.
So I'm dead stuck...
Would you be so kind to show me where I'm making a mistake and
how I can correct it ? Thx for your help

Hi,
sorry for having provided a link but, as you have understood, I'm really naive.
However I'd already tried to put the the dtostrf() function calls from setup() into loop() but without success and before using source_1, source_2 etc I had tried to use tempetrature, humidity, etc but again without success.
I'm really very clumsy

Help us help you.

can you post this version of the code - using code tags </> ?

Hi,
I hope this time I've followed the right way of posting the code.
Sorry for the inconvenience I caused...

#include <SPI.h>
#include <Wire.h>
#include "SSD1306.h"

SSD1306 display(0x3c, SDA, SCL);

#include <Adafruit_Sensor.h>
#include <Adafruit_BME280.h>

#define SEALEVELPRESSURE_HPA (1013.25)

Adafruit_BME280 bme;

float temperature, humidity, pressure, altitude;

float source_1 = temperature;
char destination_1[8];
float source_2 = humidity;
char destination_2[8];
float source_3 = pressure;
char destination_3[8];

void setup() {
  Serial.begin(115200);
  delay(100);
  Serial.println("BME found and initialized");
  bme.begin(0x76);


  display.init();
  display.flipScreenVertically();
  display.drawString(0, 0, "NEW TEST");
  display.display();
  delay(1000);
}

void loop() {
  temperature = bme.readTemperature();
  humidity = bme.readHumidity();
  pressure = bme.readPressure() / 100.0F;
  altitude = bme.readAltitude(SEALEVELPRESSURE_HPA);
  delay(1000);
  Serial.print("Temp: ");
  Serial.println(temperature);
  Serial.print("Humid: ");
  Serial.println(humidity);
  Serial.print("Pressure:  ");
  Serial.println(pressure);
  Serial.print("Altitude: ");
  Serial.println(altitude);

  dtostrf(source_1, 4, 2, destination_1);
  dtostrf(source_2, 4, 2, destination_2);
  dtostrf(source_3, 4, 2, destination_3);

  Serial.println(destination_1);
  Serial.println(destination_2);
  Serial.println(destination_3);

  display.drawString(0, 20, destination_1);
  display.drawString(0, 30, destination_2);
  display.drawString(0, 40, destination_3);
  display.display();

  delay(1000);
}

Sorry for the inconvenience.
I'll try to fix the post according to your suggestions
Thx

in the code of post #8 you did not copy temperature into source_1 etc etc
try

void loop() {
  temperature = bme.readTemperature();
  humidity = bme.readHumidity();
  pressure = bme.readPressure() / 100.0F;
  altitude = bme.readAltitude(SEALEVELPRESSURE_HPA);
  delay(1000);
  Serial.print("Temp: ");
  Serial.println(temperature);
  Serial.print("Humid: ");
  Serial.println(temperature);
  Serial.print("Pressure:  ");
  Serial.println(pressure);
  Serial.print("Altitude: ");
  Serial.println(altitude);

  dtostrf(temperature, 4, 2, destination_1);
  dtostrf(pressure, 4, 2, destination_2);
  dtostrf(altitude, 4, 2, destination_3);

  Serial.println(destination_1);
  Serial.println(destination_2);
  Serial.println(destination_3);

  display.drawString(0, 20, destination_1);
  display.drawString(0, 30, destination_2);
  display.drawString(0, 40, destination_3);
  display.display();

  delay(1000);
}

Hi Paul,
you were perfectly right.
Tha Adafruit 1306 works with Wemos D1 Mini but I have had to change RESET from 4 to 1.
Thx again

Yes, that would explain it. Look at this pinout diagram:

If you define RESET as 4, that means it will use the pin shown as "GPIO4", which is the same pin as SDA, which is one of the i²c pins. That could maybe cause some problems between the D1 mini and the sensor or oled.

If you define RESET as 1, that is the pin shown as "GPIO1" which is the same pin as TX which is used for serial monitor. That may or may not cause a strange character to appear on the serial monitor, which is not really a problem.

But did you connect any pin to the RST pin of your oled? Does your oled even have an RST pin? Many have only VCC, GND, SCL, SDA. If your oled has only 4 pins, you should set RESET to -1. You mentioned trying that in post #4 and you said "they don't work". You then said that you tried RESET as 4 and said "nothing appeared". So I am not sure what you meant exactly by "they don't work". I think it should work with -1.

It might be more convenient to make a function for printing a float:

void drawFloat(int x, int y, float f) {
  char s[8];
  dtostrf(f, 4, 2, s);
  display.drawString(x, y, s);
  }

void loop() {
  temperature = bme.readTemperature();
  humidity = bme.readHumidity();
  pressure = bme.readPressure() / 100.0F;
  altitude = bme.readAltitude(SEALEVELPRESSURE_HPA);
  delay(1000);
  Serial.print("Temp: ");
  Serial.println(temperature);
  Serial.print("Humid: ");
  Serial.println(humidity);
  Serial.print("Pressure:  ");
  Serial.println(pressure);
  Serial.print("Altitude: ");
  Serial.println(altitude);

  drawFloat(0, 20, temperature);
  drawFloat(0, 30, pressure);
  drawFloat(0, 40, altitude);
  display.display();

  delay(1000);
}

Hi Paul
Again you're perfectly right ( and I thank you also for the suggestion of making a function for printing a float)
In fact putting the reset to 1 it did cause interference with serial monito and it worked only if i put reset at - 1 as you suggested ( and the pinout drawing you show is very clear)Following your suggestions I was able to display on the screen and on the serial monitor the data fram BME280 and from DS3231.
However, when I tried to add a SD card in order to create a datalogger I encountered a new, very strange problem.
All the lines of code containing a Serial.print that are written in the setup are not visible in the sewrial monitor while if I put them in the loop they are. I tried to search some explanation on the forum and on the web but at present I'm again stuck.
Sorry for bothering you again but you helped me so much.
I'm enclosing an example to test SD card that I copied from Github and it showed the same problem`

/*
 * Micro SD Shield - Read/Write
 *
 * This example shows how to read and write data to and from an SD card file
 *
 * The WeMos Micro SD Shield uses:
 * D5, D6, D7, D8, 3V3 and G
 *
 * The shield uses SPI bus pins:
 * D5 = CLK
 * D6 = MISO
 * D7 = MOSI
 * D8 = CS
 *
 * The SD card library uses 8.3 format filenames and is case-insensitive.
 * eg. IMAGE.JPG is the same as image.jpg
 *
 * created Nov 2010 by David A. Mellis
 * modified 9 Apr 2012 by Tom Igoe
 *
 * This example code is in the public domain.
 * https://github.com/esp8266/Arduino/blob/master/libraries/SD/examples/ReadWrite/ReadWrite.ino
 */

#include <SPI.h>
#include <SD.h>

// change this to match your SD shield or module;
// WeMos Micro SD Shield V1.0.0: D8
// LOLIN Micro SD Shield V1.2.0: D4 (Default)
const int chipSelect = D8;

File myFile;

void setup() {
  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ;  // wait for serial port to connect. Needed for Leonardo only
  }
  Serial.print("Initializing SD card...");

  if (!SD.begin(chipSelect)) {
    Serial.println("initialization failed!");
    return;
  }
  Serial.println("initialization done.");

  // open the file. note that only one file can be open at a time,
  // so you have to close this one before opening another.
  myFile = SD.open("test.txt", FILE_WRITE);

  // if the file opened okay, write to it:
  if (myFile) {
    Serial.print("Writing to test.txt...");
    myFile.println("testing 1, 2, 3.");
    // close the file:
    myFile.close();
    Serial.println("done.");
  } else {
    // if the file didn't open, print an error:
    Serial.println("error opening test.txt");
  }

  // re-open the file for reading:
  myFile = SD.open("test.txt");
  if (myFile) {
    Serial.println("test.txt:");

    // read from the file until there's nothing else in it:
    while (myFile.available()) {
      Serial.write(myFile.read());
    }
    // close the file:
    myFile.close();
  } else {
    // if the file didn't open, print an error:
    Serial.println("error opening test.txt");
  }
}

void loop() {
  // nothing happens after set
}

I tried different baud rates without any result
Thanks

How could it show the same problem? In this code there are no prints to serial monitor in loop().

Hi Paul,
I think that I was not clear when trying to explain my problem.
In my sketch with Wemos D1 Mini connected to an Oled 1306, a BME 280 sensor and RTC DS3231 I've noticed that all the lines containing "Serial.print() or Serial.print(ln) written in the loop are correctly written in the serial monitor but the lines containing the same Serial.print ( for example Serial.println("BME280 found and initialized"), or Serial.println(RTC found and initialized") written in the setup were not present.
However, since the sketch was working I set aside the problem.
But when I tried to add to my sketch a SD card where the instructions containing "Serial.print" must be written in the setup I was not able to verify if the SD card was correctly recognized and the sketch did not work for the part related to the SD card.
So I tried a very simple example ( not written by me and therefore, with a high probability of being correct) that is devised to test the SD card and that I know was perfectly working with my Arduino Nano and Nano 33 and the same problem - lines with Serial.print() instruction written in the setup and not visible in the serial monitor- is present: so I think that it could be related to Wemos D1 Mini but the Authors of this sketch say that it was successfully tested with ESP 8266.
Hope this can clarify better my situation. Thx in advance

Hi,
sorry for the delay in answering your suggestions and rematks but I had some hours very busy trying to unerstand the different libraries I 'm using with Wemos.
I've really appreciated your help.
Best