Help with U8g2lib and display not working

Hi All.

Hopefully this is the right place to ask my question.

I’m fairly new to Arduino and ESP8266 having only completed a handful of projects. I’m in the process of building a Wifi bbq controller with integrated PID to control a fan and Blynk . I started with code found on github which does almost everything I need, however i need to integrate a OLED on the device as I’m sometimes out when the bbq needs checking on. And rather than leave my phone at home so my wife can check the temps, i thought it would be better to have the OLED.

However I’ve spent the last week pulling my hair out as I’m unable to get the display to work. The code compiles with no errors but still the display doesn’t work. I’ve confirmed the display works using some of the examples in the U8g2lib library.

Would somebody please take a look over my code (its not finished, and some of the code is commented out as i’m waiting on more MAX6675 boards and thermocouples) and let me know what i’m doing wrong.

/*  

#include <ESP8266WiFi.h> 
#include <ESP8266mDNS.h> //OTA
#include <WiFiUdp.h> //OTA
#include <ArduinoOTA.h> //OTA
#include <SimpleTimer.h>
#include <BlynkSimpleEsp8266.h>
#define BLYNK_PRINT Serial    // Comment this out to disable prints and save space
#include "max6675.h"
#include <SPI.h>
#include <PID_v1.h>
#include <U8g2lib.h>
#include <Wire.h>

U8G2_SH1106_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0, /* reset=*/ U8X8_PIN_NONE);


//initialize timers & freq for display
int displayFreq = 2000L;  // sets display update frequency
int displayTimer2;  //Timers to call display update fucntions

//initialize wifi
SimpleTimer timer;  //Starts timer to run the thingspeak update every x milliseconds
char ssid[] = "<your wifi SSID here>";
char password[] = "<your wifi password here>";
WiFiClient client;

//initialize for Blynk
char auth[] = "<your Blynk key here>";


//Define Variables we'll be connecting to (PID)
double Setpoint, Input, Output;

//Define the aggressive and conservative Tuning Parameters (PID)
double aggKp=8, aggKi=0.2, aggKd=1;
double consKp=2, consKi=0.1, consKd=.5;

//Specify the links and initial tuning parameters
PID myPID(&Input, &Output, &Setpoint, consKp, consKi, consKd, DIRECT);

//initialize the fan controlling PWM pin and clock speed
int fanControlpin = D0;
int fanSpeed = 400; // value between 15-1000
int minSpeed= 15;
int PWMfreq=30000;

bool StillCooking=true;  // when this gets set to false, the fan turns off completely and stays off

// initialize for thermocouples   http://www.14core.com/wiring-thermocouple-max6675-on-esp8266-12e-nodemcu/
int const thermoSOpin = D6; // pin D6  *MISO*
int const thermoSCKpin = D5;  // pin D5 *CLK*
//int const probe_A_CSpin = D1;  // 
//int const probe_B_CSpin = D2; // 
//int const probe_C_CSpin = D3; //  
int const probe_D_CSpin =D4; // 
int const probe_E_CSpin = D7; //  This probe will be used to measure the meat internal temperature

//MAX6675 probe_A_Thermocouple(thermoSCKpin, probe_A_CSpin, thermoSOpin);
//MAX6675 probe_B_Thermocouple(thermoSCKpin, probe_B_CSpin, thermoSOpin);
//MAX6675 probe_C_Thermocouple(thermoSCKpin, probe_C_CSpin, thermoSOpin);
MAX6675 probe_D_Thermocouple(thermoSCKpin, probe_D_CSpin, thermoSOpin);
MAX6675 probe_E_Thermocouple(thermoSCKpin, probe_E_CSpin, thermoSOpin); 
 
int targetTemp=125;
int pullTemp=95;
int tempBelowTarget=30;
//float probe_A=25;
//float probe_B=25;
//float probe_C=25;
float probe_D=25;
float probe_E=25;
float tempWeightedAvg=25;
//float probe_A_Last=25;
//float probe_B_Last=25;
//float probe_C_Last=25;
float probe_D_Last=25;
float probe_E_Last=25;
float tempWeightedAvgLast= 25;

BLYNK_WRITE(V0) {   //pulls data from Blynk app for target temp
  int targetTempBlynk = param.asInt();
  targetTemp = targetTempBlynk;
  Serial.printf("New target temperature = %d\n",targetTemp);
}
BLYNK_WRITE(V1) { //pulls data from Blynk app for pull temp
  int pullTempBlynk = param.asInt();
  pullTemp = pullTempBlynk;
  Serial.printf("New pull temperature = %d\n",pullTemp);
}
BLYNK_READ(V2){ //sends Blynk app the data for the fan speed
  Blynk.virtualWrite(V2, (int)(fanSpeed/10));
}
BLYNK_READ(V3){
  Blynk.virtualWrite(V3, tempBelowTarget);
}
BLYNK_READ(V4){ //sends Blynk app the data for the three sensors
  Blynk.virtualWrite(V4, tempWeightedAvgLast);
}
BLYNK_READ(V5){ //sends Blynk app the data for the food probe
  Blynk.virtualWrite(V5, probe_E_Last);
}


