BH1750, Blynk, ESP8266 12E

Hi. I find a sketch for Arduino IDE. I need sending data(lux) form sensor BH1750 to app Blynk… Thanks.

@Jiri_Bam from your other posts I believe the sketch below is your regular BH1750 code and you want to make it work with Blynk, right?

I notice you tried to use @Dave1829’s battery operated sensor code but you were struggling with an old IDE and some of the libraries. Unless you know the Blynk basics and have an identical set up to Dave you are going to struggle.

When I asked if you had tried any of the basic Blynk examples I was referring to simply controlling an LED etc before you move on to the BH1750.

#include <Wire.h>
#include <math.h> 
int BH1750address = 0x23; //i2c address
 
byte buff[2];
void setup()
{
  Wire.begin();
  Serial.begin(57600);
}
 
void loop()
{
  int i;
  uint16_t val=0;
  BH1750_Init(BH1750address);
  delay(200);
 
  if(2==BH1750_Read(BH1750address))
  {
    val=((buff[0]<<8)|buff[1])/1.2;
    Serial.print(val,DEC);     
    Serial.println("lux"); 
  }
  delay(150);
}
 
int BH1750_Read(int address) //
{
  int i=0;
  Wire.beginTransmission(address);
  Wire.requestFrom(address, 2);
  while(Wire.available()) //
  {
    buff[i] = Wire.read();  // receive one byte
    i++;
  }
  Wire.endTransmission();  
  return i;
}
 
void BH1750_Init(int address) 
{
  Wire.beginTransmission(address);
  Wire.write(0x10);//1lx reolution 120ms
  Wire.endTransmission();
}

Suggest you search the forum for “Blynkify” and read all threads from start to finish that use this term.

If you are still struggling after you have done this let us know.

Yes right. And i was try add blynk to this code but no worked it. I am no good programmer… THanks

It would take an experienced Blynker just a few minutes to convert your regular Arduino sketch for use with Blynk and in the passed I have posted details of how to do this. But if a Blynker simply provides you with the required sketch you don’t learn anything i.e. why the sketch is laid out how it is etc.

I would urge you to read the Blynkify threads but the basics are that Blynk loop()'s are very different from most basic Arduino loop()'s.

The PushData example at https://github.com/blynkkk/blynk-library/blob/master/examples/GettingStarted/PushData/PushData.ino is a good starting point as it introduces you to timed events which is critical for use with Blynk. You will need to convert the sketch from Ethernet to ESP use and then add in the BH1750, but do it in stages.

