/* Read multiple DS18B20 1-Wire digital temperature sensors by address.
PP le 1 mai 2026 */
// Include the required Arduino libraries:
#include "OneWire.h"
#include "DallasTemperature.h"
#include <LiquidCrystal_I2C.h>
const byte I2C_ADDR = 0x27;
const byte LCD_COLUMNS = 20;
const byte LCD_LINES = 4;
LiquidCrystal_I2C lcd(I2C_ADDR, LCD_COLUMNS, LCD_LINES);
// Define to which pin of the Arduino the 1-Wire bus is connected:
#define ONE_WIRE_BUS 2 // 1 wire sur broche 2
// Create a new instance of the oneWire class to communicate with any OneWire device:
OneWire oneWire(ONE_WIRE_BUS);
// Pass the oneWire reference to DallasTemperature library:
DallasTemperature sensors(&oneWire);
// Addresses of DS18B20 sensors connected to the 1-Wire bus
// MMMMMMMMMMMMMMMM adresse réelle Matériel approvisionné MMMMMMMMMMMMMMM
//byte sensor1[8] = {0x28, 0xBB, 0x22, 0xBB, 0x00, 0x00, 0x00, 0x9F};
//byte sensor2[8] = {0x28, 0xDB, 0x5E, 0xBF, 0x00, 0x00, 0x00, 0x1E};
// TTTTTTTTTTTTTTTT adresse pour simulateur avec json TTTTTTTTTTTTTTTTTT
byte sensor1[8] = {0x28, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x7E};
byte sensor2[8] = {0x28, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0xDE};
float Temperature ; // tampon valeur Température de chaque sonde
/* Pour faire des mesures à intervalle régulier */
unsigned long tempsCourant = 0;
unsigned long tempsPrecedent = 0;
float intervalle = 5000; // durée entre deux mesures en ms (à modifier si nécessaire)
void setup() {
// Begin serial communication at a baud rate of 9600:
Serial.begin(9600);
lcd.init();
lcd.backlight();
// Start up the library:
sensors.begin();
// Set the resolution for all devices to 9 bits (résolution 0,5°C)
sensors.setResolution(9);
/*
9 bits 0.5 °C 93.75 ms
10 bits 0.25 °C 187.5 ms
11 bits 0.125 °C 375 ms
12 bits 0.0625 °C 750 ms */
}
void loop() {
tempsCourant = millis(); //maj temps courant
if (tempsCourant - tempsPrecedent >= intervalle - 100) { //100 ms avant cycle mesure
// Send the command for all devices on the bus to perform a temperature conversion:
sensors.requestTemperatures(); // demande de conversion
}
if (tempsCourant - tempsPrecedent >= intervalle) { // debut cycle mesure
tempsPrecedent = tempsCourant; // maj temps precedent
lcd.clear(); // efface tout le lcd
lcd.setCursor(4, 0); // colonne, ligne
lcd.print("Temperatures:");
// XXXXXXXXXXXXXXX Sonde 1 XXXXXXXXXXXXXXXXXX
Temperature = sensors.getTempC(sensor1);
lcd.setCursor(0, 1); // colonne, ligne
lcd.print("Sonde 1: ");
lcd.print(Temperature, 1); // 1 chiffre apres la virgule
lcd.print((char)223); // symbole °
lcd.print("C");
Serial.print("Sonde 1: ");
Serial.print(Temperature, 1);
Serial.print(" \xC2\xB0"); // shows degree symbol
Serial.print("C | ");
// XXXXXXXXXXXXXXX Sonde 2 XXXXXXXXXXXXXXXXXX
Temperature = sensors.getTempC(sensor2);
lcd.setCursor(0, 2); // colonne, ligne
lcd.print("Sonde 2: ");
lcd.print(Temperature, 1); // 1 chiffre apres la virgule
lcd.print((char)223); // symbole °
lcd.print("C");
Serial.print("Sonde 2: ");
Serial.print(Temperature, 1);
Serial.print(" \xC2\xB0"); // shows degree symbol
Serial.print("C | ");
Serial.println(); // prints an empty line
}
}