ESP32-Cam based project to test the use of multiple types of sensors in combination with streaming video. This sketch has no other purpose than to test that all sensors can be read correctly, while at the same time streaming video from the web server hosted on the ESP32-Cam.
The sketch can be used to verify your hardware is functioning ok before you develop/run your own sketch, or use it as a base to add your own functionality, just use the relevant code to support whatever sensors you are going to use.
When the sketch is successfully compiled and all sensors are connected, the following will be shown as debug output:
- periodic temperature and luminosity sensor readings.
- PIR movement detection.
- switch status changes when it is opened or closed.
Using a browser, the video stream can be viewed by connecting to the URL of the ESP32-Cam. ( http://<IP adress>
)
- Copy the code (
main.cpp
and alsoconfiguration.h
) to your local project. - Set your own SSID and password.
- Update the Adafruit libraries as described below.
- Connect the sensors as shown in the Fritzing diagram and table below.
- Compile the sketch.
Sensors used:
Type | Sensor | Function | GPIO |
---|---|---|---|
I2C | TSL2561 | Luminosity (Lux) | 14 (SDA) 15 (SCL) |
Wire | DS18B20 | Temperature | 2 |
Binary | AM312 | PIR | 13 |
Switch | Reed | Open/Close | 12 |
Although not all that difficult, there are a couple of problems that must be kept in mind to make this work.
- I2C
The default I2C pins in a ESP32 are GPIO 22 (SCL) and GPIO 21 (SDA). Problem when using I2C in combination with the ESP32-Cam is that these pins are already used by the camera, so you have to remap SDA and SCL to other pins. - Attach Interrupts
The GPIO interrupt service can only be initialized once, and that already happens when the camera is initialized. This means that the normalattachInterrupt()
command can't be used. Usegpio_set_intr_type()
andgpio_isr_handler_add()
instead. - Interrupt Pins
Not just any of the available GPIO pins can be used to attach a hardware interrupt. E.g. pin 16 is constantly triggered when video is streaming. - Adafruit Libraries
When (some of?) the Adafruit Sensor libraries are used together with the ESP camera libraries, there are errors raised during compilation because different structures in these libraries share the same name. See below. - Setup Sequence
The order in which the functionality is initialized insetup()
is important to make things work and avoid errors.
A struct with the same name, sensor_t
, but with a different definition exists in both the esp_camera
library as well as some of the Adafruit Sensor libraries. When these libraries are used together in the same project, errors are raised as shown below. These errors can be solved by renaming the struct in the Arduino libraries using the following steps:
- Include the
esp_camera.h
library before theAdafruit_Sensor.h
andAdafruit_TSL2561_U.h
libraries. This will help to identify the occurrences of the struct name in the Adafruit libraries (else the errors are reported in the ESP library). - Compile the sketch. There may be errors as shown below. Click on each of the errors (in
Platform IO
, underProblems
) to open the relevant file and put the cursor on the offending line. - Rename the
sensor_t
struct in the Adafruit libraries to something else, e.g.sensor_tas
(for Adafruit Sensor).
Below are the files I had to change in my case:
-Adafruit_Sensor.h
(x2)
-Adafruit_Sensor.cpp
(x1)
-Adafruit_TSL2561_U.h
(x1)
-Adafruit_TSL2561_U.cpp
(x2)
- The white wires are only necessary when flashing the board with your sketch, or when debugging in order to see the output of debug statements in the IDE.
Note: Ensure your FTDI card pins match the layout in the diagram! My (fake?) card pin layout was the exact opposite, a mirror image of the most common cards.
.
In file included from src\main.cpp:26:0:
.pio\libdeps\esp32cam\Adafruit Unified Sensor/Adafruit_Sensor.h:155:3: error: conflicting declaration 'typedef struct sensor_t sensor_t'
} sensor_t;
^
In file included from C:\Users\johan\.platformio\packages\framework-arduinoespressif32\tools\sdk\include\esp32-camera/esp_camera.h:70:0,
from src\main.cpp:17:
C:\Users\johan\.platformio\packages\framework-arduinoespressif32\tools\sdk\include\esp32-camera/sensor.h:191:3: note: previous declaration as 'typedef struct _sensor sensor_t'
} sensor_t;
^
src\main.cpp: In function 'esp_err_t cam_Setup()':
src\main.cpp:172:57: error: 'err' was not declared in this scope
Serial.printf("Camera init failed with error 0x%x", err);
^
src\main.cpp:173:5: error: return-statement with no value, in function returning 'esp_err_t {aka int}' [-fpermissive]
return;
^
Archiving .pio\build\esp32cam\libba4\libAdafruit Unified Sensor.a
*** [.pio\build\esp32cam\src\main.cpp.o] Error 1