Esp32 BH1750 als Arduino Tab.
BH1750.ino
// ****************************************************************
// Sketch Esp32 BH1750 Modular(Tab)
// created: Jens Fleischer, 2018-09-10
// last mod: Jens Fleischer, 2020-03-26
// For more information visit: https://fipsok.de
// ****************************************************************
// Hardware: Esp32, BH1750, 2 x 4k7 Ohm Widerstand
// VCC an 3V3
// GND an GND
// SDA an D21
// SCL an D22
// ADD an (nicht verbunden oder GND I2C-Adresse 0x23) - (VCC I2C-Adresse 0x5C)
// 4k7 Ohm Widerstand von VCC auf D21
// 4k7 Ohm Widerstand von VCC auf D22
// Getestet auf: ESP32 NodeMCU-32s
/******************************************************************
Copyright (c) 2018 Jens Fleischer. All rights reserved.
This file is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This file is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
*******************************************************************/
// Diese Version von BH1750 sollte als Tab eingebunden werden.
// #include <ESP32WebServer.h> muss im Haupttab aufgerufen werden
// Die Funktionalität des ESP32 Webservers ist erforderlich.
// Die Funktion "bh1750();" muss im Setup aufgerufen werden.
/**************************************************************************************/
#include <Wire.h>
#include <BH1750.h> // https://github.com/claws/BH1750
BH1750 lux(0x23);
void bh1750() {
Wire.begin();
if (!lux.begin(BH1750::ONE_TIME_HIGH_RES_MODE)) { // einmalige Messung Sensor geht anschliesend in den Abschaltmodus.
DEBUG_P("Keinen BH1750 Sensor gefunden!"); // "BH1750::CONTINUOUS_HIGH_RES_MODE" für dauerhafte Messung
}
server.on("/bh1750", []() {
server.send(200, "application/json", "\"" + (String)lux.readLightLevel() + "\"");
});
}
Die Webseite zum Esp32 BH1750.
bh1750.html
<!DOCTYPE HTML> <!-- For more information visit: https://fipsok.de -->
<html lang="de">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="style32.css">
<title>BH1750 Beleuchtungsstärke</title>
<script>
function renew() {
fetch('/bh1750').then(function (response) {
return response.json();
}).then(function (text) {
document.querySelector('#value').innerHTML = text + ' lx';
});
}
document.addEventListener('DOMContentLoaded', renew);
setInterval(renew, 1000);
</script>
<style>
body {
padding: 10px;
font-size: 3em;
}
section {
align-items: center;
background-color: black;
width: 280px;
height: 100px;
border: .15em solid #aeaeab;
box-shadow: 5px 10px 5px rgba(0, 0, 0, 0.7);
border-radius: .2em;
}
span {
color: #02fc07;
position: relative;
top: 0.5em;
left: .3em;
letter-spacing: .1em;
font-weight: bold
}
</style>
</head>
<body>
<section>
<span id="value"></span>
</section>
</body>
</html>