Help with WeMos D1 Mini Webserver Relay

Hi :slight_smile:

Could someone help me out on this one?

When I send “http://xxx.xxx.xxx.xxx/aklsjdhwuwqeoiuys/openport2” or “http://xxx.xxx.xxx.xxx/aklsjdhwuwqeoiuys/openport1” through the browser the wemos is freezing after 5-10 sec, what am i doing wrong?

The power supply im using i 5V 1500ma with an 10v 1000uf capacitor.

#include <ESP8266WiFi.h>

// defines pins numbers
const char* ssid = "This is my Wi-Fi!";
const char* password = "1123456789";
 
int port1 = D5;
int port2 = D6;

WiFiServer server(80);
 
void setup() {
  Serial.begin(115200);
  delay(10);
 
  pinMode(port1, OUTPUT);
  pinMode(port2, OUTPUT);
  digitalWrite(port1, HIGH);
  digitalWrite(port2, HIGH);
 
  // 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
  Serial.print("Use this URL : ");
  Serial.print("http://");
  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();
 
  // Match the request
 
  if (request.indexOf("/aklsjdhwuwqeoiuys/openport1") != -1) {
    digitalWrite(port1, LOW);
    delay(500);
    digitalWrite(port1, HIGH);
  } 
  else if (request.indexOf("/aklsjdhwuwqeoiuys/openport2") != -1){
    digitalWrite(port2, LOW);
    delay(500);
    digitalWrite(port2, HIGH);
  }
 
  // 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("

");
  client.println("<meta http-equiv=\"refresh\" content=\"0;url=http://zjonesz.net:8000/asdasdjhgqwertyuqwe/\">");
  client.println("</html>");
 
  delay(1);
  Serial.println("Client disconnected");
  Serial.println("");
 
}
int port1 = D5;
int port2 = D6;

Those are pins, not ports. A port is a collection of pins.

what am i doing wrong?

Pissing away resources on the String class:

  String request = client.readStringUntil('\r');

A char array, of fixed size, and a while loop will accomplish the same thing using far fewer resources.

You could also save a lot of SRAM by learning about the F() macro.

You can try fixing the nested quotations in the html text response. They are dumb quotes. Everything inside the outermost double quotes "___" need to use single quotes ' ' instead.