LwOaid8s

ESP8266 Low Power 4-Position Socket_pr

 
Overview
   With too many devices like printers, the standby power consumption is high, and they are not compatible with various electrical systems, making them inconvenient to use. Previously, I used a K-series smart socket to connect to a power strip and set timers for all devices. Recently, I started playing with the ESP8266 and learning Arduino, which inspired me to create my own DIY smart socket project.
     Currently implemented features include: 1. Low power consumption design: using four magnetically latched relays, the relay coils are energized only when switching on/off; otherwise, only the ESP8266 consumes power in standby mode. A USB ammeter test showed a total standby current of 0.08A for the entire PCB and 0.15A for a single relay operation.
                             2. In offline mode, the four sockets can be independently controlled by buttons, with corresponding LEDs indicating on/off status.
                             3. Utilizes MQTT protocol to connect to Home Assistant, allowing control of each switch via the Home Assistant web interface or mobile client. The socket will also provide feedback on the status of the button switches. Pressing any button sends the socket's LAN IP address to the MQTT subscription topic.
                             4. Utilizes the ESP8266 webserver service, enabling web-based switch operation (also supports POST parameter control, allowing integration with other automatic control devices or programs).
                             5. Uses the ESP8266 EEPROM to store switch states. After restarting and powering on, the status of each switch can be individually set to off/on/retain the last state (via web configuration).
                             6. The web interface allows setting 4 switch names.
                             7. Pressing and holding the third button (button 1 on the power cord end) resets the EEPROM parameters.
                             8. WIFI account and password configuration: While the WiFiManager Chinese library was used for web configuration, occasional program crashes occurred, so it was changed to web page configuration. Please complete the code yourself if needed.
                             9. Web configuration and built-in timer switch functions are under development; please stay tuned.
Note: The socket has 220V AC power; non-professionals should not disassemble or modify it! This article is for demonstration purposes only. Please be aware of safety precautions if you attempt to replicate it!
This open-source project follows the GPL 3.0 open-source license. However, if you intend to use it commercially, please donate 20% of the net profit to a charity and make the entire donation order public. If you cannot do this, please do not use it commercially.
 
 
 
 
 
 
 
The
 
 
 
Home Assistant web interface supports socket status feedback
 
 
 
. For firmware upgrades, directly upload the bin file via the web to upgrade
 
 
 

the Home Assistant configuration file 
. Add "switch: !include switches.yaml" to the configuration.yaml file.
The switches.yaml file is as follows: This is for one socket. For multiple sockets, change the device name and subscription topic
: platform: mqtt
name: "GN-B3043-1F773D-K1" # Device name, set manually
state_topic: "GN-B3043-1F773D/state/K1" # "GN-B3043-1F773D" MQTT subscription topic, i.e., the socket's device name, can be set manually via the webpage. Setting it to Auto will automatically generate
the command_topic using the MAC address. "GN-B3043-1F773D/set/K1"
unique_id: "32b33778a2614be0a3290f81afdeaba9" #Entity ID, any, just unique. If not set, Home Assistant will prompt that there is no unique entity ID. payload_on: "1"
payload_off: "0"
state_on: "1"
state_off: "0"
optimistic: false
qos: 0
retain: false
icon: mdi:monitor
- platform: mqtt
name: "GN-B3043-1F773D-K2"
state_topic: "GN-B3043-1F773D/state/K2"
command_topic: "GN-B3043-1F773D/set/K2"
unique_id: "32b33778a2614be0a3290f81afdeabaa"
payload_on: "1"
payload_off: "0"
state_on: "1"
state_off: "0"
optimistic: false
qos: 0
retain: false
- platform: mqtt
name: "GN-B3043-1F773D-K3"
state_topic: "GN-B3043-1F773D/state/K3"
command_topic: "GN-B3043-1F773D/set/K3"
unique_id: "32b33778a2614be0a3290f81afdeabab"
payload_on: "1"
payload_off: "0"
state_on: "1"
state_off: "0"
optimistic: false
qos: 0
retain: false
icon: mdi:television-classic

 

- platform:mqtt
name: "GN-B3043-1F773D-K4"
state_topic: "GN-B3043-1F773D/state/K4"
command_topic: "GN-B3043-1F773D/set/K4"
unique_id: "32b33778a2614be0a3290f81afdeabac"
payload_on: "1"
payload_off: "0"
state_on: "1"
state_off: "0"
optimistic: false
qos: 0
retain: false
Program source code EPS8266_4chazuo__595_V1.8_WEB_202205012_OTA__.rar
#include
#include
#include
#include
#include
#include
#include
Ticker

 


 

flipper;//Define timer

 


 

//#define PIN_POWER 4 //Indicator light located at GPIO4
#define PIN_LED 13 //Indicator light located at GPIO2
#define KEY1 0 //KEY corresponds to GPI14
#define KEY2 2 //KEY corresponds to GPI14
#define KEY3 4 //KEY corresponds to GPI14
#define KEY4 5 //KEY corresponds to GPI14

 


 


 


 

