对于该线程的长度表示歉意。
基本上,我已经成功在 ESP32 和本地 XAMPP 服务器之间建立了活动连接。该服务器存储从 ESP32 捕获的图像,然后使用 Gemini Flash API 对其进行分析。这部分按预期工作。
但是,我正在努力建立一个活动的 WebSocket 连接,以将响应发送回我的 ESP32 以触发特定的微控制器功能。不幸的是,终端显示以下错误消息:
socket_connect(): unable to connect [10061]: No connection could be made because the target machine actively refused it in <b>C:\xampp\htdocs\image_server\index.php</b> on line <b>15</b><br />
这些是以下代码片段以及 Wireshark 捕获:
#include <Arduino.h>
#include <ArduinoJson.h>
#include <WiFi.h>
#include <WiFiClient.h>
#include <WiFiServer.h>
#include "esp_camera.h"
#include <FreeRTOS.h>
#include <task.h>
TaskHandle_t captureTaskHandle;
TaskHandle_t serverTaskHandle;
const char* ssid = "SSID";
const char* password = "PASS";
String serverName = "192.168.0.98"; // LOCAL SEVER IP ADDRESS
String serverPath = "/image_server/index.php"; // SERVER PATH => index.php
const int serverPort = 8000; // PORT XAMPP IS LISTENING ON TO RECEIVE DATA
#define port 80 // THE PORT THE ESP32 IS LISTENING ON
WiFiServer server(port);
WiFiClient client;
// CAMERA_MODEL_AI_THINKER
#define PWDN_GPIO_NUM -1
#define RESET_GPIO_NUM -1
#define XCLK_GPIO_NUM 15
#define SIOD_GPIO_NUM 4
#define SIOC_GPIO_NUM 5
#define Y2_GPIO_NUM 11
#define Y3_GPIO_NUM 9
#define Y4_GPIO_NUM 8
#define Y5_GPIO_NUM 10
#define Y6_GPIO_NUM 12
#define Y7_GPIO_NUM 18
#define Y8_GPIO_NUM 17
#define Y9_GPIO_NUM 16
#define VSYNC_GPIO_NUM 6
#define HREF_GPIO_NUM 7
#define PCLK_GPIO_NUM 13
const int timerInterval = 30000; // time between each HTTP POST image
void setup() {
Serial.begin(115200);
WiFi.mode(WIFI_STA);
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(500);
}
Serial.println();
Serial.print("ESP32-CAM IP Address: ");
Serial.println(WiFi.localIP());
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_sccb_sda = SIOD_GPIO_NUM;
config.pin_sccb_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;
// init with high specs to pre-allocate larger buffers
if(psramFound()){
config.frame_size = FRAMESIZE_SVGA;
config.jpeg_quality = 5; //0-63 lower number means higher quality
config.fb_count = 1;
} else {
config.frame_size = FRAMESIZE_CIF;
config.jpeg_quality = 12; //0-63 lower number means higher quality
config.fb_count = 1;
}
// camera init
esp_err_t err = esp_camera_init(&config);
if (err != ESP_OK) {
Serial.printf("Camera init failed with error 0x%x", err);
delay(1000);
ESP.restart();
}
// IGNORE CALLS UPON SENDING IMAGE FUNCTION IN FULL CODE.
xTaskCreate(captureTask, "Capture Task", 4096, NULL, 1, &captureTaskHandle);
// Create server task on core 1
xTaskCreate(serverTask, "Server Task", 2048, NULL, 1, &serverTaskHandle);
}
void serverTask(void *pvParameters) {
while (1) {
// Code for handling incoming connections to the ESP32
WiFiClient client = server.available(); // Wait for incoming connections
if (client) {
Serial.println("Client connected.");
// Receive data from the client (PHP script)
String receivedData = "";
while (client.connected() && client.available()) {
char c = client.read();
receivedData += c;
}
Serial.println("Received data:");
Serial.println(receivedData);
client.stop(); // Close the connection
Serial.println("Client disconnected.");
}
vTaskDelay(pdMS_TO_TICKS(100)); // Delay for server task
}
}
void loop() {
}
function sendContent($dataToSend)
{
echo('Sending data: '. $dataToSend);
$host = "192.168.0.65";
$port = 80; // Standard HTTP port
// Create a socket connection
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if (!$socket) {
echo "socket_create() failed: " . socket_last_error() . PHP_EOL;
exit;
}
// Connect to the ESP32 server
if (!socket_connect($socket, $host, $port)) {
echo "socket_connect() failed: " . socket_last_error() . PHP_EOL;
exit;
}
// Send data to the ESP32
$bytesSent = socket_write($socket, $dataToSend, strlen($dataToSend));
if ($bytesSent === FALSE) {
echo "socket_write() failed: " . socket_last_error() . PHP_EOL;
exit;
}
echo "Sent " . $bytesSent . " bytes of data to ESP32." . PHP_EOL;
// Optional: Receive response from the ESP32 (if implemented in the ESP32 code)
$response = socket_read($socket, 1024); // Adjust buffer size as needed
if ($response !== FALSE) {
echo "Received response from ESP32: " . $response . PHP_EOL;
}
// Close the socket connection
socket_close($socket);
}
问题是我从未真正在 void setup() 中使用以下命令启动服务器:
server.begin();