Make your UPS smarter with ESP8266 and PCF8591
I have a UPS that otherwise works well, but its network interface is faulty. I use it for my ioBroker server. During a power outage, I had no way to monitor the remaining battery capacity, and the UPS did not shut down the server before the battery was completely drained. I built this project to solve that problem.
Difficulty: Advanced
Note: Wiring the mains-voltage parts requires electrical expertise!
Created: January 2021
Reviewed: October 2026
UPS Monitoring: Integrating ESP8266 and PCF8591

The UPS monitor circuit
Parts used in this project:
- ESP8266 ESP-01S Wi-Fi module
- ESP-01S breadboard adapter
- USB programming adapter for the ESP8266-01 module
- PCF8591P DIP-16
- FT232RL 3.3 V USB-to-TTL serial adapter
- Resistors: 470 Ω, 1 kΩ, 4.7 kΩ and 22 kΩ
- PC817 optocoupler
- LM2596S DC-DC step-down module
- 230 V AC to 5 V DC power supply module
Warning!
This project involves equipment connected to mains voltage. Mains electricity is dangerous and can cause serious injury, fatal electric shock or fire.
Before opening the UPS, switch it off and unplug it from the wall outlet. After removing the cover, disconnect the battery immediately. With the battery connected, the inverter may start and parts of the UPS may become live at high voltage.
Always disconnect the equipment and circuit from power before working on them. Pay particular attention to protection against electric shock around mains wiring and terminals.
Use properly rated, insulated cables, wires, switches, connectors and plugs with a suitable conductor cross-section.
Only work on mains-voltage equipment if you know exactly what you are doing and accept the risks. I cannot accept responsibility for damage or injury. If you are unsure, ask a qualified professional for help.
To monitor the UPS, I built the circuit shown below using an ESP8266 and a PCF8591, then installed it inside the UPS.

The points marked “UPS input 230V” in the diagram connect to the incoming mains supply of the UPS. Use wiring suitable for this purpose. Brown indicates the live conductor and blue the neutral conductor.
The circuit is powered from the UPS battery, so it can continue working during a power outage. An LM2596 DC-DC step-down module provides a stable 3.3 V supply. Connect “UPS Battery +” in the diagram to the positive battery terminal and “UPS Battery -” to the negative battery terminal.
Before connecting the rest of the circuit, use the LM2596 trimmer to set its output to 3.3 V and check it with a meter.
The PCF8591 A/D converter measures the battery voltage through a divider made from the 22 kΩ and 4.7 kΩ resistors. The PCF8591 connects to the ESP8266-01 over I²C, as shown above: the green SDA wire goes to GPIO0 and the yellow SCL wire goes to GPIO2. Do not forget the two 4.7 kΩ I²C pull-up resistors. That completes the first part of the circuit.
Next, we detect whether mains power is present. A 230 V AC to 5 V DC power supply module, such as the Hi-Link HLK-PM01, is connected to the incoming supply of the UPS. Its 5 V output drives an optocoupler, keeping this part galvanically isolated from the ESP8266 circuit. A PC817 or a suitable equivalent can be used, with a 470 Ω series resistor on its input. Connect the optocoupler output to the ESP8266-01 RX pin, which we use as an input here. Pull that input up to the 3.3 V supply through a 1 kΩ resistor.
It is also possible to detect mains voltage with an AC-input phototransistor optocoupler, such as the SFH620A. It keeps the microcontroller galvanically isolated from the mains. With appropriately rated series resistors, it can be used for mains-voltage detection. Its output pulses at approximately 100 Hz, so the Arduino sketch must evaluate that signal differently.
The UPS monitor Arduino sketch
Copy the following sketch into the Arduino IDE. Enter your Wi-Fi SSID and password and the IP address of your ioBroker server, then upload it to the ESP8266.
/*
UPS monitor for ESP8266 ESP-01S
https://myhomethings.eu
The PCF8591 measures battery voltage on AIN0.
GPIO3/RX reads the optocoupler: LOW = mains present, HIGH = mains absent.
*/
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include <Wire.h>
const char* ssid = "SSID";
const char* password = "Password";
const char* mqtt_server = "192.168.x.xxx";
int PCF8591 = 0x48;
float ain0;
String battery_voltage;
char battery_msg[25];
unsigned long last_msg = 0;
unsigned long msg_freq = 5000;
unsigned long last_wifi_attempt = 0;
unsigned long last_mqtt_attempt = 0;
int mqtt_flag = -1; // No mains state has been published yet.
int network_voltage_pin = 3;
WiFiClient espClient;
PubSubClient client(espClient);
void setup_wifi()
{
delay(100);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
}
void reconnect()
{
if (WiFi.status() != WL_CONNECTED || millis() - last_mqtt_attempt < 5000)
{
return;
}
last_mqtt_attempt = millis();
String clientId = "ESP8266_UPS";
if (client.connect(clientId.c_str()))
{
mqtt_flag = -1; // Publish the current state again.
last_msg = millis() - 30000UL; // Read the battery on the next loop.
}
}
void setup()
{
Wire.pins(0, 2);
Wire.begin(0, 2);
setup_wifi();
client.setServer(mqtt_server, 1883);
pinMode(network_voltage_pin, INPUT);
}
void loop()
{
if (WiFi.status() != WL_CONNECTED && millis() - last_wifi_attempt >= 5000)
{
last_wifi_attempt = millis();
WiFi.reconnect();
}
if (WiFi.status() == WL_CONNECTED && !client.connected())
{
reconnect();
}
if (client.connected())
{
client.loop();
}
int network_present = digitalRead(network_voltage_pin) == LOW ? 1 : 0;
msg_freq = network_present ? 30000UL : 5000UL;
if (client.connected() && network_present != mqtt_flag)
{
if (client.publish("UPS/Network_voltage", network_present ? "true" : "false"))
{
mqtt_flag = network_present;
}
}
unsigned long Millis = millis();
if (Millis - last_msg >= msg_freq)
{
last_msg = Millis;
Wire.beginTransmission(PCF8591);
Wire.write(0x04);
if (Wire.endTransmission() != 0)
{
return;
}
if (Wire.requestFrom((uint8_t)PCF8591, (uint8_t)2) != 2)
{
return;
}
Wire.read(); // The first byte belongs to the previous conversion.
int sample = Wire.read();
if (sample < 0)
{
return;
}
ain0 = sample * (18.7468f / 255.0f);
battery_voltage = String(ain0);
battery_voltage.toCharArray(battery_msg, sizeof(battery_msg));
if (client.connected())
{
client.publish("UPS/battery_voltage", battery_msg);
}
}
}
After programming the ESP8266, install the circuit inside the UPS. It is small, but make sure you choose a suitable location.
Secure the circuit properly and keep it safely separated from high-voltage parts. Placing it in a separate enclosure is a good idea. When refitting the UPS cover, take particular care not to pinch any cables.
When the battery is reconnected, parts inside the UPS may become live at 230 V.
After uploading the sketch and reassembling the UPS, check that everything is in order. Then reconnect the battery, plug in the UPS and switch it on.
Once the Wi-Fi and MQTT connections succeed, the data should soon appear in ioBroker. We now know whether mains power is available and what the battery voltage is. But there is still one more step before our server is protected. 😉
Handling a power outage in ioBroker
In ioBroker, open the Instances tab and select javascript.0. Enable the execution of ‘exec’ commands in the adapter configuration.

