The ESP32 microcontroller is connected to the LAN8720 PHY Ethernet module via the RMII interface. ESP32 provides the MAC layer for the PHY Ethernet via the WiFi controller. Web server implemented in Arduino Core and allows controlling the thermostat through a browser in the LAN network, where the ESP32 is available at an assigned IP address within the DHCP range. The thermostat logic is executed regardless of whether the web server web page is open to the client. Changing the logic and threshold temperatures is done through HTTP requests from clients in the network via an HTML form, or the request can be sent directly through an HTTP POST request with default arguments fname and fname2. Based on the request for a specific subpage, it is possible to control the output, for example manually / automatically, or it is possible to overwrite the control data with an argument set to a certain value. They are stored in the emulated EEPROM memory in the flash memory, while the lifetime of this sector is at the level of 10,000 rewrites. It is possible to control the reference (requested) temperature and hysteresis. The web server runs on the standard HTTP port - 80.
Thermostat can automatically control the signaling relay (with inverted logic) for switching the gas boiler ON / OFF via the GPIO output state. It can thus replace the existing room thermostat and make it available in the LAN network to all clients. Thermostat can operate on any device with a browser - computer / smartphone / tablet / Smart TV and similar. As a decision algorithm, a target temperature with hysteresis is used, which is compared with the measured temperature from the Dallas DS18B20 digital temperature sensor. The target temperature and hysteresis is read from the EEPROM memory, where it is stored permanently even in the event of a power failure and is overwritten when new data is written. The resolution of the DS18B20 sensor during measurement is 12-bit, which is explained by the temperature resolution of 0.0625 °C, which is the minimum resolution step between different measurements. Data over the OneWire bus can arrive at the microcontroller upon request in 500 to 1000 ms, depending on the number of sensors on the OneWire bus, the length of the bus, etc... The decision-making logic of the thermostat is performed every 10 seconds independently of the web application, a keep-alive connection is not required to perform the logic, the system thus works autonomously and does not require the user's attention.
On the hardware side, the project uses:ESP32 | Dallas DS18B20 |
---|---|
3V3 | Vcc |
GND | GND |
D5 | DATA |
ESP32 | Relay (OMRON G3MB-202P / SRD-05VDC-SL-C) |
---|---|
5V | Vcc |
GND | GND |
D4 | IN |
ESP32 | PHY Ethernet LAN8720 |
---|---|
3V3 | Vcc |
GND | GND / RBIAS |
D18 | MDIO |
D19 | TXD0 |
D21 | TXEN |
D22 | TXD1 |
D23 | MDC |
D25 | RXD0 |
D26 | RXD1 |
D27 | CRS_DV |
HTML webpages running on ESP32 webserver:
The web interface is designed to adapt to larger and smaller screens. It is responsive, supports high-resolution widescreens, but also mobile devices. The interface uses imported Bootstrap framework CSS styles from an external CDN server, which loads the client-side device when opening a page running on ESP32. In order for the set values of the thermostat to be preserved even after a power failure, they are stored in the ESP's EEPROM memory, which is emulated in flash memory, since the platform does not physically have an EEPROM chip (memory). Reference temperature at offset 10, hysteresis at offset 100. Each value occupies a maximum of 5B in EEPROM memory + terminating character. The data is overwritten only when the HTML form is sent, the operation of the thermostat is thus maximally gentle on the EEPROM memory for its maximum durability. The state of the output exists only in the RAM memory, where it is overwritten when changed. The value is not stored in the emulated EEPROM memory in the flash memory.
By means of the Refresh meta tag, the web server refreshes the entire page every 30 seconds, and an approximate refresh time is also written to the HTML page via Javascript. It is necessary to write down the change for the thermostat by this time, otherwise the input windows for numerical inputs to the form will be reset when the page is refreshed. Based on feedback from Android device users, the Refresh time has been extended from 10 to 30 seconds. The dynamic data that primarily changes is the current value of the output - ON / OFF , which informs the operator about the actual state of the output together with the color marking. As the logic of the system is executed independently of the web server, the output may already be in a different state than the one currently displayed in the web application before the refresh. The output change is immediately printed, for example, on the UART monitor (115200 baud/s). On the website of the thermostat, the user can also find information about the uptime of the device (how long it runs), i.e. time in days, hours, minutes and seconds.
The thermostat heats from a measured temperature of 22.49 °C and below. If the temperature reaches 23.01 °C and higher, the output is switched off, the signaling relay is disconnected and the gas boiler stops heating. The heating and cooling of the room in which the measurements are performed takes place. The thermostat is activated again only when the temperature reaches 22.49 °C or lower.
The implementation includes a program for a dynamic IPv4 address assigned to the Ethernet PHY of the thermostat. The thermostat is intended only for indoor temperatures! (above 0°C), to which the logic of the system is also adapted! The thermostat can be used to replace an existing room thermostat, or to temporarily replace a heater in an aquarium / terrarium to maintain a constant temperature.