void setup() {                
  Serial.begin(9600);
  delay(10);
 //configure the fan controller 
  pinMode(fanControlpin,OUTPUT);
  analogWriteFreq(PWMfreq);
  analogWrite(fanControlpin, 300); // start the fan off
  u8g2.begin();
  u8g2.enableUTF8Print();
  Wire.begin();
  
  
// connect to wifi
  Serial.printf("\nConnecting to %s\n",ssid);
  Blynk.begin(auth, ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");   
  }
    Serial.printf("\n\nWiFi connected\n\n");
    //start OTA
    ArduinoOTA.begin(); //OTA

//PID
  Input = tempWeightedAvgLast;
  Setpoint = targetTemp;
  myPID.SetMode(AUTOMATIC);  //turn the PID on

}
  
void loop() {
  if (StillCooking){
    timer.run();
    Blynk.run();
    ArduinoOTA.handle(); //OTA
    delay(500); 
  } else {
    delay(5000);
    if(pullTemp==130){StillCooking=true;}
    timer.run();
    Blynk.run();
    ArduinoOTA.handle(); //OTA

  } 
}

 void draw() 
 {
  u8g2.clearBuffer();
  u8g2.setFont(u8g2_font_ncenB08_tr);  // choose a suitable font
  u8g2.drawStr( 0, 0, "Meat Temp");
  u8g2.setCursor(11, 0);
  u8g2.print(probe_E_Last);
  u8g2.sendBuffer();

  displayTimer2 = timer.setTimeout(displayFreq, draw2);
}

void draw2() 
 {
  u8g2.clearBuffer();
  u8g2.setFont(u8g2_font_ncenB08_tr);  // choose a suitable font
  u8g2.drawStr( 0, 0, "Smoker Temp");
  u8g2.setCursor(11, 0);
  u8g2.print(tempWeightedAvgLast);
  u8g2.sendBuffer();

}

void fanController(){
    if (tempBelowTarget > 1){  // check if the meat is almost done
        Input = tempWeightedAvgLast;
        Setpoint = targetTemp;
        double gap = Setpoint-Input; // average temp distance away from target temperature
        if(gap<=0){     
            fanSpeed=minSpeed;
            analogWrite(fanControlpin, fanSpeed);
            Serial.println("Turned fan off. [Close to target temperature]");
        } else if(gap > 0 && gap<=10) {  //we're close to setpoint, use conservative tuning parameters
          myPID.SetTunings(consKp, consKi, consKd);
          myPID.Compute();
          fanSpeed=  map(Output, 0, 255, minSpeed, 1000);
          if(fanSpeed>1000){fanSpeed=1000;}
          analogWrite(fanControlpin, fanSpeed);
          Serial.printf("** Turned fan on %d%.\n",(int)(fanSpeed/10));
        } else {//we're far from setpoint, use aggressive tuning parameters
           myPID.SetTunings(aggKp, aggKi, aggKd);
           myPID.Compute();
           fanSpeed=  map(Output, 0, 255, minSpeed, 1000);
           if(fanSpeed>1000){fanSpeed=1000;}
           analogWrite(fanControlpin, fanSpeed);
           Serial.printf("** Turned fan on %d%.\n",(int)(fanSpeed/10));
        } 
   } else {  // if the meat is at the pull temp, turn off fan, and notify that the food is done.
     fanSpeed=minSpeed;
     analogWrite(fanControlpin, fanSpeed);
     Serial.println("Turned fan off.");
     StillCooking=false;
     Serial.println("#### Finished! Time to pull :-D  ####");
  }
}

void readTempSensors(){
   //probe_A=probe_A_Thermocouple.readCelsius();
   delay(100);
   //probe_B=probe_B_Thermocouple.readCelsius();
      delay(100);
   //probe_C=probe_C_Thermocouple.readCelsius();
      delay(100);
   probe_D=probe_D_Thermocouple.readCelsius();
      delay(100);
   probe_E=probe_E_Thermocouple.readCelsius(); 
      
    tempWeightedAvg=((probe_D)+45); //add 45 to compensate for outside of barrel being cooler than center
    //probe_A_Last=(2*probe_A_Last+probe_A)/3;
    //probe_B_Last=(2*probe_B_Last+probe_B)/3;
    //probe_C_Last=(2*probe_C_Last+probe_C)/3;
    probe_D_Last=(2*probe_D_Last+probe_D)/3;
    probe_E_Last=(2*probe_E_Last+probe_E)/3;
    tempWeightedAvgLast=(2*tempWeightedAvgLast+tempWeightedAvg)/3;
    tempBelowTarget=pullTemp-probe_E_Last ;
//    Serial.printf("pull\t|target\t|A\t|B\t|C\t|D\t|E\n%d\t|%d\t|%d\t|%d\t|%d\t|%d\t|%d\n",(int)pullTemp,(int)targetTemp,(int)probe_A_Last,(int)probe_B_Last,(int)probe_C_Last,(int)probe_D_Last,(int)probe_E_Last);
}

Thanks in advance

Lee