-
Notifications
You must be signed in to change notification settings - Fork 7.6k
hostname can not set #806
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
I change the order of Statement ,than it is worked. WiFi.begin(ssid, password);
WiFi.setHostname("node1"); |
I tried it both way on a DFrobot firebeetle esp32 board and it DOES not work for me. Here is the test: #include <WiFi.h> void setup() { } void loop() { |
hostname != dns . Your dhcp server is doing whatever you are seeing there in dns. |
My google mesh router is providing the same name no matter what I do...:("espressif.lan.") |
@doronby , any luck? |
I have the same issue. All my ESP8266 boards are able to set a hostname. Why isn't this possible with ESP32? |
Should I open a new ticket for this...? |
I was hoping this issue could be re-opened... |
I hope this issue can be re-opened. It is NOT fixed for the ESP32 library. I've tried every combo I've seen discussed and none of them work. Always shows up blank on my WiFi router. Every other WiFi device I connect with shows up perfectly but not my ESP32. Neither, WiFi.setHostname() nor WiFi.softAPsetHostname() work in any call location. Arduino IDE 1.8.10 |
Hi ! |
Same here.... using ESP32-CAM |
+1 |
1 similar comment
+1 |
@llqy Can you please reopen this issue? |
Ok, for my code, I have been able to get the hostname to change if I put a few delay calls such as this:
|
Issue needs to be reopened. |
Hi all, Cheers, For detailed infos on my chip:
|
https://github.com/espressif/arduino-esp32/blob/master/libraries/WiFi/examples/WiFiIPv6/WiFiIPv6.ino#L79 The first time the device connected to the router, I was not able to see the name, but after rest and reconnect, the name showed up. Cheers |
@urmel79 - you need to put a delay after each and every modification you make to the WiFi stack otherwise the code doesn't allow background tasks to run, and hence why the name doesn't get changed. Post the code you say doesn't work and I'll test it on my devices. |
@richard-scott and @me-no-dev : I also tried on different wifi routers (both with OpenWrt, but different versions and hardware) - result: in no case the hostname was set and visible in the dhcp lease. With esp8266 I never had this problem - it worked on every wifi router in my lab like a dream ... Here is a screenshot of the OpenWrt overview: It would be really great if you could nudge me in the right direction :) Cheers, |
//
// Adapted from:
// https://github.com/marvinroger/async-mqtt-client/blob/master/examples/FullyFeatured-ESP32/FullyFeatured-ESP32.ino
//
// Hint: Ticker is used and not timers from FreeRTOS (because there are tickers available in ESP32 in the meantime ...)
//
#include <WiFi.h>
#include <Ticker.h>
#include <AsyncMqttClient.h>
#define WIFI_SSID "my_SSID"
#define WIFI_PASSWORD "my_Pwd"
#define MQTT_HOST "RaspiB3PiFaceExp"
#define MQTT_PORT 1883
#define MQTT_ROOT_TOPIC "test"
#define HOSTNAME "NodeMCU_LoRa_2_ESP32"
AsyncMqttClient mqttClient;
Ticker mqttReconnectTimer;
Ticker wifiReconnectTimer;
Ticker mqtt_pub_wifi_quality;
uint8_t g_ui_cnt = 0;
Ticker mqtt_pub_counter;
void connectToWifi() {
Serial.println("Connecting to Wi-Fi...");
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
// Alternative 1:
// [Bug:] hostname cannot be set in the moment => bug in Wifi class?
// issue: https://github.com/espressif/arduino-esp32/issues/806
delay(150);
WiFi.setHostname(HOSTNAME); // set hostname
}
void connectToMqtt() {
Serial.println("Connecting to MQTT...");
mqttClient.connect();
}
void WiFiEvent(WiFiEvent_t event) {
Serial.printf("[WiFi-event] event: %d\n", event);
switch(event) {
case SYSTEM_EVENT_STA_START:
// // Alternative 2:
// // set sta hostname here
// WiFi.setHostname(HOSTNAME);
break;
case SYSTEM_EVENT_STA_GOT_IP:
Serial.println("Connected to Wi-Fi.");
Serial.print("ESP32 hostname: ");
Serial.println(String(WiFi.getHostname()));
Serial.print("ESP32 MAC: ");
Serial.println(WiFi.macAddress());
Serial.print("ESP32 IP: ");
Serial.println(WiFi.localIP());
Serial.print("Wi-Fi signal level: ");
Serial.println(WiFi.RSSI());
connectToMqtt();
break;
case SYSTEM_EVENT_STA_DISCONNECTED:
Serial.println("Disconnected from Wi-Fi.");
mqttReconnectTimer.detach(); // ensure we don't reconnect to MQTT while reconnecting to Wi-Fi
wifiReconnectTimer.once(2, connectToWifi);
break;
}
}
void mqttPub_wifi_rssi() {
bool send_success;
// convert int to string
String str_wifi_rssi_dBm = "Wifi RSSI: " + String(WiFi.RSSI()) + " dBm";
String topic_pub = String(MQTT_ROOT_TOPIC);
topic_pub += "/wifi/rssi";
if (mqttClient.connected()) {
send_success = mqttClient.publish(topic_pub.c_str(), 0, true, str_wifi_rssi_dBm.c_str());
if (send_success) {
Serial.println(str_wifi_rssi_dBm);
}
else Serial.println("MQTT: Error sending message.");
}
}
void mqttPub_cnt() {
bool send_success;
// convert int to string
String str_cnt = "Counter: " + String(g_ui_cnt);
String topic_pub = String(MQTT_ROOT_TOPIC);
topic_pub += "/cnt";
if (mqttClient.connected()) {
send_success = mqttClient.publish(topic_pub.c_str(), 0, true, str_cnt.c_str());
g_ui_cnt++;
if (send_success) {
Serial.println(str_cnt);
}
else Serial.println("MQTT: Error sending message.");
}
}
void onMqttConnect(bool sessionPresent) {
Serial.println("Connected to MQTT.");
Serial.print("Session present: ");
Serial.println(sessionPresent);
uint16_t packetIdSub = mqttClient.subscribe("test/lol", 2);
Serial.print("Subscribing at QoS 2, packetId: ");
Serial.println(packetIdSub);
mqttClient.publish("test/lol", 0, true, "test 1");
Serial.println("Publishing at QoS 0");
uint16_t packetIdPub1 = mqttClient.publish("test/lol", 1, true, "test 2");
Serial.print("Publishing at QoS 1, packetId: ");
Serial.println(packetIdPub1);
uint16_t packetIdPub2 = mqttClient.publish("test/lol", 2, true, "test 3");
Serial.print("Publishing at QoS 2, packetId: ");
Serial.println(packetIdPub2);
mqtt_pub_wifi_quality.attach(2, mqttPub_wifi_rssi);
mqtt_pub_counter.attach(2, mqttPub_cnt);
}
void onMqttDisconnect(AsyncMqttClientDisconnectReason reason) {
Serial.println("Disconnected from MQTT.");
if (WiFi.isConnected()) {
mqttReconnectTimer.once(2, connectToMqtt);
}
}
void onMqttSubscribe(uint16_t packetId, uint8_t qos) {
Serial.println("Subscribe acknowledged.");
Serial.print(" packetId: ");
Serial.println(packetId);
Serial.print(" qos: ");
Serial.println(qos);
}
void onMqttUnsubscribe(uint16_t packetId) {
Serial.println("Unsubscribe acknowledged.");
Serial.print(" packetId: ");
Serial.println(packetId);
}
void onMqttMessage(char* topic, char* payload, AsyncMqttClientMessageProperties properties, size_t len, size_t index, size_t total) {
Serial.println("Publish received.");
Serial.print(" topic: ");
Serial.println(topic);
Serial.print(" payload: ");
Serial.println(payload);
Serial.print(" qos: ");
Serial.println(properties.qos);
Serial.print(" dup: ");
Serial.println(properties.dup);
Serial.print(" retain: ");
Serial.println(properties.retain);
Serial.print(" len: ");
Serial.println(len);
Serial.print(" index: ");
Serial.println(index);
Serial.print(" total: ");
Serial.println(total);
}
void onMqttPublish(uint16_t packetId) {
Serial.println("Publish acknowledged.");
Serial.print(" packetId: ");
Serial.println(packetId);
}
void setup() {
Serial.begin(9600);
Serial.println();
Serial.println();
WiFi.onEvent(WiFiEvent);
mqttClient.onConnect(onMqttConnect);
mqttClient.onDisconnect(onMqttDisconnect);
mqttClient.onSubscribe(onMqttSubscribe);
mqttClient.onUnsubscribe(onMqttUnsubscribe);
mqttClient.onMessage(onMqttMessage);
mqttClient.onPublish(onMqttPublish);
mqttClient.setServer(MQTT_HOST, MQTT_PORT);
connectToWifi();
g_ui_cnt = 0;
}
void loop() {
} |
…RT instead * Experimental implementation to reflect hostname of ESP32 in the network (must be further tested) - See also espressif/arduino-esp32#806
See the solution from this issue #2537 It is unrelated to |
@danielfaust: Sorry for the late reply ... but in those times I had other priorities than playing with ESPs ... But finally I tried your suggestion - it works! I can't explain why, but the hostname is set and updated correctly. Thanks a lot :) So my updated void connectToWifi() {
Serial.println("Connecting to Wi-Fi...");
WiFi.config(INADDR_NONE, INADDR_NONE, INADDR_NONE); // call is only a workaround for bug in WiFi class
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
// Alternative 1:
// [Bug:] hostname cannot be set in the moment => bug in Wifi class?
// issue: https://github.com/espressif/arduino-esp32/issues/806
// workaround: call 'WiFi.config(INADDR_NONE, INADDR_NONE, INADDR_NONE);' before 'WiFi.begin(..)'
// suggestion found here: https://github.com/espressif/arduino-esp32/issues/2537
WiFi.setHostname(HOSTNAME); // set hostname
} Cheers, |
Host name should be set only after WiFi.begin(). Hence the host name needs to be set after WiFi.begin() For me setting the host name after WiFi.begin() works.... There is no other magic inside like doing the config and all to get it work |
This is my code
Serial monitor prints the hostname I set, but when I got to the
Any idea? |
The issue is not because of the host name, but the DNS resolver issue. The way to solve the issue is
Note:- Android do not support mDNS, hence it may not work on Android based devices. Wish you all the success on your project!!!! |
Not sure if it help everyone but in my case this code works in most of situations. Sometimes, maybe 1 per 20 something crash and esp doesn't have any hostname.
Honestly, this code only works well if your esp isn't doing a lot more other things. When you start adding other tasks, everything starts to break and esp crashes and resets. Use it wisely .. happy carroting :3 |
This fixed it for me too. |
Working with an ESP32 WROOM 32 |
Please fill the info fields, it helps to get you faster support ;)
If you have a Guru Meditation Error, please decode it:
https://github.com/me-no-dev/EspExceptionDecoder
----------------------------- Remove above -----------------------------
Hardware:
Board: ?ESP32 Dev Module?
Core Installation/update date: ?11/jul/2017?
IDE name: ?Arduino IDE? ?Platform.io? ?IDF component?
Flash Frequency: ?40Mhz?
Upload Speed: ?115200?
Description:
I can not set hostname use
WiFi.setHostname("node1");
Sketch:
Debug Messages:
The text was updated successfully, but these errors were encountered: