Solar Hot Water Battery - Ver 2


This system replaces the previous version that used IotWatt as the power measuring device. This version, for 3 phase systems, uses two JSY power monitors, JSY-MK-193 and JSY-MK-345. A single phase version of the code is available that just uses one JSY-MK-193 module.
This project aims to store the excess solar production in the hot water and so save $0.169 / kWh, which is the difference between the Off-Peak hot water (Controlled Load) price, $0.2189 / kWh, and the rebate for exporting solar generation $0.05 / kWh. To do this the hot water heating element needs to be rewired from the Off Peak meter to the normal Net Meter which handles the solar and the house electricity. Also a triac needs to be added in series with the hot water element and controlled to only allow heating when there is excess solar cell production.
An ESP32 board controls a triac to supply power to the hot water tank when there is excess Solar generation. The SP32 reads the power monitors, via a non-blocking Modbus library, to obtain the current Import/Export kW, the accumulated electricity and the kWhrs that have been supplied to the hot water tank.
To allow for cloudy, no sun, days there are two fall back options:-
1) a “1hr Boost” button on the control module which will supply 1hr of electricity to the hot water element regardless of solar cell production and
2) a manual switch in the switch board that will restore the system to its original Off Peak setup.
The “1hr Boost” button will heat the hot water immediately but at a cost of $0.3674 / kWh, whereas switching the manual rotary switch back to Off Peak will delay heating the water until the next off time the Controlled Load meter is turned on by the electricity distributor, typically overnight, but at a reduced cost of $0.21.89 / kWh.
A separate ESP32 module connects to the control module via TCP to provide a real time display of the electricity usage, daily (and previous day's) hot water kWhrs and cost. It also allows the “1hr Boost” to be turned on and off remotely.
This project is also available on-line at HotWater from Excess Solar V2
Table of Contents
Parts List
Operation
Handling Failures
Hot Water Monitor Plots and Data
Setting Up the Local WiFi Connections
Construction
Software Programming
Telnet Data Format
Code Descriptions
Conclusion
Supplies
Parts List (prices exclude shipping and US tariffs)
Control Module (Single Phase)
1 x RainbowLink 4 Channel USB to Serial Converter (RS485 / RS232 / TTL) SKU: TEL0185 ~US$12.90 (for configuration and testing)
1 x DFRobot DC-DC Buck-Mode Power Module (8~28V to 5V) SKU: DFR0571 ~US$2.90
1 x 40A Solid State Switch (240 AC triac DC control) and 10-400A heat sink (see text below) ~ e.g. CG Instrument Store ZGT-40 DA on Aliexpress ~US$16.00
1 x Junction Box (clear lid) 115 x 90 x 55mm – e.g DGWLG store on Aliexpress ~US$10.00 – to mount Triac in.
1 x XIAO – ESP32S3 board ~ US$5.00 OR Beetle ESP32 V2 SKU: DFR0575 ~US9.90
1 x Red Led – e.g. Jaycar ZD1790 ~US$1.50 or Sparkfun COM-00528 ~ US$1.05
1 x 220 Ohm resistor – eg Sparkfun COM-17994 ~ US$0.30
2 x 1N5819 40V 1A Schottky Diode e.g. Jaycar ZR1020 ~US$0.50
1x 12V 1A power supply – e.g. from Shop1103809314 on Aliexpress ~US$3.00
1x 20A 1-0-2 rotary cam change over switch – e.g.Yaming electric Store on Aliexpress (see switch diagram below) ~ US$7.50
1 x momentary push button e.g. Jaycar SP0657 ~US$3.10
1 x PCB Spring Terminal Block 2way e.g. Jaycar HM3140 ~US$0.80
2 x Weidmuller PCB Mount Screw Terminals – 5.08mm 2 way e.g. Jaycar HM3130 ~US$0.85
1 x Weidmuller PCB Mount Screw Terminals – 5.08mm 3 way e.g. Jaycar HM3132 ~US$1.10
1 x Vero board e.g. Jaycar HP9552 ~US$4.70
1 x YX-G485 RS485 to RS232 TTL converter (isolated) e.g EC-Buying Ali on Aliexpress ~US$7.40
1 x JSY-MK-193 50A version with Open CT RS485 ~US$35.70
1 x Junction Box (clear lid) 125 x 175 x 75mm – e.g AG-1217 from Sheng Tengyu Electric Store on Aliexpress ~US$12.70 – to mount JSY-MK-193 and circuit in
For 3 Phase Power Add the following
1 x JSY-MK-354 50A version with Open CT RS485 ~US$27.50
1 x Junction Box (clear lid) 125 x 175 x 75mm – e.g AG-1217 from Sheng Tengyu Electric Store on Aliexpress ~US$12.70 – to mount JSY-MK-354 in
Monitor Module
1 x Fire_Beetle ESP32 E N16R2 (16Meg flash) SKU: DFR1139 ~US$12.90
1 x Fermion: DS3231 Precise RTC SKU: DFR0819 ~US$8.90 – Note: This is uses the slightly more expensive, but more accurate DS3231 module
1 x Enclosure – e.g. Medium Room Sensor Enclosure ~US$4.20
1 x 5V@3A USB Power Supply SKU: FIT0640 (US pins) ~US$7.50 OR SKU: FIT0639 (EU pins) ~US7.50 OR Raspberry Pi 4 Power Supply (Australian Plug) ~US$10.50
1 x pfodApp Android App ~ US$12.5
Software
Arduino IDE (V1.8.19+)
ESP32 Board support V3.0.7 modified as described below
Solar Hot Water Monitor (SolarHotWaterMonitor_V2.zip)
ESP32S3_modbus (ESP32S3_modbus.zip)
Libraries (libraries.zip)
Modbus Tester (local copy - Windows)
esp32fs.jar from https://github.com/me-no-dev/arduino-esp32fs-plugin
Operation



There are three (3) modes of operation to heat the hot water. The manual rotary switch on the switch board allows the choice between Solar and the the original Off Peak (Ctrl Load) setting. On the Solar setting the app allows the choice between Solar or 1hr Boost.
With the manual switch in the Off Peak setting a separate meter supplies power to the water tank at times determined by the electricity supplier. Electricity supplied via this meter is at the concessional rate of 21.9c/kWh. In this setting any excess solar is exported for 5c/kWh refund. In the Off Peak setting the triac is bypassed to provide electricity to the hot water tank when ever the electricity supplier switches the Controlled Load meter on.
If the manual switch is in the Off position the triac and hot water element are isoloated from both the Mains and the Off Peak supplies. If the manual rotary switch on the switch board is in the Off or Off Peak Position, the pfodApp will display “Hot Water Off Peak or Off”.
In the Solar setting, the triac is kept off until there is more than 4kW of excess solar being exported. At that point the triac is turned on and if the hot water tank is not completely up to temperature then its thermostat supplies power the element. This power is registered by a separate CT and the kWs being used by the hot water tank is displayed as an Orange pie segment on the app's Export/Import pie chart and accumulated as the rising red line in the bottom chart on the webpage.
The Export value is the total available excess Solar for use by other home appliances. If say a electric kettle and a toaster is turned on then the 3.5kW hotwater + 2kW kettle = 5.5kW > 4.5kW, would exceed the available excess Solar and the system would start to import power. The software detects that from the JSY total real power reading and turns off the hotwater to allow the kettle to run on the excess Solar. So in Solar mode any house appliances take precedence over supplying power to the hot water heater.
The remaining segment of the pie chart when Exporting indicates if there is sufficient excess solar to turn on major appliance. That is it is Green if there is 2.5kW or move available an Yellow otherwise.
Pressing the 1hr Boost button switches the triac on for 1hr regardless of the excess solar available. This means that if the hotwater tank is not completely hot the element will consume 3.5kW either from the excess solar or importing from the mains.
Again if the hot water tank is drawing power there is Orange pie segment indicated on the app. However when boosting Hot Water, the Export value only shows the available excess solar after subtracting the hot water usage. This is because if you now turn on a kettle, the hot water does not turn off and you will be importing power to run the kettle.
The displayed “Cost today” is obtained from the integrating the JSY real power to get the accumulated kWh for the day and is multiplied by cost / kWh. The Cost today is positive, and shown in Red, if you are paying for the electricity. The Cost today is negative, and shown in Green, if you are getting a refund because the value of the Exports exceeded the cost of the Imports (and Off Peak if any).
How to tell if the hot water tank is completely heated
With the manual switch in the Solar position, than with the app will display “Hot Water was Fully Heated” if power was applied by the hot water thermostat was off. This is determined by checking if the triac is turned on but the Solar hot water CT is not measuring any current flowing through the hot water element. That is the hot water tank thermostat has switched off. In any case the accumulated hot water kWh for today is shown . If the end of the day the hot water is not showing as was Fully Heated, then the displayed kWhrs can be used to indicate how close to fully heated the hot water is. For this household typically hot water usage is between 5kWhrs and 9kWhrs each day. A whole tank of cold water takes about 15kWhrs to heat. Yesterday's values are also shown so that any overnight OffPeak kWhrs are accounted for. Yesterday's cost includes any overnight (up to midnight) consumption from heaters etc. The web page charts can also be used to see how much hot water was heated over the preceding 7 days.
When the System does not Work
This system depends on there being enough excess Solar for long enough to reheat the hot water that the family has used, i.e. 5kWhr to 9kWhrs. If there are one or more rainy days, you can use the displayed kWh to see if you need should manually switch to OffPeak. 5kWhrs or more is sufficient for 2 days OffPeak are needed. If the hot water runs cold you can use the 1hr Boost to heat about a ¼ of a tank. Choosing 1hr Boost will heat the water immediately and show “Hot Water was Fully Heated” if the tank is completely heated, but at a cost of 36.7c/kWhr. Manually switching to OffPeak over night will delay heating, but at the cheaper rate of 21.9c/kWhr.
The Yesterday's hot water kWhrs together with todays hot water kWhr help you decide in the evening if you need to switch to OffPeak overnight to reheat the hot water.
Handling Failures


This system is more reliable than the IotWatt system it replaces.
The Hot Water Controller Runs if the WiFi fails
This Version 2 control module runs completely locally. It does not need a WiFi connection to control the hot water. In the previous IotWatt system the triac control was connected to the IoTWatt power monitor via WiFi. Since both units are mounted inside a metal meter box, a stable WiFi connection can be problematic. A WiFi connection is only needed for remote monitoring and data collection. A low voltage serial connection is provided to allow WiFi bridge to be mounted outside the meter box if necessary.
The Controller and Monitor Runs Off-Line – No internet connection needed.
At the monitor / data collecting / plotting module, only a local WiFi connection is needed. No connection to the internet is necessary so the system can be run completely off-line. The previous IotWatt monitor required an Internet connection to supply the javascript (.js) packages used for its web display and connection to an NTP server to keep time. Version 2 hosts all necessary javascript plotting packages locally in the ESP32's file system and uses an RTC (Real Time Clock) module to keep time. Locally served web pages allow you to set the RTC time as well as the local time zone. If an internet connection is available, you can optionally enable an NTP client to keep synchronize the RTC.
Manual 1hr Boost Button
A push button on the case provides manual control to turn on the “1hr Boost” to immediately heat the hot water regardless of the solar generation. The boost can be cancelled by turning the three position switch from Solar to Off and back again. The manual boost can also be turned on/off via commands on the telnet or serial connection. See Telnet Data Format
Manual fall back to Off Peak
If the a JSY power monitor or the ESP32 fails, then the system can be manually switched back to Off Peak. If the JSY power monitor returns an error the Red Led start a fast flash. If the failure persists for 5 measurement cycles, the ESP32 reboots and tries again.
Hot Water Controller Error Indications
As noted above, the system is designed to work independently of the local WiFi network.
If the ESP32 cannot connect to the local WiFi network, then the Red Led will flash slowly and the ESP32 will continue to run, reading the power and controlling the triac.
The Red Led is turned on, solidly, whenever the triac is ON. If the manual “1hr Boost” has been used to turn the triac on, it can be cancelled, early, by turning the three position switch from Solar to Off and back again.
If the Red Led in on solidly (i.e. triac On), you can check the WiFi connection by turning the three position switch from Solar to Off. If there is no WiFi connection the Red Led will flash slowly, if there is a connection the Red Led will go off.
Led Off – Normal control with Hot Water Triac OFF
Led On – Normal control with Hot Water Triac ON
Led Slow Flash – No WiFi connection. (Triac ON overrides this indication)
Led Fast Flash – Hot Water control not running ==> Either a Modbus error OR WiFi setup if push button pressed while powering up.
Hot Water Monitor Plots and Data








The Hot Water Monitor connects via telnet to the Hot Water Controller and receives and stores the power data every 2 seconds. The last 7 days data, at 2 sec intervals, is saved in the Firebeetle 2 ESP32-E's flash memory. The data is first buffered in approximately 40 min blocks to minimise the number of flash writes. This combined with the LittleFS's built-in ware levelling ensures the flash memory will be operational for many years.
This data can be displayed in a number of ways.
pfodApp Monitor
Connecting to the Hot Water Monitor's 4989 port using the pfodApp, displays the current Import / Export kW, the cost so far today and the hot water kWhr today. It also shows yesterdays cost and hot water kWhr. If the hot water was fully heated, that is also shown.
The current time is served from the Hot Water Monitor's system clock using the set time zone. The Hot Water Monitor pfodApp server will accept up to 4 simultaneous connections.
Web Page Charts
The How Water Monitor also has a web server, on port 80, that will display today's data and update it once a minute.
The plotting is provided by the CanvasJS javascript package which is also served from the Hot Water Monitor. No external internet connection is need. You can mouse over to get fine detail.
Each plot contains at most 3000 points. Today's plot is sent as one block. You can also display any period in the last 7 days, in which case the data is sent in chunks, limited to no more than 0.5sec processing time, to insure the incoming telnet data is still processed.
You can also zoom in on the plot by highlighting an area with the mouse.
Time and Time Zone settings
The web pages can also be used set / adjust the RTC (Real Time Clock's) local time and set the local time zone.
The Hot Water Monitor code also includes an NTP client which, if an internet connection is available, will synchronize the RTC time. The RTC will in turn synchronize the system clock. If the Hot Water Monitor is working off-line (no internet) then the RTC may driff up to +/- 10 seconds a month. In which case the Set Time web page can be used to adjust it when necessary. See RTC_NTP_replacement library for the details.
Daily Costs and Usage
The web server also provides access to the historical daily data via the Costs button
This webpage can be saved and loaded into a spreadsheet for further analysis. The data includes the date and time the record was written, the daily import, export, offPeak, hot Water and cost as well as if the hot water was fully heated that day.
In-accurate readings from JSY-MK modules
Also included in the Costs output, for testing, are the cumulative JSY import, export kWh, JSY offPeak and JSY Hot Water kWh. Comparing these values to the calculated Import/Export values and to the electricity supply company's meter show that the JSY import / export value are both too high, while the JSY OffPeak and HW kWh agree with the calculated values.
Testing has shown that the Import / Export readings from the JSY-MK-345 module are so in-accurate as to be unusable for calculating the daily cost of electricity. Fortunately a simple software fix in the Hot Water Monitor's code can over come this problem. See How to Accurately Calculate Your Household Electricity Cost using a JSY-MK-354
Directory Listing and Data Dump
The Directory List show the state of the ESP32's file system
The Data Dump provides the facility to export an entire day's readings (at 2 second intervals) about 43000 lines of data per day. Again the dump of the day's data is sent in chunks limited to 0.5sec of processing time each.
Setting Up the Local WiFi Connections
Hot Water Controller
Hold the push button down while turning on the 12V power pack. After 2 seconds the led will start flashing fast and a local Access Point started, HotWaterConfig, that serves a WiFi configuration web page where you can set the SSID, password and IP of the system. After which ESP32S3 will automatically reboot to run normally. Once connected to the local WiFi network, the system can be monitored and controlled via a TCP connection. See below for the data format and commands.
The default password for HotWaterConfig access point is 12345678 which you can change in the code.
The IP address set for the Hot Water Controller needs to set in the sketch for the Hot Water Monitor so that it connect via Telnet to pick up the data to display and plot.
Since the control modules are mounted inside a metal meter box, the WiFi signal strength is reduced. Here an XIAO-ESP32S3 with an external antenna is used to improve reception, however if that is not enough the system can also be monitored and controlled via Serial. This allows a four core cable (+5V, GND, TX, RX) to be run outside the meter box to another, external, ESP32 / ESP8266 which can act as a bridge between Serial and WiFi. For example see this Cheap/Simple Wifi Shield project.
Hot Water Monitor
The Hot Water Monitor uses the ESPAutoWiFiConfig library to configure the WiFi settings via a webpage served on a Local Access Point. Refer to ESPAutoWiFiConfig tutorial for the details.
Construction - JSY-MK Power Monitors Config





There are three (3) modules to build. i) the Hot Water Triac switch, ii) the Hot Water Controller iii) the Hot Water Monitor.
Configuration and Checking of the JSY-MK Power Monitors
The instantaneous power is measured, in the 3-Phase version, using two (2) JSY-MK power monitors, JSY-MK-193 (manual local copy) and JSY-MK-354 (manual local copy) These particular modules communicate via RS485 Modbus. There are also RS323 versions available, but they still use Modbus. By default the modules come pre-set as address 1. For the 3-Phase system that uses two modules, one of them needs its address changed to something other than 1.
To do this configuration, and to do general testing of the modules, DFROBOT's Rainbowlink is particularly suited as is not only supplies USB (com) to RS485 (and RS232-TTL) but also a 12V supply to power the modules.
Rainbowlink
The Rainbowlink provides four (4) individual USB (COM) connections, A,B,C,D. One RS485, 1 x RS232 and 2 x TTL, as well as 3V3, 5V and 12V supplies. Plug in the supplied USB cable and check the Window's device manager
That will identify which COM number is Channel A, the RS458 connection. Another way to find the RS485 COM number is to open the Modbus Tester, choose Serial (RTU) connection and then choose each of the available COM ports in turn and try to connect. On the Rainbowlink, see which COM port makes the RS485 blue TX light blink (left side top on the above photo).
Setting the JSY-MK Module Address and Baud Rate
To set the module's address or to change the Baud Rate from the default 9600,8,N,1. Connect the Rainbowlink's A,B,Gnd and 12V to the JSY-MK module. (If you are using JSY module with an RS232-TTL connection then use one of the TTL pin connections and the appropriate supply voltage to suit your module.)
Open the Modbus Tester and choose Serial (RTU). There are a number of different commands the Modbus Tester can send 1,2,3,4,5,6,15,16 (decimal), however the JSY-MK modules only accepts two commands, 3 (0x03) and 16 (0x10). That is F:3 or F:16 in modbustester.exe
To set the module address (and baud rate) on the JSY-MK-354, your need to write to register 0x0004 (0004H)
The default value is 0x0106. To set the module to address 2 (9600,8,N,1) write 0x0206 to register 0x0004.
Note: Use Slave Id 1 until the new address has been written. Also click the F:16 button to open the Write Multiple Registers and then choose Hex from the drop down list to enter the hex value, 0206, to be written. The register count should be just 1.
You can also use the modbustester.exe to read the various measurement registers the JSY-MK module provides (see the manual). Connect mains power and check the direction of the current transformers (CT) to give the correct reading.
NOTE Plug the CT into the module first BEFORE clipping over a power cable. DO NOT unplug the CT from the unit as it will generate very high voltages at the pins and cause damage.
When using a TTL - RS485 converter, the modbustester.exe times out if you try to read more than 5 values at once so limit to reading 4 values at a time when testing.
Hot Water Triac Switch Construction


