diff --git a/DallasTemperature.h b/DallasTemperature.h index e6399d6..db0f9ce 100644 --- a/DallasTemperature.h +++ b/DallasTemperature.h @@ -1,7 +1,7 @@ #ifndef DallasTemperature_h #define DallasTemperature_h -#define DALLASTEMPLIBVERSION "4.0.2" +#define DALLASTEMPLIBVERSION "4.0.3" // Configuration #ifndef REQUIRESNEW diff --git a/examples/ESP-WebServer/ESP-WebServer.ino b/examples/ESP-WebServer/ESP-WebServer.ino new file mode 100644 index 0000000..dd652ca --- /dev/null +++ b/examples/ESP-WebServer/ESP-WebServer.ino @@ -0,0 +1,375 @@ +#include +#include +#include +#include + +/* + SETUP INSTRUCTIONS + + 1) Change WiFi SSID and Password: + const char* ssid = "YourSSID"; + const char* password = "YourPassword"; + + 2) Polling Interval (milliseconds): + const unsigned long READ_INTERVAL = 10000; // 10 seconds + + 3) Number of Readings (History Length): + const int HISTORY_LENGTH = 360; // 1 hour at 10-second intervals +*/ + +const char* ssid = "YourSSID"; +const char* password = "YourPassword"; + +const int oneWireBus = 4; +const int MAX_SENSORS = 8; +const int HISTORY_LENGTH = 360; +const unsigned long READ_INTERVAL = 10000; + +DeviceAddress sensorAddresses[MAX_SENSORS]; +float tempHistory[MAX_SENSORS][HISTORY_LENGTH]; +int historyIndex = 0; +int numberOfDevices = 0; +unsigned long lastReadTime = 0; + +OneWire oneWire(oneWireBus); +DallasTemperature sensors(&oneWire); +ESP8266WebServer server(80); + +String getAddressString(DeviceAddress deviceAddress); +void handleRoot(); +void handleSensorList(); +void handleTemperature(); +void handleHistory(); +void updateHistory(); + +const char MAIN_page[] PROGMEM = R"=====( + + + + + + Arduino Temperature Control Library - Sensor Data Graph + + + + +
+

+ Arduino Temperature Control Library - Sensor Data +

+ +
+
+ Dashboard +
+
+ API Docs +
+
+ Setup +
+
+ +
+ +
+
Loading sensor data...
+
+
+ + + + +
+ +
+

© 2025 Miles Burton. All Rights Reserved.

+

+ Licensed under the + + MIT License + . +

