SSD1306 OLED Display humidy % using sprintf_P

Hi there, i have a ESP8266 based weatherstation with a suite of sensors attached including a dht11 temperature / humidity hydrometer. It displays this data on a 0.96" OLED via i2c using the ssd1306 library:

#include "SSD1306Wire.h"
#include "OLEDDisplayUi.h"
#include "Wire.h"

However, trying to display the percent ‘%’ symbol on screen is causing me a major headache. I’m trying to display the temperature and humidity using sprintf_P to format the string and display it on the oled via the display functions:

  char buff2[5];
  sprintf_P(buff2, PSTR("%.02f %%"), humidity); //results like 23.500001
  display->drawString(26 + x, 30 + y, String(buff2));//humidity;

However the I can’t get the format string correct to escape the percentage symbol ‘%’. I have tried % and %% to no avail. The esp8266 seems to default to using the format string from the previous command which is wrong:

  sprintf_P(buff, PSTR("%.02f °C"), temperature); //results like 23.500001
  display->drawString(30 + x, 15 + y, String(buff));//temperature;

Can anyone please tell me howto display this correctly?

The full code for the function is:

display->setContrast(162, 31, 0); //usually 255, dim to save screen
  display->setTextAlignment(TEXT_ALIGN_CENTER);
  display->setFont(ArialMT_Plain_10);
  display->drawString(64 + x, 5 + y, "Living Room");

  display->setFont(ArialMT_Plain_16);//ArialMT_Plain_24);

  //unused code:
  //char result[8]; // Buffer big enough for 7-character float
  //dtostrf(temperature, 6, 2, result); // Leave room for too large numbers!
  char buff[16];
  sprintf_P(buff, PSTR("%.02f °C"), temperature); //results like 23.500001
  display->drawString(30 + x, 15 + y, String(buff));//temperature;
  Serial.print("Temperature buffer: ");
  Serial.println(String(buff));
  
  //display->drawString(90 + x, 15 + y, String(Light));//light;
  memset(buff, 0, sizeof(buff));//clear array
  char buff2[5];
  sprintf_P(buff2, PSTR("%.02f %%"), humidity); //results like 23.500001
  display->drawString(26 + x, 30 + y, String(buff2));//humidity;
  Serial.print("humidity buffer: ");
  Serial.println(String(buff2));

Also, the digits of the float humidity are displayed in the wrong place, the correct display in totality should be ‘55.77 %’
The actual display is ''77 0.00 °C" (see attachment)

I dont really understand the printf functions, so any hints and tips are much appreciated!

Thanks in advance,
Garreth

tft.print( "Humidity " + String(ePtr[2]) + “%” );

As a note, the ESP32 runs under freeRTOS which cleans up Strings.

Just use separate tft.print statements, and avoid the use of String.

Idahowalker:
tft.print( "Humidity " + String(ePtr[2]) + "%" );

what is PSTR?
make sure you string, buff2, is long enough

why not simply

  char buff2 [10];
  sprintf_P(buff2, "%.02f %%", humidity);

Thanks guys. It turns out %% does work, the OTA upload just failed for some reason.

I don't seem to be able access the tft object, where is this from?
Cheers :slight_smile:

i can display "%" on my SD1106 OLED

    sprintf (s0, "thr %04d, rev %3d%%", throttle, reverser);

gcjr:
what is PSTR?

Not actually applicable to an ESP8266, PSTR tells the compiler to store the quoted text in flash memory (PROGMEM).

david_2018:
Not actually applicable to an ESP8266, PSTR tells the compiler to store the quoted text in flash memory (PROGMEM).

ESP8266 is one the few platforms where it actually is applicable.