Hi! im currently doing NodeMCU lproject. this is the very first time im doing NodeMCU project that will control Servo motor. so i have a problem: whenever i ran the code, it works ok and i dont see a problem. the major problem is that whenever i open my serial monitor 115200, i always get a message of forever ". . . . . . . . . . . . . . . .. . ." but it avail no connection that i can join to. before, i manage to to connect and work an Example Code from File> Example>WiFi>WiFi Access point and this code works. i can freely blink my LED on and off but now im recieve no connection on this new code
i've been watching this for 5min straight but it is still same as that. according to the link i saw before it said that my internet connection should be at 2.4GHz but i am conneted to a 2.4GHz WiFi (see picture) but the problem persist.
Hardware: ESP-32 DevKitC ESP32 Nodemcu
Boards:
-NodeMCU-32s
-ESP32s Dev Module
-Node32s
*i tried changing boards to ensure but i see no progress...
my code:
#include <WiFi.h>
#include <ESP32Servo.h>
#include <WiFiClient.h>
#include <WiFiAP.h>
Servo myservo; // create servo object to control a servo
// Servo GPIO pin
static const int servoPin = 18;
// Network credentials
const char* ssid = "yyyyyyy";
const char* password = "xxxxxxx";
// Web server on port 80 (http)
WiFiServer server(80);
// Variable to store the HTTP request
String header;
// Decode HTTP GET value
String valueString = String(5);
int pos1 = 0;
int pos2 = 0;
// Current time
unsigned long currentTime = millis();
// Previous time
unsigned long previousTime = 0;
// Define timeout time in milliseconds (example: 2000ms = 2s)
const long timeoutTime = 2000;
void setup() {
ESP32PWM::allocateTimer(0);
ESP32PWM::allocateTimer(1);
ESP32PWM::allocateTimer(2);
ESP32PWM::allocateTimer(3);
myservo.setPeriodHertz(50);
myservo.attach(servoPin,500, 2400);
Serial.begin(115200);
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.hostname("Est-Host");
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected.");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
server.begin();
}
void loop(){
WiFiClient client = server.available();
if (client) {
currentTime = millis();
previousTime = currentTime;
Serial.println("New Client.");
String currentLine = "";
while (client.connected() && currentTime - previousTime <= timeoutTime) {
currentTime = millis();
if (client.available()) {
char c = client.read();
Serial.write(c);
header += c;
if (c == '\n') {
if (currentLine.length() == 0) {
client.println("HTTP/1.1 200 OK");
client.println("Content-type:text/html");
client.println("Connection: close");
client.println();
client.println("<!DOCTYPE html><html>");
client.println("<head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">");
client.println("<link rel=\"icon\" href=\"data:,\">");
client.println("<style>body { text-align: center; font-family: \"Trebuchet MS\", Arial; margin-left:auto; margin-right:auto; }");
client.println(".slider { -webkit-appearance: none; width: 300px; height: 25px; border-radius: 10px; background: #ffffff; outline: none; opacity: 0.7;-webkit-transition: .2s; transition: opacity .2s;}");
client.println(".slider::-webkit-slider-thumb {-webkit-appearance: none; appearance: none; width: 35px; height: 35px; border-radius: 50%; background: #ff3410; cursor: pointer; }</style>");
client.println("<script src=\"https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js\"></script>");
client.println("</head><body style=\"background-color:#70cfff;\"><h1 style=\"color:#ff3410;\">Servo Control</h1>");
client.println("<h2 style=\"color:#ffffff;\">Position: <span id=\"servoPos\"></span>°</h2>");
client.println("<input type=\"range\" min=\"0\" max=\"180\" class=\"slider\" id=\"servoSlider\" onchange=\"servo(this.value)\" value=\""+valueString+"\"/>");
client.println("<script>var slider = document.getElementById(\"servoSlider\");");
client.println("var servoP = document.getElementById(\"servoPos\"); servoP.innerHTML = slider.value;");
client.println("slider.oninput = function() { slider.value = this.value; servoP.innerHTML = this.value; }");
client.println("$.ajaxSetup({timeout:1000}); function servo(pos) { ");
client.println("$.get(\"/?value=\" + pos + \"&\"); {Connection: close};}</script>");
client.println("</body></html>");
if(header.indexOf("GET /?value=")>=0) {
pos1 = header.indexOf('=');
pos2 = header.indexOf('&');
valueString = header.substring(pos1+1, pos2);
myservo.write(valueString.toInt());
Serial.print("Val =");
Serial.println(valueString);
}
client.println();
break;
} else {
currentLine = "";
}
} else if (c != '\r') {
currentLine += c;
}
}
}
// Clear the header variable
header = "";
// Close the connection
client.stop();
Serial.println("Client disconnected.");
Serial.println("");
}
}