The construction of the Hot Water Triac switch, which turns the hot water heating element on/off, is simply a matter of attaching the heat sink and providing power and control connections.
The Triac only needs to switch 20A and the heat sink only needs to be able to handle 44W, but it is inexpensive to use higher powered versions. The Triacs available from Aliexpress are very cheap. There have been reports of counterfeit FOTEK solid state AC switches. Although the solid state switch used here is not a 'FOTEK' one, 20A solid state switches from Digikey cost ~US$100 so do not be surprised if your solid state switch fails. Just be prepared to use the Rotary Switch to restore the hot water heater to Off Peak if the triac fails. The mains power to the Triac is via screw connectors to allow for easy bypassing if the triac fails.
Hot Water Controller (3-Phase)







The 3-Phase Hot Water controller consists of two boxes. One contains the JSY-MK-193 single phase power monitor module and the triac controller micro, TTL – RS485 converter, power supply and connectors. The second box contains the 3-Phase power monitor. For Single Phase systems only the first box is needed.
The schematic of the installation is (pdf version)
The right hand side of the schematic shows the components mounted on the vero board.
Programming the Hot Water Controller
The Hot Water Controller uses an XIAO-ESP32S3 or DFROBOT Beetle ESP32 V2 (with appropriate wiring changes). To program the ESP32, unzip ESP32S3_modbus.zip to your Arduino Sketch directory and install the necessary libraries. Rename your existing libraries dir to say libraries_org, and then unzip the libraries.zip file to your Arduino Sketch directory to install and new libraries directory with all the necessary libraries. These libraries are used for both the Hot Water Controller and the Hot Water Monitor.
Open the Arduino IDE, with the ESP32 V3.0.7 board support installed, and select the appropriate board, e.g XIAO_ESP32S3, and program the micro. As downloaded, the ESP32S3_modbus.zip is configured for a 3-Phase system. At the top of modbusTask.cpp, comment out #define THREE_PHASE to configure for a single phase system that uses just the JSY-MK-193
Connecting your computer to the micro's USB connection, lets you monitor the data being sent via the Telnet connection. See above for setting up the WiFi connection.
The Hot Water Monitor -- Construction


