1889浏览
查看: 1889|回复: 2

[ESP8266/ESP32] FireBeetle 2 ESP32-S3 搭建外网可访问的网络摄像头(内网穿透)

[复制链接]
本篇博文使用ESP32-S3搭建网络摄像头,相比较局域网摄像头,本篇博文将分享如何搭建外网可以访问的网络摄像头。
这主要是使用内网穿透技术,内网穿透是为了使具有某一个特定源 IP 地址和源端口号的数据包(这里指局域网摄像头)不被网络地址转换设备屏蔽而正确路由到内网主机。
主要流程分为两步:
1、先实现局域网访问网络摄像头;
2、在此基础,使用内网穿透的方式,搭建外网可访问的网络摄像头。
1、局域网摄像头
ESP32实现局域网摄像头的方式比较简单,驱动代码如下:
  1. #include "esp_camera.h"
  2. #include <WiFi.h>
  3. //
  4. // WARNING!!! PSRAM IC required for UXGA resolution and high JPEG quality
  5. //            Ensure ESP32 Wrover Module or other board with PSRAM is selected
  6. //            Partial images will be transmitted if image exceeds buffer size
  7. //
  8. //            You must select partition scheme from the board menu that has at least 3MB APP space.
  9. //            Face Recognition is DISABLED for ESP32 and ESP32-S2, because it takes up from 15
  10. //            seconds to process single frame. Face Detection is ENABLED if PSRAM is enabled as well
  11. // ===================
  12. // Select camera model
  13. // ===================
  14. //#define CAMERA_MODEL_WROVER_KIT // Has PSRAM
  15. // #define CAMERA_MODEL_ESP_EYE // Has PSRAM
  16. //#define CAMERA_MODEL_ESP32S3_EYE // Has PSRAM
  17. //#define CAMERA_MODEL_M5STACK_PSRAM // Has PSRAM
  18. //#define CAMERA_MODEL_M5STACK_V2_PSRAM // M5Camera version B Has PSRAM
  19. //#define CAMERA_MODEL_M5STACK_WIDE // Has PSRAM
  20. //#define CAMERA_MODEL_M5STACK_ESP32CAM // No PSRAM
  21. //#define CAMERA_MODEL_M5STACK_UNITCAM // No PSRAM
  22. //#define CAMERA_MODEL_AI_THINKER // Has PSRAM
  23. //#define CAMERA_MODEL_TTGO_T_JOURNAL // No PSRAM
  24. //#define CAMERA_MODEL_XIAO_ESP32S3 // Has PSRAM
  25. // ** Espressif Internal Boards **
  26. //#define CAMERA_MODEL_ESP32_CAM_BOARD
  27. //#define CAMERA_MODEL_ESP32S2_CAM_BOARD
  28. //#define CAMERA_MODEL_ESP32S3_CAM_LCD
  29. #define CAMERA_MODEL_DFRobot_FireBeetle2_ESP32S3 // Has PSRAM
  30. //#define CAMERA_MODEL_DFRobot_Romeo_ESP32S3 // Has PSRAM
  31. #include "camera_pins.h"
  32. #include "DFRobot_AXP313A.h"
  33. DFRobot_AXP313A axp;
  34. // ===========================
  35. // Enter your WiFi credentials
  36. // ===========================
  37. const char* ssid = "";
  38. const char* password = "";
  39. void startCameraServer();
  40. void setupLedFlash(int pin);
  41. void setup() {
  42.   Serial.begin(115200);
  43.   Serial.setDebugOutput(true);
  44.   Serial.println();
  45.   while(axp.begin() != 0){
  46.     Serial.println("init error");
  47.     delay(1000);
  48.   }
  49.   
  50.   axp.enableCameraPower(axp.eOV2640);  // 给摄像头供电
  51.   camera_config_t config;
  52.   config.ledc_channel = LEDC_CHANNEL_0;
  53.   config.ledc_timer = LEDC_TIMER_0;
  54.   config.pin_d0 = Y2_GPIO_NUM;
  55.   config.pin_d1 = Y3_GPIO_NUM;
  56.   config.pin_d2 = Y4_GPIO_NUM;
  57.   config.pin_d3 = Y5_GPIO_NUM;
  58.   config.pin_d4 = Y6_GPIO_NUM;
  59.   config.pin_d5 = Y7_GPIO_NUM;
  60.   config.pin_d6 = Y8_GPIO_NUM;
  61.   config.pin_d7 = Y9_GPIO_NUM;
  62.   config.pin_xclk = XCLK_GPIO_NUM;
  63.   config.pin_pclk = PCLK_GPIO_NUM;
  64.   config.pin_vsync = VSYNC_GPIO_NUM;
  65.   config.pin_href = HREF_GPIO_NUM;
  66.   config.pin_sccb_sda = SIOD_GPIO_NUM;
  67.   config.pin_sccb_scl = SIOC_GPIO_NUM;
  68.   config.pin_pwdn = PWDN_GPIO_NUM;
  69.   config.pin_reset = RESET_GPIO_NUM;
  70.   config.xclk_freq_hz = 20000000;
  71.   config.frame_size = FRAMESIZE_UXGA;
  72.   config.pixel_format = PIXFORMAT_JPEG; // for streaming
  73.   //config.pixel_format = PIXFORMAT_RGB565; // for face detection/recognition
  74.   config.grab_mode = CAMERA_GRAB_WHEN_EMPTY;
  75.   config.fb_location = CAMERA_FB_IN_PSRAM;
  76.   config.jpeg_quality = 12;
  77.   config.fb_count = 1;
  78.   
  79.   // if PSRAM IC present, init with UXGA resolution and higher JPEG quality
  80.   //                      for larger pre-allocated frame buffer.
  81.   if(config.pixel_format == PIXFORMAT_JPEG){
  82.     if(psramFound()){
  83.       config.jpeg_quality = 10;
  84.       config.fb_count = 2;
  85.       config.grab_mode = CAMERA_GRAB_LATEST;
  86.     } else {
  87.       // Limit the frame size when PSRAM is not available
  88.       config.frame_size = FRAMESIZE_SVGA;
  89.       config.fb_location = CAMERA_FB_IN_DRAM;
  90.     }
  91.   } else {
  92.     // Best option for face detection/recognition
  93.     config.frame_size = FRAMESIZE_240X240;
  94. #if CONFIG_IDF_TARGET_ESP32S3
  95.     config.fb_count = 2;
  96. #endif
  97.   }
  98. #if defined(CAMERA_MODEL_ESP_EYE)
  99.   pinMode(13, INPUT_PULLUP);
  100.   pinMode(14, INPUT_PULLUP);
  101. #endif
  102.   // camera init
  103.   esp_err_t err = esp_camera_init(&config);
  104.   if (err != ESP_OK) {
  105.     Serial.printf("Camera init failed with error 0x%x", err);
  106.     return;
  107.   }
  108.   sensor_t * s = esp_camera_sensor_get();
  109.   // initial sensors are flipped vertically and colors are a bit saturated
  110.   if (s->id.PID == OV3660_PID) {
  111.     s->set_vflip(s, 1); // flip it back
  112.     s->set_brightness(s, 1); // up the brightness just a bit
  113.     s->set_saturation(s, -2); // lower the saturation
  114.   }
  115.   // drop down frame size for higher initial frame rate
  116.   if(config.pixel_format == PIXFORMAT_JPEG){
  117.     s->set_framesize(s, FRAMESIZE_QVGA);
  118.   }
  119. #if defined(CAMERA_MODEL_M5STACK_WIDE) || defined(CAMERA_MODEL_M5STACK_ESP32CAM)
  120.   s->set_vflip(s, 1);
  121.   s->set_hmirror(s, 1);
  122. #endif
  123. #if defined(CAMERA_MODEL_ESP32S3_EYE)
  124.   s->set_vflip(s, 1);
  125. #endif
  126. // Setup LED FLash if LED pin is defined in camera_pins.h
  127. #if defined(LED_GPIO_NUM)
  128.   setupLedFlash(LED_GPIO_NUM);
  129. #endif
  130.   WiFi.begin(ssid, password);
  131.   WiFi.setSleep(false);
  132.   while (WiFi.status() != WL_CONNECTED) {
  133.     delay(500);
  134.     Serial.print(".");
  135.   }
  136.   Serial.println("");
  137.   Serial.println("WiFi connected");
  138.   startCameraServer();
  139.   Serial.print("Camera Ready! Use 'http://");
  140.   Serial.print(WiFi.localIP());
  141.   Serial.println("' to connect");
  142. }
  143. void loop() {
  144.   // Do nothing. Everything is done in another task by the web server
  145.   delay(10000);
  146. }
