For a practical example, I would like to make a device that automatically turns on the room light by pressing a light switch. I will combine Arduino + servo motor + 3D printer + Wi-Fi module to make it happen!
Note: the auto light switch we are about to make only works for a buttoned light switch system.
Let’s start by 3D printing the parts that will press the light switch button.
The servo motor used this time is MG996R, which has a stronger torque than the one used in the previous series. This servo motor has a torque of 9.4 kg and an operating voltage of 4.8 – 6.6 V. We’ll also be using ESP-WROOM-02 Wifi and try to operate the switch by moving the servo motor from PC or smartphone.
Figure 9. Parts for pressing switches
Figure 10. Outputting with 3D printer
Figure 11. Room switch
Figure 12. Installation of 3D parts on servo motor
Figure 13. Mounting on the room light switch
I used a strip tape to attach the device to the light switch.
Figure 14. Strip tape
When you are done, test to see if the servo motor works correctly and it turns on the light.
Does it work? Good! So as you’ve already realized, the switch that we just made can only turn on the light. This type of switch cannot, unfortunately, turn the light off and also turn the light back on. I want our switch to be able to press both ends so that it can be turned on and off automatically.
Figure 15. Measuring switch size
Let’s measure the width of the room light switch. It’s about 4 cm for my switch. I thought it would be better to create protruding parts onto the supporter part to press the light switch button on both sides. So, I 3D printed the parts as shown below.
Figure 16. Parts that can press both ends of switch button
Figure 17. Mounting onto servo motor
Once it’s mounted onto the servo motor, we will write a program to ESP-WROOM-02 so that we can operate the switch remotely.
Figure 18. Final remote light switch setup
This mechanical system is connected to the wireless router installed in the room, and based on the acquired IP address, we can provide a function that can operate the servo motor.
When ESP-WROOM-02 is powered on, we can set up a reporting server to hit the external API for relaying the acquired IP address by e-mail after getting the IP address from the wireless router.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 |
#include <ESP8266WiFi.h> #include <Servo.h> const char *ssid = "[wifi_ssid]"; const char *password = "[wifi_password]"; //IP report server configuration const char* host = "xxxx.xx.xxx"; String url = "/xxxx/"; Servo myservo; Servo myservo2; WiFiServer server(80); String IpAddress2String(const IPAddress& ipAddress) { return String(ipAddress[0]) + String(".") +\ String(ipAddress[1]) + String(".") +\ String(ipAddress[2]) + String(".") +\ String(ipAddress[3]) ; } void setup() { Serial.begin(115200); delay(10); Serial.println("Start!"); Serial.println(); Serial.print("Connecting to "); Serial.println(ssid); WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(1500); Serial.print("."); } Serial.println(""); Serial.println("WiFi connected"); server.begin(); Serial.println("Server started"); Serial.println(WiFi.localIP()); //********************************************************* // IP address notification //********************************************************* WiFiClient client; const int httpPort = 80; if (!client.connect(host, httpPort)) { Serial.println("connection failed"); return; } url += IpAddress2String(WiFi.localIP()); client.print(String("GET ") + url + " HTTP/1.1\r\n" + "Host: " + host + "\r\n" + "Connection: close\r\n\r\n"); delay(1000); Serial.print("Connecting url to "); Serial.println(url); //********************************************************************* } void loop() { WiFiClient client = server.available(); if (!client) { return; } Serial.println("new client"); while(!client.available()){ delay(1); } String req = client.readStringUntil('\r'); Serial.println(req); client.flush(); // Match the request int selectServo = 0; int posSlash0 = req.indexOf("/"); String tmp = req.substring(0,posSlash0); int posSlash1st = req.indexOf("/",1+posSlash0); String action = req.substring(1+posSlash0,posSlash1st); int posSlash2nd = req.indexOf("/",1+posSlash1st); String sel = req.substring(1+posSlash1st,posSlash2nd); int posSlash3rd = req.indexOf("/",1+posSlash2nd); String valueTmp = req.substring(1+posSlash2nd,posSlash3rd); int posSpace = valueTmp.indexOf(" "); String value = valueTmp.substring(0,posSpace); if(action == "favicon.ico HTTP"){ return; } //GET /servo/1/60 HTTP/1.1 Serial.print("POS:"); Serial.print(posSlash1st); Serial.print(":"); Serial.print(posSlash2nd); Serial.print(":"); Serial.println(posSlash3rd); Serial.print("ACTION:"); Serial.println(action); Serial.print("SELECT SERVO:"); Serial.println(sel); Serial.print("VALUE:"); Serial.println(value); if(sel == "0"){ myservo.write(value.toInt()); } client.flush(); String s = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n<!DOCTYPE HTML>\r\n<html>"; s += "<head><meta name='viewport' content='width=device-width, initial-scale=1'>"; s += "<link href='https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css' rel='stylesheet' media='screen'>"; s += "<script type='text/javascript' src='https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js'></script>"; s += "</head><body>"; s += "\r\nSERVO "; s += sel; s += " is "; s += String(value); s += "<div class='container'><div class='row'>"; s += "<div class='col-md-12'><h3>Servo0</h3></div>"; s += "<div class='col-md-12'><a class='btn btn-primary' href='/servo/0/0'>ON</a></div>\n"; s += "<div class='col-md-12'><a class='btn btn-info' href='/servo/0/50'>OFF</a></div>\n"; s += "<div class='col-md-12'><a class='btn btn-info' href='/servo/0/30'>STAY</a></div>\n"; s += "</div></div>"; s += "</body>"; s += "</html>\n"; client.print(s); delay(1); Serial.println("Client disonnected"); } |
As shown in the video above, I was able to switch the light ON / OFF from my PC.
Today we created a servo-controlled light switch that can be controlled wirelessly and remotely. There are many uses for these type of devices. Next time, we’re going to get a little more creative and combine multiple servo motors to make a camera stabilizer or gimbal. So stay tuned!
Related articles:
Use Arduino to Control a Motor Part 1 – Motor Basics
Use Arduino to Control a Motor Part 3 – Making an RC Car Using a Servo Motor for the Steering
Using Arduino with Parts and Sensors – Stepper Motor Part 1