Smart Energy Management for Residential PV Microgrids: ESP32-Based Indirect Control of Commercial Inverters for Enhanced Flexibility
Abstract
1. Introduction
1.1. Evolution of Energy Management Strategies in PV Microgrids
1.2. Limitations of Current Real-World Implementations
1.3. The Emergence of Smart Inverters and Direct Control
1.4. Research Gaps and Contributions of the Authors
2. Description of the Framework
2.1. Problem Overview
2.2. Microgrid Description
2.3. Control Scheme
- Power Injection: When the system detects a need to inject energy into the grid, the ESP32 adds a negative offset to the wattmeter readings to indicate higher demand on non-critical loads. This prompts the inverter to respond by injecting the required amount of power to balance the system, drawing either from the PV generation or the stored energy in the BESS.
- Power Absorption: Conversely, when the system needs to absorb excess energy from the grid into the battery (e.g., during periods of low demand), the ESP32 adjusts the wattmeter readings with a positive offset to simulate an overproduction of energy from the PV system. The inverter, in response, absorbs power from the grid to maintain equilibrium.
3. Implementation
3.1. Hardware Setup
- UART1 (Pins 26 RX, 27 TX): Configured for slave mode, this port emulates the external wattmeter, responding to requests from the inverter.
- UART2 (Pins 32 RX, 33 TX): Configured for master mode, this port communicates with the actual external wattmeter to acquire real-time data.
3.2. Communication via Modbus
- Modbus RTU: The ESP32 acts in dual roles: as a slave, it manipulates the wattmeter readings and sends them to the inverter, indirectly controlling the power flows. Simultaneously, it functions as a master, requesting real-time power readings from the actual wattmeter to process and adjust the behavior of the system dynamically.
- Modbus TCP/IP: Over WiFi, the ESP32 retrieves critical operational data from the inverter, such as the SoC of the battery and performance metrics, to further optimize energy management decisions.
3.3. Wattmeter Emulation and Initial Scanning
- Modbus Traffic Analysis: An initial analysis of the native Modbus traffic between the inverter and a real wattmeter revealed that the inverter primarily queries two specific registers: 30012 (for wattmeter model identification) and 404356 (for power readings). Figure 6 shows one of the tests for this initial analysis.
- Register Creation: The software of the ESP32 has been designed to create and populate these specific Modbus registers internally during startup, allowing it to accurately mimic the behavior of, namely, the wattmeter. After this initial exchange, the inverter periodically reads 16 consecutive registers starting from address 30001. These registers contain critical electrical parameters, such as active power, voltage, and current, that the inverter uses to monitor the power flow between the grid and the microgrid. The ESP32 emulates this behavior, responding with the necessary data to maintain seamless operation.
- Sampling Rate: A sampling time of 500 ms was selected for reading wattmeter data, striking a balance between data refresh rate and optimizing the processing load of the ESP32. The sampling time of 500 ms was selected because it is precisely half the period of 1 s that the inverter uses to sample the wattmeter readings. This ensures the ESP32 is always prepared with updated data before the inverter makes its request.
3.4. Access to Inverter Registers
3.5. Obtaining PVPC Prices
3.6. ESP32 Software Development
- Library Integration: Essential libraries, including those for Modbus communication (RTU and TCP/IP), were integrated to facilitate data exchange.
- System Initialization: The program initializes various components, including Modbus RTU (both master and slave roles), Modbus TCP/IP client, and WiFi connectivity.
- Wattmeter Emulation Logic: A dedicated function, CreateIregs(), is executed once at startup to set up the registers of the emulated wattmeter. A callback function is then assigned to the specific power reading register (404356) to trigger a flag, signaling the completion of the initial scan by the inverter and enabling the controller to begin delivering dynamic wattmeter readings.
- Data Acquisition: The software continuously reads data from the physical wattmeter (via Modbus RTU master) every 500 ms using the millis() function to ensure non-blocking operation.
- PVPC Price Acquisition: The system incorporates a function (getPVPC()) to retrieve real-time energy prices from the Spanish regulated market (Red Eléctrica Española-REE) API. This involves sending HTTP GET requests and parsing the JSON response to extract the 24-hourly prices, which are crucial for optimizing energy management strategies.
3.7. Flexibility Framework
- Flexibility Market Mode: Firstly, during the hours when flexibility markets are active, an aggregator will send signals for tracking. In this work, these signals will not be generated directly but will be emulated, and the device must comply with them at all times.
- PVPC Self-consumption Mode: The second mode of operation applies to the hours when the flexibility market is not active. During these periods, the system will operate locally, using retail market prices (in this case, the PVPC) to develop an economic optimization strategy. This involves deciding when to charge or discharge the battery based on retail market prices. It is worth noting that these prices are published at 6:00 p.m. on the previous day, enabling the system to plan in advance the best hours of the day for these operations, provided that it is not participating in flexibility markets at that time.
4. Results
4.1. Setup
4.2. General System Performance
4.3. Flexibility Market Mode
4.4. PVPC Self-Consumption Mode
4.5. Summary
5. Discussion
5.1. Representativeness of Experimental Validation
5.2. Long-Duration Operation and Nighttime Cycles
5.3. System Behavior Under Transient Fluctuations
5.4. Scalability and Multi-Inverter Microgrids
5.5. Economic Benefit
5.6. Comparative Analysis with Advanced Flexible EMS Methodologies
6. Conclusions and Future Work
Author Contributions
Funding
Institutional Review Board Statement
Informed Consent Statement
Data Availability Statement
Conflicts of Interest
Abbreviations
References
- Cajot, S.; Peter, M.; Bahu, J.M.; Koch, A.; Maréchal, F. Energy Planning in the Urban Context: Challenges and Perspectives. Energy Procedia 2015, 78, 3366–3371. [Google Scholar] [CrossRef]
- McGarry, C.; Dixon, J.; Elders, I.; Galloway, S. A high-resolution geospatial and socio-technical methodology for assessing the impact of electrified heat and transport on distribution network infrastructure. Sustain. Energy Grids Netw. 2023, 35, 101118. [Google Scholar] [CrossRef]
- McKenna, K.; Abraham, S.A.; Wang, W. Major Drivers of Long-Term Distribution Transformer Demand; Technical report; National Renewable Energy Laboratory (NREL): Golden, CO, USA, 2024. [CrossRef]
- Pastore, L.M. Combining Power-to-Heat and Power-to-Vehicle strategies to provide system flexibility in smart urban energy districts. Sustain. Cities Soc. 2023, 94, 104548. [Google Scholar] [CrossRef]
- Regina, H.; Sarah, F.; Mihai, C.; Anjali, W.; Steven, W. Challenges and opportunities of flexibility markets related to operational planning. In Proceedings of the 2023 19th International Conference on the European Energy Market (EEM), Lappeenranta, Finland, 6–8 June 2023; pp. 1–5. [Google Scholar] [CrossRef]
- Veisi, M.; Adabi, F.; Kavousi-Fard, A.; Karimi, M. A Framework of Electricity Market Based on Two-Layer Stochastic Power Management for Microgrids. IEEE Access 2022, 10, 41047–41063. [Google Scholar] [CrossRef]
- Strbac, G.; Hatziargyriou, N.; Lopes, J.P.; Moreira, C.; Dimeas, A.; Papadaskalopoulos, D. Microgrids: Enhancing the Resilience of the European Megagrid. IEEE Power Energy Mag. 2015, 13, 35–43. [Google Scholar] [CrossRef]
- Saad, A.; Faddel, S.; Mohammed, O. IoT-Based Digital Twin for Energy Cyber-Physical Systems: Design and Implementation. Energies 2020, 13, 4762. [Google Scholar] [CrossRef]
- Nguyen, V.H.; Tran, Q.T.; Besanger, Y.; Jung, M.; Nguyen, T.L. Digital twin integrated power-hardware-in-the-loop for the assessment of distributed renewable energy resources. Electr. Eng. 2022, 104, 377–388. [Google Scholar] [CrossRef]
- Shayeghi, H.; Shahryari, E.; Moradzadeh, M.; Siano, P. A Survey on Microgrid Energy Management Considering Flexible Energy Sources. Energies 2019, 12, 2156. [Google Scholar] [CrossRef]
- Jain, V.; Singh, B.; Seema. A Grid Connected PV Array and Battery Energy Storage Interfaced EV Charging Station. IEEE Trans. Transp. Electrif. 2023, 9, 3723–3730. [Google Scholar] [CrossRef]
- Silva, J.A.A.; López, J.C.; Guzman, C.P.; Arias, N.B.; Rider, M.J.; da Silva, L.C. An IoT-based energy management system for AC microgrids with grid and security constraints. Appl. Energy 2023, 337, 120904. [Google Scholar] [CrossRef]
- Aatabe, M.; Abbadi, R.E.; Vargas, A.N.; Bouzid, A.E.M.; Bawayan, H.; Mosaad, M.I. Stochastic Energy Management Strategy for Autonomous PV–Microgrid Under Unpredictable Load Consumption. IEEE Access 2024, 12, 84401–84419. [Google Scholar] [CrossRef]
- Alpízar-Castillo, J.; Linders, K.; Slaifstein, D.; Ramírez-Elizondo, L.; Bauer, P. Economic Opportunities of Power Curtailment and Peak Shaving on Residential PV-BESS Systems. In Proceedings of the 2024 20th International Conference on the European Energy Market (EEM), Istanbul, Turkiye, 10–12 June 2024; pp. 1–6. [Google Scholar] [CrossRef]
- Khan, M.R.; Haider, Z.M.; Malik, F.H.; Almasoudi, F.M.; Alatawi, K.S.S.; Bhutta, M.S. A Comprehensive Review of Microgrid Energy Management Strategies Considering Electric Vehicles, Energy Storage Systems, and AI Techniques. Processes 2024, 12, 270. [Google Scholar] [CrossRef]
- Chatzigeorgiou, N.G.; Theocharides, S.; Makrides, G.; Georghiou, G.E. A review on battery energy storage systems: Applications, developments, and research trends of hybrid installations in the end-user sector. J. Energy Storage 2024, 86, 111192. [Google Scholar] [CrossRef]
- Akarne, Y.; Essadki, A.; Nasser, T.; Annoukoubi, M.; Charadi, S. Optimized control of grid-connected photovoltaic systems: Robust PI controller based on sparrow search algorithm for smart microgrid application. Glob. Energy Interconnect. 2025, 8, 523–536. [Google Scholar] [CrossRef]
- Khalid, W.; Jamil, M.; Khan, A.A.; Awais, Q. Open-Source Internet of Things-Based Supervisory Control and Data Acquisition System for Photovoltaic Monitoring and Control Using HTTP and TCP/IP Protocols. Energies 2024, 17, 4083. [Google Scholar] [CrossRef]
- Benavides, D.; Arévalo, P.; Ortega, A.C.; Sánchez-Sutil, F.; Villa-Ávila, E. Energy Management Model for a Remote Microgrid Based on Demand-Side Energy Control. Energies 2024, 17, 170. [Google Scholar] [CrossRef]
- Artale, G.; Cataliotti, A.; Cosentino, V.; Di Cara, D.; Guaiana, S.; Panzavecchia, N.; Tinè, G. Real-Time Power Flow Monitoring and Control System for Microgrids Integration in Islanded Scenarios. IEEE Trans. Ind. Appl. 2019, 55, 7186–7197. [Google Scholar] [CrossRef]
- Nizam, M.S.M.; Abdullah, E.; Hidayat, N.M.; Hashim, N.M.Z.; Hassan, M.A.A. Real-Time Energy Monitoring in Renewable EV Charging Stations: An ESP32-Based System Integrating Modbus, MQTT, and ESP-NOW Protocols. In Proceedings of the 2024 IEEE 22nd Student Conference on Research and Development (SCOReD), Selangor, Malaysia, 19–20 December 2024; pp. 339–344. [Google Scholar] [CrossRef]
- NREL. Grid Modernization. Available online: https://www.nrel.gov/grid (accessed on 10 July 2025).
- Chen, S.; Ebe, F.; Morris, J.; Lorenz, H.; Kondzialka, C.; Heilscher, G. Implementation and Test of an IEC 61850-Based Automation Framework for the Automated Data Model Integration of DES (ADMID) into DSO SCADA. Energies 2022, 15, 1552. [Google Scholar] [CrossRef]
- SMA. Modbus Protocol Interface. Available online: https://www.sma.de/en/products/product-features-interfaces/modbus-protocol-interface (accessed on 10 July 2025).
- SolarEdge Inverters—SunSpec Logging Interface. Available online: https://knowledge-center.solaredge.com/sites/kc/files/sunspec-implementation-technical-note.pdf (accessed on 10 July 2025).
- Systems, E. ESP32 Microcontroller. Manufactured by Espressif Systems, Shanghai, China. Purchased from Digi-Key Electronics, Thief River Falls, MN, USA (via Spain). Microcontroller model: ESP-WROOM-32.
- Suzhou Talesun Solar Technologies Co., Ltd. Talesun Solar Panels. Manufactured by Suzhou Talesun Solar Technologies Co., Ltd., Suzhou, China. Purchased from the distributor AutoSolar Energy Solutions S.L.U., Alcàsser, Valencia, Spain.
- Pylon Technologies Co., Ltd. Force L2 Lithium-Ion Batteries. Manufactured by Pylon Technologies Co., Ltd. (Pylontech), Shanghai, China. Purchased from the distributor AutoSolar Energy Solutions S.L.U., Alcàsser, Valencia, Spain.
- Ingeteam Power Technology, S.A. INGECON SUN STORAGE 1Play TL M Hybrid Inverter. Manufactured by Ingeteam Power Technology, S.A., Sarriguren, Navarra, Spain. Purchased from the distributor AutoSolar Energy Solutions S.L.U., Alcàsser, Valencia, Spain.
- Carlo Gavazzi. EM111DINAV81XS1X Energy Meter. Manufactured by Carlo Gavazzi in China. Purchased from the distributor AutoSolar Energy Solutions S.L.U., Alcàsser, Valencia, Spain.
- NOYITO. Módulo Conversor TTL-RS485 (Producto con ID B07BJJ7ZF8); Fabricado en China; Adquirido a través del distribuidor Amazon: Madrid, España; Available online: https://www.amazon.com/-/he/NO-TTLRS485-335/dp/B07BJJ7ZF8 (accessed on 16 September 2024).
- PVPC. ESIOS Electricidad. Available online: https://www.esios.ree.es/es/pvpc# (accessed on 28 January 2024).
- Zhang, N.; Yan, J.; Hu, C.; Sun, Q.; Yang, L.; Gao, D.W.; Guerrero, J.M.; Li, Y. Price-Matching-Based Regional Energy Market With Hierarchical Reinforcement Learning Algorithm. IEEE Trans. Ind. Inform. 2024, 20, 11103–11114. [Google Scholar] [CrossRef]
- Yang, L.; Li, X.; Sun, M.; Sun, C. Hybrid Policy-Based Reinforcement Learning of Adaptive Energy Management for the Energy Transmission-Constrained Island Group. IEEE Trans. Ind. Inform. 2023, 19, 10751–10762. [Google Scholar] [CrossRef]











