Page 1 of 2

Setting mac address and hostname using functions.

Posted: Wed Mar 14, 2018 9:10 am
by snahmad75
Hi,
My code is this.

I can connect to my Wifi Access point and my http server is working using IP address assigned by DHCP.

I am setting host name using
tcpip_adapter_set_hostname(TCPIP_ADAPTER_IF_STA, "mytest");
but I cannot ping it using this name. My DHCP server is windows server.

Another question how can I set mac address as well?

My code is this:

#include "HttpServer.h"
#include "HttpRequest.h"
#include "HttpResponse.h"
#include <Task.h>
#include <WiFi.h>
#include <WiFiEventHandler.h>
#include <tcpip_adapter.h>
#include "esp_log.h"
#include "sdkconfig.h"

extern "C" {
void app_main(void);
}

static WiFi *wifi;




static void helloWorldHandler(HttpRequest* pRequest, HttpResponse* pResponse)
{
pResponse->setStatus(HttpResponse::HTTP_STATUS_OK, "OK");
pResponse->addHeader(HttpRequest::HTTP_HEADER_CONTENT_TYPE, "text/plain");
pResponse->sendData("Hello back");
pResponse->close();
}

class HttpTask: public Task {
void run(void *data) {
ESP_LOGD("http", "Testing http ...");

HttpServer* pHttpServer = new HttpServer();
pHttpServer->addPathHandler(
HttpRequest::HTTP_METHOD_GET,
"/helloWorld",
helloWorldHandler);
pHttpServer->start(80);


return;
}
};
static HttpTask *http_task;

class MyWiFiEventHandler: public WiFiEventHandler {
public:
esp_err_t staGotIp(system_event_sta_got_ip_t event_sta_got_ip)
{
printf("staGotIp\n");
/*
http_task = new HttpTask();
http_task->setStackSize(12000);
http_task->start();
*/
return ESP_OK;
}

esp_err_t apStart()
{
printf("apStart\n");
esp_err_t err = tcpip_adapter_set_hostname(TCPIP_ADAPTER_IF_STA, "mytest");
printf("tcpip_adapter_set_hostname err=%d\n", err);
return ESP_OK;
}
};

void app_main(void)
{


MyWiFiEventHandler *eventHandler = new MyWiFiEventHandler();

wifi = new WiFi();
wifi->setWifiEventHandler(eventHandler);
vTaskDelay(10000 / portTICK_PERIOD_MS);

bool is_connected = wifi->connectAP("????", "????");

esp_err_t err = tcpip_adapter_set_hostname(TCPIP_ADAPTER_IF_STA, "mytest");
printf("tcpip_adapter_set_hostname err=%d\n", err);

const char * hostname;
err = tcpip_adapter_get_hostname(TCPIP_ADAPTER_IF_STA, &hostname);
//if(err)
{
printf("tcpip_adapter_get_hostname err=%d, host=%s\n", err, hostname);
}

test t;
t.execute();

while(1)
{
printf("is_connected=%d\n", (int)is_connected);
vTaskDelay(100 / portTICK_PERIOD_MS);
}
}




Thanks,
Naeem

Re: Setting mac address and hostname using functions.

Posted: Wed Mar 14, 2018 2:26 pm
by kolban
My understanding of name resolution (DNS) is that your device (ESP32) can't declare its own hostname and have that resolved from a remote PC. Imagine I came onto your LAN and declared "MY" DNS name is "passwordServer" .... that would be a horrible situation (I think). Instead, the way DNS works is by having a "database" of hostname to IP address mappings. Some gateways that host their own DNS servers may have "magic" such that when a DHCP IP address is assigned, they have mappings configured at the gateway.

For dynamic DNS there is the concept of "mDNS" which provides a capability for devices to advertise their existence (and identity) for which some PC operating systems can "include" in a DNS search.

This of course then begs the question "What does tcpip_adapter_set_hostname() actually do and when should it be used?" ... I'd love to hear comments on that notion from the community.

Re: Setting mac address and hostname using functions.

Posted: Wed Mar 14, 2018 4:45 pm
by fly135

Code: Select all

  ESP_ERROR_CHECK(esp_wifi_init(&cfg));
  // Give our wifi a mac address
  uint8_t mac[6] = { 0x10, 0x84, 0x2C, 0x80, 0, 2 };
  esp_wifi_set_mac(ESP_IF_WIFI_STA, mac);

Re: Setting mac address and hostname using functions.

Posted: Wed Mar 14, 2018 4:52 pm
by snahmad75
Hi,

Thanks for reply.

I will try setting up mac address for now. Setting hostname was working on our local network for firmwares using keil stack using
Keil function.
https://www.keil.com/pack/doc/mw/Networ ... _func.html


Thanks,
Naeem

Re: Setting mac address and hostname using functions.

Posted: Wed Mar 14, 2018 8:09 pm
by WiFive
kolban wrote:My understanding of name resolution (DNS) is that your device (ESP32) can't declare its own hostname and have that resolved from a remote PC. Imagine I came onto your LAN and declared "MY" DNS name is "passwordServer" .... that would be a horrible situation (I think). Instead, the way DNS works is by having a "database" of hostname to IP address mappings. Some gateways that host their own DNS servers may have "magic" such that when a DHCP IP address is assigned, they have mappings configured at the gateway.

For dynamic DNS there is the concept of "mDNS" which provides a capability for devices to advertise their existence (and identity) for which some PC operating systems can "include" in a DNS search.

