#include 
#include 
#include 

// SoftAP 
const char* ssid = "ESP32-RADAR";
const char* password = "";

// 
const int trigPins[2] = {13, 15};
const int echoPins[2] = {12, 14};

// Web 
WebServer server(80);

// 
#define PWDN_GPIO_NUM     -1
#define RESET_GPIO_NUM    -1
#define XCLK_GPIO_NUM      0
#define SIOD_GPIO_NUM     26
#define SIOC_GPIO_NUM     27

#define Y9_GPIO_NUM       35
#define Y8_GPIO_NUM       34
#define Y7_GPIO_NUM       39
#define Y6_GPIO_NUM       36
#define Y5_GPIO_NUM       21
#define Y4_GPIO_NUM       19
#define Y3_GPIO_NUM       18
#define Y2_GPIO_NUM        5
#define VSYNC_GPIO_NUM    25
#define HREF_GPIO_NUM     23
#define PCLK_GPIO_NUM     22

void startCamera() {
  camera_config_t config;
  config.ledc_channel = LEDC_CHANNEL_0;
  config.ledc_timer   = LEDC_TIMER_0;
  config.pin_d0       = Y2_GPIO_NUM;
  config.pin_d1       = Y3_GPIO_NUM;
  config.pin_d2       = Y4_GPIO_NUM;
  config.pin_d3       = Y5_GPIO_NUM;
  config.pin_d4       = Y6_GPIO_NUM;
  config.pin_d5       = Y7_GPIO_NUM;
  config.pin_d6       = Y8_GPIO_NUM;
  config.pin_d7       = Y9_GPIO_NUM;
  config.pin_xclk     = XCLK_GPIO_NUM;
  config.pin_pclk     = PCLK_GPIO_NUM;
  config.pin_vsync    = VSYNC_GPIO_NUM;
  config.pin_href     = HREF_GPIO_NUM;
  config.pin_sscb_sda = SIOD_GPIO_NUM;
  config.pin_sscb_scl = SIOC_GPIO_NUM;
  config.pin_pwdn     = PWDN_GPIO_NUM;
  config.pin_reset    = RESET_GPIO_NUM;
  config.xclk_freq_hz = 20000000;
  config.pixel_format = PIXFORMAT_JPEG;

  config.frame_size   = FRAMESIZE_QVGA;  // 图像大小:320x240
  config.jpeg_quality = 10;
  config.fb_count     = 1;

  esp_err_t err = esp_camera_init(&config);
  if (err != ESP_OK) {
    Serial.printf("Camera init failed with error 0x%x", err);
    return;
  }
}

float readDistanceCM(int trigPin, int echoPin) {
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  long duration = pulseIn(echoPin, HIGH, 30000);
  return (duration == 0) ? -1 : duration * 0.0343 / 2.0;
}

// 距离 JSON 接口
void handleDistance() {
  float d1 = readDistanceCM(trigPins[0], echoPins[0]);
  float d2 = readDistanceCM(trigPins[1], echoPins[1]);

  String json = "{";
  json += "\"レーダー1\":" + String(d1, 1) + ",";
  json += "\"レーダー2\":" + String(d2, 1);
  json += "}";

  server.send(200, "application/json", json);
}

// 图像流
void handleStream() {
  WiFiClient client = server.client();
  String response = "HTTP/1.1 200 OK\r\n";
  response += "Content-Type: multipart/x-mixed-replace; boundary=frame\r\n\r\n";
  client.print(response);

  while (1) {
    camera_fb_t *fb = esp_camera_fb_get();
    if (!fb) continue;
    client.printf("--frame\r\nContent-Type: image/jpeg\r\nContent-Length: %u\r\n\r\n", fb->len);
    client.write(fb->buf, fb->len);
    client.print("\r\n");
    esp_camera_fb_return(fb);
    delay(100);
    if (!client.connected()) break;
  }
}

// 主页面 HTML
const char MAIN_page[] PROGMEM = R"rawliteral(



  
  ESP32 駐車レーダー
  


  

リアルタイム映像+距離センサー

測定中...
)rawliteral"; // void handleRoot() { server.send_P(200, "text/html", MAIN_page); } void setup() { Serial.begin(115200); for (int i = 0; i < 2; i++) { pinMode(trigPins[i], OUTPUT); pinMode(echoPins[i], INPUT); } // startCamera(); // SoftAP WiFi.softAP(ssid, password); IPAddress IP = WiFi.softAPIP(); Serial.print("SoftAP IP address: "); Serial.println(IP); // server.on("/", handleRoot); server.on("/distance", handleDistance); server.on("/mjpeg", HTTP_GET, handleStream); server.begin(); } void loop() { server.handleClient(); }