Hi,
first of all Im a newbie so sorry if the problem im encountering is just an easy fix.
I want to send an stream of images to a PC using ESP32-Cam for an image processing application. I need to do this using UDP.
now I have made a code using ChatGPT and tweaked it a little bit, but the problem is that every time the ESP32 connects to the WIFI, it automatically resets.
I powered it with a suitable power source and a 650uF capacitor to prevent power shortage.
I also turned off the watch dog and the brownout detection in the code just in case, but couldn't fix the problem.
here is my code right now:
#include <WiFi.h>
#include <esp_camera.h>
#include <WiFiUdp.h>
const char* ssid = "***";
const char* password = "***";
WiFiUDP udp;
const unsigned int udpPort = 1234;
IPAddress remoteIP(192, 168, 100, 4);
#define PWDN_GPIO_NUM 32
#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 setup() {
Serial.begin(115200);
WiFi.setSleep(false); // Disable Wi-Fi sleep mode
// Disable brownout detector
esp_sleep_pd_config(ESP_PD_DOMAIN_RTC_PERIPH, ESP_PD_OPTION_OFF);
// Print heap memory before Wi-Fi connection attempt
Serial.print("Free heap before Wi-Fi: ");
Serial.println(ESP.getFreeHeap());
Serial.print("Connecting to Wi-Fi...");
WiFi.begin(ssid, password);
// Wait for Wi-Fi connection
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("\nConnected!");
Serial.println(WiFi.localIP());
// Print heap memory after Wi-Fi connection
Serial.print("Free heap after Wi-Fi: ");
Serial.println(ESP.getFreeHeap());
// Camera configuration
camera_config_t config;
config.pin_pwdn = PWDN_GPIO_NUM;
config.pin_reset = RESET_GPIO_NUM;
config.pin_xclk = XCLK_GPIO_NUM;
config.pin_sccb_sda = SIOD_GPIO_NUM;
config.pin_sccb_scl = SIOC_GPIO_NUM;
config.pin_d7 = Y7_GPIO_NUM;
config.pin_d6 = Y6_GPIO_NUM;
config.pin_d5 = Y5_GPIO_NUM;
config.pin_d4 = Y4_GPIO_NUM;
config.pin_d3 = Y3_GPIO_NUM;
config.pin_d2 = Y2_GPIO_NUM;
config.pin_vsync = VSYNC_GPIO_NUM;
config.pin_href = HREF_GPIO_NUM;
config.pin_pclk = PCLK_GPIO_NUM;
config.xclk_freq_hz = 20000000;
config.ledc_timer = LEDC_TIMER_0;
config.ledc_channel = LEDC_CHANNEL_0;
config.pixel_format = PIXFORMAT_JPEG; // Use JPEG format for better compression
config.frame_size = FRAMESIZE_VGA; // Lower resolution for reduced memory usage
config.jpeg_quality = 50; // Higher compression (smaller files)
config.fb_count = 1; // Reduce buffer count to conserve memory
// Print free heap memory before camera initialization
Serial.print("Free heap before camera init: ");
Serial.println(ESP.getFreeHeap());
// Initialize camera
if (esp_camera_init(&config) != ESP_OK) {
Serial.println("Camera init failed");
while (true)
; // Halt if camera fails
}
// Print free heap memory after camera initialization
Serial.print("Free heap after camera init: ");
Serial.println(ESP.getFreeHeap());
// Start UDP communication
udp.begin(udpPort);
Serial.println("UDP initialized.");
delay(5000); // Yield to system tasks
}
void loop() {
camera_fb_t* fb = esp_camera_fb_get(); // Capture a frame from the camera
if (!fb) {
Serial.println("Camera capture failed");
delay(1000); // Allow recovery time
return;
}
size_t packetSize = 1024; // Send in chunks
size_t remaining = fb->len;
size_t offset = 0;
// Send frame data over UDP in smaller packets
while (remaining > 0) {
size_t chunkSize = (remaining > packetSize) ? packetSize : remaining;
udp.beginPacket(remoteIP, udpPort);
udp.write(fb->buf + offset, chunkSize);
udp.endPacket();
offset += chunkSize;
remaining -= chunkSize;
// Yield to avoid watchdog timer reset
yield();
}
// Return the frame buffer
esp_camera_fb_return(fb);
Serial.println("Frame sent.");
delay(5000); // Yield to system tasks
}
and heres the error I get in the serial monitor after running it:
I used the camera_pin.h file in the examples to set the camera pins in this code.
with the AI Thinker camera, which worked fine in the other examples with HTTP
That is not what your error messages show. Does not look like it ever connected.
I would remove the Wifi. setSleep and esp_sleep_pd_config and see if it works.
yes this is actually after some tweaking i did, if i remove the lines you said, the Wifi will connect, creap an IP and reset again, this is my serial monitor (keeps sending this):
rst:0x8 (TG1WDT_SYS_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
configsip: 0, SPIWP:0xee
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:DIO, clock div:1
load:0x3fff0030,len:4604
ho 0 tail 12 room 4
load:0x40078000,len:15488
load:0x40080400,len:4
load:0x40080404,len:3180
entry 0x400805b8
Free heap before Wi-Fi: 294452
Connecting to Wi-Fi........
Connected!
192.168.100.110
Free heap after Wi-Fi: 241484
Free heap before camera init: 241484
ets Jul 29 2019 12:21:46
I don't have the ESP-cam but I ran your code on an ESP32 devkit and it got as far as printing "UDP initialized" then gave an error. Not sure but it probably has something to do with sending the UDP packets.
I found this project on Github and used its code which is fairly different from the one generated by ChatGPT, it had some errors while compiling because of a change in the ESP32 library, and also the camera pin config wasnt correct,
so heres the fixed code:
/////////////////////////////////////////////////////////////////
/*
ESP32CAM | Sending images via UDP?
Video Tutorial: https://youtu.be/1kHxd5FOUEU
Created by Eric N. (ThatProject)
*/
/////////////////////////////////////////////////////////////////
#include "esp_camera.h"
#include <WiFi.h>
#include <WiFiUdp.h>
//#define CAMERA_MODEL_WROVER_KIT // Has PSRAM
//#define CAMERA_MODEL_ESP_EYE // Has PSRAM
//#define CAMERA_MODEL_ESP32S3_EYE // Has PSRAM
//#define CAMERA_MODEL_M5STACK_PSRAM // Has PSRAM
//#define CAMERA_MODEL_M5STACK_V2_PSRAM // M5Camera version B Has PSRAM
//#define CAMERA_MODEL_M5STACK_WIDE // Has PSRAM
//#define CAMERA_MODEL_M5STACK_ESP32CAM // No PSRAM
//#define CAMERA_MODEL_M5STACK_UNITCAM // No PSRAM
//#define CAMERA_MODEL_M5STACK_CAMS3_UNIT // Has PSRAM
#define CAMERA_MODEL_AI_THINKER // Has PSRAM
//#define CAMERA_MODEL_TTGO_T_JOURNAL // No PSRAM
//#define CAMERA_MODEL_XIAO_ESP32S3 // Has PSRAM
// ** Espressif Internal Boards **
//#define CAMERA_MODEL_ESP32_CAM_BOARD
//#define CAMERA_MODEL_ESP32S2_CAM_BOARD
//#define CAMERA_MODEL_ESP32S3_CAM_LCD
//#define CAMERA_MODEL_DFRobot_FireBeetle2_ESP32S3 // Has PSRAM
//#define CAMERA_MODEL_DFRobot_Romeo_ESP32S3 // Has PSRAM
#include "camera_pins.h"
#define CHUNK_LENGTH 1460
const char* ssid = "MilaGroup";
const char* password = "ZJ52Jbj8";
const int udpPort = 8000;
const char* udpAddress = "192.168.100.4";
boolean connected = false;
WiFiUDP udp;
void setup() {
Serial.begin(115200);
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.frame_size = FRAMESIZE_VGA; // Set to VGA resolution (640x480)
config.pixel_format = PIXFORMAT_JPEG; // for streaming
//config.pixel_format = PIXFORMAT_RGB565; // for face detection/recognition
config.grab_mode = CAMERA_GRAB_WHEN_EMPTY;
config.fb_location = CAMERA_FB_IN_PSRAM;
config.jpeg_quality = 8;
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);
return;
}
//Connect to the WiFi network
connectToWiFi(ssid, password);
}
void loop() {
//only send data when connected
if (connected) {
camera_fb_t* fb = NULL;
esp_err_t res = ESP_OK;
fb = esp_camera_fb_get();
if (!fb) {
Serial.println("Camera capture failed");
esp_camera_fb_return(fb);
return;
}
if (fb->format != PIXFORMAT_JPEG) {
Serial.println("PIXFORMAT_JPEG not implemented");
esp_camera_fb_return(fb);
return;
}
sendPacketData((const char*)fb->buf, fb->len, CHUNK_LENGTH);
esp_camera_fb_return(fb);
}
}
void connectToWiFi(const char* ssid, const char* pwd) {
Serial.println("Connecting to WiFi network: " + String(ssid));
WiFi.disconnect(true);
WiFi.onEvent(WiFiEvent);
WiFi.begin(ssid, pwd);
Serial.println("Waiting for WIFI connection...");
}
void WiFiEvent(WiFiEvent_t event, WiFiEventInfo_t info) {
switch (event) {
case ARDUINO_EVENT_WIFI_STA_GOT_IP:
Serial.print("WiFi connected! IP address: ");
Serial.println(WiFi.localIP());
udp.begin(WiFi.localIP(), udpPort);
connected = true;
break;
case ARDUINO_EVENT_WIFI_STA_DISCONNECTED:
Serial.println("WiFi lost connection");
connected = false;
break;
}
}
void sendPacketData(const char* buf, uint16_t len, uint16_t chunkLength) {
uint8_t buffer[chunkLength];
size_t blen = sizeof(buffer);
size_t rest = len % blen;
for (uint8_t i = 0; i < len / blen; ++i) {
memcpy(buffer, buf + (i * blen), blen);
udp.beginPacket(udpAddress, udpPort);
udp.write(buffer, chunkLength);
udp.endPacket();
//Serial.println("Frame Sent");
}
if (rest) {
memcpy(buffer, buf + (len - rest), rest);
udp.beginPacket(udpAddress, udpPort);
udp.write(buffer, rest);
udp.endPacket();
//Serial.println("Frame Sent1");
}
}
to use this you have to add the code below in a new tab named "camera_pins.h"
I fixed the issue.
apparently the ESP32 ran out of memory or something similar (I'm just guessing). I fixed it by using a different partition scheme (in tools drop down menu) while uploading the code.