Help With Code - DFRobot_BME280_IIC on arduino mkr1010 to blynk

Hello community:smiley:!

could please someone tell me whats wrong with my code?
My connections are right - when I run an example from DFRobot the data prints to the serial monitor!

Any help appreciated!
Thanx in advance.

Here’s the code


// Blynk
#include "DFRobot_BME280.h"
#include "Wire.h"
#define BLYNK_PRINT Serial
// bme280
#include <SPI.h>
#include <WiFiNINA.h>
#include <BlynkSimpleWiFiNINA.h>
#define SEA_LEVEL_PRESSURE    1015.0f   // sea level pressure

// ******** use abbreviations instead of full names ********
typedef DFRobot_BME280_IIC    BME;

BME   bme(&Wire, 0x77);

char auth[] = "******";
// Set password to "" for open networks.
char ssid[] = "******";
char pass[] = "******";
 
float   temp = bme.getTemperature();
uint32_t    press = bme.getPressure();
float   alti = bme.calAltitude(SEA_LEVEL_PRESSURE, press);
float   humi = bme.getHumidity();


BlynkTimer timer;

// This function sends Arduino's up time every second to Virtual Pin (5).
// In the app, Widget's reading frequency should be set to PUSH. This means
// that you define how often to send data to Blynk App.
void myTimerEvent1()
{
  // You can send any value at any time.
  // Please don't send more that 10 values per second.
  Blynk.virtualWrite(V5, millis() / 1000);

}

void myTimerEvent2()
{
  // You can send any value at any time.
  // Please don't send more that 10 values per second.
  Blynk.virtualWrite(V10, temp);



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

  Blynk.begin(auth, ssid, pass);
  // You can also specify server:
  //Blynk.begin(auth, ssid, pass, "blynk-cloud.com", 8442);
  //Blynk.begin(auth, ssid, pass, IPAddress(192,168,1,100), 8442);

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

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

Some information about what you see in the serial monitor and the app when you run this sketch would be useful.

Your code that takes a temperature, pressure and humidity reading is floating outside of a function and needs to be moved into the beginning of the myTimerEvent2 function
.

Pete.

Hello Pete,

thank you for your fast reply! :smiley:
I modified the code as instructed and I can get some date from it. unfortunatelly its the wrong date. do you have any idea why?


// Blynk
#include "DFRobot_BME280.h"
#include "Wire.h"
#define BLYNK_PRINT Serial
// bme280
#include <SPI.h>
#include <WiFiNINA.h>
#include <BlynkSimpleWiFiNINA.h>
#define SEA_LEVEL_PRESSURE    1015   // sea level pressure

// ******** use abbreviations instead of full names ********
typedef DFRobot_BME280_IIC    BME;

BME   bme(&Wire, 0x77);

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




BlynkTimer timer;
  

// This function sends Arduino's up time every second to Virtual Pin (5).
// In the app, Widget's reading frequency should be set to PUSH. This means
// that you define how often to send data to Blynk App.
void myTimerEvent1()
{
  // You can send any value at any time.
  // Please don't send more that 10 values per second.
  Blynk.virtualWrite(V5, millis() / 1000);

}

void myTimerEvent2() 
{
  
  float   temp = bme.getTemperature();
  uint32_t    press = bme.getPressure();
  float   alti = bme.calAltitude(SEA_LEVEL_PRESSURE, press);
  float   humi = bme.getHumidity();
  // You can send any value at any time.
  // Please don't send more that 10 values per second.
  Blynk.virtualWrite(V10, temp);
  Blynk.virtualWrite(V7, humi);
  Blynk.virtualWrite(V8, press);
  Blynk.virtualWrite(V9, alti);
}


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

  Blynk.begin(auth, ssid, pass);
  // You can also specify server:
  //Blynk.begin(auth, ssid, pass, "blynk-cloud.com", 8442);
  //Blynk.begin(auth, ssid, pass, IPAddress(192,168,1,100), 8442);

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

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

If you look at the code examples for the DFRobot BME280 they all contain this line of code in void setup:

bme.begin();

so I think you’re failing to initialise your sensor.

Pete.

2 Likes

And if it still doesn’t work run I2C scanner and verify the BME address you may need to use address 76 instead of 77.

2 Likes

Thank you Pete!
its working now!

Since I am planning to build a more complex device for Iot I would like to learn more about C/C++ do you know where I can get this kind of knowlege? Do you know any tutorials online?

Thank You!
Best regards,

Yannis

thank you for your answer.
the port is right:) I got it working now thanx to a suggestion from Pete.
Have a nice day :slight_smile:

It depends on your learning style.
YouTube is a good place to look if you like that sort of thing. There are also numerous blogs and online tutorials.
If printed books are your thing then there are lots of them too.

@daveblynk as referring to the device address on the I2C bus. Ports are something else entirely.

Pete.

Yes right! the address is 0x77.

Thank you for your assistance and recommendations!