复制代码
代码中有几点需要注意:
1、宏定义选择适配的摄像头模式。
  1. // ===================
  2. // Select camera model
  3. // ===================
  4. //#define CAMERA_MODEL_WROVER_KIT // Has PSRAM
  5. // #define CAMERA_MODEL_ESP_EYE // Has PSRAM
  6. //#define CAMERA_MODEL_ESP32S3_EYE // Has PSRAM
  7. //#define CAMERA_MODEL_M5STACK_PSRAM // Has PSRAM
  8. //#define CAMERA_MODEL_M5STACK_V2_PSRAM // M5Camera version B Has PSRAM
  9. //#define CAMERA_MODEL_M5STACK_WIDE // Has PSRAM
  10. //#define CAMERA_MODEL_M5STACK_ESP32CAM // No PSRAM
  11. //#define CAMERA_MODEL_M5STACK_UNITCAM // No PSRAM
  12. //#define CAMERA_MODEL_AI_THINKER // Has PSRAM
  13. //#define CAMERA_MODEL_TTGO_T_JOURNAL // No PSRAM
  14. //#define CAMERA_MODEL_XIAO_ESP32S3 // Has PSRAM
  15. // ** Espressif Internal Boards **
  16. //#define CAMERA_MODEL_ESP32_CAM_BOARD
  17. //#define CAMERA_MODEL_ESP32S2_CAM_BOARD
  18. //#define CAMERA_MODEL_ESP32S3_CAM_LCD
  19. #define CAMERA_MODEL_DFRobot_FireBeetle2_ESP32S3 // Has PSRAM
  20. //#define CAMERA_MODEL_DFRobot_Romeo_ESP32S3 // Has PSRAM
