Add MDNS feature that allows the cameras to be found

pull/136/head
me-no-dev 2020-03-26 15:30:39 +02:00
parent cfa42f74fe
commit 69d9d66faa
7 changed files with 173 additions and 18 deletions

View File

@ -1,4 +1,4 @@
set(COMPONENT_SRCS "app_main.c" "app_wifi.c" "app_camera.c" "app_httpd.c")
set(COMPONENT_SRCS "app_main.c" "app_wifi.c" "app_camera.c" "app_httpd.c" "app_mdns.c")
set(COMPONENT_ADD_INCLUDEDIRS "include")
set(COMPONENT_REQUIRES

View File

@ -20,6 +20,7 @@
#include "driver/ledc.h"
//#include "camera_index.h"
#include "sdkconfig.h"
#include "app_mdns.h"
#if defined(ARDUINO_ARCH_ESP32) && defined(CONFIG_ARDUHAL_ESP_LOG)
#include "esp32-hal-log.h"
@ -705,8 +706,12 @@ static esp_err_t cmd_handler(httpd_req_t *req)
int res = 0;
if (!strcmp(variable, "framesize")) {
if (s->pixformat == PIXFORMAT_JPEG)
if (s->pixformat == PIXFORMAT_JPEG) {
res = s->set_framesize(s, (framesize_t)val);
if (res == 0) {
app_mdns_update_framesize(val);
}
}
}
else if (!strcmp(variable, "quality"))
res = s->set_quality(s, val);

View File

@ -23,10 +23,12 @@
#include "app_camera.h"
#include "app_wifi.h"
#include "app_httpd.h"
#include "app_mdns.h"
void app_main()
{
app_wifi_main();
app_camera_main();
app_httpd_main();
app_mdns_main();
}

View File

@ -0,0 +1,110 @@
/*
* ESPRESSIF MIT License
*
* Copyright (c) 2020 <ESPRESSIF SYSTEMS (SHANGHAI) PTE LTD>
*
* Permission is hereby granted for use on ESPRESSIF SYSTEMS products only, in which case,
* it is free of charge, to any person obtaining a copy of this software and associated
* documentation files (the "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the Software is furnished
* to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or
* substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
*/
#include "sdkconfig.h"
#include "esp_log.h"
#include "esp_wifi.h"
#include "esp_camera.h"
#include "mdns.h"
#include "app_camera.h"
static const char *TAG = "camera mdns";
void app_mdns_update_framesize(int size)
{
char framesize[4];
snprintf(framesize, 4, "%d", size);
if(mdns_service_txt_item_set("_esp-cam", "_tcp", "framesize", (char*)framesize)){
ESP_LOGE(TAG, "mdns_service_txt_item_set() framesize Failed");
}
}
void app_mdns_main()
{
char iname[64];
char hname[64];
uint8_t mac[6];
char framesize[4];
char pixformat[4];
if(esp_read_mac(mac, ESP_MAC_WIFI_STA) != ESP_OK){
ESP_LOGE(TAG, "esp_read_mac() Failed");
return;
}
sensor_t * s = esp_camera_sensor_get();
const char * model;
switch(s->id.PID){
case OV2640_PID: model = "OV2640"; break;
case OV3660_PID: model = "OV3660"; break;
case OV5640_PID: model = "OV5640"; break;
case OV7725_PID: model = "OV7725"; break;
default: model = "UNKNOWN"; break;
}
snprintf(iname, 64, "%s-%s-%02X%02X%02X", CAM_BOARD, model, mac[3], mac[4], mac[5]);
snprintf(framesize, 4, "%d", s->status.framesize);
snprintf(pixformat, 4, "%d", s->pixformat);
char * src = iname, * dst = hname, c;
while (*src) {
c = *src++;
if (c >= 'A' && c <= 'Z') {
c -= 'A';
}
*dst++ = c;
}
if(mdns_init() != ESP_OK){
ESP_LOGE(TAG, "mdns_init() Failed");
return;
}
if(mdns_hostname_set(hname) != ESP_OK){
ESP_LOGE(TAG, "mdns_hostname_set(\"%s\") Failed", hname);
return;
}
if(mdns_instance_name_set(iname) != ESP_OK){
ESP_LOGE(TAG, "mdns_instance_name_set(\"%s\") Failed", iname);
return;
}
if(mdns_service_add(NULL, "_http", "_tcp", 80, NULL, 0) != ESP_OK){
ESP_LOGE(TAG, "mdns_service_add() HTTP Failed");
return;
}
mdns_txt_item_t camera_txt_data[] = {
{(char*)"board" ,(char*)CAM_BOARD},
{(char*)"model" ,(char*)model},
{(char*)"stream_port" ,(char*)"81"},
{(char*)"framesize" ,(char*)framesize},
{(char*)"pixformat" ,(char*)pixformat}
};
if(mdns_service_add(NULL, "_esp-cam", "_tcp", 80, camera_txt_data, 5)) {
ESP_LOGE(TAG, "mdns_service_add() ESP-CAM Failed");
}
}

View File

@ -33,6 +33,8 @@
#include "lwip/err.h"
#include "lwip/sys.h"
#include "mdns.h"
/* The examples use WiFi configuration that you can set via 'make menuconfig'.
If you'd rather not, just change the below entries to strings with
@ -83,6 +85,7 @@ static esp_err_t event_handler(void *ctx, system_event_t *event)
default:
break;
}
mdns_handle_system_event(ctx, event);
return ESP_OK;
}

View File

@ -25,6 +25,7 @@
#define _APP_CAMERA_H_
#if CONFIG_CAMERA_MODEL_WROVER_KIT
#define CAM_BOARD "WROVER-KIT"
#define PWDN_GPIO_NUM -1
#define RESET_GPIO_NUM -1
#define XCLK_GPIO_NUM 21
@ -44,6 +45,7 @@
#define PCLK_GPIO_NUM 22
#elif CONFIG_CAMERA_MODEL_ESP32_CAM_BOARD
#define CAM_BOARD "ESP-DEVCAM"
#define PWDN_GPIO_NUM 32
#define RESET_GPIO_NUM 33
#define XCLK_GPIO_NUM 4
@ -63,6 +65,7 @@
#define PCLK_GPIO_NUM 25
#elif CONFIG_CAMERA_MODEL_ESP_EYE
#define CAM_BOARD "ESP-EYE"
#define PWDN_GPIO_NUM -1
#define RESET_GPIO_NUM -1
#define XCLK_GPIO_NUM 4
@ -82,6 +85,7 @@
#define PCLK_GPIO_NUM 25
#elif CONFIG_CAMERA_MODEL_M5STACK_PSRAM
#define CAM_BOARD "M5CAM"
#define PWDN_GPIO_NUM -1
#define RESET_GPIO_NUM 15
#define XCLK_GPIO_NUM 27
@ -101,6 +105,7 @@
#define PCLK_GPIO_NUM 21
#elif CONFIG_CAMERA_MODEL_M5STACK_WIDE
#define CAM_BOARD "M5CAMW"
#define PWDN_GPIO_NUM -1
#define RESET_GPIO_NUM 15
#define XCLK_GPIO_NUM 27
@ -120,6 +125,7 @@
#define PCLK_GPIO_NUM 21
#elif CONFIG_CAMERA_MODEL_AI_THINKER
#define CAM_BOARD "AI-THINKER"
#define PWDN_GPIO_NUM 32
#define RESET_GPIO_NUM -1
#define XCLK_GPIO_NUM 0
@ -140,23 +146,24 @@
#elif CONFIG_CAMERA_MODEL_CUSTOM
#define PWDN_GPIO_NUM CONFIG_CAMERA_PIN_PWDN
#define RESET_GPIO_NUM CONFIG_CAMERA_PIN_RESET
#define XCLK_GPIO_NUM CONFIG_CAMERA_PIN_XCLK
#define SIOD_GPIO_NUM CONFIG_CAMERA_PIN_SIOD
#define SIOC_GPIO_NUM CONFIG_CAMERA_PIN_SIOC
#define CAM_BOARD "CUSTOM"
#define PWDN_GPIO_NUM CONFIG_CAMERA_PIN_PWDN
#define RESET_GPIO_NUM CONFIG_CAMERA_PIN_RESET
#define XCLK_GPIO_NUM CONFIG_CAMERA_PIN_XCLK
#define SIOD_GPIO_NUM CONFIG_CAMERA_PIN_SIOD
#define SIOC_GPIO_NUM CONFIG_CAMERA_PIN_SIOC
#define Y9_GPIO_NUM CONFIG_CAMERA_PIN_Y9
#define Y8_GPIO_NUM CONFIG_CAMERA_PIN_Y8
#define Y7_GPIO_NUM CONFIG_CAMERA_PIN_Y7
#define Y6_GPIO_NUM CONFIG_CAMERA_PIN_Y6
#define Y5_GPIO_NUM CONFIG_CAMERA_PIN_Y5
#define Y4_GPIO_NUM CONFIG_CAMERA_PIN_Y4
#define Y3_GPIO_NUM CONFIG_CAMERA_PIN_Y3
#define Y2_GPIO_NUM CONFIG_CAMERA_PIN_Y2
#define VSYNC_GPIO_NUM CONFIG_CAMERA_PIN_VSYNC
#define HREF_GPIO_NUM CONFIG_CAMERA_PIN_HREF
#define PCLK_GPIO_NUM CONFIG_CAMERA_PIN_PCLK
#define Y9_GPIO_NUM CONFIG_CAMERA_PIN_Y9
#define Y8_GPIO_NUM CONFIG_CAMERA_PIN_Y8
#define Y7_GPIO_NUM CONFIG_CAMERA_PIN_Y7
#define Y6_GPIO_NUM CONFIG_CAMERA_PIN_Y6
#define Y5_GPIO_NUM CONFIG_CAMERA_PIN_Y5
#define Y4_GPIO_NUM CONFIG_CAMERA_PIN_Y4
#define Y3_GPIO_NUM CONFIG_CAMERA_PIN_Y3
#define Y2_GPIO_NUM CONFIG_CAMERA_PIN_Y2
#define VSYNC_GPIO_NUM CONFIG_CAMERA_PIN_VSYNC
#define HREF_GPIO_NUM CONFIG_CAMERA_PIN_HREF
#define PCLK_GPIO_NUM CONFIG_CAMERA_PIN_PCLK
#endif
#ifdef __cplusplus

View File

@ -0,0 +1,28 @@
// Copyright 2015-2020 Espressif Systems (Shanghai) PTE LTD
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef _CAMERA_MDNS_H_
#define _CAMERA_MDNS_H_
#ifdef __cplusplus
extern "C" {
#endif
void app_mdns_main();
void app_mdns_update_framesize(int size);
#ifdef __cplusplus
}
#endif
#endif /* _CAMERA_MDNS_H_ */