CameraWebsocket.ino 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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 = 8093;
  19. String serverName = "60.250.156.230";
  20. String serverPath = "/upload.php"; // The default serverPath should be upload.php
  21. const int serverPort = 80;
  22. void startCameraServer();
  23. using namespace websockets;
  24. WebsocketsClient client;
  25. //Set Static IP Address
  26. IPAddress local_IP(192, 168, 50, 83);
  27. //Set Gateway IP Address
  28. IPAddress gateway(192, 168, 50, 254);
  29. IPAddress subnet(255, 255, 255, 0);
  30. const int timerInterval = 30000; // time between each HTTP POST image
  31. unsigned long previousMillis = 0; // last time image was sent
  32. void setup() {
  33. Serial.begin(115200);
  34. Serial.setDebugOutput(true);
  35. Serial.println();
  36. camera_config_t config;
  37. config.ledc_channel = LEDC_CHANNEL_0;
  38. config.ledc_timer = LEDC_TIMER_0;
  39. config.pin_d0 = Y2_GPIO_NUM;
  40. config.pin_d1 = Y3_GPIO_NUM;
  41. config.pin_d2 = Y4_GPIO_NUM;
  42. config.pin_d3 = Y5_GPIO_NUM;
  43. config.pin_d4 = Y6_GPIO_NUM;
  44. config.pin_d5 = Y7_GPIO_NUM;
  45. config.pin_d6 = Y8_GPIO_NUM;
  46. config.pin_d7 = Y9_GPIO_NUM;
  47. config.pin_xclk = XCLK_GPIO_NUM;
  48. config.pin_pclk = PCLK_GPIO_NUM;
  49. config.pin_vsync = VSYNC_GPIO_NUM;
  50. config.pin_href = HREF_GPIO_NUM;
  51. config.pin_sscb_sda = SIOD_GPIO_NUM;
  52. config.pin_sscb_scl = SIOC_GPIO_NUM;
  53. config.pin_pwdn = PWDN_GPIO_NUM;
  54. config.pin_reset = RESET_GPIO_NUM;
  55. config.xclk_freq_hz = 10000000;
  56. config.pixel_format = PIXFORMAT_JPEG;
  57. //init with high specs to pre-allocate larger buffers
  58. if(psramFound()){
  59. config.frame_size = FRAMESIZE_UXGA;
  60. config.jpeg_quality = 10;
  61. config.fb_count = 2;
  62. } else {
  63. config.frame_size = FRAMESIZE_SVGA;
  64. config.jpeg_quality = 12;
  65. config.fb_count = 1;
  66. }
  67. // camera init
  68. esp_err_t err = esp_camera_init(&config);
  69. if (err != ESP_OK) {
  70. Serial.printf("Camera init failed with error 0x%x", err);
  71. return;
  72. }
  73. sendPhoto();
  74. sensor_t * s = esp_camera_sensor_get();
  75. // initial sensors are flipped vertically and colors are a bit saturated
  76. if (s->id.PID == OV3660_PID) {
  77. s->set_vflip(s, 1); // flip it back
  78. s->set_brightness(s, 2); // up the brightness just a bit
  79. s->set_saturation(s, -2); // lower the saturation
  80. s->set_contrast(s, 2); // -2 to 2
  81. }
  82. // drop down frame size for higher initial frame rate
  83. s->set_framesize(s, FRAMESIZE_QVGA);
  84. #if defined(CAMERA_MODEL_M5STACK_WIDE) || defined(CAMERA_MODEL_M5STACK_ESP32CAM)
  85. s->set_hmirror(s, 0); // 0 = disable , 1 = enable
  86. s->set_vflip(s, -1); // 0 = disable , 1 = enable
  87. s->set_brightness(s, -2);
  88. s->set_framesize(s, FRAMESIZE_QVGA);
  89. #endif
  90. client.send("Hi Server!");
  91. //if(!WiFi.config(local_IP, gateway, subnet)) {
  92. Serial.println("Set IP Failed");
  93. //}
  94. WiFi.begin(ssid, password);
  95. while (WiFi.status() != WL_CONNECTED) {
  96. delay(500);
  97. Serial.print(".");
  98. }
  99. Serial.println("");
  100. Serial.println("WiFi connected");
  101. startCameraServer();
  102. Serial.print("Camera Ready! Use 'http://");
  103. Serial.print(WiFi.localIP());
  104. Serial.println("' to connect");
  105. while(!client.connect(websocket_server_host, websocket_server_port, "/")){
  106. delay(500);
  107. Serial.print(".");
  108. }
  109. Serial.println("Websocket Connected!");
  110. }
  111. void loop() {
  112. camera_fb_t *fb = esp_camera_fb_get();
  113. if(!fb){
  114. Serial.println("Camera capture failed");
  115. esp_camera_fb_return(fb);
  116. return;
  117. }
  118. if(fb->format != PIXFORMAT_JPEG){
  119. Serial.println("Non-JPEG data not implemented");
  120. return;
  121. }
  122. client.sendBinary((const char*) fb->buf, fb->len);
  123. esp_camera_fb_return(fb);
  124. if ((WiFi.status()!= WL_CONNECTED)){
  125. WiFi.reconnect();
  126. Serial.println("WiFi reconnected");
  127. }
  128. while(!client.connect(websocket_server_host, websocket_server_port, "/")){
  129. delay(500);
  130. Serial.print(".");
  131. }
  132. Serial.println("Websocket Connected!");
  133. //client.poll();
  134. }