- We are integrating an ESP32 camera module,TTL(FT231RL), a PIR motion sensor (HC-SR 501), and a DHT22 temperature sensor.
Circuit connections
Actual Circuit
- Board Selected in IDE: AI Thinker ESP32-CAM
We have a working code for the camera module that displays a live video stream on the local IP. When integrating PIR and DHT22 and using the below code, we are continuously getting no serial data received error.
Combined Code:
#include "esp_camera.h"
#include <WiFi.h>
#include <WebServer.h>
#include "DHT.h"
// WiFi Credentials
const char* ssid = "Ipdgang";
const char* password = "heyhihowr";
// Pin Configuration for ESP32-CAM
#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
// PIR and DHT22 Pins
#define PIR_PIN 33
#define DHT_PIN 4
#define DHT_TYPE DHT22
// Declare WebServer object
WebServer server(80);
DHT dht(DHT_PIN, DHT_TYPE);
// Function Prototypes
void handleRoot();
void handleCapture();
void handleStream();
void handleSensorData();
void detectMotion();
// Global Variables
volatile bool motionDetected = false;
void IRAM_ATTR onMotionDetected() {
motionDetected = true;
}
void setup() {
// Disable brownout detector
Serial.begin(115200);
// Initialize PIR sensor
pinMode(PIR_PIN, INPUT);
attachInterrupt(digitalPinToInterrupt(PIR_PIN), onMotionDetected, RISING);
// Initialize DHT sensor
dht.begin();
// Camera Configuration
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_VGA;
config.jpeg_quality = 10;
config.fb_count = 2;
// Camera Initialization
esp_err_t err = esp_camera_init(&config);
if (err != ESP_OK) {
Serial.printf("Camera init failed with error 0x%x", err);
return;
}
// WiFi Connection
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("\nWiFi Connected");
Serial.print("Camera Stream Address: http://");
Serial.print(WiFi.localIP());
Serial.println("/");
// Web Server Routes
server.on("/", HTTP_GET, handleRoot);
server.on("/capture", HTTP_GET, handleCapture);
server.on("/stream", HTTP_GET, handleStream);
server.on("/sensors", HTTP_GET, handleSensorData);
server.begin();
}
void handleRoot() {
String html = "<html><body>"
"<h1>ESP32-CAM Web Server</h1>"
"<a href='/capture'>Capture Image</a><br>"
"<a href='/stream'>Start Stream</a><br>"
"<a href='/sensors'>View Sensor Data</a>"
"</body></html>";
server.send(200, "text/html", html);
}
void handleCapture() {
camera_fb_t *fb = esp_camera_fb_get();
if (!fb) {
server.send(500, "text/plain", "Failed to capture image");
return;
}
server.send_P(200, "image/jpeg", (const char*)fb->buf, fb->len);
esp_camera_fb_return(fb);
}
void handleStream() {
WiFiClient client = server.client();
String response = "HTTP/1.1 200 OK\r\n"
"Content-Type: multipart/x-mixed-replace; boundary=frame\r\n\r\n";
client.print(response);
while(client.connected()) {
camera_fb_t *fb = esp_camera_fb_get();
if (!fb) continue;
String frameHeader = "--frame\r\n"
"Content-Type: image/jpeg\r\n"
"Content-Length: " + String(fb->len) + "\r\n\r\n";
client.print(frameHeader);
client.write(fb->buf, fb->len);
client.print("\r\n");
esp_camera_fb_return(fb);
delay(100);
}
}
void handleSensorData() {
float temperature = dht.readTemperature();
float humidity = dht.readHumidity();
if (isnan(temperature) || isnan(humidity)) {
server.send(500, "text/plain", "Failed to read from DHT sensor");
return;
}
String html = "<html><body>"
"<h1>Sensor Data</h1>"
"<p>Temperature: " + String(temperature) + "°C</p>"
"<p>Humidity: " + String(humidity) + "%</p>"
"</body></html>";
server.send(200, "text/html", html);
}
void loop() {
server.handleClient();
detectMotion();
}
void detectMotion() {
if (motionDetected) {
Serial.println("Motion detected!");
motionDetected = false;
}
}
Output Window:
Sketch uses 1023797 bytes (32%) of program storage space. Maximum is 3145728 bytes.
Global variables use 50420 bytes (15%) of dynamic memory, leaving 277260 bytes for local variables. Maximum is 327680 bytes.
esptool.py v4.6
Serial port COM6
Connecting......................................
A fatal error occurred: Failed to connect to ESP32: No serial data received.
For troubleshooting steps visit: https://docs.espressif.com/projects/esptool/en/latest/troubleshooting.html
Serial Monitor Output: Getting garbage values.