Can't ping esp32 cam hostname

Hi everyone I am working with the esp32CAM (AI THINKER model) and I'm following this tutorial esp32 hostname change, not sure why I can't ping the new hostname. I've seen comments mentioning routers don't allow this kind of communication between devices Router problem I have the same router; however it is possible to ping a raspberry inside that same network, I also have tried using my phone's hotspot and own data without luck, any ideas on what's going on here?

Here is the code from the first link:

#include <WiFi.h>
 
// Replace with your own network credentials

const char* RED[] = {"****","****"};
const char* PAS[] = {"****","****"};
const char* MyHostName = "testcam";

const char* ssid = RED[0];
const char* password = PAS[0];

 
void setup(){
    Serial.begin(115200);
    WiFi.setHostname(MyHostName);
    WiFi.mode(WIFI_STA);
    WiFi.begin(ssid, password);
    Serial.println("\nConnecting to WiFi Network ..");
    // Wait until connection is established
    while(WiFi.status() != WL_CONNECTED){
        Serial.print(".");
        delay(100);
    }
    // Print ESP32's IP & HostName
    Serial.println("\nConnected to the WiFi network");
    Serial.print("Local ESP32 IP: ");
    Serial.println(WiFi.localIP());
    Serial.print("ESP32 HostName: ");
    Serial.println(WiFi.getHostname());
}
 
void loop(){
    // Do Nothing
}

First, the page you linked us has some pictures not showing because they're strangely referenced and my browser won't load them.
Anyway, one thing is the "hostname discovery" another thing is letting your PC convert the name into the real IP address when pinging or referring to it for other things. That requires the PC connected to a DNS dynamically, so the question is: are you sure your PC has the router as its primary DNS server? And if it is, does the ping just say "unreachable"? Could you post here some kinda screenshot to show us your test?

Thanks for your response, here is the screenshot of the command line


and a screenshot of the arduino Serial monitor that shows the "original" hostname
SM
, its in spanish but it translates to: The ping request couldn't find the host esp32-949B54. Check the name and try again.

What I'm attempting to do is a web service with Flask and the esp32cam, however I need to know beforehand the ip's to make requests between the server and the camera, a colleague recommended me to try pinging the hostname of the esp32 to get its ip.

So I still can't ping the hostname using the Wifi library; however following this tutorials esp32 mdns about mDNS and installing external libraries, the ping works fine barebones.

#include <ESPmDNS.h>
#include <WiFi.h>
#include <ESPAsyncWebServer.h>
  
const char* ssid = "****";
const char* password =  "****";
  
  
void setup(){
  Serial.begin(115200);
  
  WiFi.begin(ssid, password);
  
  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.println("Connecting to WiFi..");
  }
 
  if(!MDNS.begin("camara")) { // your custom hostname
     Serial.println("Error starting mDNS");
     return;
  }
  
  Serial.println(WiFi.localIP());
  Serial.println(WiFi.getHostname());
  
}
  
void loop(){}

I still have to try out if the camera webserver still works. Anyways the question still remains. Why can't you ping the hostname with Wifi library?

probably a windows issue, find myself having to implement the host file to make things work sometimes..
host file only works if you can use static ips, which you should be able to do, might have to add an exception into your dhcp server so it knows not to assign it..
host file is at c:\windows\systems32\drivers\ect , hint: win10 and above have to change permissions on the file or you won't be able to save your changes..

been working with my cams too..
just released my latest build..
ESP32 Cam Server..
A small windows server app, cams connect to it..
Still a work in progress, maybe gives you some ides..

good luck, have fiun.. ~q

This is clearly a DNS configuration issue. To convert a name into its IP address the PC look first on your "etc\hosts" file then queries the DNS server(s) you configured on your PC. So, what is your local network setting and which DNS server(s) you set? For instance, if you set Google (8.8.8.8) as DNS server it won't work because it's only your router that "knows" such hostname. Meaning you must set your router IP address as primary DNS (and google as secondary).
To investigate this possible issue, open again a command prompt and run the two commands "ifconfig" and then "nslookup esp32-949B54" and see if the DNS is your router's IP address (I suppose not). If in doubt, copy/paste here both results and I'll try helping you.

