In the previous article we gave an overview of the project of the smart controlled pool. In this second part, we will discuss the heart of the controller. This is based on an ESP32, but could be implemented with a few modifications with an ESP8266.
Task of the pool controller
The controller will take over the central control. It cyclically measures the temperatures of the pool water and the heat storage of the solar system. It will also switch the pumps (filters and heating). The ESP is therefore both sensor and actuator. The data is exchanged via WLAN via MQTT.
Measuring temperature is usually the beginning of programming IoT sensors. There are various project examples. The temperature sensors often come DHT11 or the more accurate DHT22 for use. However, since the temperature sensors in the project must be placed externally, we use the waterproof sensor DS18B20. One of the two sensors for measuring the temperature of the pool water, the other for measuring the temperature in the buffer tank.
The measured temperatures are read out every 60 seconds by means of a timer and published as a message via MQTT. The interval is a trade-off between the inertia of water warming and the debugging of the program. Presumably values would suffice every 5 minutes, but the integration into OpenHAB also makes the minute's clock much easier later. More on that later.
Control wireless sockets
Since we do not want to handle 230 volt alternating voltage directly in the project, we switch the pumps via radio sockets. In the case of radio sockets, there are some that can be used via ESPs with a 433 MHz radio transmitter can control. A list of compatible radio sockets can be found in the Wiki of the library used.
Structure of the circuit
The circuit on the plug-in board looks like this:
In the case of temperature sensors, there are also pull-up resistors with 4.7k" between the data line and the power supply +3V VCC.
The data pins on the ESP32 are documented as follows:
#define PIN_DS_SOLAR 16 Temp Sensor Solar
#define PIN_DS_POOL 17 Temp Sensor Pool
#define PIN_RSSWITCH 18 for 433MHz transmitter
Source code of the project
The complete source code of the project can be found in the author's GitHub repository: https://github.com/stritti/smart-swimming-pool.
In this project, it is important to us to use proven software components and to solve as much as possible via existing libraries. For this reason, the following Libs are used:
- rc-switch: Control of the radio sockets
- OneWire: Support for I2C sensors
- DallasTemperature: Reading the temperature of the sensors
- PubSubClient: Receive and send MQTT messages
- RemoteDebug: Debugging via Telnet
- ESPBASE: Template for IoT Projects
These libraries are stored in the configuration of Platform.io (platformio.ini) and are automatically loaded and integrated from the Internet when compiled.
The program for the pool controller is based on ESPBASE, which provides some setup functions as written in the previous article. At the beginning of the code, some variables are created and the defines for the pin assignment are defined.
In addition to the two functions setup() and loop(), some other functions are implemented. Essentially, this involves receiving and sending MQTT messages and implementing them. In addition, a timer is implemented that reads out the temperature sensors cyclically and publishes them via MQTT messages.
- In the Loopfunction, the Wi-Fi connection is checked again and again and, if necessary, restored. This was a treacherous problem: the ESP repeatedly lost the Wi-Fi connection and without the Reconnect it could no longer send data.
- The function publish is used to send the MQTT messages. Thus, the data from a restart and the temperature measurement are published as JSON messages.
- The timer for sending the temperatures is split. The reason is that the actual timer methods must be as short as possible. In the implementation, the timer calls the onTimer function, which only interruptCounter Increments. In the loop function, this is checked and the actual onTemperaturefunction triggered. In this method, the internal temperature of the ESP is also read out and published.
- The function onMQTTCallback is called when a message is sent to a topic that starts with '/pool/switch/'. The message contains the information of the radio socket whether it should be switched on or off. The topic is further divided into the group and the device encoding. This means that the coding of the DIP switches of the socket transformed into 0 and 1. As a result, this solution can be flexibly used elsewhere.
The source code of the pool controller can be found directly in the following file: https://github.com/stritti/smart-swimming-pool/blob/master/Pool-Controller/src/pool-control.ino
Once the circuit is set up and the project is loaded onto the ESP, we can already test. The ESP will open a Wi-Fi network as an access point because no Wi-Fi is set up by default. If you connect to the pool controller with your smartphone, you can set up the right Wi-Fi. After that, the microcontroller automatically restarts and tries to connect to the specified Wi-Fi.
If everything is set up correctly, a status message should be published via MQTT at the start of the ESP. You can track all messages with the following command on the Raspberry:
mosquitto_sub -h localhost -v -t /#
The IP address of the MQTT server may need to be adjusted in the source code.
After that, the temperatures should be output on the Raspberry in the minute cycle.
In the other direction we can switch the radio sockets by sending a suitable message. The topic is "/pool/switch/<group>/<device>". Group and Device are the dip switches of the socket as 0 and 1. "ON" or "OFF" is sent as a message.
What happens next?
This lays the foundations: we can read out the temperatures and we can switch sockets.
In the following article we connect the pool controller with the smart home solution openHAB and make the pool really smart by rules.