int dspin = 16; // Pin 14 of 74HC595, data input pin SI.
int clkpin = 12; // Pin 11 of 74HC595, clock line SCK.
int latpin = 14; // Pin 12 of 74HC595, output memory latch line RCK.

 

String AutoRigName = "";
// Device name (if "Auto", it will be automatically generated using MAC).
String RIG_IDENTIFIER; // EEPROM address 120.

 

String K1_name = "Switch 1"; // Switch 1 name, EEPROM address 200.
String K2_name = "Switch 2"; // Switch 2 name, EEPROM address 220.
String K3_name = "Switch 3"; // Switch 3 name, EEPROM address 240.
String K4_name = "Switch 4"; // Switch 4 name, EEPROM address 260.

 


 

const char ssid[] = "*****"; // WiFi name, EEPROM. Address 140
const char pass[] = "****"; //WiFi password EEPROM Address 160

 


 


 


 

ADC_MODE(ADC_VCC);//Set ADC mode to read system voltage

 

int wifi_flg = 0;//WiFi status flags 0 Not connected 1 Connecting 2 Network configuration 3 WiFi connected 4 MQTT connected
int power_time = 0;//Countdown to power off

 

int K1_flg;//Define switch 1 status flag EEPROM Address 101
int K2_flg;//Define switch 1 status flag EEPROM Address 102
int K3_flg;//Define switch 1 status flag EEPROM Address 103
int K4_flg;//Define switch 1 status flag EEPROM Address 104

 


 

int O595_1_flg = 255;//Define 595 1 flag
int O595_2_flg = 255;//Define 595 1 flag
int K1_ACflag = 2; // EEPROM address 181
int K2_ACflag = 2; // EEPROM address 182
int K3_ACflag = 2; // EEPROM address 183
int K4_ACflag = 2; // EEPROM address 184

 

uint32_t t=0;//Key timing

 


 


 

WiFiClient espClient;
PubSubClient client(espClient);

 

const char* mqtt_server = "*******.f3322.org";//mqtt service address
const char* clientID ="8266-001"; // Device ID

 

//WiFiUDP Udp;//Instantiate WiFiUDP object
//unsigned int localUdpPort = 4321; // Custom local listening port
//unsigned int remoteUdpPort = 8629; // Custom remote listening port
//char incomingPacket[255]; // Save the message sent by the UDP tool
//char replyPacket[] = "Hi, this is esp8266
"; // The message sent only supports English

 


 

void callback(char *topic, byte* payload, unsigned int length); // Callback function declaration, used to pass the MQTT client constructor as a parameter
//void write_String(int a,String str); // Write string to EEPROM

 

// Parameters: MQTT server address, port number, callback function name, connection (WIFI)
PubSubClient mqttClient(mqtt_server, 1833, callback,espClient);
ESP8266WebServer server(80);
ESP8266HTTPUpdateServer httpUpdater;

 

// Variable constant definition:
String postForms()
{
String htmlCode = "
";
htmlCode += "
";
htmlCode += "
";
htmlCode += "
";
htmlCode += "
";
htmlCode += "
";
htmlCode += "
"; htmlCode += " ";
htmlCode += "
";
htmlCode += "
GN-B3034 Smart Socket Control Page

 


";
htmlCode += "
Please enter 0 or 1 to control the switch

 


";
htmlCode += "
htmlCode += " 0 is off, 1 is on

";
htmlCode += "Switch 1
htmlCode += K1_flg;
htmlCode += "" size="5" initial-scale = 2.0 autocomplete="off">(";
htmlCode += K1_name;
htmlCode += ")

";
htmlCode += "Switch 2
htmlCode += K2_flg;
htmlCode += "" size="5" initial-scale = 2.0 autocomplete="off">(";
htmlCode += K2_name;
htmlCode += ")

";
htmlCode += "switch 3
htmlCode += K3_flg;
htmlCode += "" size="5" initial-scale = 2.0 autocomplete="off">(";
htmlCode += K3_name;
htmlCode += ")

";
htmlCode += "switch 4
htmlCode += K4_flg
; += "" size="5" initial-scale = 2.0 autocomplete="off">(";
htmlCode += K4_name;
htmlCode += ")

";
htmlCode += "

";
htmlCode += "
System parameter settings

";
htmlCode += "
htmlCode += "Network name
" ;
htmlCode += " " ; htmlCode += " "; htmlCode += "Network Password "; htmlCode += " "; htmlCode += " htmlCode += "Device Name htmlCode += read_String(120); htmlCode += "" size="20" initial-scale = 2.0 autocomplete="off"> "; htmlCode += "(Automatically generated by the system when set to Auto) "; htmlCode += " Power-on switch status (Off/On/Remain last) "; htmlCode += " Switch 1 | Switch 2 | Switch 3 | Switch 4 "; htmlCode += " htmlCode += " "; htmlCode += " "; htmlCode += "



























 







 

参考设计图片
×
 
 
Search Datasheet?

Supported by EEWorld Datasheet

Forum More
Update:2026-03-26 19:13:38

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
community

Robot
development
community

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号