Skip to content

Commit

Permalink
Added mDNS example
Browse files Browse the repository at this point in the history
  • Loading branch information
dentellaluca committed Apr 29, 2017
1 parent 2169453 commit 747f7c7
Show file tree
Hide file tree
Showing 5 changed files with 435 additions and 0 deletions.
9 changes: 9 additions & 0 deletions 09_mdns/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#
# This is a project Makefile. It is assumed the directory this Makefile resides in is a
# project subdirectory.
#

PROJECT_NAME := 09_mdns

include $(IDF_PATH)/make/project.mk

33 changes: 33 additions & 0 deletions 09_mdns/main/Kconfig.projbuild
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
menu "Wifi Configuration"

config WIFI_SSID
string "WiFi SSID"
default "myssid"
help
SSID (network name) for the example to connect to.

config WIFI_PASSWORD
string "WiFi Password"
default "myssid"
help
WiFi password (WPA or WPA2) for the example to use.

Can be left blank if the network has no security set.

endmenu

menu "mDNS Configuration"

config MDNS_HOSTNAME
string "mDNS Hostname"
default "esp32"
help
mDNS Hostname

config MDNS_INSTANCE
string "mDNS Instance"
default "ESP32 Demo Board"
help
mDNS Instance

endmenu
Empty file added 09_mdns/main/component.mk
Empty file.
106 changes: 106 additions & 0 deletions 09_mdns/main/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/event_groups.h"
#include "esp_system.h"
#include "esp_wifi.h"
#include "esp_event_loop.h"
#include "esp_log.h"
#include "nvs_flash.h"
#include "mdns.h"


// Event group
static EventGroupHandle_t wifi_event_group;
const int CONNECTED_BIT = BIT0;


// Wifi event handler
static esp_err_t event_handler(void *ctx, system_event_t *event)
{
switch(event->event_id) {

case SYSTEM_EVENT_STA_START:
esp_wifi_connect();
break;

case SYSTEM_EVENT_STA_GOT_IP:
xEventGroupSetBits(wifi_event_group, CONNECTED_BIT);
break;

case SYSTEM_EVENT_STA_DISCONNECTED:
xEventGroupClearBits(wifi_event_group, CONNECTED_BIT);
break;

default:
break;
}

return ESP_OK;
}


// Main task
void main_task(void *pvParameter)
{
// mDNS server instance
mdns_server_t* mDNS = NULL;

// wait for connection
printf("Main task: waiting for connection to the wifi network... ");
xEventGroupWaitBits(wifi_event_group, CONNECTED_BIT, false, true, portMAX_DELAY);
printf("connected!\n");

// print the local IP address
tcpip_adapter_ip_info_t ip_info;
ESP_ERROR_CHECK(tcpip_adapter_get_ip_info(TCPIP_ADAPTER_IF_STA, &ip_info));
printf("IP Address: %s\n", ip4addr_ntoa(&ip_info.ip));
printf("Subnet mask: %s\n", ip4addr_ntoa(&ip_info.netmask));
printf("Gateway: %s\n", ip4addr_ntoa(&ip_info.gw));

// create and configure the mDNS server
ESP_ERROR_CHECK(mdns_init(TCPIP_ADAPTER_IF_STA, &mDNS));
ESP_ERROR_CHECK(mdns_set_hostname(mDNS, CONFIG_MDNS_HOSTNAME));
ESP_ERROR_CHECK(mdns_set_instance(mDNS, CONFIG_MDNS_INSTANCE));

while(1) {
vTaskDelay(1000 / portTICK_RATE_MS);
}
}


// Main application
void app_main()
{
// initialize NVS
ESP_ERROR_CHECK(nvs_flash_init());

// create the event group to handle wifi events
wifi_event_group = xEventGroupCreate();

// initialize the tcp stack
tcpip_adapter_init();

// initialize the wifi event handler
ESP_ERROR_CHECK(esp_event_loop_init(event_handler, NULL));

// initialize the wifi stack in STAtion mode with config in RAM
wifi_init_config_t wifi_init_config = WIFI_INIT_CONFIG_DEFAULT();
ESP_ERROR_CHECK(esp_wifi_init(&wifi_init_config));
ESP_ERROR_CHECK(esp_wifi_set_storage(WIFI_STORAGE_RAM));
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));

// configure the wifi connection and start the interface
wifi_config_t wifi_config = {
.sta = {
.ssid = CONFIG_WIFI_SSID,
.password = CONFIG_WIFI_PASSWORD,
},
};
ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config));
ESP_ERROR_CHECK(esp_wifi_start());
printf("Connecting to %s\n", CONFIG_WIFI_SSID);

// start the main task
xTaskCreate(&main_task, "main_task", 2048, NULL, 5, NULL);
}
Loading

0 comments on commit 747f7c7

Please sign in to comment.