复制代码
2、无线路由器SSID和密码要填写正确。
  1. // ===========================
  2. // Enter your WiFi credentials
  3. // ===========================
  4. const char* ssid = "";
  5. const char* password = "";
复制代码
3、给摄像头供电
  1. axp.enableCameraPower(axp.eOV2640);  // 给摄像头供电
复制代码
4、板卡需要外接天线,否则可能无法连接路由器。
编译下载程序到板卡中,确保局域网访问网络摄像头可正常使用。
2、内网穿透网络摄像头
内网穿透我们使用花生壳这款软件提供的内网穿透服务。
在官网下载APP:https://hsk.oray.com/
下载安装完成后,在内网穿透服务点击新建映射,如下图所示:
填写新建映射的基本信息,请注意内网主机和内网端口是局域网摄像头的主机和端口(端口默认为80) ,如下图所示:
新建映射完成后,可以在APP看到新增的设备列表,如下图所示:
复制访问网址,在浏览器中打开:http://2j90962r69.goho.co:47918/
即使不在同一个局域网内也可以正常访问摄像头啦。




ESP玩控君  学徒

发表于 2024-9-6 23:05:52

终于知道我的esp 8266还有esp 32为什么连不上路由器了,原来是因为没有接天线信,谢谢大佬指点(*^o^*)
回复

使用道具 举报

ESP玩控君  学徒

发表于 2024-9-6 23:05:52

终于知道我的esp 8266还有esp 32为什么连不上路由器了,原来是因为没有接天线信,谢谢大佬指点(*^o^*)
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

为本项目制作心愿单
购买心愿单
心愿单 编辑
[[wsData.name]]

硬件清单

  • [[d.name]]
btnicon
我也要做!
点击进入购买页面
上海智位机器人股份有限公司 沪ICP备09038501号-4

© 2013-2024 Comsenz Inc. Powered by Discuz! X3.4 Licensed

mail