Nodemcu, DHT22 and BMP280 - combine code

Hi,

i am having the Nodemcu + DHT22 working and sending data to Blynk for me to view on my iPhone App. In another code I have data from the BMP280 to display on the Serial Monitor. I have tried to combine the 2 working codes into one…but I do not get any data from the BMP280 send to Blynk.

Can someone spot what I am missing in the code?

Connections are:
Nodemcu 3v3 - DHT +
Nodemcu Gnd - DHT -
Nodemcu D4 - DHT signal
Nodemcu D2 - BMP SDA
Nodemcu D1 - BMP SCL
Nodemcu 3v3 - BMP VCC
Nodemcu 3v3 - BMP CSB
Nodemcu 3v3 - BMP SD0
Nodemcu Gnd - BMP GND

#define BLYNK_PRINT Serial    // Comment this out to disable prints and save space
#include <SPI.h>
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <SimpleTimer.h>
#include <DHT.h>
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BMP280.h>


char auth[] = "bbbbbbbbbbb";
char ssid[] = "bbbbbbb";  //Enter your WIFI Name
char pass[] = "bbbbbb";  //Enter your WIFI Password

#define DHTPIN 2          // Digital pin 4
#define DHTTYPE DHT22   // DHT 22, AM2302, AM2321
#define BMP_SCK 13
#define BMP_MISO 12
#define BMP_MOSI 11 
#define BMP_CS 10

Adafruit_BMP280 bme; // I2C

DHT dht(DHTPIN, DHTTYPE);
SimpleTimer timer;

void sendSensor()
{
  float h = dht.readHumidity();
  float t = dht.readTemperature();
  float a = bme.readTemperature();
  float b = bme.readPressure()/100;
  float c = bme.readAltitude(1015.80);

  if (isnan(h) || isnan(t)) {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }
  // You can send any value at any time.
  // Please don't send more that 10 values per second.
  Blynk.virtualWrite(V5, h);  //V5 is for Humidity on DHT22
  Blynk.virtualWrite(V6, t);  //V6 is for Temperature on DHT22
  Blynk.virtualWrite(V7, a);  //V7 is for Temperature on BMP280
  Blynk.virtualWrite(V8, b);  //V8 is for Pressure on BMP280
  Blynk.virtualWrite(V9, c);  //V9 is for Altitude on BMP280
}

void setup()
{
  Serial.begin(9600); // See the connection status in Serial Monitor
  Blynk.begin(auth, ssid, pass);

  dht.begin();

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

void loop()
{
  Blynk.run(); // Initiates Blynk
  timer.run(); // Initiates SimpleTimer
}

FYI, I fixed your post… you had the wrong characters at the end of the code… backticks, not commas or apostrophes :slight_smile:

Thank you Gunner - can you fix my code as well :smile:

Nope… how will you learn if I do? :stuck_out_tongue:

I volunteer my time here to try to teach about Blynk, not how to program, or be someones code mechanic… or at least not until I see someone putting efforts into learning on their own first :wink:

If both your separate codes worked before then go through them, line by line if needed, to figure out why and how they worked. Then make sure you have all the needed parts in your combined code.

Thank you coach…

You actually helped me there…I just needed the “bme.begin();” at the end!!!

:star_struck:

1 Like

Good!.. now here is another tip.

BlynkTimer is built into the library and based on SimpleTimer. So you no longer need to include the SimpleTimer.h library and just change the initialization to this…

BlynkTimer timer;

Otherwise sometimes it will give compiling errors… at least it does when I dig up old codes of mine.

Thanks - changed it at it works!

CeeeWeee, would you post code for the bmp280 only please

Just got home…here is the full code:

#include <SPI.h>
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <SimpleTimer.h>
#include <DHT.h>
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BMP280.h>


char auth[] = "...";
char ssid[] = ...";  //Enter your WIFI Name
char pass[] = ...";  //Enter your WIFI Password

#define DHTPIN 2          // Digital pin 4
#define DHTTYPE DHT22   // DHT 22, AM2302, AM2321
#define BMP_SCK 13
#define BMP_MISO 12
#define BMP_MOSI 11 
#define BMP_CS 10

Adafruit_BMP280 bme; // I2C

DHT dht(DHTPIN, DHTTYPE);
SimpleTimer timer;

void sendSensor()
{
  float h = dht.readHumidity();
  float t = dht.readTemperature();
  float a = bme.readTemperature();
  float b = bme.readPressure()/100;
  float c = bme.readAltitude(1015.80);

  if (isnan(h) || isnan(t)) {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }
  // You can send any value at any time.
  // Please don't send more that 10 values per second.
  Blynk.virtualWrite(V5, h);  //V5 is for Humidity on DHT22
  Blynk.virtualWrite(V6, t);  //V6 is for Temperature on DHT22
  Blynk.virtualWrite(V7, a);  //V7 is for Temperature on BMP280
  Blynk.virtualWrite(V8, b);  //V8 is for Pressure on BMP280
  Blynk.virtualWrite(V9, c);  //V9 is for Altitude on BMP280
}

void setup()
{
  Serial.begin(9600); // See the connection status in Serial Monitor
  Blynk.begin(auth, ssid, pass);

  dht.begin();
  bme.begin();

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

void loop()
{
  Blynk.run(); // Initiates Blynk
  timer.run(); // Initiates SimpleTimer
}

Hope this is a good start…

@CeeeWeee Is yours the 6 pin China model also? My wiring is SDO and CSB = 3.3 SDA to D2 and SCL to D1, but it is not working. How did you wire it up?

David

EDIT Duh! just seen the wiring in the first post… I have it the same. Did you have to change the address in the H file?

Edit: Do you know where you got your BMP library from?

Edit: Maybe I should have started a new topic… I have tried a BMP library and a BME lib from Adafruit. They both out put a 0.00 for temp and 0.00 for humidity and 44330.0 for Alt.

For others having issues try a different wiring. The h file in the library has both addresses 76 and 77 but for some reason I had to change my SDO to GRN which changes the I2C address AND the 77 to 76 to make it work.

dot H file in the library

I2C ADDRESS/BITS/SETTINGS/
#define BMP280_ADDRESS (0x76) /< The default I2C address for the sensor. */
#define BMP280_ADDRESS_ALT
(0x77) /
< Alternative I2C address for the sensor. */

Got it going!

hey mate, i use the cactus BME library - seems to work best out of all the other libraries i tried.

The Adafruit library allows you to specify the I2C address in code when you initialise these sensor object(s).

I use a very simple bit of I2C scanner code (which I think is part of the basic Arduino IDE examples, but if not then there are lots of examples if you google them). This is handy because it lets you know that you have the sensor wiring correct, then tells you the address of the sensor(s) you have hooked-up to the bus. You can then specify the the addresses you want to use in the code, and change one of the sensor addresses if you have a conflict.

Not all sensors allow you to change the address, but the purple BMP280 sensors have solder pads on the board that allows you to change address.

Pete.

Curious if anyone has messed with the Adafruit libs for this. For testing only I hooked both sensors to one Node. With the scanner I am getting the 76 and the alt 77 but the alt address doesn’t give any readings so obviously the “alt” code isn’t in the h and cpp files???

Try the cactus code

The Catcus Lib says BME but it will work for my BMP??

I’ve used ebay sensors, purple and blue.

Your code references both bme & bmp, so not sure…

edit: a quick read here: https://www.instructables.com/id/Library-for-BMP280/

indicates it could be worth a trying to use a BME library even if you only have BMP labeled sensors.