Just to complete the picture, if possible please tell us why you need to ping it and/or change the hostname, and from which device (a PC, another Arduino, a smartphione, all of them, what?). Maybe a different solution could apply.

Thanks for your answer, I will check your work as soon as I have some more free time! In the end I don't really know what happened, The Wifi library was odd because it has a method to "change" the hostname and from my illiterate understanding the esp32 requests that hostname to the router (in my case my phone's hotspot), apparently it worked whenever I checked my connected devices to the hotspot it was possible to read the custom hostname however whenever I wanted to ping the device to obtain the device's ip it would never find it.

On the other hand when using the mDNS libraries I was able to ping the custom hostname and that did the job for me.

Hi and thanks for your comment, I'm sorry for the very late response, but if you are still willing to help here is my response, thanks in advance.

If I'm honest I don't really know, my PC has DHCP enabled, so that means I'm not messing around with static ips (because I haven't been able to understand/achieve setting a static ip and still get access to internet), I'm guessing my computer has the default DNS which should be the network ip for broadcast, something like "192.168.x.1".

Right now I don't have the esp32 cam with me but I can definetly follow this steps later tomorrow.

I wanted to ping the esp32cam from my raspberry pi, so every time I could get its latest ip (no matter if the router/network set new ip's), in some way I was avoiding setting static ip's as mentioned before because my lack of experience and because somewhere I read (I can't remember the website) that setting static ip for the esp32cam would cause problems with it's own server (didn't bother testing out if this was true or not because compiling code for this board would take around 2 minutes between each execution) .

I am also planning on having two different networks (independent networks) but that's a plan for the near/far future. And I believe hostnames would be the easiest way to actually know each other's devices ips to establish communication between each other.

Well, if you can't (don't want to) set a static IP, you could solve that by saying to your router DHCP it must assign the same local IP address to the device (anyone or to that specific, identified by the MAC address). It's an option you can set on the router, called "lease time".

If not, IMHO the simplest, general, and best solution for a device discovery (aka I don't know the IP address of the party I need to connect to, because it's dynamic and can change anytime it starts) is using UDP broadcasts. I'm trying now to briefly describe you below such method.

When the server (or any device) needs to "discovery" who's online and which IP address has been assigned, sends an UDP broadcast message ("broadcast" is a packet sent to all the local network over a specific port): any client device listening on that port will/should reply with another UDP message directly to the caller, not another broadcast (the packet includes the sender's IP address, ad any further information you could need to send to clients, like the ID of the server or configuration info, or anything needed in this process).
As soon as the server receives the client reply, the packet includes the IP address together with any further information the client needs to send to the server (like client name, ID, and/or anything else useful).

HTH

esphost


Theses are the results of the commands, on the rasp it didn't even work and for windows it couldn't find the esp

I haven't looked it up yet, but are there any special libraries to do so? And I'm guessing I would have to code it onto the esp32 and also the rasp to get their respective "handshakes"

It's what I'm doing for my CamServer..
When CamServer is actively listening it also broadcast every 10 seconds its current IP and port..
EspCams listen for this and auto configure themselves..
Working pretty good..

also got some udp demos in my git..
UDP Sender..
there's a receiver in there too..

good luck.. ~q

If 192.168.54.214 is your router, it looks like it, as your local DNS server, doesn't (can't?) map the hostname to the IP address but I don't know why, sorry, as it's a matter of router configuration (maybe it's a feature to be activated somewhere).

With WeMos D1 boards (ESP8266) I used NTPClient. I have never used UDP on ESP32 until now, but I think a quick search for "ESP32 UDP example" could give you more direct information. I see some examples use WiFiUdp library, so you probably just need it.

Right, IMHO exactly it's the best way to handle devices dynamic IPs.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.