BME280 Arduino Sketch

Hi all,
I’m trying to get my BME280 to report to Cayenne via a NodeMCU, but just can’t seem to get my sketch to work. Admittedly I’m a bit of a beginner, so I’ve been using existing code and trying to edit to fit my needs, but with no luck.

The only examples I could find are a few years old and aren’t working - is this a combination anyone has used lately that they could help me with?

I know the BMP180 is officially supported, but I’d like the combined inputs of heat and humidity…

Thanks in advance!

can you share your code? will check what is the issue with it.

Thanks for your reply!

It’s code I borrowed from another thread in this forum, but I’m met with errors while trying to verify.

Error seems to be:

“Cayennenew:24:37: error: invalid conversion from ‘int’ to ‘SPIClass*’ [-fpermissive]
Adafruit_BME280 bme(I2C_SDA, I2C_SCL);”

Sketch:

//#define CAYENNE_DEBUG
#define CAYENNE_PRINT Serial
#include <CayenneMQTTESP8266.h>
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BME280.h>
#define I2C_SDA 32
#define I2C_SCL 33

Adafruit_BME280 bme(I2C_SDA, I2C_SCL);

// WiFi network info.
char ssid = “MY SSID”;
char wifiPassword = “MY PASSWORD”;

// Cayenne authentication info. This should be obtained from the Cayenne Dashboard.
char username = “MY CAYENNE USERNAME”;
char password = “MY CAYENNE PASSWORD”;
char clientID = “MY CAYENNE CLIENTID”;

void setup() {
Serial.begin(9600);
Cayenne.begin(username, password, clientID, ssid, wifiPassword);
bme.begin(0x76);
}

void loop() {
Cayenne.loop();
}

// Default function for sending sensor data at intervals to Cayenne.
// You can also use functions for specific channels, e.g CAYENNE_OUT(1) for sending channel 1 data.
CAYENNE_OUT_DEFAULT()
{
// Write data to Cayenne here. This example just sends the current uptime in milliseconds on virtual channel 0.
Cayenne.virtualWrite(0, millis());

Cayenne.celsiusWrite(1, bme.readTemperature());
Cayenne.virtualWrite(2, bme.readPressure(), “bp”, “pa”);
Cayenne.virtualWrite(3, bme.readHumidity(), “rel_hum”, “p”);
}

// Default function for processing actuator commands from the Cayenne Dashboard.
// You can also use functions for specific channels, e.g CAYENNE_IN(1) for channel 1 commands.
CAYENNE_IN_DEFAULT()
{
CAYENNE_LOG(“Channel %u, value %s”, request.channel, getValue.asString());
//Process message here. If there is an error set an error message using getValue.setError(), e.g getValue.setError(“Error message”);
}

try this:

/*
  This example shows how to connect to Cayenne using an ESP8266 and send/receive sample data.
  The CayenneMQTT Library is required to run this sketch. If you have not already done so you can install it from the Arduino IDE Library Manager.
  Steps:
  1. If you have not already installed the ESP8266 Board Package install it using the instructions here: https://github.com/esp8266/Arduino#installing-with-boards-manager.
  2. Select your ESP8266 board from the Tools menu.
  3. Set the Cayenne authentication info to match the authentication info from the Dashboard.
  4. Set the network name and password.
  5. Compile and upload the sketch.
  6. A temporary widget will be automatically generated in the Cayenne Dashboard. To make the widget permanent click the plus sign on the widget.
*/


#define CAYENNE_DEBUG
#define CAYENNE_PRINT Serial
#include <CayenneMQTTESP8266.h>

// WiFi network info.
char ssid[] = "xxxx";
char wifiPassword[] = "yyyy";


char username[] = "";
char password[] = "";
char clientID[] = "";

#include <Wire.h>
#include <SPI.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BME280.h>

#define BME_SCK 13
#define BME_MISO 12
#define BME_MOSI 11
#define BME_CS 10

#define SEALEVELPRESSURE_HPA (1013.25)

Adafruit_BME280 bme; // I2C
//Adafruit_BME280 bme(BME_CS); // hardware SPI
//Adafruit_BME280 bme(BME_CS, BME_MOSI, BME_MISO, BME_SCK); // software SPI

unsigned long delayTime;

void setup() {
  Serial.begin(9600);
  while (!Serial);   // time to get serial running
  Serial.println(F("BME280 test"));

  unsigned status;

  // default settings
  // (you can also pass in a Wire library object like &Wire2)
  status = bme.begin();
  if (!status) {
    Serial.println("Could not find a valid BME280 sensor, check wiring, address, sensor ID!");
    Serial.print("SensorID was: 0x"); Serial.println(bme.sensorID(), 16);
    Serial.print("        ID of 0xFF probably means a bad address, a BMP 180 or BMP 085\n");
    Serial.print("   ID of 0x56-0x58 represents a BMP 280,\n");
    Serial.print("        ID of 0x60 represents a BME 280.\n");
    Serial.print("        ID of 0x61 represents a BME 680.\n");
    while (1);
  }

  Serial.println("-- Default Test --");
  delayTime = 1000;

  Serial.println();
  Cayenne.begin(username, password, clientID, ssid, wifiPassword);

}


void loop() {
  Cayenne.loop();
}

CAYENNE_OUT_DEFAULT()
{
  int x = bme.readTemperature();
  int y = bme.readHumidity();
  int z = bme.readPressure();
  Cayenne.celsiusWrite(1, x);
  Cayenne.virtualWrite(2, y, "rel_hum", "p");
  Cayenne.virtualWrite(3, z, "bp", "pa");
}

That’s done it, thanks so much! Are you using the same project?

Is it possible to include the numbers after the decimal for Temp/Humidity? (E.g. 21.7 degrees C)

Thanks again, I was tearing my hair out with this one.

no, i just modified your code for it to work properly.

it should be 21.7, what is the value is getting.