NannyCam.ino 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. #include <WiFi.h>
  2. #include "esp_camera.h"
  3. // Replace with your network credentials
  4. const char* ssid = "Flipper0";
  5. const char* password = "12345678";
  6. // Create an instance of the nannyServer
  7. WiFiServer nannyServer(80);
  8. void nanny_cam_setup() {
  9. //Serial.begin(115200);
  10. // Start access point
  11. WiFi.mode(WIFI_AP);
  12. WiFi.softAP(ssid, password);
  13. // Print IP address
  14. IPAddress IP = WiFi.softAPIP();
  15. // Start the camera
  16. startCamera();
  17. // Start the nannyServer
  18. nannyServer.begin();
  19. //Serial.println("Server started");
  20. Serial.print("SSID: ");
  21. Serial.println(ssid);
  22. Serial.print("PW: ");
  23. Serial.println(password);
  24. Serial.print("IP: ");
  25. Serial.println(IP);
  26. }
  27. void nanny_cam_loop() {
  28. //Serial.print(".");
  29. // Wait for a client to connect
  30. WiFiClient client = nannyServer.available();
  31. if (client) {
  32. //Serial.println("New client connected");
  33. handleStreamRequest();
  34. }
  35. }
  36. void startCamera() {
  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 = 20000000;
  57. config.pixel_format = PIXFORMAT_JPEG;
  58. // Frame size: 640x480
  59. config.frame_size = FRAMESIZE_VGA;
  60. // Two buffers for streaming video
  61. config.jpeg_quality = 10;
  62. config.fb_count = 2;
  63. // Initialize the camera
  64. esp_err_t err = esp_camera_init(&config);
  65. if (err != ESP_OK) {
  66. Serial.printf("Camera init failed with error 0x%x", err);
  67. return;
  68. }
  69. // Set the focus to infinity
  70. /*sensor_t* s = esp_camera_sensor_get();
  71. s->set_focus(s, 0);*/
  72. }
  73. void handleStreamRequest() {
  74. WiFiClient client = nannyServer.available();
  75. // Send HTTP headers
  76. client.println("HTTP/1.1 200 OK");
  77. client.println("Content-Type: multipart/x-mixed-replace; boundary=frame");
  78. client.println("");
  79. // Start streaming JPEG frames
  80. while (client.connected()) {
  81. camera_fb_t* fb = esp_camera_fb_get();
  82. if (!fb) {
  83. Serial.println("Camera capture failed");
  84. break;
  85. }
  86. // Send frame header
  87. client.println("--frame");
  88. client.println("Content-Type: image/jpeg");
  89. client.print("Content-Length: ");
  90. client.println(fb->len);
  91. client.println("");
  92. // Send frame data
  93. client.write(fb->buf, fb->len);
  94. client.println("");
  95. // Release the buffer
  96. esp_camera_fb_return(fb);
  97. }
  98. // Disconnect the client
  99. client.stop();
  100. //Serial.println("Client disconnected");
  101. }