Heltec LoRa ESP32 w/OLED

I am trying to get the above board working with a simple BME280 I2C sensor. I’ve searched around to try and find the correct libraries but I continue to get errors. I am a bit of a newbie with all this! The ‘standard’ Rui Santos BME280 sketch loads fine and works, but the sketch I found that ‘should’ be compatible with the above board fails.

Here’s my code

#include <SPI.h>
#include <LoRaLib.h>
#include <ssd1306.h>
#include <Arduino.h>
#include <Adafruit_BME280.h> // from https://github.com/Takatsuki0204/BME280-I2C-ESP32

#define I2C_SDA 4
#define I2C_SCL 15
#define SEALEVELPRESSURE_HPA (1013.25)
#define BME280_ADD 0x76

Adafruit_BME280 bme(I2C_SDA, I2C_SCL);

//OLED pins to ESP32 GPIOs via this connecthin:
//OLED_SDA -- GPIO4
//OLED_SCL -- GPIO15
//OLED_RST -- GPIO16

ssd1306  display(0x3c, 4, 15);


// WIFI_LoRa_32 ports

// GPIO5  -- SX1278's SCK
// GPIO19 -- SX1278's MISO
// GPIO27 -- SX1278's MOSI
// GPIO18 -- SX1278's CS
// GPIO14 -- SX1278's RESET
// GPIO26 -- SX1278's IRQ(Interrupt Request)

#define SS      18
#define RST     14
#define DI0     26
#define BAND    433E6  //915E6 

int counter = 0;

void I2Cscan() {

  byte error;
  byte address;
  int numberOfDevices = 0;
  String addressMessage = "";

  Serial.println("Scanning...\n");

  for (address = 1; address < 127; address++) {

    // The i2c_scanner uses the return value of
    // Write.endTransmisstion to see if
    // a device acknowledged at that address.

    Wire.beginTransmission(address);
    error = Wire.endTransmission();

    if (0 == error) {
      addressMessage = "I2C device found at address 0x";
      if (address < 16) {
        addressMessage += "0";
      }
      addressMessage += String(address);
      addressMessage += "!";
      Serial.printf("%s\n", addressMessage.c_str());
      Serial.println();

      numberOfDevices++;
    } else if (4 == error) {
      addressMessage = "Unknown error at address 0x";
      if (address < 16) {
        addressMessage += "0";
      }
      addressMessage += String(address);
      Serial.printf("%s\n", addressMessage.c_str());
      Serial.println();
    } // if (0 == error)
  } // for (address = 1...)

  if (0 == numberOfDevices)
    Serial.println("No I2C devices found.\n");
  else
    Serial.println("Done\n");
}


void getValues() {
  Serial.print("Temperature = ");
  Serial.print(bme.readTemperature());
  Serial.println(" ℃");

  Serial.print("Pressure = ");

  Serial.print(bme.readPressure() / 100.0F);
  Serial.println(" hPa");

  Serial.print("Approx. Altitude = ");
  Serial.print(bme.readAltitude(SEALEVELPRESSURE_HPA));
  Serial.println(" m");

  Serial.print("Humidity = ");
  Serial.print(bme.readHumidity());
  Serial.println(" %");

  Serial.println();
}

void setup() {
  pinMode(25,OUTPUT); //Send success, LED will bright 1 second
  
  pinMode(16,OUTPUT);
  digitalWrite(16, LOW);    // set GPIO16 low to reset OLED
  delay(50); 
  digitalWrite(16, HIGH);
  
  Serial.begin(115200);
  while (!Serial); //If just the the basic function, must connect to a computer
 
  // Initialising the UI will init the display too.
  display.init();

  I2Cscan();
  
  display.flipScreenVertically();
  display.setFont(ArialMT_Plain_10);
  display.setTextAlignment(TEXT_ALIGN_LEFT);
  display.drawString(5,5,"LoRa Sender");
  display.display();
  
  SPI.begin(5,19,27,18);
  LoRa.setPins(SS,RST,DI0);
  Serial.println("LoRa Sender");
  if (!LoRa.begin(BAND)) {
    Serial.println("Starting LoRa failed!");
    while (1);
  }
  Serial.println("LoRa Initial OK!");
  display.drawString(5,20,"LoRa Initializing OK!");
  display.display();
  delay(2000);

  bool status;

  status = bme.begin(BME280_ADD);
  if (!status) {
    Serial.println("Could not find a valid BME280 sensor, check wiring!");
    //while (1);
  }
  
}

