Setting mac address and hostname using functions.
Setting mac address and hostname using functions.
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
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.
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.
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
Re: Setting mac address and hostname using functions.
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.
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
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.
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.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.
Re: Setting mac address and hostname using functions.
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
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.
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);
}
}
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.
Can anyone answer my last query. where to call function to setup mac address.
Re: Setting mac address and hostname using functions.
Perhaps https://github.com/espressif/esp-idf/tr ... ac_address example might help you?
Re: Setting mac address and hostname using functions.
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;
}
};
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: Google [Bot] and 141 guests