Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

BH1750 with BLYNK

#define BLYNK_PRINT Serial


#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <Wire.h>
#include <SPI.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define OLED_RESET -1
Adafruit_SSD1306 OLED = Adafruit_SSD1306(128, 32, &Wire,OLED_RESET);

char auth[] = "LxLsRNgCJmUzGSsueQfDtmfgu1vkVrj3"; //โทเคน


char ssid[] = "oak"; //ชื่อwifi
char pass[] = "oakoakoak"; //รหัสwifi

int BH1750address = 0x23; // BH1750 address


byte buff[2];

void setup()
{
Serial.begin(9600);
OLED.begin(SSD1306_SWITCHCAPVCC, 0x3C); // I2C addr 0x3C
Blynk.begin(auth, ssid, pass);
Serial.println("Running");
}

void loop()
{
uint16_t val = 0;
BH1750_Init(BH1750address);
delay(200);
if (2 == BH1750_Read(BH1750address))
{
val = ((buff[0] << 8) | buff[1]) / 1.2; // Light density in Lux
OLED.clearDisplay();
OLED.setTextColor(WHITE);
OLED.setCursor(0, 0);
OLED.setTextSize(1);
OLED.println("BH1750");
OLED.setCursor(0, 20);
OLED.println(String(val) + " lx");
OLED.display();
Blynk.virtualWrite(4,val);
Blynk.run();
}
delay(100);
}

int BH1750_Read(int address) // Reading BH1750


{
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) // Initial BH1750


{
Wire.beginTransmission(address);
Wire.write(0x10); // lx reolution 120ms
Wire.endTransmission();
}

You might also like