| Comparison Criterion | Proposed EMS | [33] | [34] |
|---|---|---|---|
| Core Methodology | Indirect Control (Wattmeter Emulation) | HRL | HPRL |
| Execution Platform | Low-Cost/IoT | High-Performance Computing Platform | High-Performance Computing Platform |
| Main Objective | Unlock BESS Injection in Commercial Inverters | Trading Optimization in Regional Market | Adaptive Management in Constrained Island Systems |
| Validation Environment | Real-World Physical Testbed | Simulation/Numerical Examples | Simulation |
| Entry Barrier (Cost) | Low | High | High |
Disclaimer/Publisher’s Note: The statements, opinions and data contained in all publications are solely those of the individual author(s) and contributor(s) and not of MDPI and/or the editor(s). MDPI and/or the editor(s) disclaim responsibility for any injury to people or property resulting from any ideas, methods, instructions or products referred to in the content. |
© 2025 by the authors. Licensee MDPI, Basel, Switzerland. This article is an open access article distributed under the terms and conditions of the Creative Commons Attribution (CC BY) license (https://creativecommons.org/licenses/by/4.0/).
Share and Cite
Tradacete-Ágreda, M.; Sánchez-Pérez, A.; Santos-Pérez, C.; Hueros-Barrios, P.J.; Rodríguez-Sánchez, F.J.; Espolio-Maestro, J. Smart Energy Management for Residential PV Microgrids: ESP32-Based Indirect Control of Commercial Inverters for Enhanced Flexibility. Sensors 2025, 25, 6595. https://doi.org/10.3390/s25216595
Tradacete-Ágreda M, Sánchez-Pérez A, Santos-Pérez C, Hueros-Barrios PJ, Rodríguez-Sánchez FJ, Espolio-Maestro J. Smart Energy Management for Residential PV Microgrids: ESP32-Based Indirect Control of Commercial Inverters for Enhanced Flexibility. Sensors. 2025; 25(21):6595. https://doi.org/10.3390/s25216595
Chicago/Turabian StyleTradacete-Ágreda, Miguel, Alfonso Sánchez-Pérez, Carlos Santos-Pérez, Pablo José Hueros-Barrios, Francisco Javier Rodríguez-Sánchez, and Jorge Espolio-Maestro. 2025. "Smart Energy Management for Residential PV Microgrids: ESP32-Based Indirect Control of Commercial Inverters for Enhanced Flexibility" Sensors 25, no. 21: 6595. https://doi.org/10.3390/s25216595
APA StyleTradacete-Ágreda, M., Sánchez-Pérez, A., Santos-Pérez, C., Hueros-Barrios, P. J., Rodríguez-Sánchez, F. J., & Espolio-Maestro, J. (2025). Smart Energy Management for Residential PV Microgrids: ESP32-Based Indirect Control of Commercial Inverters for Enhanced Flexibility. Sensors, 25(21), 6595. https://doi.org/10.3390/s25216595

