← Back to Projects
Arduino Microcontroller
A Microcontroller can be attached electronically to a motor and be programmed to recieve a wireless input via wifi to trigger the motor and turn a kettle on.
#include
#include
const char* ssid = "";
const char* password = "";
WiFiServer server(80); // HTTP server on port 80
const int servoPin = 13; // Pin where LED is connected
Servo myServo;
void setup() {
Serial.begin(115200);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("\nConnected to Wi-Fi");
Serial.print("IP Address: ");
Serial.println(WiFi.localIP());
myServo.attach(servoPin); // Use global myServo
myServo.write(0); // Start at 0 degrees
server.begin(); // Start web server
}
void loop() {
WiFiClient client = server.available(); // Listen for web browser
if (client) {
Serial.println("New Client");
// Wait until request is fully received
String request = "";
while (client.connected() && client.available() == 0) {
delay(1);
}
while (client.available()) {
char c = client.read();
request += c;
}
Serial.println(request); // Show full HTTP request
// Handle commands
if (request.indexOf("GET /on") >= 0) {
myServo.write(90); // Rotate 90 degrees
Serial.println("Servo moved to 90°");
delay(2000);
myServo.write(0); // Back to 0 degrees
Serial.println("Servo moved to 0°");
}
// Send response page
client.println("HTTP/1.1 200 OK");
client.println("Content-type: text/html");
client.println();
client.println(R"rawliteral(
ESP32 Servo Control
)rawliteral");
}
}