Display of temperature values on an adafruit ssd1306

Please I am having problems displaying the temperature values on an adafruit ssd1306 LCD. I have tested the LCD with the code from @peekay123 and it is getting to display “Hello World”. I have also been able to display time and date on it. I subscribed to one of my events involving temperature measurements and I have tested it and verified that the subscription is working as required using the blynk app however, the problem is that the LCD is not displaying the subscribed temperature values. Am not sure if I will need to create a new function for the display part. Please help me out. Below is the code am using…


// This #include statement was automatically added by the Particle IDE.
#include "Adafruit_SSD1306/Adafruit_SSD1306.h"


#define OLED_DC     A1
#define OLED_CS     A2
#define OLED_RESET  D0
Adafruit_SSD1306 display(OLED_DC, OLED_RESET, OLED_CS);



#define NUMFLAKES 10
#define XPOS 0
#define YPOS 1
#define DELTAY 2


#define LOGO16_GLCD_HEIGHT 16 
#define LOGO16_GLCD_WIDTH  16

static const unsigned char logo16_glcd_bmp[] =
{ 0B00000000, 0B11000000,
  0B00000001, 0B11000000,
  0B00000001, 0B11000000,
  0B00000011, 0B11100000,
  0B11110011, 0B11100000,
  0B11111110, 0B11111000,
  0B01111110, 0B11111111,
  0B00110011, 0B10011111,
  0B00011111, 0B11111100,
  0B00001101, 0B01110000,
  0B00011011, 0B10100000,
  0B00111111, 0B11100000,
  0B00111111, 0B11110000,
  0B01111100, 0B11110000,
  0B01110000, 0B01110000,
  0B00000000, 0B00110000 };

#if (SSD1306_LCDHEIGHT != 64)
#error("Height incorrect, please fix Adafruit_SSD1306.h!");
#endif

float temperature = 100;
int displayTime();

void setup()
{
    
    Particle.subscribe("Body_Temperature", myHandler, "380034000a47343432313031");
    Serial.begin(9600);
    
    display.begin(SSD1306_SWITCHCAPVCC);   // by default, we'll generate the high voltage from the 3.3v line internally! (neat!)
    display.clearDisplay();   // clears the screen and buffer
    delay(1000);
    
    display.setTextSize(2);
    display.setTextColor(WHITE);
    display.setCursor(30,45);
    display.println("Temperature");
    display.println(temperature);
}

void loop()
{
    //Show the time and date
    displayTime();
}


void myHandler(const char *event, const char *data)
{
temperature = atof (data);
}

Probably need something extra like:

int displayTime(void){
 display.setCursor(xxxx,xxxx);
 display.println(temperature);
}


bool updateDisplay = false;

void loop()
{
    if (updateDisplay == true){
    //Show the time and date
    displayTime();
    displayUpdate = false;
  }
}

void myHandler(const char *event, const char *data)
{
  temperature = atof (data);
  displayUpdate = true;
}

Hi - you’ll need to call display.display(); after your print statements. Screen changes are buffered. So, you can print text, draw circles, lines, etc and then call display.display() and it’ll all show up in one fell swoop.

1 Like

Thanks @kennethlimcp and @MikeWhitten but the problem is not been able to display the temperature values on the LCD. The time and date are been displayed as shown in the image below. I just want to display the subscribed temperature values on the LCD as well.

I need help on how to get the LCD to display the temperature values. thanks

Where’s the date and time from? Don’t see it in your code.

Ah, ok. That helps. So… I see you’ve got a call to display the time inside loop() but the only call I see to display the temperature is inside setup(). That’s not going to get called regularly, just once, I think. So what @kennethlimcp shows is correct in adding a temperature display to something that’s being called from loop().

I’m awfully new to this myself but it sure looks that way.

Below is the full code including the date and time function @kennethlimcp

// This #include statement was automatically added by the Particle IDE.
#include "Adafruit_SSD1306/Adafruit_SSD1306.h"


#define OLED_DC     A1
#define OLED_CS     A2
#define OLED_RESET  D0
Adafruit_SSD1306 display(OLED_DC, OLED_RESET, OLED_CS);



#define NUMFLAKES 10
#define XPOS 0
#define YPOS 1
#define DELTAY 2


#define LOGO16_GLCD_HEIGHT 16 
#define LOGO16_GLCD_WIDTH  16

static const unsigned char logo16_glcd_bmp[] =
{ 0B00000000, 0B11000000,
  0B00000001, 0B11000000,
  0B00000001, 0B11000000,
  0B00000011, 0B11100000,
  0B11110011, 0B11100000,
  0B11111110, 0B11111000,
  0B01111110, 0B11111111,
  0B00110011, 0B10011111,
  0B00011111, 0B11111100,
  0B00001101, 0B01110000,
  0B00011011, 0B10100000,
  0B00111111, 0B11100000,
  0B00111111, 0B11110000,
  0B01111100, 0B11110000,
  0B01110000, 0B01110000,
  0B00000000, 0B00110000 };

#if (SSD1306_LCDHEIGHT != 64)
#error("Height incorrect, please fix Adafruit_SSD1306.h!");
#endif

float temperature = 100;
int displayTime();

void setup()
{
    
    Particle.subscribe("Body_Temperature", myHandler, "380034000a47343432313031");
    Serial.begin(9600);
    
    display.begin(SSD1306_SWITCHCAPVCC);   // by default, we'll generate the high voltage from the 3.3v line internally! (neat!)
    display.clearDisplay();   // clears the screen and buffer
    delay(1000);
    
    display.setTextSize(2);
    display.setTextColor(WHITE);
    display.setCursor(30,45);
    display.println("Temp = ");
    display.println(temperature);
}

void loop()
{
    //Show the time and date
    displayTime();
}


void myHandler(const char *event, const char *data)
{
temperature = atof (data);
}



int displayTime()
{
/*Set the courser and display the time in hours, minutes and
seconds. Remember this is standard time so you need to add or decrease
the hours depending on your time zone*/
display.clearDisplay();
display.setTextSize(2);
display.setTextColor(WHITE);
display.setCursor(0,0);
display.print(Time.hour());
display.print(":");
display.print(Time.minute());
display.print(":");
display.print(Time.second());


/*Set the cursor and display the date in days, months and
year*/
display.setCursor(0,25);
display.print(Time.day());
display.print("/");
display.print(Time.month());
display.print("/");
display.println(Time.year());
display.display();
delay(1000);
}

Will this work?

https://gist.github.com/kennethlimcp/b028924c42e537ec350d

BTW: i recommending using github or gist to share huge source code :slight_smile:

1 Like

@kennethlimcp - I see a display.display() call after the date / time prints but not after the temperature. Isn’t it still going to be missing or am I not seeing something?

That’s true… updated the code. Thanks :slight_smile:

1 Like

You might also want to update the comment // print time when you are actually printing the temperature :wink:

@luckygodswill, just for understand the issue:
How did you think your original code would have displayed the temperature?
displayTime() always clears the display with the initial statement display.clearDisplay();, so you’d need to re-print the temperature each time, just the same as you’d do with date and time.

BTW: There is a Time.format() statement that helps with formatting dates and times :wink:

2 Likes

Thanks a lot @kennethlimcp @MikeWhitten and @ScruffR for your time and support. I really appreciate it. It is working as expected now using the code from @kennethlimcp ’ s github at https://gist.github.com/kennethlimcp/b028924c42e537ec350d

1 Like