+
+ + + + +)====="; + +void setup() { + Serial.begin(115200); + sensors.begin(); + + for (int i = 0; i < MAX_SENSORS; i++) { + for (int j = 0; j < HISTORY_LENGTH; j++) { + tempHistory[i][j] = 0; + } + } + + numberOfDevices = sensors.getDeviceCount(); + if (numberOfDevices > MAX_SENSORS) { + numberOfDevices = MAX_SENSORS; + } + + for (int i = 0; i < numberOfDevices; i++) { + sensors.getAddress(sensorAddresses[i], i); + } + + sensors.setResolution(12); + + WiFi.begin(ssid, password); + while (WiFi.status() != WL_CONNECTED) { + delay(500); + } + + server.on("/", HTTP_GET, handleRoot); + server.on("/temperature", HTTP_GET, handleTemperature); + server.on("/sensors", HTTP_GET, handleSensorList); + server.on("/history", HTTP_GET, handleHistory); + + server.begin(); +} + +void loop() { + server.handleClient(); + unsigned long t = millis(); + if (t - lastReadTime >= READ_INTERVAL) { + updateHistory(); + lastReadTime = t; + } +} + +void updateHistory() { + sensors.requestTemperatures(); + for (int i = 0; i < numberOfDevices; i++) { + float tempC = sensors.getTempC(sensorAddresses[i]); + tempHistory[i][historyIndex] = tempC; + } + historyIndex = (historyIndex + 1) % HISTORY_LENGTH; +} + +void handleRoot() { + server.send(200, "text/html", MAIN_page); +} + +void handleSensorList() { + String json = "{\"sensors\":["; + for (int i = 0; i < numberOfDevices; i++) { + if (i > 0) json += ","; + json += "{\"id\":" + String(i) + ",\"address\":\"" + getAddressString(sensorAddresses[i]) + "\"}"; + } + json += "]}"; + server.send(200, "application/json", json); +} + +void handleTemperature() { + sensors.requestTemperatures(); + String json = "{\"sensors\":["; + for (int i = 0; i < numberOfDevices; i++) { + if (i > 0) json += ","; + float c = sensors.getTempC(sensorAddresses[i]); + float f = sensors.toFahrenheit(c); + json += "{\"id\":" + String(i) + ",\"address\":\"" + getAddressString(sensorAddresses[i]) + "\","; + json += "\"celsius\":" + String(c) + ",\"fahrenheit\":" + String(f) + "}"; + } + json += "]}"; + server.send(200, "application/json", json); +} + +void handleHistory() { + String json = "{\"interval_ms\":" + String(READ_INTERVAL) + ",\"sensors\":["; + for (int i = 0; i < numberOfDevices; i++) { + if (i > 0) json += ","; + json += "{\"id\":" + String(i) + ",\"address\":\"" + getAddressString(sensorAddresses[i]) + "\",\"history\":["; + for (int j = 0; j < HISTORY_LENGTH; j++) { + int idx = (historyIndex - j + HISTORY_LENGTH) % HISTORY_LENGTH; + if (j > 0) json += ","; + json += String(tempHistory[i][idx]); + } + json += "]}"; + } + json += "]}"; + server.send(200, "application/json", json); +} + +String getAddressString(DeviceAddress deviceAddress) { + String addr; + for (uint8_t i = 0; i < 8; i++) { + if (deviceAddress[i] < 16) addr += "0"; + addr += String(deviceAddress[i], HEX); + } + return addr; +} diff --git a/examples/ESP-WebServer/README.md b/examples/ESP-WebServer/README.md new file mode 100644 index 0000000..e6d959a --- /dev/null +++ b/examples/ESP-WebServer/README.md @@ -0,0 +1,61 @@ +# 🌡️ Arduino Sketch: DS18B20 Sensor via Wi-Fi (REST Endpoints & Dashboard) + +This **example Sketch** demonstrates how to use the [Arduino Temperature Control Library](https://github.com/milesburton/Arduino-Temperature-Control-Library) on an **ESP8266** or **ESP32** to read temperature data from **Maxim (Dallas) DS18B20** sensors. The Sketch publishes the readings via Wi-Fi in two ways: + +1. **REST Endpoints** - Ideal for Node-RED, Home Assistant, or other automation platforms. +2. **A Human-Friendly Dashboard** - A simple web interface, powered by [Chart.js](https://www.chartjs.org/) and [Tailwind CSS](https://tailwindcss.com/), displaying current and historical temperatures. + +--- + +## 🔎 Features +- Reads from one or more **DS18B20** temperature sensors +- Configurable **polling interval** (in milliseconds) and **history length** (number of readings) +- **Lightweight dashboard** that visualizes the last N readings +- **REST endpoints** for easy integration: + - `/temperature` - current readings + - `/sensors` - sensor addresses + - `/history` - historical data + +--- + +## 📚 Potential Use Cases +- **Node-RED** automation flows: Perform regular HTTP GET requests against `/temperature` or `/history` +- **Home Assistant** integrations: Use built-in REST sensors to track temperature over time +- **Extensible to other sensor types** (humidity, light, pressure, etc.) by following the same approach + +--- + +## 🛠️ Getting Started +1. **Clone or download** this repository +2. **Open the Sketch** (the `.ino` file) in the Arduino IDE (or other environment) +3. **Install dependencies**: + - [Arduino Temperature Control Library](https://github.com/milesburton/Arduino-Temperature-Control-Library) + - ESP8266 or ESP32 core for Arduino +4. **Set your Wi-Fi credentials** in the code: +```cpp +const char* ssid = "YourNetwork"; +const char* password = "YourPassword"; +``` +5. **Adjust** the interval and history: +```cpp +// Configuration +const unsigned long READ_INTERVAL = 10000; // e.g. 10 seconds +const int HISTORY_LENGTH = 360; // 1 hour at 10-second intervals +``` +6. **Connect** the DS18B20 sensor(s) to the ESP, using OneWire with a pull-up resistor +7. **Upload** the Sketch to your device +8. **Check** the serial monitor for the IP +9. **Navigate** to that IP in your browser to see the chart-based interface + +--- + +## ❓ Questions & Support +- **Library Matters**: If you have issues with the **Arduino Temperature Control Library** itself, please open a ticket in the [official repository](https://github.com/milesburton/Arduino-Temperature-Control-Library/issues) +- **This Sketch**: For help customizing this example, dealing with Wi-Fi issues, or setting up the chart dashboard, and other device-specific tweaks, please visit the [Arduino Forum](https://forum.arduino.cc/). You will find many friendly developers there ready to help. + +--- + +## 📜 License +This project is distributed under the [MIT License](https://opensource.org/licenses/MIT). + +> We hope you find this Sketch useful for monitoring temperatures - both in a machine-readable (REST) and human-friendly (web dashboard) format. Happy hacking! 🚀 \ No newline at end of file diff --git a/library.json b/library.json index 68e80d5..af6e85c 100644 --- a/library.json +++ b/library.json @@ -32,7 +32,7 @@ { "paulstoffregen/OneWire": "^2.3.5" }, - "version": "4.0.2", + "version": "4.0.3", "frameworks": "arduino", "platforms": "*" } diff --git a/library.properties b/library.properties index b10c922..c94891c 100644 --- a/library.properties +++ b/library.properties @@ -1,5 +1,5 @@ name=DallasTemperature -version=4.0.2 +version=4.0.3 author=Miles Burton , Tim Newsome , Guil Barros , Rob Tillaart maintainer=Miles Burton sentence=Arduino library for Dallas/Maxim temperature ICs