void loop() {
  Serial.print("Sending packet: ");
  Serial.println(counter);
  
  display.clear();
  display.setFont(ArialMT_Plain_16);
  display.drawString(3, 5, "Sending packet ");
  display.drawString(50, 30, String(counter));
  display.display();

  // send packet
  LoRa.beginPacket();
  LoRa.print("Hello..");
  LoRa.print(counter);
  LoRa.endPacket();
  
  counter++;
  digitalWrite(25, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(1000);                       // wait for a second
  digitalWrite(25, LOW);    // turn the LED off by making the voltage LOW
  delay(1000);                       // wait for a second
  
  delay(3000);
  getValues();
}

Here are my errors…

Arduino: 1.8.13 (Windows Store 1.8.42.0) (Windows 10), Board: "WiFi LoRa 32(V2), Disabled, 240MHz (WiFi/BT), 921600, None, REGION_EU868, None"

ESP32-LORA-BME280:12:37: error: invalid conversion from 'int' to 'SPIClass*' [-fpermissive]

 Adafruit_BME280 bme(I2C_SDA, I2C_SCL);

                                     ^

In file included from D:\Users\Malcolm\My Documents\Arduino\ESP32-LORA-BME280\ESP32-LORA-BME280.ino:5:0:

D:\Users\Malcolm\My Documents\Arduino\libraries\Adafruit_BME280_Library-master/Adafruit_BME280.h:215:3: note:   initializing argument 2 of 'Adafruit_BME280::Adafruit_BME280(int8_t, SPIClass*)'

   Adafruit_BME280(int8_t cspin, SPIClass *theSPI = &SPI);

   ^

ESP32-LORA-BME280:19:1: error: 'ssd1306' does not name a type

 ssd1306  display(0x3c, 4, 15);

 ^

D:\Users\Malcolm\My Documents\Arduino\ESP32-LORA-BME280\ESP32-LORA-BME280.ino: In function 'void setup()':

ESP32-LORA-BME280:118:3: error: 'display' was not declared in this scope

   display.init();

   ^

ESP32-LORA-BME280:123:19: error: 'ArialMT_Plain_10' was not declared in this scope

   display.setFont(ArialMT_Plain_10);

                   ^

ESP32-LORA-BME280:124:28: error: 'TEXT_ALIGN_LEFT' was not declared in this scope

   display.setTextAlignment(TEXT_ALIGN_LEFT);

                            ^

ESP32-LORA-BME280:129:7: error: expected unqualified-id before '.' token

   LoRa.setPins(SS,RST,DI0);

       ^

ESP32-LORA-BME280:131:12: error: expected primary-expression before '.' token

   if (!LoRa.begin(BAND)) {

            ^

D:\Users\Malcolm\My Documents\Arduino\ESP32-LORA-BME280\ESP32-LORA-BME280.ino: In function 'void loop()':

ESP32-LORA-BME280:154:3: error: 'display' was not declared in this scope

   display.clear();

   ^

ESP32-LORA-BME280:155:19: error: 'ArialMT_Plain_16' was not declared in this scope

   display.setFont(ArialMT_Plain_16);

                   ^

ESP32-LORA-BME280:161:7: error: expected unqualified-id before '.' token

   LoRa.beginPacket();

       ^

ESP32-LORA-BME280:162:7: error: expected unqualified-id before '.' token

   LoRa.print("Hello..");

       ^

ESP32-LORA-BME280:163:7: error: expected unqualified-id before '.' token

   LoRa.print(counter);

       ^

ESP32-LORA-BME280:164:7: error: expected unqualified-id before '.' token

   LoRa.endPacket();

       ^

exit status 1

invalid conversion from 'int' to 'SPIClass*' [-fpermissive]



This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.

Is it simply a case of my using incorrect libraries? Apologies, but this is all above my expertise.

Does the Adafruit BME280 library show that it supports re-assigment of the SDA and SCL pins ?

Adafruit_BME280 bme(I2C_SDA, I2C_SCL);

Arduino code for BME280 does not work on ESP32. Try this.

Danois90:
Arduino code for BME280 does not work on ESP32. Try this.

The standard Arduino code does work on my ESP32 and prints the results to the serial port. Your suggestion does go a step further by adding a webserver and I will try that. However, the board I have has an onboard 0.96" OLED display and I'd like to print the results to both that and an onboard webserver.

Odd that the BME280 works since the compiler fires a (not unsignificant) warning on its constructor. You should look up the docs for the library you use for the display, maybe there is a better lib like the one from adafruit which works better for you.

srnet:
Does the Adafruit BME280 library show that it supports re-assigment of the SDA and SCL pins ?

Adafruit_BME280 bme(I2C_SDA, I2C_SCL);

It must do, as it works on this basic sketch definition…
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BME280.h>
#define I2C_SDA 4
#define I2C_SCL 15

The natural ESP32 I2C pins are

GPIO_NUM_22, scl
GPIO_NUM__21, sda
GPIO_NUM_18, sck

If using the natural GPIO pins no need to spec pin numbers in the I2C initializer.

The error that the OP points to is when using the Ada Fruity library and when passing a defined I2C object to the initializer; such as using the alternate I2C port available on the ESP32. The ESP32 has 2 I2C controllers. Not passing a I2C object means the default and natural I2C bus will be used.

ESP32 I2C API

I've used the AdaFruity BME280 with an ESP32 on the I2C buss.

bumpydog:
It must do, as it works on this basic sketch definition…
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BME280.h>
#define I2C_SDA 4
#define I2C_SCL 15

That redacted snippet is missing the line that is apparently being rejected in your other code.

So what does the missing Adafruit_BME280 bme … line in the ‘basic sketch definition’ say ?