Skip to content
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

dynamic WiFi.hostname("newname") #5652

Merged
merged 4 commits into from
Jan 24, 2019
Merged

dynamic WiFi.hostname("newname") #5652

merged 4 commits into from
Jan 24, 2019

Conversation

d-a-v
Copy link
Collaborator

@d-a-v d-a-v commented Jan 23, 2019

Effect of WiFi.hostname("new name") on the network:

hostname

My AP runs openwrt with dnsmasq, and I can ping the new hostname as soon as Wifi.hostname("name") is called on the esp.

Sketch:


#include <ESP8266WiFi.h>
#include <PolledTimeout.h>

#ifndef STASSID
#define STASSID "your-ssid"
#define STAPSK  "your-password"
#endif

const char* ssid     = STASSID;
const char* password = STAPSK;

void connect ()
{
  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
}

void setup() {
  Serial.begin(115200);
  Serial.println();
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);

  WiFi.persistent(false);
  connect();
  WiFi.hostname("chameleon");
  Serial.printf("host name is %s, will change every 20 seconds, ping me !\n", WiFi.hostname().c_str());
}

esp8266::polledTimeout::periodic renew(20000);
int count = 0;

void loop()
{
  if (renew)
  {
    String name = String(++count) + "chameleonchameleonchameleon";
    Serial.printf("setting hostname to: %s\n", name.c_str());
    Serial.printf("ret=%d\n", WiFi.hostname(name));
    Serial.printf("hostname=%s\n", WiFi.hostname().c_str());
  }
}

Logs:

...
setting hostname to: 17chameleonchameleonchameleon
hostname '17chameleonchameleonchameleon' is not compliant with RFC952
ret=0
...

(but esp name stays "nicely" responding to name 17chameleonchameleonchameleon)

@d-a-v d-a-v requested a review from devyte January 23, 2019 18:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants