Having to remember (or find a DCHP served) IP address and then type it into the browser is frustrating. At some point as the firmware came together I had one of those "Oh Doh!" moments where you remember something important. The Espressif IDF provides a Multicast DNS (mDNS) library that runs on their network stack. I had actually used this in the tCam project but hadn't used it there for domain resolution.
With mDNS the iCamMini can be found by typing the URL "icam.local" in the browser. The ".local" suffix tells the browser use mDNS to try to find the domain on the local network. Then the iCamMini replies with its address and the browser opens the default page at that address.
While you can still access the camera using its IPV4 address (e.g. http://10.0.1.50), this makes it easy to find cameras given a DHCP address or when you move the camera to a new network or make it mobile by having it act as the AP point.
It's actually easy to implement. The routine I use to start mDNS after the network comes up is shown below.
static bool start_mdns()
{
char s[16];
const esp_app_desc_t* app_desc;
esp_err_t ret;
esp_netif_ip_info_t ip_info;
// Attempt to initialize mDNS
ret = mdns_init();
if (ret != ESP_OK) {
ESP_LOGE(TAG, "Could not initialize mDNS (%d)", ret);
return false;
}
// Set our hostname
ret = mdns_hostname_set("icam");
if (ret != ESP_OK) {
ESP_LOGE(TAG, "Could not set mDNS hostname (%d)", ret);
return false;
}
// Set our default instance
ret = mdns_instance_name_set(wifi_config.ap_ssid);
if (ret != ESP_OK) {
ESP_LOGE(TAG, "Could not set mDNS instance %s (%d)", wifi_config.ap_ssid, ret);
return false;
}
// Get info for our TXT records
app_desc = esp_app_get_description(); // Get version info
esp_netif_get_ip_info(wifi_netif, &ip_info); // Get IP address
sprintf(s, IPSTR, IP2STR(&ip_info.ip));
service_txt_data[0].key = txt_item_keys[0];
service_txt_data[0].value = s;
service_txt_data[1].key = txt_item_keys[1];
service_txt_data[1].value = app_desc->version;
// Initialize service
ret = mdns_service_add(NULL, "_http", "_tcp", 80, service_txt_data, NUM_SERVICE_TXT_ITEMS);
if (ret != ESP_OK) {
ESP_LOGE(TAG, "Could not initialize mDNS service (%d)", ret);
return false;
}
mdns_running = true;
return true;
}
Aside from initializing the library and giving the device the name "icam" the important bit is the mdns_service_add() at the bottom. It tells the library to respond to requests from a browser. The TXT records contain the camera's IP address and firmware version. TXT records are additional information that is present along with the response. Normally the browser doesn't care about these but a general purpose mDNS browsing utility will display them.
Discussions
Become a Hackaday.io Member
Create an account to leave a comment. Already have an account? Log In.