This of course then begs the question "What does tcpip_adapter_set_hostname() actually do and when should it be used?" ... I'd love to hear comments on that notion from the community.
Yes it is used for dhcp hostname registration which works with some gateways for dns resolution and a common pattern is to use the last 4 digits of the Mac address as a suffix for uniqueness. It is also used for mdns default setting.

Re: Setting mac address and hostname using functions.

Posted: Wed Mar 14, 2018 10:02 pm
by snahmad75
Hi,

I will try setting up mac address.

For Host name.

I think my Wifi network did not support DNS. I will try out on my Ethernet network and will let you know.

Thanks,
Naeem

Re: Setting mac address and hostname using functions.

Posted: Thu Mar 15, 2018 10:31 am
by snahmad75
esp_wifi_set_mac is not working for me
see documentation.

https://github.com/espressif/esp-idf/bl ... esp_wifi.h
attention 1. This API can only be called when the interface is disabled

Where to call this function in my example program below. I did try to put before and after connectAP. no luck.



#include "HttpServer.h"
#include "HttpRequest.h"
#include "HttpResponse.h"
#include <Task.h>
#include <WiFi.h>
#include <WiFiEventHandler.h>
#include <tcpip_adapter.h>
#include "esp_log.h"
#include "sdkconfig.h"
#include <test.h>
#include <test2.h>
#include <esp_wifi.h>

extern "C" {
void app_main(void);
}

static WiFi *wifi;




static void helloWorldHandler(HttpRequest* pRequest, HttpResponse* pResponse)
{
pResponse->setStatus(HttpResponse::HTTP_STATUS_OK, "OK");
pResponse->addHeader(HttpRequest::HTTP_HEADER_CONTENT_TYPE, "text/plain");
pResponse->sendData("Hello back");
pResponse->close();
}

class HttpTask: public Task {
void run(void *data) {
ESP_LOGD("http", "Testing http ...");

HttpServer* pHttpServer = new HttpServer();
pHttpServer->addPathHandler(
HttpRequest::HTTP_METHOD_GET,
"/helloWorld",
helloWorldHandler);
pHttpServer->start(80);


return;
}
};
static HttpTask *http_task;

class MyWiFiEventHandler: public WiFiEventHandler {
public:
esp_err_t staGotIp(system_event_sta_got_ip_t event_sta_got_ip)
{
printf("staGotIp\n");
/*
http_task = new HttpTask();
http_task->setStackSize(12000);
http_task->start();
*/
return ESP_OK;
}

esp_err_t apStart()
{
printf("apStart\n");

return ESP_OK;
}
};

void app_main(void)
{


MyWiFiEventHandler *eventHandler = new MyWiFiEventHandler();

wifi = new WiFi();
wifi->setWifiEventHandler(eventHandler);
vTaskDelay(10000 / portTICK_PERIOD_MS);

// Give our wifi a mac address
uint8_t mac[6] = { 0x70, 0xB3, 0xD5, 0x6A, 0x02, 0x01 };
esp_err_t err= esp_wifi_set_mac(ESP_IF_WIFI_STA, mac);
printf("esp_wifi_set_mac err=%d\n", err);

//wifi->setIPInfo("192.168.1.200", "192.168.1.254", "255.255.255.0");
bool is_connected = wifi->connectAP(???", "????");
while(1)
{
printf("is_connected=%d\n", (int)is_connected);
vTaskDelay(100 / portTICK_PERIOD_MS);
}
}

Re: Setting mac address and hostname using functions.

Posted: Fri Mar 16, 2018 10:10 am
by snahmad75
Can anyone answer my last query. where to call function to setup mac address.

Re: Setting mac address and hostname using functions.

Posted: Fri Mar 16, 2018 10:35 am
by ESP_igrr

Re: Setting mac address and hostname using functions.

Posted: Fri May 04, 2018 9:30 am
by snahmad75
I don't need to set mac address.

but I do need to set host name to uniquely identify esp32 on my wifi network.

before tcpip_adapter_get_hostname STA err=0, host=espressif
tcpip_adapter_set_hostname with err=0
staGotIp
after tcpip_adapter_get_hostname STA err=0, host=testing

Still cannot ping using host name.

class MyWiFiEventHandler: public WiFiEventHandler {
public:
esp_err_t staGotIp(system_event_sta_got_ip_t event_sta_got_ip)
{
const char * before_hostname;
esp_err_t err = tcpip_adapter_get_hostname(TCPIP_ADAPTER_IF_STA, &before_hostname);
//if(err)
{
printf("before tcpip_adapter_get_hostname STA err=%d, host=%s\n", err, before_hostname);
}


err = tcpip_adapter_set_hostname(TCPIP_ADAPTER_IF_STA, "testing");
printf("tcpip_adapter_set_hostname with err=%d\n", (int)err);

ACDTRACE(ES_NoError, "staGotIp\n");
main_task = new MainTask();
main_task->setStackSize(12000);
main_task->start();

const char * hostname;
err = tcpip_adapter_get_hostname(TCPIP_ADAPTER_IF_STA, &hostname);
//if(err)
{
printf("after tcpip_adapter_get_hostname STA err=%d, host=%s\n", err, hostname);
}


return ESP_OK;
}

esp_err_t apStart()
{
ACDTRACE(ES_NoError, "apStart\n");

return ESP_OK;
}
};