You can add up to 3 user defined sensors. Insert your sensor's payload scheme in [`sensor.cpp`](https://github.com/cyberman54/ESP32-Paxcounter/blob/master/src/sensor.cpp).
The following exampls show how to add a custom temperature and humidty sensor.
2. Add sensor specific code to `sensor_init` in [`sensor.cpp`](https://github.com/cyberman54/ESP32-Paxcounter/blob/master/src/sensor.cpp)
3. Add sensor specific code to `sensor_read` function in [`sensor.cpp`](https://github.com/cyberman54/ESP32-Paxcounter/blob/master/src/sensor.cpp)
4. Add Payload functions to [`payload.h`](https://github.com/cyberman54/ESP32-Paxcounter/blob/master/include/payload.h) and [`payload.cpp`](https://github.com/cyberman54/ESP32-Paxcounter/blob/master/src/payload.cpp) (Optional)
5. Use payload functions in [`sensor.cpp`](https://github.com/cyberman54/ESP32-Paxcounter/blob/master/src/sensor.cpp) to send sensor data
## Example 1: Custom Temperature and Humidity Sensor *GY-21*
To use a custom sensor you first have to enable the Sensor which you want to use. For this you have to edit or add `HAS_SENSOR_1` in either the `paxcounter.conf` or the `hal` file of your board.
1. Define `HAS_GY21` either in hal file of your board or in `paxcounter.conf` file.
### 2. Add sensor specific code to `sensor_init` function
```c linenums="1" title="src/sensor.cpp"
#if (HAS_GY21) // (1)
if (ht2x.begin() != true) // reset sensor, set heater off, set resolution, check power
// (sensor doesn't operate correctly if VDD < +2.25v)
{
ESP_LOGE(TAG, "HTU2xD/SHT2x not connected, fail or VDD < +2.25v");
} else {
ESP_LOGE(TAG, "HTU2xD/SHT2x/GY21 found");
}
#endif // HAS_GY21
```
1. Define `HAS_GY21` either in hal file of your board or in `paxcounter.conf` file.
### 3. Add sensor specific code to sensor_read function
In this case we choose that our custom sensor is Sensor 3. This means the data will be sent on `SENSOR3PORT` which is by default `12`. You can change this in the `paxcounter.conf` file.
```c linenums="78" title="src/sensor.cpp"
case 3:
#if (HAS_GY21)
ESP_LOGE(TAG, "Reading Sensor 3, GY21"); // (1)
temperature =
ht2x.readTemperature(); // accuracy +-0.3C in range 0C..60C at 14-bit
delay(100);
humidity =
ht2x.readHumidity(); // accuracy +-2% in range 20%..80%/25C at 12-bit
1. These logs are only for debugging. You can remove them if you want.
2. These logs are only for debugging. You can remove them if you want.
3. These logs are only for debugging. You can remove them if you want.
### 4. Payload functions for a custom sensor
If you have added your custom sensor code as described before you can also add custom payload function if you need others than the provided ones. For this you have to change two files. First you have to add your payload function to the `payload.h` file.
=== "Example for a custom temperature / humidity payload function"
Then you have to add your payload function to the `payload.cpp` file. You can provide functions for all payload formates (see [Payload Formats](../payloadformat.md)) or just add it for the one you are using.
Example for a custom temperature / humidity payload function
After you have added your custom sensor code and payload function you can send the data. For this you have to add the following code to the `sensor.cpp` file.
Now you can build and upload the code to your ESP. Do not forget to erase the flash before uploading since you probably changed the `paxcounter.conf` file.