In this tutorial NodeMCU, How to control Servo motor remotely in a WIFI network using a local web page connected in the same network.
Refer Previous Post for Controlling servo using Web slider.
After that, connect the servo motor with the Arduino. Make the connections of the servo motor with the Arduino as follows:
Black wire of servo motor to the GND pin of NodeMCU
Red wire of servo motor to the 5V pin of NodeMCU
Yellow wire of servo motor to the Gpio 5 of NodeMCU
Installing the ESP8266_Arduino_Servo_Library
The ESP32 Arduino Servo Library makes it easier to control a servo motor with your ESP8266, using the Arduino IDE. Follow the next steps to install the library in your Arduino IDE:
Download the ESP32_Arduino_Servo_Library. You should have a .zip folder in your Downloads folder, Unzip the .zip folder and you should get ESP32-Arduino-Servo-Library.
After installing the library, go to your Arduino IDE. Make sure you have the Nodemcu 1.0 ESP-12E board selected, and then, Copy and Paste code in Arduino IDE.
#include <ESP8266WiFi.h>
#include <Servo.h>
Servo servo;
const char* ssid = "SSID"; //YOUR SSID
const char* password = "PW"; //YOUR WIFI PSSWORD
WiFiServer server(80);
void setup() {
Serial.begin(115200);
delay(10);
servo.attach(5); //D1 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 value = 0;
// Match the request
if (request.indexOf("/Req=0") != -1) {
servo.write(0); //Moving servo to 0 degree
value=0;
}
if (request.indexOf("/Req=90") != -1) {
servo.write(90); //Moving servo to 90 degree
value=90;
}
if (request.indexOf("/Req=180") != -1) {
servo.write(180); //Moving servo to 180 degree
value=180;
}
Serial.print("Servo Angle:");
Serial.println(value);
// Return the response
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println(""); //
client.println("<!DOCTYPE HTML>");
client.println("<html>");
client.println("<h1 align=center>Node MCU ESP8266 Wifi Controlled Servo Motor</h1><br><br>");
client.print("Servo Current angle = ");
client.print(value);
client.println("<br><br>");
client.println("<a href=\"/Req=0\"\"><button>Servo angle Set to = 0 degree</button></a>");
client.println("<a href=\"/Req=90\"\"><button>Servo angle Set to= 90 degree</button></a>");
client.println("<a href=\"/Req=180\"\"><button>Servo angle Set to= 180 degree</button></a><br/>");
client.println("</html>");
delay(15);
Serial.println("Client disonnected");
Serial.println("");
}
Upload the Code
Now, upload the code to your ESP8266. Make sure you have the right board and COM port selected.
After uploading, open the Serial Monitor at a baud rate of 115200. Press the ESP8266 reset button. The ESP8266 IP address should be printed in the serial monitor.
留言