Skip to content

ESP32Cam - Test project combining video with multiple sensors, including TSL2561 (I2C), DS18B20 (OneWire), AM312 and Switch.

Notifications You must be signed in to change notification settings

JJFourie/ESP32Cam_I2C_OneWire_Switch

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

14 Commits
 
 
 
 
 
 

Repository files navigation

ESP32Cam_I2C_OneWire_Switch

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.

Description

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

Setup

  1. Copy the code (main.cpp and also configuration.h) to your local project.
  2. Set your own SSID and password.
  3. Update the Adafruit libraries as described below.
  4. Connect the sensors as shown in the Fritzing diagram and table below.
  5. 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

Issues

Although not all that difficult, there are a couple of problems that must be kept in mind to make this work.

  1. 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.
  2. Attach Interrupts
    The GPIO interrupt service can only be initialized once, and that already happens when the camera is initialized. This means that the normal attachInterrupt() command can't be used. Use gpio_set_intr_type() and gpio_isr_handler_add() instead.
  3. 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.
  4. 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.
  5. Setup Sequence
    The order in which the functionality is initialized in setup() is important to make things work and avoid errors.

Resolve Struct Conflict

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:

  1. Include the esp_camera.h library before the Adafruit_Sensor.h and Adafruit_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).
  2. Compile the sketch. There may be errors as shown below. Click on each of the errors (in Platform IO, under Problems) to open the relevant file and put the cursor on the offending line.
  3. 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)
Wire Diagram

Fritzing Diagram

  • 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.

.

Compile Errors caused by struct conflict

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


About

ESP32Cam - Test project combining video with multiple sensors, including TSL2561 (I2C), DS18B20 (OneWire), AM312 and Switch.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published