Yes. I was mix this two sketch together… But something is wrong:( Because data form BH1750 no sending to app Blynk. Thanks.



#define BLYNK_PRINT Serial    // Comment this out to disable prints and save space
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <SimpleTimer.h>
#include <DHT.h>
#define DHTPIN 12 //pin gpio 12 in sensor
#define DHTTYPE DHT22   // DHT 22 Change this if you have a DHT11
DHT dht(DHTPIN, DHTTYPE);
#include <BH1750.h>

#include <Wire.h>
#include <math.h> 
int BH1750address = 0x23; //i2c address

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "75a3e91cbdf148b35eea63e9c1";  // Put your Auth Token here. (see Step 3 above)

SimpleTimer timer;
 
byte buff[2];
void setup()
{
  Wire.begin();
  Serial.begin(9600); // See the connection status in Serial Monitor
   Blynk.begin(auth, "AUS", "Jern72"); //insert here your SSID and password
 
  // Setup a function to be called every second
  timer.setInterval(10000L, sendUptime);
 
}

void sendUptime()
{
  // You can send any value at any time.
  // Please don't send more that 10 values per second.
   //Read the Temp and Humidity from DHT
  float h = dht.readHumidity();
  float t = dht.readTemperature();

  float lux;
  Blynk.virtualWrite(4, lux);
  
  Blynk.virtualWrite(12, t); // virtual pin 
  Blynk.virtualWrite(13, h); // virtual pin 
}

void loop()
{
  int i;
  uint16_t val=0;
  BH1750_Init(BH1750address);
  delay(200);
 
  if(2==BH1750_Read(BH1750address))
  {
    val=((buff[0]<<8)|buff[1])/1.2;
    Serial.print(val,DEC);     
    Serial.println("lux"); 
  }
   Blynk.run();
  timer.run();
  delay(150);
}

int BH1750_Read(int address) //
{
  int i=0;
  Wire.beginTransmission(address);
  Wire.requestFrom(address, 2);
  while(Wire.available()) //
  {
    buff[i] = Wire.read();  // receive one byte
    i++;
  }
  Wire.endTransmission();  
  return i;
}
 
void BH1750_Init(int address) 
{
  Wire.beginTransmission(address);
  Wire.write(0x10);//1lx reolution 120ms
  Wire.endTransmission();
}
  

A typical Blynk loop() looks like this:

void loop(){
  Blynk.run();
  timer.run();
}

Anything else that you wanted to have in loop() should go in the timer event.

Yes. So i was trying repair:

void loop(){
  Blynk.run();
  timer.run();
}

But it is same. ESP8266 is conecting to blynk but lux no sending to my app Blynk… Is there another wrong… Thanks.

I thing this is no good:

 float lux;
  Blynk.virtualWrite(4, lux);

You are right, you can’t simply declare a float and send it to the Blynk server. Once you have declared the float, your sensor needs to provide the value before sending it to the server.

The stuff you had in loop() is still required, but not in loop().

Post your updated sketch when you have made the changes.

Below is the simplest BH1750 Arduino sketch I can find but it depends precisely which model you have.

#include <Wire.h>
#include <BH1750.h>  // https://github.com/claws/BH1750
BH1750 lightMeter;
 
void setup(){
  Serial.begin(9600);
  lightMeter.begin();
}
 
void loop() {
  uint16_t lux = lightMeter.readLightLevel();
  Serial.print("Light: ");
  Serial.print(lux);
  Serial.println(" lx");
  delay(1000);
}

So for Blynk you need to keep the first 3 lines and lightMeter.begin in setup().

In sendUptime() you need:

uint16_t lux = lightMeter.readLightLevel();
Blynk.virtualWrite(4, lux);

SO YES. Now it is working:-) THank you so much.


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

#include <Wire.h>
#include <BH1750.h>  // https://github.com/claws/BH1750
BH1750 lightMeter;

#define DHTPIN 12 //pin gpio 12 in sensor
#define DHTTYPE DHT22   // DHT 22 Change this if you have a DHT11
DHT dht(DHTPIN, DHTTYPE);

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "75a3e91cbdf1457eea63e9c1";  // Put your Auth Token here. (see Step 3 above)

SimpleTimer timer;

void setup()
{
  Serial.begin(9600); // See the connection status in Serial Monitor
   Blynk.begin(auth, "ASUS", "Jesin72"); //insert here your SSID and password

  lightMeter.begin();
 
  // Setup a function to be called every second
  timer.setInterval(10000L, sendUptime);
}

void sendUptime()
{

   uint16_t lux = lightMeter.readLightLevel();
   Blynk.virtualWrite(4, lux);
  
  // You can send any value at any time.
  // Please don't send more that 10 values per second.
   //Read the Temp and Humidity from DHT
  float h = dht.readHumidity();
  float t = dht.readTemperature();
  
  Blynk.virtualWrite(12, t); // virtual pin 
  Blynk.virtualWrite(13, h); // virtual pin 
}

void loop()
{

uint16_t lux = lightMeter.readLightLevel();
  Serial.print("Light: ");
  Serial.print(lux);
  Serial.println(" lx");
  delay(1000);
  
  Blynk.run();
  timer.run();
}



@Jiri_Bam glad it worked for you but you didn’t learn very much. If you had read the Blynkify threads you would know why your loop() is a mess and the first five lines shouldn’t be there.

Yes. And what dou you mean: Blynkify? FOrum here? THanks.

Blynkify means turn an Arduino sketch into a sketch that will run with Blynk and generally it has just 2 lines (run and timer) with no delays.

Ok. Thakn you:-)

Hello Jiri_Bam!

What pins to you connect the BH1750? are you using NodeMcu in this project?
Thanks. :slight_smile:

First, Welcome to the Blynk forum.

Please make note of the TimeDate stamps before posting, this topic is almost a year old.

The BH1750 is an I2C device, so whatever pins are available on your device for I2C are what you would use.