I’m sharing my experience of transforming a USB socket into a WiFi socket. I spent three days reorganizing and testing the source code, and I made some improvements to the app. I believe that with my source code and app, you too can have your own WiFi socket.
One socket (I used a USB socket that comes with two USB charging ports, one two-hole socket, and one five-hole socket. I chose it because it has a large internal space. Choosing the right USB socket is actually not easy because it needs to accommodate all the components. I used the NR-128 model, which costs about 25.3 yuan.)
One 220V to 5V (500mA+) adapter/module (I used an old Kindle adapter that I had lying around, which is 750mA and fits perfectly in this socket. Ordinary modules cost between 3-5 yuan.)
One ESP8266 module (Currently, I recommend purchasing the ESP-12F from Aisino. I used an old GPRS ESP8266 module that I bought a long time ago, and even though their website is down, the module still works. It costs around 12 yuan.)
One 5V to 3.3V module (You can use a 500mA or above 1117, but I prefer DC/DC, and I like to have some current headroom, so I used a Mini-360 adjustable power supply module, set to output 3.3V, priced around 2 yuan.) One relay (module) (For convenience, I used an adjustable level-triggered relay module and set it to high level effective, costing around 4 yuan.)
One USB Type-A male connector (This component is optional; it is used to conveniently draw power from my 5V adapter. If you use a module directly, you can skip this component, which costs about 0.15 yuan.)
My total cost is approximately 43 yuan (excluding the 5V adapter). PS: There are also DIY kits for WiFi sockets available at the rich man’s house, but they only include a three-hole socket, and the relay is not a module, requiring you to build the circuit yourself, which you can also consider. The cost of that is lower than mine. I won’t post the link here to avoid being accused of advertising. (Actually, I don’t have permission to post links)
The circuit is simple. 5V powers the relay, and 3.3V powers the ESP8266. The GPIO13 of the ESP8266 serves as the control signal pin for the relay module. The live wire of the controlled switch connects to the COM port of the relay module, and the output connects to the NO port (which conducts when energized).
First, open the socket to see the circuit board inside the USB charging port.
Remove the circuit board of the USB port. Due to its large size, it’s difficult to use the 5V module without damaging the PCB, so I decided to remove it.
An old Kindle adapter that I had lying around fits perfectly inside.
Using a USB Type-A male connector makes it easy to draw out the 5V.
I initially tried to disassemble the adapter’s casing, but it was very sturdy. So I gave up and placed it directly inside. Surprisingly, the adapter and USB head fit perfectly in that position, staying put. The dimensions were just right.
Set the output voltage of the DC-DC step-down module and confirm it with a multimeter, then seal the adjustable resistor with hot melt glue to avoid touching it during assembly. After that, assemble the circuit according to the circuit diagram.
My design only controls the two-hole socket below the USB port, while the right-side five-hole socket remains always powered.
The copper wires inside are very stiff and heat up easily, making soldering and handling difficult.
All components are in place.
Then reassemble it as it was, perfectly. Write the ID by hand.
Note: If you have small children at home, please be sure to seal the USB port, as there is a risk of electric shock if children poke around with phone charging cables.
I used Arduino IDE to program and flash the firmware of the ESP8266. After installing the ESP8266 library in Arduino IDE, you can program the ESP8266 directly using Arduino IDE, which is very convenient. Below is the source code for the ESP8266. Please retain copyright information when copying and disseminating. Thank you.
1. /*21ic Second Disassembly + DIY Competition
2. Author: simonliu009@21ic
3. QQ: 150739525
4. Please retain copyright information when copying and disseminating. Thank you.
5. */
6.
7. #include <FS.h>
8.
9. #include <ESP8266WiFi.h>
10. #include <EthernetUdp.h>
11.
12. #include <DNSServer.h>
13.
14. #include <SPI.h>
15. // #include <Ethernet.h>
16.
17. #define relay1 13
18.
19. unsigned int localPort = 8267;
20. char packetBuffer[10];
21.
22.
23. const char *ssid = “Asus”;//Change to your desired WiFi SSID
24. const char *password = “esp20170317”;//Your desired WiFi password
25.
26. WiFiUDP Udp;
27.
28.
29.
30. void setup() {
31. // put your setup code here, to run once:
32. Serial1.begin(9600);
33. Serial1.println();
34. pinMode(relay1, OUTPUT);
35. digitalWrite(relay1, LOW);
36.
37.
38. WiFi.mode(WIFI_STA);
39. WiFi.begin(ssid, password);
40.
41.
42. while (WiFi.status() != WL_CONNECTED)
43.
44. {
45. delay(500);
46. Serial1.print(“.”);
47. }//If not connected, send dots to serial…
48.
49.
50. Serial1.println(“Wifi Connected :)”);
51. Serial1.println(“Local Ip Address”);
52. Serial1.println(WiFi.localIP());
53.
54. Udp.begin(localPort); // Start UDP listening
55. Serial1.print(“UDP Port”);
56. Serial1.print(localPort);
57. Serial1.println(“Listening started……”);
58. }
59.
60. void loop()
61. {
62. // if there’s data available, read a packet
63. int packetSize = Udp.parsePacket();
64.
65. if (packetSize)
66. {
67.
68. for (int i = 0; i<=9; i++){
69. packetBuffer[i] = ‘Z’;
70. }
71.
72. Udp.read(packetBuffer, 10); // Read the UDP packet into the buffer
73.
74.
75. if(packetBuffer[0]==’L’&&packetBuffer[1]==’A’&&packetBuffer[2]==’M’&&packetBuffer[3]==’P’&&packetBuffer[4]==’O’&&packetBuffer[5]==’N’)
76. {
77. // delay(parseInt(packetBuffer[]) * 1000);
78. digitalWrite(relay1, HIGH);
79. }
80.
81. if(packetBuffer[0]==’L’&&packetBuffer[1]==’A’&&packetBuffer[2]==’M’&&packetBuffer[3]==’P’&&packetBuffer[4]==’O’&&packetBuffer[5]==’F’&&packetBuffer[6]==’F’)
82. {
83. digitalWrite(relay1, LOW);
84. }
85.
86. }
87.
88. }
89.
90.
91.
92.
Read the Original Article
http://bbs.21ic.com/icview-1699310-1-1.html