Use this forum to chat about hardware specific topics for the ESP8266 (peripherals, memory, clocks, JTAG, programming)

User avatar
By Dolbowent
#96325 My code works and it does operate but after a few seconds or if I give it to many changes to quick it disconnect. If I connect it and do nothing it stay a little longer then disconnect.

Iv tried every thing, been working on code variations and spent 40 hours on this so far. I'm trying to do this for a school project and I am running out of time. Can you please please help me with this.

Here is my code:
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>

// Replace with your network credentials
const char* ssid = "tryit";
const char* password = "12345678";

// Create an instance of the server
ESP8266WebServer server(80);

// Define the PWM pin and initial duty cycle
const int pwmPin = D1;
int pwmDuty = 0;

void handleRoot() {
// HTML web page
String html = "<html><head><title>ESP8266 PWM Control</title></head><body>";
html += "<h1>ESP8266 PWM Control</h1>";
html += "<p>Duty cycle: " + String(pwmDuty) + "</p>";
html += "<form action='/dutycycle' method='POST'><input type='range' name='dutycycle' min='0' max='1023' value='" + String(pwmDuty) + "'><br><br>";
html += "<input type='submit' value='Set Duty Cycle'></form><br><br>";
html += "<form action='/fullon' method='POST'><input type='submit' value='Full On'></form>";
html += "</body></html>";

server.send(200, "text/html", html);
}

void handleDutyCycle() {
if (server.hasArg("dutycycle")) {
String dutycycleStr = server.arg("dutycycle");
pwmDuty = dutycycleStr.toInt();
analogWrite(pwmPin, pwmDuty);
Serial.println("PWM duty cycle set to " + String(pwmDuty));
}
server.sendHeader("Location", "/");
server.send(303);
}

void handleFullOn() {
pwmDuty = 1023;
analogWrite(pwmPin, pwmDuty);
Serial.println("PWM duty cycle set to " + String(pwmDuty));
server.sendHeader("Location", "/");
server.send(303);
}

void setup() {
// Start serial communication
Serial.begin(115200);

// Connect to Wi-Fi network
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.mode(WIFI_AP);
WiFi.softAP(ssid, password);
IPAddress myIP = WiFi.softAPIP();
Serial.print("AP IP address: ");
Serial.println(myIP);

// Initialize the PWM pin as output
pinMode(pwmPin, OUTPUT);
analogWrite(pwmPin, pwmDuty);

// Route requests
server.on("/", handleRoot);
server.on("/dutycycle", HTTP_POST, handleDutyCycle);
server.on("/fullon", HTTP_POST, handleFullOn);

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

void loop() {
// Listen for incoming requests
server.handleClient();
delay(10);
}
User avatar
By rooppoorali
#96342 Increase the delay time in the loop function from 10ms to a higher value, such as 100ms or 500ms. This will reduce the number of times the server handles incoming requests and may help stabilize the connection.

Add a Serial.print statement to the handleClient function to check for any error messages that may indicate what's causing the connection to drop.