/* ============================================
code is placed under the MIT license
Copyright (c) 2026 J-M-L
For the Arduino Forum : https://forum.arduino.cc/u/j-m-l
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
===============================================
*/
#include <OneWire.h>
#include <DallasTemperature.h>
class OneWireBus {
public:
byte busPin;
OneWire oneWire;
DallasTemperature capteurs;
byte nbDevices;
DeviceAddress* deviceAddresses;
float* dernieresTemperatures;
OneWireBus(byte pin) : busPin(pin), oneWire(pin), capteurs(&oneWire), nbDevices(0),
deviceAddresses(nullptr), dernieresTemperatures(nullptr) {}
~OneWireBus() {
if (deviceAddresses != nullptr) delete[] deviceAddresses;
if (dernieresTemperatures != nullptr) delete[] dernieresTemperatures;
}
void rechercheCapteurs() {
byte adresse[8];
oneWire.reset_search();
nbDevices = 0;
while (oneWire.search(adresse)) nbDevices++;
deviceAddresses = new DeviceAddress[nbDevices];
if (deviceAddresses == nullptr) {
Serial.println("Erreur : impossible d'allouer le tableau des adresses !");
while (true) yield();
}
dernieresTemperatures = new float[nbDevices];
if (dernieresTemperatures == nullptr) {
Serial.println("Erreur : impossible d'allouer le tableau des temperatures !");
while (true) yield();
}
for (byte i = 0; i < nbDevices; i++) dernieresTemperatures[i] = NAN;
oneWire.reset_search();
byte idx = 0;
Serial.print("Bus sur la broche ");
Serial.println(busPin);
while (oneWire.search(adresse)) {
memcpy(deviceAddresses[idx], adresse, 8);
Serial.print("Capteur ");
Serial.print(idx);
Serial.print(" adresse = {");
for (byte j = 0; j < 8; j++) {
Serial.print("0x");
if (adresse[j] < 16) Serial.print("0");
Serial.print(adresse[j], HEX);
if (j < 7) Serial.print(",");
}
Serial.println("}");
idx++;
}
}
void begin() {
capteurs.begin();
rechercheCapteurs();
capteurs.setWaitForConversion(false);
capteurs.requestTemperatures();
}
bool mettreAJourTemperatures() {
if (!capteurs.isConversionComplete()) return false; // conversion non terminée, on sort
for (byte i = 0; i < nbDevices; i++) {
float t = capteurs.getTempC(deviceAddresses[i]);
if (!isnan(t) && (isnan(dernieresTemperatures[i]) || abs(t - dernieresTemperatures[i]) >= 0.1)) {
Serial.print("Bus ");
Serial.print(busPin);
Serial.print(" Capteur ");
Serial.print(i);
Serial.print(" = ");
Serial.print(t, 1);
Serial.println(" °C");
dernieresTemperatures[i] = t;
}
}
capteurs.requestTemperatures(); // relance la prochaine conversion
return true;
}
// Retourne un pointeur constant vers les dernières températures et leur nombre
const float* obtenirDernieresTemperatures(byte &nombreCapteurs) const {
nombreCapteurs = nbDevices;
return dernieresTemperatures;
}
};
// Création des bus
OneWireBus lesBus[] = {7, 8};
const byte nbBus = sizeof lesBus / sizeof * lesBus;
void setup() {
Serial.begin(115200);
for (auto& bus : lesBus) bus.begin();
}
void loop() {
for (auto& bus : lesBus) bus.mettreAJourTemperatures();
}