Mechanical Switch to a Smart switch

servo switch

 

Mechanical Switch to a Smart switch

This project is inspired from https://pulsar124.fandom.com/ .

Things required
Switch
NodeMCU
Jumpers
Servo Motors
Power Bank

We see many smart devices in the market. One of them is a smart switching device. Its easy to build one on your own.
Connect Servo Motor with NodeMCU, powerbank is connected with NodeMCU for powering the NodeMCU. Make it a compact box circuit. Now place it near the switch board, the servo should be nearer to the switch which is to be triggered.

Now to make it as an IoT Device you just need to code a bit. It is easier to use the WiFi chip of NodeMCU for this purpose.

Server Motor shaft rotation is based on the pwm signal frequency and duty cycle.


#include <ESP8266WiFi.h>
#include <Servo.h>
Servo servo;

const char* ssid = "Your SSID";
const char* password = "Your Wifi Password";

WiFiServer server(80);

void setup() {
Serial.begin(115200);
delay(10);
servo.attach(2); //Gpio-2 of nodemcu with pwm pin of servo motor
// Connect to WiFi network
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);

WiFi.begin(ssid, password);

while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");

// Start the server
server.begin();
Serial.println("Server started");

// Print the IP address on serial monitor
Serial.print("Use this URL to connect: ");
Serial.print("http://"); //URL IP to be typed in mobile/desktop browser
Serial.print(WiFi.localIP());
Serial.println("/");

}

void loop() {
// Check if a client has connected
WiFiClient client = server.available();
if (!client) {
return;
}

// Wait until the client sends some data
Serial.println("new client");
while(!client.available()){
delay(1);
}

// Read the first line of the request
String request = client.readStringUntil('\r');
Serial.println(request);
client.flush();

int mode = 0;
// Match the request

if (request.indexOf("/Req=0") != -1) {
servo.write(0); //Moving servo to 0 degree
mode=0;
}

if (request.indexOf("/Req=180") != -1) { 
servo.write(0); //Moving servo to 180 degree
mode=180;
}

// Return the response
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println(""); // do not forget this one
client.println("<!DOCTYPE HTML>");
client.println("<html>");
client.println("<h1 align=center>Servo motor control over WiFi</h1><br><br>");
client.print("Current mode = ");
client.print(mode); 
client.println("<br><br>");

client.println("<a href=\"/Req=0\"\"><button>ON</button></a>");
client.println("<a href=\"/Req=180\"\"><button>OFF</button></a><br/>");

client.println("</html>");
delay(1);
Serial.println("Client disonnected");
Serial.println("");

}

 

Thank You https://pulsar124.fandom.com/

SHARE THIS!!!

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.