OLED 128x64, SH1106, NodeMCU. Displaying variables problem (solved)

Hi,

I have this OLED 1.3" 128x64 SPI SH1106 (7pin) display and NodeMCU 1.0. Trying to print variables From DHT11-sensor.

How to do it?

This code where "temperature" is my variable doesn´t seem to work. It prints text just fine, but the variable not:

bool drawFrame1(SH1106 *display, SH1106UiState* state, int x, int y) {

  display->setTextAlignment(TEXT_ALIGN_LEFT);
  display->setFont(ArialMT_Plain_10);
  display->drawString(0 + x, 10 + y, "TEMPERATURE : ");
  display->drawString(80 + x, 10 + y, (temperature));

  return false;
}

You have not said which library or even what type temperature is.

Assuming that temperature exists, you want to draw a String. So you should convert the expression to a String:

  display->drawString(80 + x, 10 + y, String(temperature));

David.

Thanks.

I tried that but no luck. Here´s te whole code if it helps to understand this. I guess it´s not so long.

#define BLYNK_PRINT Serial


#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <SimpleTimer.h>
#include <SimpleDHT.h>
#include <Wire.h>
#include <SPI.h>
#include "SH1106.h"
#include "SH1106Ui.h"


// Pin definitions for I2C
//#define OLED_SDA    D2  // pin 14
//#define OLED_SDC    D4  // pin 12
//#define OLED_ADDR   0x3C

/* Hardware Wemos D1 mini SPI pins
  D5 GPIO14   CLK         - D0 pin OLED display
  D6 GPIO12   MISO (DIN)  - not connected
  D7 GPIO13   MOSI (DOUT) - D1 pin OLED display
  D1 GPIO5    RST         - RST pin OLED display
  D2 GPIO4    DC          - DC pin OLED
  D8 GPIO15   CS / SS     - CS pin OLED display
*/

// Pin definitions for SPI
// ESP8266
//#define OLED_RESET  5   // RESET
//#define OLED_DC     4   // Data/Command
//#define OLED_CS     15  // Chip select
// Node MCU
#define OLED_RESET  D1   // RESET
#define OLED_DC     D2   // Data/Command
#define OLED_CS     D8   // Chip select

// Uncomment one of the following based on OLED type
SH1106 display(true, OLED_RESET, OLED_DC, OLED_CS); // FOR SPI
//SH1106   display(OLED_ADDR, OLED_SDA, OLED_SDC);    // For I2C
SH1106Ui ui     ( &display );


// for DHT11,
//      VCC: 5V or 3V
//      GND: GND
//      DATA: 2
int pinDHT11 = 2;
SimpleDHT11 dht11(pinDHT11);

//Ph-SensorStuff:

const int analogInPin = A0;
int sensorValue = 0;
unsigned long int avgValue;
float b;
int buf[10], temp;

//------------------

SimpleTimer timer;

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "*****";

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "*****";
char pass[] = "****";



bool msOverlay(SH1106 *display, SH1106UiState* state) {
  display->setTextAlignment(TEXT_ALIGN_RIGHT);
  display->setFont(ArialMT_Plain_10);
  display->drawString(128, 0, "Samsara Exit Hydroponics ");
  return true;
}


bool drawFrame1(SH1106 *display, SH1106UiState* state, int x, int y) {

  display->setTextAlignment(TEXT_ALIGN_LEFT);
  display->setFont(ArialMT_Plain_10);
  display->drawString(0 + x, 10 + y, "TEMPERATURE : ");
  display->drawString(80 + x, 10 + y, String(temperature));

  return false;
}


bool drawFrame2(SH1106 *display, SH1106UiState* state, int x, int y) {

  display->setTextAlignment(TEXT_ALIGN_LEFT);
  display->setFont(ArialMT_Plain_10);
  display->drawString(0 + x, 10 + y, "HUMIDITY : ");

  return false;
}

