CameraWebsocket_test.ino 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. #include "esp_camera.h"
  2. #include <WiFi.h>
  3. #include <ArduinoWebsockets.h>
  4. #include <HTTPClient.h>
  5. #include "esp_camera.h"
  6. //
  7. // WARNING!!! Make sure that you have either selected ESP32 Wrover Module,
  8. // or another board which has PSRAM enabled
  9. //
  10. // Select camera model
  11. //#define CAMERA_MODEL_WROVER_KIT
  12. //#define CAMERA_MODEL_ESP_EYE
  13. //#define CAMERA_MODEL_M5STACK_PSRAM
  14. #define CAMERA_MODEL_M5STACK_WIDE
  15. //#define CAMERA_MODEL_AI_THINKER
  16. #include "camera_pins.h"
  17. const char* ssid = "goldin_lab1";
  18. const char* password = "goldin53743001";
  19. const char* websocket_server_host = "60.250.156.230";
  20. const uint16_t websocket_server_port = 8090;
  21. String IFTTTUrl="http://maker.ifttt.com/trigger/Test/with/key/c3xo5EvpBX64fPEqxphcR4jBTzDh1r2joTDsB_BslOA";
  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. IPAddress dns(8, 8, 8, 8); // Google DNS
  31. const char* getphoto ="getphoto";
  32. const char* statusok ="statusok";
  33. void setup() {
  34. Serial.begin(115200);
  35. Serial.setDebugOutput(true);
  36. Serial.println();
  37. camera_config_t config;
  38. config.ledc_channel = LEDC_CHANNEL_0;
  39. config.ledc_timer = LEDC_TIMER_0;
  40. config.pin_d0 = Y2_GPIO_NUM;
  41. config.pin_d1 = Y3_GPIO_NUM;
  42. config.pin_d2 = Y4_GPIO_NUM;
  43. config.pin_d3 = Y5_GPIO_NUM;
  44. config.pin_d4 = Y6_GPIO_NUM;
  45. config.pin_d5 = Y7_GPIO_NUM;
  46. config.pin_d6 = Y8_GPIO_NUM;
  47. config.pin_d7 = Y9_GPIO_NUM;
  48. config.pin_xclk = XCLK_GPIO_NUM;
  49. config.pin_pclk = PCLK_GPIO_NUM;
  50. config.pin_vsync = VSYNC_GPIO_NUM;
  51. config.pin_href = HREF_GPIO_NUM;
  52. config.pin_sscb_sda = SIOD_GPIO_NUM;
  53. config.pin_sscb_scl = SIOC_GPIO_NUM;
  54. config.pin_pwdn = PWDN_GPIO_NUM;
  55. config.pin_reset = RESET_GPIO_NUM;
  56. config.xclk_freq_hz = 10000000;
  57. config.pixel_format = PIXFORMAT_JPEG;
  58. //init with high specs to pre-allocate larger buffers
  59. if(psramFound()){
  60. config.frame_size = FRAMESIZE_UXGA;
  61. config.jpeg_quality = 10;
  62. config.fb_count = 2;
  63. } else {
  64. config.frame_size = FRAMESIZE_SVGA;
  65. config.jpeg_quality = 12;
  66. config.fb_count = 1;
  67. }
  68. // camera init
  69. esp_err_t err = esp_camera_init(&config);
  70. if (err != ESP_OK) {
  71. Serial.printf("Camera init failed with error 0x%x", err);
  72. return;
  73. }
  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, 1); // 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,dns)) {
  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. /*
  134. String url=IFTTTUrl+"?value1="+getphoto+"&value2="+statusok;
  135. //client.poll();
  136. //Start to send data to IFTTT
  137. HTTPClient http;
  138. Serial.print("[HTTP] begin...\n");
  139. http.begin(url); //HTTP
  140. Serial.print("[HTTP] GET...\n");
  141. // start connection and send HTTP header
  142. int httpCode = http.GET();
  143. // httpCode will be negative on error
  144. if(httpCode > 0) {
  145. // HTTP header has been send and Server response header has been handled
  146. Serial.printf("[HTTP] GET... code: %d\n", httpCode);
  147. // file found at server
  148. if(httpCode == HTTP_CODE_OK) {
  149. String payload = http.getString();
  150. Serial.println(payload);
  151. }
  152. } else {
  153. Serial.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str());
  154. }
  155. http.end();
  156. //delay(1000);//send data every 20 seconds
  157. */
  158. }