Kingwebbie001
Kingwebbie001

Reputation: 31

Sending data from local XAMPP php server to ESP32-S3-VROOM, returning => unable to connect [10061]

Apologies for the length of this thread.

Essentially, I've managed to set up an active connection between my ESP32 and a local XAMPP server. This server stores a captured image from the ESP32, which is then analysed using the Gemini Flash API. This part works as expected.

However, I'm struggling to establish an active WebSocket connection to send the response back to my ESP32 to trigger specific microcontroller functions. Unfortunately, the terminal is displaying the following error message:

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 />

These are the following code snippets along with a Wireshark capture:

C++ Arduino Code:

#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() {
}

PHP send function:

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);
}

Wireshark capture:

Wireshark network capture (2 requests)

Upvotes: 1

Views: 62

Answers (1)

Kingwebbie001
Kingwebbie001

Reputation: 31

The problem was I never actually begin the server in void setup() using:

server.begin();

Upvotes: 0

Related Questions