CameraWebsocket_esp32.ino 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. #include "esp_camera.h"
  2. #include <WiFi.h>
  3. #include <ArduinoWebsockets.h>
  4. #include <HTTPClient.h>
  5. //
  6. // WARNING!!! Make sure that you have either selected ESP32 Wrover Module,
  7. // or another board which has PSRAM enabled
  8. //
  9. // Select camera model
  10. //#define CAMERA_MODEL_WROVER_KIT
  11. //#define CAMERA_MODEL_ESP_EYE
  12. //#define CAMERA_MODEL_M5STACK_PSRAM
  13. //#define CAMERA_MODEL_M5STACK_WIDE
  14. #define CAMERA_MODEL_AI_THINKER
  15. #include "camera_pins.h"
  16. const char* ssid = "goldin_lab1";
  17. const char* password = "goldin53743001";
  18. const char* websocket_server_host = "60.250.156.230";
  19. const uint16_t websocket_server_port = 8093;
  20. String IFTTTUrl="http://maker.ifttt.com/trigger/Test/with/key/c3xo5EvpBX64fPEqxphcR4jBTzDh1r2joTDsB_BslOA";
  21. void startCameraServer();
  22. using namespace websockets;
  23. WebsocketsClient client;
  24. //Set Static IP Address
  25. IPAddress local_IP(192, 168, 50, 83);
  26. //Set Gateway IP Address
  27. IPAddress gateway(192, 168, 50, 254);
  28. IPAddress subnet(255, 255, 255, 0);
  29. IPAddress dns(8, 8, 8, 8); // Google DNS
  30. const char* getphoto ="getphoto";
  31. const char* statusok ="statusok";
  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. sensor_t * s = esp_camera_sensor_get();
  74. // initial sensors are flipped vertically and colors are a bit saturated
  75. if (s->id.PID == OV3660_PID) {
  76. s->set_vflip(s, 1); // flip it back
  77. s->set_brightness(s, 2); // up the brightness just a bit
  78. s->set_saturation(s, -2); // lower the saturation
  79. s->set_contrast(s, 2); // -2 to 2
  80. }
  81. // drop down frame size for higher initial frame rate
  82. s->set_framesize(s, FRAMESIZE_QVGA);
  83. #if defined(CAMERA_MODEL_M5STACK_WIDE) || defined(CAMERA_MODEL_M5STACK_ESP32CAM)
  84. s->set_hmirror(s, 1); // 0 = disable , 1 = enable
  85. s->set_vflip(s, 0); // 0 = disable , 1 = enable
  86. s->set_brightness(s, -2);
  87. s->set_framesize(s, FRAMESIZE_QVGA);
  88. #endif
  89. client.send("Hi Server!");
  90. if(!WiFi.config(local_IP, gateway, subnet,dns)) {
  91. Serial.println("Set IP Failed");
  92. }
  93. WiFi.begin(ssid, password);
  94. while (WiFi.status() != WL_CONNECTED) {
  95. delay(500);
  96. Serial.print(".");
  97. }
  98. Serial.println("");
  99. Serial.println("WiFi connected");
  100. startCameraServer();
  101. Serial.print("Camera Ready! Use 'http://");
  102. Serial.print(WiFi.localIP());
  103. Serial.println("' to connect");
  104. while(!client.connect(websocket_server_host, websocket_server_port, "/")){
  105. delay(500);
  106. Serial.print(".");
  107. }
  108. Serial.println("Websocket Connected!");
  109. }
  110. void loop() {
  111. camera_fb_t *fb = esp_camera_fb_get();
  112. if(!fb){
  113. Serial.println("Camera capture failed");
  114. esp_camera_fb_return(fb);
  115. return;
  116. }
  117. if(fb->format != PIXFORMAT_JPEG){
  118. Serial.println("Non-JPEG data not implemented");
  119. return;
  120. }
  121. client.sendBinary((const char*) fb->buf, fb->len);
  122. esp_camera_fb_return(fb);
  123. if ((WiFi.status()!= WL_CONNECTED)){
  124. WiFi.reconnect();
  125. Serial.println("WiFi reconnected");
  126. }
  127. while(!client.connect(websocket_server_host, websocket_server_port, "/")){
  128. delay(500);
  129. Serial.print(".");
  130. }
  131. Serial.println("Websocket Connected!");
  132. String url=IFTTTUrl+"?value1="+getphoto+"&value2="+statusok;
  133. //client.poll();
  134. //Start to send data to IFTTT
  135. HTTPClient http;
  136. Serial.print("[HTTP] begin...\n");
  137. http.begin(url); //HTTP
  138. Serial.print("[HTTP] GET...\n");
  139. // start connection and send HTTP header
  140. int httpCode = http.GET();
  141. // httpCode will be negative on error
  142. if(httpCode > 0) {
  143. // HTTP header has been send and Server response header has been handled
  144. Serial.printf("[HTTP] GET... code: %d\n", httpCode);
  145. // file found at server
  146. if(httpCode == HTTP_CODE_OK) {
  147. String payload = http.getString();
  148. Serial.println(payload);
  149. }
  150. } else {
  151. Serial.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str());
  152. }
  153. http.end();
  154. //delay(1000);//send data every 20 seconds
  155. }