Open the Scripts tab and create a new JavaScript.

Copy in the following code:
on({id: 'mqtt.0.UPS.battery_voltage'/*battery voltage*/}, function (obj) {
if(getState('mqtt.0.UPS.Network_voltage'/*Network voltage*/).val == "false")
{
if(getState('mqtt.0.UPS.battery_voltage'/*battery voltage*/).val < 11.0)
{
exec('sudo shutdown -h now');
}
}
});
The JavaScript checks whether mains power is present. If it is absent, the script checks the battery voltage. When that voltage falls below 11 V, it issues a command to shut down the computer.
With this simple circuit and a few lines of code, we can help prevent operating-system damage during a power outage.
What happens when the power returns?
Let’s wake the server again! This method works when the PC is connected to the network by an Ethernet cable. Check that your computer supports Wake-on-LAN (WoL). Many computers do.
If it does, let’s put the hardware together:

We need an ESP8266-01 and a matching USB programming adapter. Add a phone charger as the power supply, connect them, and the hardware is ready.
Open the Arduino IDE and copy in the sketch below. Enter your Wi-Fi credentials and the MAC address of the computer you want to wake, then upload the sketch to the ESP8266-01.
/*
One-time Wake-on-LAN sender for ESP8266 ESP-01S
This module must lose power during an outage and restart when mains returns.
*/
#include <ESP8266WiFi.h>
#include <WiFiUdp.h>
#include <WiFiClient.h>
#include <WakeOnLan.h>
const char* ssid = "SSID";
const char* password = "Password";
const char* MACAddress = "xx:xx:xx:xx:xx:xx";
WiFiUDP UDP;
WiFiClient client;
WakeOnLan WOL(UDP);
bool wol_sent = false;
void setup_wifi()
{
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
}
}
void setup()
{
setup_wifi();
delay(300000);
// The Wi-Fi connection may have dropped during the five-minute wait.
if (WiFi.status() != WL_CONNECTED)
{
setup_wifi();
}
WOL.setRepeat(3, 100);
WOL.calculateBroadcastAddress(WiFi.localIP(), WiFi.subnetMask());
wol_sent = WOL.sendMagicPacket(MACAddress);
}
void loop()
{
if (!wol_sent)
{
if (WiFi.status() != WL_CONNECTED)
{
setup_wifi();
}
WOL.calculateBroadcastAddress(WiFi.localIP(), WiFi.subnetMask());
wol_sent = WOL.sendMagicPacket(MACAddress);
if (!wol_sent)
{
delay(5000);
return;
}
}
delay(1000);
ESP.deepSleep(0);
}
Connect the Wake-on-LAN device to a wall outlet supplied by the mains, not to one of the UPS battery-backed outlets!
← More Arduino / ESP Examples and Projects
