Sunday, July 29, 2018

Adding mDNS to ESP32 camera demo

I was trying to add mDNS to this demo of using a camera (in my case, an OV2640) with the ESP32 to stream video over WiFi to a web browser.

After some trial and error, and looking at examples and such, it boiled down to:

1. Add a function to start mDNS.
static void start_mdns_service()
{
    //initialize mDNS service
    esp_err_t err = mdns_init();
    if (err) {
        printf("MDNS Init failed: %d\n", err);
        return;
    }

    //set hostname
    ESP_ERROR_CHECK( mdns_hostname_set(MY_HOSTNAME) );
    //set default instance
    ESP_ERROR_CHECK( mdns_instance_name_set(MY_INSTANCE) );
}

Remember to declare the function! And define MY_HOSTNAME and MY_INSTANCE using #define statements, such as:
#define MY_HOSTNAME "esp32cam"
#define MY_INSTANCE "ESP32 Camera Demo"

2. Add a line to handle mDNS in the event_handler() function. This statement goes right after the switch() block, and before the final return statement.
mdns_handle_system_event(ctx, event);

3. Call the start_mdns_service() function before you initialize WiFi. For example:
start_mdns_service();
s_wifi_event_group = xEventGroupCreate();
.... followed by rest of statements to start WiFi...

You can then access the ESP32 module through the name esp32cam.local, for example. To stream video from the camera to a web browser, open
http://esp32cam.local/jpg_stream
in the browser.

Good luck tinkering!

2 comments:

Anonymous said...

I apologize for the noob question but trying to accomplish this as well.
I've done some coding but always trial and error. Thought i would actually ask someone this time how it was implemented.

I've included the espmdns library and then defined the variables as stated but then got lost in where to add the mdns start code. you stated event_handler but nothing in this code is named that way so that leads me to believe this is a generic term to whatever the event handler is for the code you are trying to do this for insert it there.

Thats where im lost. No idea which is the main event handler to insert the mdns call to initiate the function.

Any insight would be appreciated.

Teck said...

Dear Unknown,

Thank you for the comment. Sorry for the late reply, somehow I am not getting notifications for comments and didn't see this until today.

To understand this blog post, you will need to refer to the original code by igrr.
https://github.com/igrr/esp32-cam-demo/blob/master/main/app_main.c
The functions referred to in this post refer to the functions defined in this C file.

If you look near the end of the file, you will find a function defined by:
static esp_err_t event_handler(void *ctx, system_event_t *event)

This is the event_handler function I mentioned.

As for WiFi statements, you will find them in the function below that, defined by:
static void initialise_wifi(void)

You should see the first two statements in this function as:
tcpip_adapter_init();
s_wifi_event_group = xEventGroupCreate();

Just insert
start_mdns_service();
between these two lines.

You should then get something like:
tcpip_adapter_init();
start_mdns_service();
s_wifi_event_group = xEventGroupCreate();