Skip to content
TheChatArchiveThe Chat Archive
gpt-4o-2024-08-06· OpenAIAi2 WildChat-4.8M (AI2)

#include <ESP8266WiFi.h> #include <ESP8266WebServer.h> #include <OneWire.h> #include…

  1. User

    #include <ESP8266WiFi.h> #include <ESP8266WebServer.h> #include <OneWire.h> #include <DallasTemperature.h> #include <EEPROM.h>

    #define ONE_WIRE_BUS 2 // GPIO для DS18B20 OneWire oneWire(ONE_WIRE_BUS); DallasTemperature sensors(&oneWire); ESP8266WebServer server(80);

    float setPoint = 20.0; // Устанавливаемая температура float hysteresis = 1.0; // Гистерезис int relayPin = 5; // GPIO для реле unsigned long lastGraphUpdate = 0; // Время последнего обновления графиков

    // Хранение исторических данных const int maxHistorySize = 60; // Максимальное количество записей float temperatureHistory[maxHistorySize]; int relayStateHistory[maxHistorySize]; int historyIndex = 0;

    // Параметры потребления энергии const float powerConsumptionRate = 1.5; // Потребление в кВт const float costPerKWh = 4.6; // Стоимость за кВтч float totalEnergyConsumed = 0.0; // Общее потребление энергии в кВтч

    // Объявление функций void handleRoot(); void handleSet(); void handleData(); void updateGraphs();

    void setup() { Serial.begin(115200); EEPROM.begin(512);

    // Чтение настроек из EEPROM
    EEPROM.get(0, setPoint);
    EEPROM.get(4, hysteresis);
    
    pinMode(relayPin, OUTPUT);
    digitalWrite(relayPin, LOW); // Реле выключено
    
    // Создание точки доступа
    WiFi.softAP("ThermoRegulator", "password");
    
    // Установка статического IP-адреса
    IPAddress local_IP(192, 168, 4, 1); 
    IPAddress gateway(192, 168, 4, 1); 
    WiFi.softAPConfig(local_IP, gateway, IPAddress(255, 255, 255, 0));
    
    server.on("/", handleRoot);
    server.on("/set", handleSet);
    server.on("/data", handleData); // Обработчик для получения данных
    server.begin();
    

    }

    void loop() { server.handleClient();

    sensors.requestTemperatures();
    float currentTemp = sensors.getTempCByIndex(0);
    
    if (currentTemp < (setPoint - hysteresis)) {
        digitalWrite(relayPin, HIGH); // Включить реле
        totalEnergyConsumed += powerConsumptionRate / (3600 / 5); // Добавляем потребление каждые 5 секунд
    } else if (currentTemp > (setPoint + hysteresis)) {
        digitalWrite(relayPin, LOW); // Выключить реле
    }
    
    // Сохранение исторических данных
    temperatureHistory[historyIndex] = currentTemp;
    relayStateHistory[historyIndex] = digitalRead(relayPin);
    historyIndex = (historyIndex + 1) % maxHistorySize; // Циклическое обновление индекса
    
    unsigned long currentMillis = millis();
    
    // Обновление графиков каждые 5 секунд
    if (currentMillis - lastGraphUpdate >= 5000) {
        lastGraphUpdate = currentMillis;
        updateGraphs(); // Функция для обновления графиков
    }
    

    }

    void updateGraphs() { // Здесь добавьте код для обновления графиков на клиенте через AJAX или WebSocket }

    void handleRoot() { String html = ""; html += ""; // Установка кодировки UTF-8 для поддержки кириллицы html += "<script src="https://cdn.jsdelivr.net/npm/chart.js\">"; html += ""; html += "body { text-align: center; font-size: 24px; }"; // Увеличенный размер шрифта для всего тела html += "h1 { font-size: 3em; }"; // Увеличенный заголовок html += "h2 { font-size: 2em; }"; // Увеличенные подзаголовки html += "input[type=range], input[type=submit] { width: 80%; height: 40px; font-size: 20px; }"; // Увеличенные элементы управления html += "span { display: inline-block; min-width: 60px; font-size: 20px; }"; // Увеличенные размеры для отображения значений html += "canvas { width: 90%; height: 300px; margin: 20px auto; }"; // Уменьшенная высота графиков html += "";

    html += "</head><body><h1>Терморегулятор</h1>";
    
    html += "<form action=\"/set\" method=\"GET\">";
    
    html += "Установленная температура: <input type=\"range\" min=\"10\" max=\"70\" step=\"0.1\" name=\"temp\" value=\"" + String(setPoint) + "\" oninput=\"updateValue(this.value, 'tempValue')\">";
    html += "<span id='tempValue'>" + String(setPoint) + "</span>°C<br>";
    
    html += "Гистерезис: <input type=\"range\" min=\"0.1\" max=\"10\" step=\"0.1\" name=\"hyst\" value=\"" + String(hysteresis) + "\" oninput=\"updateValue(this.value, 'hystValue')\">";
    html += "<span id='hystValue'>" + String(hysteresis) + "</span>°C<br>";
    
    html += "<input type=\"submit\" value=\"Сохранить\">";
    
    // Отображение текущей температуры и состояния реле 
    float currentTemp = sensors.getTempCByIndex(0);
    String relayState = (digitalRead(relayPin) == HIGH) ? "Включено" : "Выключено";
    
    html += "<h2>Текущая температура: <span id='currentTemp'>" + String(currentTemp) + " °C</span></h2>";
    html += "<h2>Состояние реле: <span id='relayState'>" + relayState + "</span></h2>";
    
    // График состояния реле 
    html += "<canvas id='relayChart'></canvas>";
    
    // График потребления энергии 
    html += "<canvas id='energyChart'></canvas>";
    
    html += "<script>";
    
    // AJAX-запросы для обновления данных без перезагрузки страницы 
    html += "function fetchData() { ";
    html += "fetch('/data') ";
    html += ".then(response => response.json()) ";
    html += ".then(data => { ";
    html += "document.getElementById('currentTemp').innerText = data.currentTemp.toFixed(1) + ' °C'; ";
    html += "document.getElementById('relayState').innerText = data.relayState ? 'Включено' : 'Выключено'; ";
    html += "}); ";
    html += "} ";
    
    // Обновление данных каждую секунду 
    html += "setInterval(fetchData, 1000);";
    
    // Инициализация графиков 
    html += "var relayCtx = document.getElementById('relayChart').getContext('2d');";
    html += "var relayChart = new Chart(relayCtx, {";
    html += "type: 'line',";
    html += "data: { labels: [], datasets: [{ label: 'Состояние реле', data: [], borderColor: 'blue', fill: false }] },";
    html += "options: { scales: { yAxes: [{ ticks: { beginAtZero:true } }] } }";
    html += "});";
    
    html += "var energyCtx = document.getElementById('energyChart').getContext('2d');";
    html += "var energyChart = new Chart(energyCtx, {";
    html += "type: 'line',";
    html += "data: { labels: [], datasets: [{ label: 'Потребление энергии (кВтч)', data: [], borderColor: 'orange', fill: false }] },";
    html += "options: { scales: { yAxes: [{ ticks: { beginAtZero:true } }] } }";
    html += "});";
    
    // Добавьте данные в графики (пример)
    html += "function updateGraphs() {";
    html += "var now = new Date().toLocaleTimeString();";
    
    // Обновление графика состояния реле 
    html += "relayChart.data.labels.push(now);";
    html += "relayChart.data.datasets[0].data.push(" + String(digitalRead(relayPin)) + ");"; 
    html += "if (relayChart.data.labels.length > "+String(maxHistorySize)+") { relayChart.data.labels.shift(); relayChart.data.datasets[0].data.shift(); }";
    html += "relayChart.update();";
    
    // Потребление энергии (пример)
    if (digitalRead(relayPin) == HIGH) {
        totalEnergyConsumed += powerConsumptionRate / (3600 / 5); // Добавляем потребление каждые 5 секунд
        
        float costInRubles = totalEnergyConsumed * costPerKWh;
        
        energyChart.data.labels.push(now); 
        energyChart.data.datasets[0].data.push(totalEnergyConsumed); 
        if (energyChart.data.labels.length > "+String(maxHistorySize)+") { energyChart.data.labels.shift(); energyChart.data.datasets[0].data.shift(); } 
        energyChart.update(); 
        
        html+= "document.getElementById('costDisplay').innerText = 'Стоимость потребления: ' + costInRubles.toFixed(2) + ' руб.';";
    }
    
    html+= "}";
    
    // Обновление графиков каждые 5 секунд 
    html+= "setInterval(updateGraphs, 5000);";
    
    // Отображение стоимости потребления в рублях 
    html+= "<h2 id='costDisplay'>Стоимость потребления: 0.00 руб.</h2>";
    
    // Закрывающий тег скрипта и HTML документа 
    html+= "</script></body></html>";
    
    server.send(200, "text/html", html);
    

    }

    void handleSet() { if (server.hasArg("temp")) { setPoint = server.arg("temp").toFloat(); EEPROM.put(0, setPoint); // Сохранение температуры в EEPROM EEPROM.commit(); }

    if (server.hasArg("hyst")) {
    	hysteresis = server.arg("hyst").toFloat();
    	EEPROM.put(4, hysteresis); // Сохранение гистерезиса в EEPROM
    	EEPROM.commit();
    }
    
    server.sendHeader("Location", "/");
    server.send(302, "text/plain", "");
    

    }

    void handleData() { float currentTemp = sensors.getTempCByIndex(0); int relayState = digitalRead(relayPin);

    String jsonResponse = "{\"currentTemp\": " + String(currentTemp) + ", \"relayState\": " + String(relayState) + "}";
    
    server.send(200, "application/json", jsonResponse);
    

    }

  2. Assistant

    Your code sets up a basic thermostat system that uses an ESP8266 microcontroller to control and monitor a DS18B20 temperature sensor and a relay. The setup includes a web server that allows users to view and update settings via a web interface. Here's a brief breakdown of the main components and functionality:

    1. Library Inclusions:

      • Libraries for the ESP8266 WiFi and Web Server functionality.
      • OneWire for communication with the DS18B20 temperature sensor.
      • DallasTemperature for handling readings from the DS18B20.
      • EEPROM for storing persistent settings (like the set point and hysteresis values).
    2. Hardware Setup:

      • A DS18B20 temperature sensor connected via a OneWire bus.
      • A relay connected to control an external device based on temperature readings.
    3. Web Server:

      • The server hosts a web interface allowing the user to:
        • View the current temperature.
        • Adjust the desired temperature (set point) and hysteresis.
        • Observe charts that display temperature history, relay state, and energy consumption.
    4. Temperature Control Logic:

      • If the current temperature drops below (setPoint - hysteresis), the relay is turned on to presumably activate a heater.
      • If the temperature exceeds (setPoint + hysteresis), the relay is turned off.
    5. Graph Management:

      • Temperature and relay state data are continually logged, allowing graph updates every 5 seconds.
      • Graphs are displayed on the web interface using Chart.js library for visualizing the state of the relay and energy consumption.
    6. Energy Consumption:

      • Estimates energy consumption based on the relay state and predefined power consumption rates.
      • Displays the estimated energy cost.
    7. Persisting Settings:

      • Uses EEPROM to save and recall the set point and hysteresis values whenever the device is reset.

    Recommendations for Improvement:

    • Security: Your WiFi configuration uses a hardcoded SSID and password. Consider implementing a mechanism to change these values safely, like using a configuration portal.

    • Input Validation: Add validation for the user input to avoid invalid values for temperature and hysteresis, which could lead to unwanted behavior.

    • UI Enhancements: Improve the web UI to be more user-friendly, possibly adding more information or interaction capabilities.

    • Sensor Error Handling: Add checks to ensure the temperature sensor provides valid readings before using the values in calculations and control logic.

    • Energy Calculation Frequency: Currently assumes the relay state checking aligns perfectly with the energy calculation frequency. Consider decoupling these functionalities for accuracy under different conditions.

    • Power and Energy Model: Clarify what the power consumption model is and update calculation mechanisms if necessary to reflect real-world usage accurately.

    These recommendations can improve the robustness and user experience of your thermostat system.