MyHomeThings
Published © GPL3+

Make your UPS smarter with ESP8266 and PCF8591

The UPS did not shut down the computer in the event of a power outage. This hack makes the UPS smarter, I made this circuit with ESP8266

IntermediateFull instructions provided2 hours408
Make your UPS smarter with ESP8266 and PCF8591

Things used in this project

Hardware components

Old UPS
×1
ESP8266 ESP-01
Espressif ESP8266 ESP-01
×1
Through Hole Resistor, 4.7 kohm
Through Hole Resistor, 4.7 kohm
×3
Through Hole Resistor, 15 kohm
Through Hole Resistor, 15 kohm
×1
Through Hole Resistor, 1 kohm
Through Hole Resistor, 1 kohm
×1
Through Hole Resistor, 470 ohm
Through Hole Resistor, 470 ohm
×1
PCF8591 A / D converter
×1
HI-Link HLK-PM01
×1
LM2596S DC-DC power adapter
×1
TPL 621 optocoupler
×1

Software apps and online services

Arduino IDE
Arduino IDE
ioBroker

Story

Read more

Schematics

Make your UPS smarter with ESP8266 and PCF8591

Code

UPS_Controll.ino

Arduino
/*************************************
  *   https://myhomethings.eu
  *   Generic ESP8266 module
  *   Flash size: 1M (no SPIFFS)
/************************************/

#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;
int mqtt_flag = 0;
int network_voltage_pin = 3;

WiFiClient espClient;
PubSubClient client(espClient);

void setup_wifi() 
{
  delay(100);
  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);
  
  while(WiFi.status() != WL_CONNECTED) 
  {
    delay(500);
  }
  randomSeed(micros());
}

void reconnect() 
{
  while(!client.connected()) 
  {
    String clientId = "ESP8266_UPS";
    if (client.connect(clientId.c_str()))
    {
      // pass
    } 
    else 
    {
      delay(6000);
    }
  }
}

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 (!client.connected()) 
  {
    reconnect();
  }
  client.loop();

  if(digitalRead(network_voltage_pin) == HIGH && mqtt_flag == 0)
  {
    client.publish("UPS/Network_voltage", "false");
    msg_freq = 5000;
    mqtt_flag = 1;
  }
  if(digitalRead(network_voltage_pin) == LOW && mqtt_flag == 1)
  {
    client.publish("UPS/Network_voltage", "true");
    msg_freq = 30000;
    mqtt_flag = 0;
  }

  unsigned long Millis = millis();
  if (Millis - last_msg > msg_freq)
  {
    last_msg = Millis;
    Wire.beginTransmission(PCF8591);
    Wire.write(0x04);
    Wire.endTransmission();
    Wire.requestFrom(PCF8591, 2);
    ain0 = Wire.read(); 
    ain0 = Wire.read();
    ain0 = ain0 * (13.73 / 255);
    battery_voltage = String(ain0);
    battery_voltage.toCharArray(battery_msg, 25);
    client.publish("UPS/battery_voltage", battery_msg);
  }
}

Server_shutdown

JavaScript
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');
        setState('javascript.0.scriptEnabled.common.Server_Halt'/*scriptEnabled common Server Halt*/, false);
    }
  }
});

WakeOnLan.ino

Arduino
/**************************************/
//  https://myhomethings.eu           //
//  Generic ESP8266 module            //
//  Flash size: 1M (no SPIFFS)        //
/**************************************/

#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);

void setup_wifi() 
{
  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);
  
  while (WiFi.status() != WL_CONNECTED) 
  {
    delay(500);
  }
  randomSeed(micros());
}

void setup()
{
  setup_wifi();
  delay(300000);
  
  WOL.setRepeat(3, 100);
  WOL.calculateBroadcastAddress(WiFi.localIP(), WiFi.subnetMask());
  WOL.sendMagicPacket(MACAddress);
}

void loop()
{
  delay(1000);
  ESP.deepSleep(0);
}

Credits

MyHomeThings

MyHomeThings

7 projects • 0 followers

Comments