My first blynk project BME280 with LDR

Hello Blynk friends

I finished my first Blynk project and need some tips or improvements on my coding.

Setup is

  • Board NodeMCU
  • Blynk local server
  • BME280 sensor
  • LDR sensor
  • Blynk app on android and iPhone

This is the code im using (works but im sure it can (and needs? corrections)

#include <SPI.h>
#define BLYNK_PRINT Serial // Comment this out to disable prints and save space
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <SimpleTimer.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 (1023.25)

// Blynk auth
char auth[] = "##############################";

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "######";
char pass[] = "###########";

//Adafruit_BME280 bme(BME_CS);
Adafruit_BME280 bme;
//Adafruit_BME280 bme(BME_CS, BME_MOSI, BME_MISO,  BME_SCK);

SimpleTimer timer;

void sendSensor()
  Wire.begin(); //just added
  float t = bme.readTemperature();
  float p  = (bme.readPressure() / 100.0F);
  float h =  bme.readHumidity();
  float a =  bme.readAltitude(SEALEVELPRESSURE_HPA);
  if (isnan(t) || isnan(p) || isnan(h) || isnan(a)) {
    Serial.println("Failed to read from BME sensor!");
  // You can send any value at any time.
  // Please don't send more that 10 values per second.
  Blynk.virtualWrite(V1, t);
  // Blynk.virtualWrite(V1, p);
  Blynk.virtualWrite(V2, h);
  // Blynk.virtualWrite(V3, a);

void showRSSI(){
  Blynk.virtualWrite(V30, String("Wifi: ") + map(WiFi.RSSI(), -105, -40, 0, 100) + String('%'));

void MyLDR(){

int LDR = A0;
int LDRValue = analogRead(LDR);
//Serial.print("sensor = ");

  Blynk.virtualWrite(V20, LDRValue);     //Value Display  V0

if (LDRValue <=900 )
//Serial.println("Lampen aan");
Blynk.virtualWrite(V5, 255);
//Serial.println("Lampen uit");
Blynk.virtualWrite(V5, 0);

void setup()

  Blynk.begin(auth, ssid, pass, "private", 8080);
  timer.setInterval(3000L, sendSensor);
  timer.setInterval(2000L, MyLDR);
  timer.setInterval(1000L, showRSSI);

void CheckConnection(){    // check every 11s if connected to Blynk server
    Serial.println("Not connected to Blynk server"); 
    Blynk.connect();  // try to connect to server with default timeout
    Serial.println("Connected to Blynk server");     

void loop() {

Im a total noob at coding some any tip or corrections to improve my sketch are welcome. Thnx!

I’m curious as to why you added the Wire.begin command in your sendSensor function.
I don’t think it’s required, but if it is then it only needs to be run once, not every time you take a sensor reading.

I’ve recently finished the hardware and initial software side of a weather station which uses the BME280 and a light sensor, among other things. My light sensor is an SPI device rather than analogue and it gives some very useful data. Plotting the results on SuperChart make it easy to see which days were bright and which were overcast…


1 Like

Thanks Pete! I will remove the Wire.Begin from the sendSensor function.

Im using the LDR just to check if the lights are on or off so i kept it simple and used analog.

The other thing that you might want to look at is your timers. You have timers that run every second, two seconds and three seconds.
That means that every two seconds the one and two second timers will coincide, and every 6 seconds all three will coincide.
Whilst this isn’t really a major problem,it’s something to be avoided if possible.

Think about how frequently you really want these readings, and consider combining all or some of the actions into a single times function. If you do go for the single timed function then you may need to add some commands into that function to ensure that the Blynk library isn’t starved of processor time.


Ah yes :smile: Nice find. I didnt pay attention to that at all. Ill read up on the combining into a single function. Thanks for the tips Pete!

Also you might want to consider using the BlynkTimer rather than the SimpleTimer