Since moving into a rented apartment, there have been significant changes in my living environment. In fact, I am quite satisfied with most of the changes, but whenever I lie in bed playing with my phone at night and feel sleepy, I always feel very annoyed: I am here, and the light switch is over there. There's nothing I can do but get out of bed again to turn off the light, and then crawl back into bed in the dark - by that time, the sleepiness has long disappeared (crying)
So, I came up with the idea of a light switch device - a remote-controlled switch for turning off the lights.
Design Solution#
Since I only wanted to create a simple gadget to free up my hands, I didn't plan to make it too complicated. Considering the design of the switches at home (as shown in the figure below), all I need is a servo motor that can swing left and right when powered on, and provide a certain electrical signal to physically control the up and down movement of the switch.
The solution is very simple, all I need is a microcontroller and a servo motor. Considering the strength required to toggle the switch, if the switch is very stiff, you can consider providing additional power supply to maintain the servo motor's output power. Fortunately, the switches at home are obedient enough (just right, who knows how many times I adjusted the angle and height), so I won't add any additional accessories this time.
Referring to many "light switch" videos on Bilibili, I chose the esp8266 microcontroller, similar to the figure below:
The servo motor used is sg90
:
The total cost is less than 20 yuan...
(Ps: The ready-made switch gadget on Taobao can be sold for a hundred yuan with a different appearance and a few more modules... You can see the profit margin in it. We engineering students can't be fooled into spending money on it~)
The remaining task is to design the control solution.
The most common option is to control it with a mobile app. Here, you need to rely on third-party providers. Writing your own app is also possible, but mainly because I don't want to increase the learning curve (to put it simply, I'm just lazy 😂). The connection methods include Bluetooth and WiFi, which can be chosen based on your development board requirements.
Implementation Details#
Now that the solution is settled, let's move on to the practical part!
Hardware Implementation#
The hardware part is very simple, just make sure to check the pin diagram provided by the seller and insert the wires correctly.
Pay attention to the wire sequence. If you find that the sequence is incorrect, you may need to remove the wire fixer of the servo motor and change the wire sequence yourself. Luckily, I didn't need to do that with the one I bought, I just plugged it in and connected it to the PC.
Software Implementation#
Here, you will need the following software and installation packages:
- Configuration package for the 8266 development board
- Development software for the development board
- Adapter library for the Blink app by blink
- COM serial port driver
Installing Arduino#
First, install Arduino and simply click "Next" during the installation process.
After the installation is complete, open Arduino. The interface should look like this:
Importing the ESP8266 Configuration Package#
First, open the Arduino IDE menu > File > Preferences, and enter the following URL in the "Additional Board Manager URLs" input box: https://www.arduino.cn/package_esp8266com_index.json
Then click on the configuration package in the folder:
Import the configuration.
Installing the Third-Party Library#
Add the library and select blinker-library-0.3.80210803.zip
Configuring the Development Board#
Next, create a new file, save it, and configure the development board information as shown in the figure below:
If you don't have the 8266
development option, as shown in the figure below, search for 8266
and install the corresponding module
Next, you just need to write the code. A template has already been provided.
#define BLINKER_WIFI
#define BLINKER_MIOT_LIGHT
#include <Blinker.h>
#include <Servo.h>
Servo myservo;// Define the servo motor
char auth[] = "xxxxxxxx"; // Light Key
char ssid[] = "SELF-WIFI"; // WiFi name
char pswd[] = "password"; // WiFi password
// Create component objects, make sure the names here match those in the Blink app
BlinkerButton Button1("test");
int counter = 0;
// If you are using Xiao Ai, it listens to this power event
void miotPowerState(const String & state)
{
BLINKER_LOG("need set power state: ", state);
if (state == BLINKER_CMD_ON) {
myservo.write(150);// Rotate the servo motor 150 degrees when receiving the "on" command
BlinkerMIOT.powerState("on");
BlinkerMIOT.print();// Feedback the state
delay(1000);// Delay for 1 second
myservo.write(90);// Servo motor returns to zero, back to the vertical state
}
else if (state == BLINKER_CMD_OFF) {
myservo.write(30); // Servo motor rotates 30°
BlinkerMIOT.powerState("off");
BlinkerMIOT.print();
delay(1000);
myservo.write(90);
}
}
// This function will be executed when the button is pressed
void button1_callback(const String & state)
{
BLINKER_LOG("get button state: ", state);
if (state=="on")
{
myservo.write(170);// Rotate the servo motor 150 degrees when receiving the "on" command
delay(1000);// Delay
myservo.write(90);// Servo motor returns to zero (90 degrees)
} else if(state=="press"||state=="tap")
{
myservo.write(10);// Rotate the servo motor 10 degrees after long-pressing the switch button
delay(1000);// Delay
myservo.write(90);// Servo motor returns to zero (90 degrees)
}
}
// If an unbound component is triggered, the content inside it will be executed
void dataRead(const String & data)
{
BLINKER_LOG("Blinker readString: ", data);
counter++;
}
void setup()
{
// Initialize the serial port
Serial.begin(115200);
BLINKER_DEBUG.stream(Serial);
// Initialize the servo motor
myservo.attach(2);// The IO port of the servo motor, D4 port of the nodemcu
myservo.write(90);// The servo motor returns to zero (vertical) when powered on
BlinkerMIOT.attachPowerState(miotPowerState);
// Initialize Blinker
Blinker.begin(auth, ssid, pswd);
Blinker.attachData(dataRead);
Button1.attach(button1_callback);
}
void loop() {
BLINKER_LOG("Blinked running...");
Blinker.run();
}
After that, simply click on the "Verify" button in the upper left corner to check for any errors.
If there are no errors, the next step is to upload the program to the development board. First, pay attention to the port in the figure below. After installing the COM port driver, the port should be displayed correctly.
If it doesn't, check the Device Manager to see if the port is displayed correctly.
Installing the App#
Remember the key
in the code? Now it's time to download the Blink app and apply for a key
.
It can usually be found in the app store, but if not, you can download it from the official website.
Registering the Device#
After downloading and registering a Blink account, click on the plus sign in the upper right corner -> Standalone Device -> Network Access
Aliyun is usually recommended for network access, but in my current version, only Blink service is available. In practice, it doesn't matter whether it's for control or integration with Xiao Ai. It's up to you to choose.
After completing the device registration, as shown in the figure above, you will get a key
that you can fill in the code.
Development Interface#
Now you can create your own switch interface according to the instructions in the code. I found a simple interface configuration file online.
{¨config¨{¨headerColor¨¨transparent¨¨headerStyle¨¨dark¨¨background¨{¨img¨¨assets/img/headerbg.jpg¨¨isFull¨«}}¨dashboard¨|{¨type¨¨btn¨¨ico¨¨fad fa-lightbulb-on¨¨mode¨Ê¨t0¨¨开灯¨¨t1¨¨文本2¨¨bg¨Ì¨cols¨Í¨rows¨Í¨key¨¨test¨´x´Ë´y´Ì¨speech¨|÷¨cus¨¨on¨¨lstyle¨Ë¨clr¨¨#076EEF¨}{ßAßBßC¨fad fa-lightbulb¨ßEÉßF¨关灯¨ßHßIßJÌßKÍßLÍßMßN´x´Ë´y´ÑßO|÷ßRËßSßT}{ßA¨deb¨ßEÉßJÉßKÑßLÌßM¨debug¨´x´É´y´¤D}÷¨actions¨|¦¨cmd¨¦¨switch¨‡¨text¨‡ßQ¨打开?name¨¨off¨¨关闭?name¨—÷¨triggers¨|{¨source¨ßa¨source_zh¨¨开关状态¨¨state¨|ßQßd÷¨state_zh¨|¨打开¨¨关闭¨÷}÷}
Import this file into the configuration information. (The garbled text above is real (sweat))
After importing, the configuration should look like this:
Now you can control and check the configuration by yourself.
Integration with Xiao Ai#
Integrating with Xiao Ai is actually very simple, it depends on whether the third-party service provider supports it or not.
Mi Home -> Add Other Devices -> Blink -> Sync Configuration
Device Installation#
This step is actually the most difficult in practice because everyone's situation is different, and the strength of the button is also different. At first, I was very worried that the servo motor's power might not be enough to toggle the switch. In reality, after trying many angles, I still couldn't achieve it successfully.
I have tried extending the servo motor's fan blade to increase the contact area, but none of these solutions worked well in the end. Finally, I tried to go back to the starting point and raised the servo motor to create a certain gap between the fan blade and the switch. This way, when the fan blade rotates, there will be an inertia to assist in applying force. Then I fixed the servo motor with cardboard and 502 adhesive, and used transparent tape for secondary reinforcement on the outer layer. Finally, I managed to successfully toggle the switch (although sometimes it is still not very sensitive).
The final implementation is shown in the following image:
Results#
Here is a link to see the final results.