CameraWeb.ino 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. #include "esp_camera.h"
  2. #include <WiFi.h>
  3. #include <ArduinoWebsockets.h>
  4. //
  5. // WARNING!!! Make sure that you have either selected ESP32 Wrover Module,
  6. // or another board which has PSRAM enabled
  7. //
  8. // Select camera model
  9. //#define CAMERA_MODEL_WROVER_KIT
  10. //#define CAMERA_MODEL_ESP_EYE
  11. //#define CAMERA_MODEL_M5STACK_PSRAM
  12. #define CAMERA_MODEL_M5STACK_WIDE
  13. //#define CAMERA_MODEL_AI_THINKER
  14. #include "camera_pins.h"
  15. const char* ssid = "goldin_lab1";
  16. const char* password = "goldin53743001";
  17. const char* websocket_server_host = "60.250.156.230";
  18. const uint16_t websocket_server_port = 8090;
  19. using namespace websockets;
  20. WebsocketsClient client;
  21. void setup() {
  22. Serial.begin(115200);
  23. Serial.setDebugOutput(true);
  24. Serial.println();
  25. camera_config_t config;
  26. config.ledc_channel = LEDC_CHANNEL_0;
  27. config.ledc_timer = LEDC_TIMER_0;
  28. config.pin_d0 = Y2_GPIO_NUM;
  29. config.pin_d1 = Y3_GPIO_NUM;
  30. config.pin_d2 = Y4_GPIO_NUM;
  31. config.pin_d3 = Y5_GPIO_NUM;
  32. config.pin_d4 = Y6_GPIO_NUM;
  33. config.pin_d5 = Y7_GPIO_NUM;
  34. config.pin_d6 = Y8_GPIO_NUM;
  35. config.pin_d7 = Y9_GPIO_NUM;
  36. config.pin_xclk = XCLK_GPIO_NUM;
  37. config.pin_pclk = PCLK_GPIO_NUM;
  38. config.pin_vsync = VSYNC_GPIO_NUM;
  39. config.pin_href = HREF_GPIO_NUM;
  40. config.pin_sscb_sda = SIOD_GPIO_NUM;
  41. config.pin_sscb_scl = SIOC_GPIO_NUM;
  42. config.pin_pwdn = PWDN_GPIO_NUM;
  43. config.pin_reset = RESET_GPIO_NUM;
  44. config.xclk_freq_hz = 10000000;
  45. config.pixel_format = PIXFORMAT_JPEG;
  46. //init with high specs to pre-allocate larger buffers
  47. if(psramFound()){
  48. config.frame_size = FRAMESIZE_UXGA;
  49. config.jpeg_quality = 10;
  50. config.fb_count = 2;
  51. } else {
  52. config.frame_size = FRAMESIZE_SVGA;
  53. config.jpeg_quality = 12;
  54. config.fb_count = 1;
  55. }
  56. // camera init
  57. esp_err_t err = esp_camera_init(&config);
  58. if (err != ESP_OK) {
  59. Serial.printf("Camera init failed with error 0x%x", err);
  60. return;
  61. }
  62. sensor_t * s = esp_camera_sensor_get();
  63. // initial sensors are flipped vertically and colors are a bit saturated
  64. if (s->id.PID == OV3660_PID) {
  65. s->set_vflip(s, 1); // flip it back
  66. s->set_brightness(s, 1); // up the brightness just a bit
  67. s->set_saturation(s, -2); // lower the saturation
  68. }
  69. // drop down frame size for higher initial frame rate
  70. s->set_framesize(s, FRAMESIZE_QVGA);
  71. #if defined(CAMERA_MODEL_M5STACK_WIDE) || defined(CAMERA_MODEL_M5STACK_ESP32CAM)
  72. s->set_vflip(s, 1);
  73. s->set_hmirror(s, 1);
  74. #endif
  75. WiFi.begin(ssid, password);
  76. while (WiFi.status() != WL_CONNECTED) {
  77. delay(500);
  78. Serial.print(".");
  79. }
  80. Serial.println("");
  81. Serial.println("WiFi connected");
  82. Serial.print("Camera Ready! Use 'http://");
  83. Serial.print(WiFi.localIP());
  84. Serial.println("' to connect");
  85. while(!client.connect(websocket_server_host, websocket_server_port, "/")){
  86. delay(500);
  87. Serial.print(".");
  88. }
  89. Serial.println("Websocket Connected!");
  90. }
  91. void loop() {
  92. camera_fb_t *fb = esp_camera_fb_get();
  93. if(!fb){
  94. Serial.println("Camera capture failed");
  95. esp_camera_fb_return(fb);
  96. return;
  97. }
  98. if(fb->format != PIXFORMAT_JPEG){
  99. Serial.println("Non-JPEG data not implemented");
  100. return;
  101. }
  102. client.sendBinary((const char*) fb->buf, fb->len);
  103. esp_camera_fb_return(fb);
  104. }