bool drawFrame3(SH1106 *display, SH1106UiState* state, int x, int y) {

  display->setFont(ArialMT_Plain_10);

  // The coordinates define the left starting point of the text
  display->setTextAlignment(TEXT_ALIGN_LEFT);
  display->drawString(0 + x, 11 + y, "PH-LEVEL : ");

  return false;
}


// how many frames are there?
int frameCount = 3;
// this array keeps function pointers to all frames
// frames are the single views that slide from right to left
bool (*frames[])(SH1106 *display, SH1106UiState* state, int x, int y) = { drawFrame1, drawFrame2, drawFrame3 };

bool (*overlays[])(SH1106 *display, SH1106UiState* state)             = { msOverlay };
int overlaysCount = 1;




void sendSensor ()
{
  Serial.println("=================================");
  Serial.println("Sample DHT11...");

  // read without samples.
  byte temperature = 0;
  byte humidity = 0;
  int err = SimpleDHTErrSuccess;
  if ((err = dht11.read(&temperature, &humidity, NULL)) != SimpleDHTErrSuccess) {
    Serial.print("Read DHT11 failed, err="); Serial.println(err); delay(1000);
    return;
  }

  Serial.print("Sample OK: ");
  Serial.print((int)temperature); Serial.print(" *C, ");
  Serial.print((int)humidity); Serial.println(" H");
  Blynk.virtualWrite(V5, humidity);
  Blynk.virtualWrite(V6, temperature);

  // DHT11 sampling rate is 1HZ.
  //delay(1500);
}


void sensor()

{
  for (int i = 0; i < 10; i++)
  {
    buf[i] = analogRead(analogInPin);
    delay(10);
  }
  for (int i = 0; i < 9; i++)
  {
    for (int j = i + 1; j < 10; j++)
    {
      if (buf[i] > buf[j])
      {
        temp = buf[i];
        buf[i] = buf[j];
        buf[j] = temp;
      }
    }
  }
  avgValue = 0;
  for (int i = 2; i < 8; i++)
    avgValue += buf[i];
  float pHVol = (float)avgValue * 5.0 / 1024 / 6;
  float phValue = -5.70 * pHVol + 21.34;
  Serial.print("PH-sensor = ");
  Serial.println(phValue);
  Blynk.virtualWrite(V4, phValue);
  //delay(20);
}


void setup()
{
  // Debug console
  Serial.begin(115200);

  ui.setTargetFPS(30);


  // You can change this to
  // TOP, LEFT, BOTTOM, RIGHT
  ui.setIndicatorPosition(BOTTOM);

  // Defines where the first frame is located in the bar.
  ui.setIndicatorDirection(LEFT_RIGHT);

  // You can change the transition that is used
  // SLIDE_LEFT, SLIDE_RIGHT, SLIDE_TOP, SLIDE_DOWN
  ui.setFrameAnimation(SLIDE_LEFT);

  // Add frames
  ui.setFrames(frames, frameCount);

  // Add overlays
  ui.setOverlays(overlays, overlaysCount);

  // Inital UI takes care of initalising the display too.
  ui.init();

  display.flipScreenVertically();




  Serial.println("\Starting");
  Blynk.begin(auth, ssid, pass, IPAddress(139, 59, 206, 133), 80);
  int mytimeout = millis() / 1000;


  // Setup a function to be called every second
  timer.setInterval(4500L, sendSensor);
  timer.setInterval(4000L, sensor);
}

void loop()
{
  Blynk.run();
  timer.run();

  int remainingTimeBudget = ui.update();

  if (remainingTimeBudget > 0) {

    delay(remainingTimeBudget);

  }
}

temperature is a local variable in sendSensor() function.

The compiler should give an error in the drawFrame1() function.

I suggest that you sit down with a nice cup of tea and a sheet of paper. Design your program properly.

David.

Seems like a good cup of chai helped. Stupid mistake. So it works now thanks a lot!