Zunächst die Adressen deiner Sensoren scannen.
Die Anzahl der Sensoren muss mit der Anzahl der im Sketch eingetragenen Adressen übereinstimmen.
Esp8266 Ds18b20 Liste als Arduino Tab.
Ds18b20list.ino
// ****************************************************************
// Sketch Esp8266 Ds18b20 Modular(Tab)
// created: Jens Fleischer, 2018-08-26
// last mod: Jens Fleischer, 2018-08-26
// 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 "ds18b20list();" muss im Setup aufgerufen werden.
/**************************************************************************************/
#include <DallasTemperature.h> // Version 3.8.0 https://github.com/milesburton/Arduino-Temperature-Control-Library
#include <OneWire.h> // Version 2.3.4 https://github.com/PaulStoffregen/OneWire
const uint8_t ONE_WIRE_Bus = 12; // Sensor Pin einstellen
// Trage alle Adressen deiner Ds18b20, die am Sensorpin angeschlossen sind ein.
DeviceAddress sensor[] = { {0x28, 0xff, 0x33, 0x10, 0x81, 0x16, 0x04, 0x54}, {0x28, 0xff, 0x07, 0x6f, 0x30, 0x17, 0x04, 0x2e},
{0x28, 0xff, 0x13, 0x6e, 0x30, 0x17, 0x04, 0x2e}
};
// Trage alle Namen deiner Ds18b20 in derselben Reinfolge wie bei den Adressen ein.
const char* dsname[] = {"IN", "OUT", "Solar", "Speicher", "Vorlauf", "Rücklauf"};
OneWire oneWireDs(ONE_WIRE_Bus);
DallasTemperature Ds18b20(&oneWireDs);
void ds18b20list() { // Funktionsaufruf "ds18b20list();" muss im Setup eingebunden werden
Ds18b20.begin();
Ds18b20.setWaitForConversion(false);
Ds18b20.requestTemperatures();
server.on("/lots", handleListDevice);
}
void handleListDevice() {
const byte anzahl = sizeof(sensor) / 8 ;
if (anzahl == Ds18b20.getDS18Count()) {
String temp = "[";
for (int i = 0; i < anzahl; i++ ) {
if (temp != "[") temp += ',';
temp += (String)"{\"name\":\"" + dsname[i] + "\",\"wert\":\"" + Ds18b20.getTemp(sensor[i]) + "\"}";
}
server.send(200, "application/json", temp += "]");
}
else {
Serial.println("Die Anzahl der Sensoren stimmt nicht überein.");
server.send(200, "application/json", "[{\"name\":\"Fehler Anzahl DS18b20\",\"wert\":\"\"}]");
}
Ds18b20.requestTemperatures();
}
Die Webseite zum Esp8266 DS18B20 List.
ds18b20list.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">
<title>Esp8266 Ds18B20</title>
<link rel="stylesheet" href="style.css">
<style>
main {
background-color: #03deff;
border: 0.5em solid #03deff;
box-shadow: 0.3em 0.3em 0.2em #616362;
font-size: 2em;
}
</style>
<script>
function list() {
fetch('lots').then(function (response) {
return response.json();
}).then(function (json) {
for (var i = 0; i < json.length; i++) {
var listItem = document.createElement('span');
listItem.innerHTML = '<strong>' + json[i].name + '</strong>';
document.getElementById('links').appendChild(listItem);
}
for (var i = 0; i < json.length; i++) {
var listItem = document.createElement('span');
listItem.setAttribute('id', 'wert' + i);
document.getElementById('rechts').appendChild(listItem);
}
});
}
document.addEventListener('DOMContentLoaded', list);
function update() {
fetch('lots').then(function (response) {
return response.json();
}).then(function (array) {
for (var i = 0; i < array.length; i++) {
if (array[0].wert != '') document.querySelector('#wert' + i).innerHTML = (array[i].wert / 128).toFixed(2) + '°C';
}
});
}
document.addEventListener('DOMContentLoaded', update);
setInterval(update, 2000); // Interval einstellen in ms -->
</script>
</head>
<body>
<h1>Ds18B20 Liste</h1>
<main>
<aside id="links">
</aside>
<aside id="rechts">
</aside>
</main>
</body>
</html>