Setting mac address and hostname using functions.

snahmad75
Posts: 445
Joined: Wed Jan 24, 2018 6:32 pm

Setting mac address and hostname using functions.

Postby snahmad75 » Wed Mar 14, 2018 9:10 am

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

User avatar
kolban
Posts: 1683
Joined: Mon Nov 16, 2015 4:43 pm
Location: Texas, USA

Re: Setting mac address and hostname using functions.

Postby kolban » Wed Mar 14, 2018 2:26 pm

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.
Free book on ESP32 available here: https://leanpub.com/kolban-ESP32

User avatar
fly135
Posts: 606
Joined: Wed Jan 03, 2018 8:33 pm
Location: Orlando, FL

Re: Setting mac address and hostname using functions.

Postby fly135 » Wed Mar 14, 2018 4:45 pm

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);

snahmad75
Posts: 445
Joined: Wed Jan 24, 2018 6:32 pm

Re: Setting mac address and hostname using functions.

Postby snahmad75 » Wed Mar 14, 2018 4:52 pm

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

WiFive
Posts: 3529
Joined: Tue Dec 01, 2015 7:35 am

Re: Setting mac address and hostname using functions.

Postby WiFive » Wed Mar 14, 2018 8:09 pm

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.

snahmad75
Posts: 445
Joined: Wed Jan 24, 2018 6:32 pm

Re: Setting mac address and hostname using functions.

Postby snahmad75 » Wed Mar 14, 2018 10:02 pm

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

snahmad75
Posts: 445
Joined: Wed Jan 24, 2018 6:32 pm

Re: Setting mac address and hostname using functions.

Postby snahmad75 » Thu Mar 15, 2018 10:31 am

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);
}
}

snahmad75
Posts: 445
Joined: Wed Jan 24, 2018 6:32 pm

Re: Setting mac address and hostname using functions.

Postby snahmad75 » Fri Mar 16, 2018 10:10 am

Can anyone answer my last query. where to call function to setup mac address.

ESP_igrr
Posts: 2072
Joined: Tue Dec 01, 2015 8:37 am

Re: Setting mac address and hostname using functions.

Postby ESP_igrr » Fri Mar 16, 2018 10:35 am


snahmad75
Posts: 445
Joined: Wed Jan 24, 2018 6:32 pm

Re: Setting mac address and hostname using functions.

Postby snahmad75 » Fri May 04, 2018 9:30 am

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;
}
};

Who is online

Users browsing this forum: MicroController and 117 guests