The Hot Water Monitor's circuit is trivial. (pdf version)
Just a DFROBOT's Firebeetle ESP32-E connected to a DFROBOT's DS3231 RTC module.
Software Programming

Modifying ESP32 Board support for the Hot Water Monitor – Fire_Beetle ESP32 E N16R2 (16Meg flash)
The Hot Water Monitor uses a Fire_Beetle ESP32 E N16R2 (16Meg flash) board to collect the data, every 2 seconds, from the Hot Water Controller and save the last 7 days for plotting. The 16Meg onboard flash memory is used to save the last 7 day files of data (about 11.6Meg of data) as well as other web page and configuration files. This avoids the need to add an SD card and SD card module.
The ESP32 board support needs to be modified to configure it to divide up the 16Meg of flash memory so that most of it is allocated to the LittleFS (SPIFFS) file system. The modifications, detailed below, do not affect the programming of other boards. They only add optional support for a very large file system on the Fire_Beetle ESP32 E N16R2.
The modification consists of two parts.
I) add an new partition file the allocates most of the flash memory to SPIFFS
II) modify the boards.txt file to add the new partition as a menu option.
Add a new partition file
In the directory C:\Users\<userNameHere>\AppData\Local\Arduino15\packages\esp32\hardware\esp32\3.0.7\tools\partitions
add the file app2MB_spiffs_rest.csv which allocates 2MB to program and (almost) all of the rest to SPIFFS.
Update the board.txt file
One line needs to modified and two extra lines need to added to the dfrobot_firebeetle2_esp32e board definition in the board.txt file in directory C:\Users\<userNameHere>\AppData\Local\Arduino15\packages\esp32\hardware\esp32\3.0.7
The file board.txt has these mods. Just replace the existing board.txt file in C:\Users\<userNameHere>\AppData\Local\Arduino15\packages\esp32\hardware\esp32\3.0.7 with the modified file.
Installing ESP32 Sketch Data Upload
The SolarHotWaterMonitor_V2.ino sketch needs a number of files uploaded into the LittleFS file system. These files are in the data subdirectory of the sketch directory. They consist of the web page files, css and .js files. With these data files uploaded to the ESP32 no internet connection is needed to display the charts of the electricity usage. To upload the date directory the ESP32 Sketch Data Upload tool needs to be installed.
Install esp32fs.jar file as described on https://github.com/me-no-dev/arduino-esp32fs-plugin
Close and restart the Arduino IDE.
Selecting the FireBeetle 2 ESP32-E board
Open the Arduino IDE and select the FireBeetle 2 ESP32-E board from the ESP32 (long) list. Also change the Flash Size to “16MB(128Mb)” and choose the Partition Scheme “16M with spiffs (2MB APP/13.9MB SPIFFS)”
Once these modifications have been made the Tools menu should look like this
Modifying Async-TCP to run on Core 1 (Arduino Core)
In library.zip file, the code in the Async-TCP.h file in directory C:\Users\<userNameHere>\Documents\Arduino\libraries\AsyncTCP-main\src has been modifed to run on the Arduino core.
The following lines in Async-TCP.h were changed
If you have download Async-TCP directly, you will need to make these changes to Async-TCP.h
Programming the Hot Water Monitor
To program the DFROBOT's Firebeetle ESP32-E, unzip SolarHotWaterMonitor_V2.zip to your Arduino Sketch directory and install the necessary libraries. Rename your existing libraries dir to say libraries_org, and then unzip the libraries.zip file to your Arduino Sketch directory to install and new libraries directory with all the necessary libraries. These libraries are used for both the Hot Water Controller and the Hot Water Monitor.
Open the Arduino IDE, with the ESP32 V3.0.7 board support installed, and
– select the Board Firebeetle 2 ESP32-E.
– select Flash Size: 16MB (128Mb)
– select Partition Scheme: 16M with spiffs (2MB App/13.9MB SPIFFS)
and program the Firebeetle ESP32-E with the SolarHotWaterMonitor_V2.ino sketch. Then close the Arduino IDE Monitor and upload the data to the Firebeetle ESP32-E using the ESP32 Sketch Data Upload option. Choose LittleFS as the file system. This installs the web page support for the plots.
Configuring the Hot Water Monitor Software
The default timezone of the Hot Water Monitor is set by the line
See
https://github.com/nayarsystems/posix_tz_db/blob/master/zones.csv
for a list of time zones and their posix_tz strings that can be copied and pasted into the get_DefaultTZ or into the Set Time Zone webpage.
This setting can be changed later via the build-in web pages, if needed.
The IP address of the Hot Water Controller need to be set in the monitor. Edit the line to suite
The electricity cost also needs to be configured at the top of the processData.cpp file
These values are used in the calculateTotalCost method. In this code the DAILY_CHARGE is not included as it cannot be effected by changes in how and when the electricity is used. The simple calculation in calculateTotalCost does not take into account the time of day, but that can be easily added as each records timestamped.
Having made these configuration setting, re-program the Firebeetle ESP32-E. See above for setting up the WiFi connection.
Telnet Data Format
The data format for both the Telnet and Serial output is in CSV format as follows:-
HWC, readCounter, state (0-Off, 1-Solar, 2-OffPeak), Hwtriac (1-On, 0-Off), Hwboost (1-On, 0-Off), modbusError (0-no errors), Power_W (instantaneous Watts), HW_W (instantaneous Watts), Import_kWh (Cumulated kWhrs), Export_kWh (Cumulated kWhrs), HW_kWh (Cumulated kWhrs), OffPeak_kWh (Cumulated kWhrs), EOD<newline>
The HWC and EOD constants define the start and end of each data line. The Import_kWh and Export_kWh are un-reliable, see How to Accurately Calculate Your Household Electricity Cost using a JSY-MK-354 and so can be ignored, except for confirming they are in error. Instead the Hot Water Monitor calculates the kWh from the Power_W field and the timestamp when the record is received. The readCounter can be used to detect missed records.
There are two commands that can be sent as inputs, either by telnet or via the serial connection (including the USB connection). Those are boost on and boost off to manually turn the hot water triac on or off. If turned on with boost on, the triac will automatically turn off after 1hr
Code Descriptions
The Hot Water Controller Code
The ESP32S3_modbus code has two main sections. The code to read the JSY energy monitors via RS485 / modbus and the telnet handling. These two sets of code run on separate cores in the ESP32. The code reading the energy monitors runs on the Arduino loop() core while the telnet handling runs on the ESP32 WiFi core.
The JSY energy monitors' registers are read using the NonBlockingModbusMaster library. The telnet is handled via the HS_AsyncTCP library. Communication between the two cores is via volatile variables. (VolatileVars.cpp / .h) In addition to using volatile variables, SemaphoreHandle_t varsLock is used to update/copy all volatile vars as a block so that telnet will send a consistant set of data in each record. A change in the value of the recordCounter, which is the last volatile to be updated, is the trigger to send the next record.
There are two versions of the software, on for single phase and one for 3-Phase. The single phase code is in singlePhaseMethods.h and the 3-Phase code is in threePhaseMethods.h. The #define THREE_PHASE at the top of the modbusTask.cpp file determines which set of methods are used.
The NonBlockingModbusMaster library allows register reads to be linked together into a single job. Each read method has a processing method which process the results, sets the global variables and calls the next read method. The readModbusValues() method is called often from the loop() code to check if the current job has finished, read the three position switch state, set the triac on/off and update the volatile variables for the telnet code (and the Serial output) to pick up and send. The bottom of the readModbusValues() method checks if the job is not processing and if it is not then is restarts the job every 2 seconds. Note: the nbModbusMaster.justFinished() call only returns true one time when the job is finished, so !nbModbusMaster.isProcessing() is used to check if the job is running or not.
The JSY modules update every 250ms and the fastest read time is 500ms but the recommended read cycle time is 1 second. This code reads the registers once every 2 seconds.
The Hot Water Controller does not automatically reboot each day as it uses very little if any dynamic memory allocation. It will reboot if are modbus errors for 5 cycles. In which case it will automatically reconnect to the WiFi network and the Hot Water Monitor is automatically reconnect to the telnet service.
The Hot Water Monitor Code
The SolarHotWaterMonitor_V2 code:-
– connects to the Hot Water Controller via telnet and collect the power reading and the switch and triac states, sent every 2 seconds
– timestamps the record and saves it in a temporary buffer which it writes to the Flash memory file system approximately every 40mins.
– using that data, the SolarHotWaterMonitor_V2 serves the pfodApp and webpage displays.
The webpage displays use the CanvasJS charting javascript package, which is locally hosted in the ESP32's file system so the monitor is not dependent on any internet access. A RTC (Real Time Clock) module is used to provide system time via the RTC_NTP_replacement library.
Claude Code AI was used extensively to write the charting script and web page button processing and to generate the plot data. The plot data is chucked, using ESPAsyncWebServer library. The chunks were limited to less than 500ms processing time to prevent watch dog rests and missed telnet data records. Note: the Costs webpage is not currently chunked. This needs to be fixed in the next year or so as the data increases.
While Claude Code AI was useful, the code produced often had logical errors which needed to be debugged. Usually Claude helped with this by adding logging messages etc, but some simple logical errors were beyond its comprehension. Where Claude was particularly useful was in modifying existing code to add/remove fields to plots/file records.
The graphical pfodApp display was designed using pfodGUIdesigner. Two (2) basic display widgets were designed:- the pie chart, (Dwg_1.cpp) and an updateable label (Label.cpp). The pfodGUI designer generated the necessary code to display these widgets. That code was then augmented to add setters/getters to update the widgets with the latest values/state. See the pfodGUIdesigner tutorial for how to specify which primitive elements of the widget are updateable.
This code at the global level instantiates the widgets and connects them to the pfodParser command processing so that their commands are automatically routed to the correct widget.
The sendMainDwg() method positions and inserts the widgets. The main menu requests updates every 2.5 seconds so the display will see the latest 2 second data.
The Solar Hot Water Monitor reboots each night, a few minutes after midnight to clean up the ESP32's dynamic memory.
Conclusion
This page covered using excess solar generation to heat the hot water. If the house requires more power, the hot water is automatically switched off if needed to prevent importing electricity from the mains and then switched back on when excess solar generation is available. The kW readings are sourced from the two JSY-MK energy monitors. A 1hr Boost button is available to heat the how water regardless of the solar production and a manual rotary switch was installed on the switch board to allow the hot water to be switch back to the separate Controlled Load (OffPeak) meter in cases of extended overcast weather.
pfodApp is used to display the current Export/Import power and whether the hot water is being heated as well as the accumulated cost of electricity for today. Web pages with detailed charts are available for the last 7 days, as well as all the historical daily usage and costs, and to set the time / time zone. The system is designed to be robust and can be run completely off-line. The charting javascript is served locally and an RTC (Real Time Clock) module keeps time.