Esp8266 Ds18b20 als Arduino Tab.
Ds18b20.ino
// ****************************************************************
// Sketch Esp8266 Ds18b20 Modular(Tab)
// created: Jens Fleischer, 2018-05-16
// last mod: Jens Fleischer, 2018-08-08
// For more information visit: https://fipsok.de
// ****************************************************************
// Hardware: Esp8266, DS18B20, 4k7 Ohm Widerstand
// D6 = GPIO12 Anschluss DS18B20
// 4k7 Ohm Widerstand von VCC auf GPIO12
// Getestet auf: Nodemcu, Wemos D1 Mini Pro
/******************************************************************
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 Ds18b20 sollte als Tab eingebunden werden.
// #include <ESP8266WebServer.h> muss im Haupttab aufgerufen werden
// Die Funktionalität des ESP8266 Webservers ist erforderlich.
// Die Funktion "ds18b20();" muss im Setup aufgerufen werden.
/**************************************************************************************/
#include <DallasTemperature.h> //https://github.com/milesburton/Arduino-Temperature-Control-Library Version 3.8.0
const byte ONE_WIRE_PIN = D6; // Pin für Ds18b20 einstellen
OneWire oneWire(ONE_WIRE_PIN);
DallasTemperature DS18B20(&oneWire);
void ds18b20() { // Funktionsaufruf "ds18b20();" muss im Setup eingebunden werden
DS18B20.begin();
// setze die globale Auflösung auf 9, 10, 11 oder 12 Bits
//DS18B20.setResolution(12); // nur zum ändern der Auflösung notwendig
DS18B20.getDeviceCount() ? printf("\nDS18B20 Auflösung: %d Bit\n", DS18B20.getResolution()) : printf("\nKein DS18B20 am Pin %d gefunden\n", ONE_WIRE_PIN);
DS18B20.setWaitForConversion(false);
DS18B20.requestTemperatures();
server.on("/ds18b20", []() {
server.send(200, "application/json", "\"" + (String)DS18B20.getTempCByIndex(0) + "\"");
DS18B20.requestTemperatures();
});
}
Die Webseite zum Esp8266 DS18B20.
ds18b20.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.0">
<link rel="stylesheet" href="style.css">
<title>DS18B20 Temperatur</title>
<script>
function renew() {
fetch('/ds18b20').then(function (response) {
return response.json();
}).then(function (text) {
var elem = document.querySelector('#wert');
text != '-127.00' ? elem.innerHTML = text.replace(".",",") + '°C' : elem.innerHTML = 'Fehler';
});
}
document.addEventListener('DOMContentLoaded', renew);
setInterval(renew, 1000);
</script>
<style>
body {
padding: 10px;
font-size: 3em;
}
main {
display: flex;
flex-direction: column;
align-items: center;
background-color: black;
width: 280px;
height: 100px;
padding: 0.2em;
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>
<main>
<span id='wert'></span>
</main>
</body>
</html>