Search code examples
c++serverarduinoclientesp32

After assigning static IP to my ESP32 server doesn't respond anymore


So I've got ESP32 acting as a server, and when calling http://IP_OF_ESP32:7777/SOMETEXT my code enables me to view the text written after the slash, the problem is that after assigning static IP to my ESP32 it doesn't work anymore here's my code

#include <WiFi.h>

const char* ssid = "Inovec1";
const char* password =  "ccb255fd8f52";

WiFiServer server(7777);

IPAddress local_IP(192, 168, 121, 100);
IPAddress gateway(192, 168, 1, 1);
IPAddress subnet(255, 255, 0, 0);
IPAddress primaryDNS(8, 8, 8, 8);
IPAddress secondaryDNS(8, 8, 4, 4);

void setup() {
  Serial.begin(115200);
  WiFi.config(local_IP,gateway,subnet,primaryDNS, secondaryDNS);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(100);
    Serial.print(".");
  }
  Serial.println(WiFi.localIP());
  server.begin();
}

void loop() {
  WiFiClient client = server.available();
  String message;
  while(client.available()){
    char c = client.read();
    message += c;
  }
  String command = getCommand(message);
  if(command.length()>0)
    Serial.println(command);
}

String getCommand(String s){
  String toFind1 = "GET /";
  String toFind2 = " HTTP";
  int start = s.indexOf(toFind1)+toFind1.length();
  int end = s.indexOf(toFind2);
  return s.substring(start,end);
}



Solution

  • So as a first try give your ESP a IP address within the address range of your PC.

    IPAddress local_IP(192, 168, 0, 10);
    

    If that works ( I assume there is no other device with that address) your problem is not the firmware/program but your routing. (Routing, Subnets and so on is a topic on its own)