Arduino Temperature Control Library

repository·master·Indexed 21 days ago

https://github.com/milesburton/arduino-temperature-control-library

A library for interfacing with Maxim Temperature Integrated Circuits using the 1-Wire protocol. It supports devices such as the DS18B20, DS18S20, DS1822, DS1820, MAX31820, and MAX31850/1. The library requires the OneWire library as a dependency and provides functionality for reading temperature data, managing sensor addresses, and configuring memory footprints via preprocessor definitions.

Tokens
1.4K
Snippets
2
Records
7
Agent score
27%

What's inside DallasTemperature

  1. Install the DallasTemperature library

    master

    You can install the library using the Arduino IDE Library Manager (recommended) or via manual ZIP installation.

    1. Open Arduino IDE.
    2. Go to Tools > Manage Libraries...
    3. Search for DallasTemperature and click Install.
    4. Important: You must also install the OneWire library by Paul Stoffregen using the same method, as it is a required dependency.

    Manual Installation

    1. Download the latest release from the GitHub releases page.
    2. In Arduino IDE, go to Sketch > Include Library > Add .ZIP Library...
    3. Select the downloaded ZIP file.
    4. Repeat these steps for the required OneWire library.
  2. Set up the ESP-WebServer example for DS18B20 sensors

    master

    This example demonstrates how to use the Arduino Temperature Control Library on an ESP8266 or ESP32 to read temperature data from Maxim (Dallas) DS18B20 sensors and serve it via Wi-Fi. It provides both a human-friendly web dashboard (using Chart.js and Tailwind CSS) and machine-readable REST endpoints.

    Prerequisites

    1. Arduino Temperature Control Library installed.
    2. ESP8266 or ESP32 core installed in your Arduino IDE.
    3. DS18B20 sensor(s) connected to the ESP using OneWire with a pull-up resistor.

    Setup Steps

    1. Clone or download the repository and open the .ino file in the Arduino IDE.
    2. Configure your Wi-Fi credentials in the sketch.
    3. Adjust the polling interval and history length to suit your needs.
    4. Upload the sketch to your device.
    5. Open the Serial Monitor to find the device's IP address.
    6. Navigate to the IP address in a web browser to view the dashboard.
    // Set Wi-Fi credentials
    const char* ssid = "YourNetwork";
    const char* password = "YourPassword";
    
    // Configuration
    const unsigned long READ_INTERVAL = 10000; // e.g. 10 seconds
    const int HISTORY_LENGTH = 360; // 1 hour at 10-second intervals
  3. Hardware setup for 1-Wire temperature sensors

    master

    To use the library, you must correctly wire your Maxim temperature integrated circuits.

    Pull-up Resistor

    • Connect a 4k7 Ω pull-up resistor between the 1-Wire data line and 5V power.
    • Note: If using ESP32 or ESP8266, you may need to adjust the resistor value accordingly.

    Sensor Wiring (DS18B20 Example)

    • Ground pins 1 and 3.
    • The center pin is the data line.
    • For detailed pull-up requirements, refer to the DS18B20 datasheet (page 7).
  4. Configure DallasTemperature via preprocessor definitions

    master

    You can reduce the library's memory footprint by defining specific macros at the top of DallasTemperature.h before including it in your sketch.

    MacroPurpose
    REQUIRESNEWMinimizes code size by assuming the sensor is already initialized.
    REQUIRESALARMSEnables alarm functionality.
    #define REQUIRESNEW      // Use if you want to minimise code size
    #define REQUIRESALARMS   // Use if you need alarm functionality
  5. Basic usage example with DallasTemperature

    master

    This example demonstrates how to initialize a single sensor on a 1-Wire bus and read its temperature in Celsius.

    1. Include OneWire.h and DallasTemperature.h.
    2. Initialize the OneWire object with your data pin.
    3. Initialize the DallasTemperature object with the OneWire instance.
    4. Call sensors.begin() in setup().
    5. Call sensors.requestTemperatures() in loop() before reading values.
    6. Use sensors.getTempCByIndex(index) to retrieve the temperature.
    #include <OneWire.h>
    #include <DallasTemperature.h>
    
    // Data wire is connected to GPIO 4
    #define ONE_WIRE_BUS 4
    
    OneWire oneWire(ONE_WIRE_BUS);
    DallasTemperature sensors(&oneWire);
    
    void setup(void) {
      Serial.begin(9600);
      sensors.begin();
    }
    
    void loop(void) { 
      sensors.requestTemperatures(); 
      
      delay(750); 
      
      float tempC = sensors.getTempCByIndex(0);
      Serial.print("Temperature: ");
      Serial.print(tempC);
      Serial.println("°C");
      delay(1000);
    }
  6. Use REST endpoints for automation integration

    master

    The ESP-WebServer example exposes several REST endpoints that allow you to integrate temperature data into automation platforms like Node-RED or Home Assistant via HTTP GET requests.

    Available Endpoints

    • /temperature: Returns current temperature readings.
    • /sensors: Returns the addresses of the connected sensors.
    • /history: Returns historical temperature data.

    Integration Examples

    • Node-RED: Use HTTP request nodes to poll /temperature or /history for automation flows.
    • Home Assistant: Use the built-in REST sensor integration to track temperature values over time.