Custom sensors
You can add up to 3 user defined sensors. Insert your sensor's payload scheme in sensor.cpp
.
The following exampls show how to add a custom temperature and humidty sensor.
- Add variables or needed libraries
- Add sensor specific code to
sensor_init
insensor.cpp
- Add sensor specific code to
sensor_read
function insensor.cpp
- Add Payload functions to
payload.h
andpayload.cpp
(Optional) - Use payload functions in
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.
You might also add a constant for your custom sensor in the paxcounter.conf
file. This is optional but can be used to identify the sensor type.
shared/paxcounter_orig.conf | |
---|---|
- See usage of this in the example below
1. Add variables or needed libraries
If you want to use any libary for you custom sensor you have to add it to the platformio.ini
file.
In this example we use the HTU2xD_SHT2x_Si70xx
for the GY-21 sensor.
platformio.ini | |
---|---|
- Selected the env you want to use. In this example we use the
usb
env. - Add the libary to the
lib_deps
section.
Add the import of libary in the sensor.cpp
file.
sensor.cpp | |
---|---|
- Define
HAS_GY21
either in hal file of your board or inpaxcounter.conf
file.
2. Add sensor specific code to sensor_init
function
- Define
HAS_GY21
either in hal file of your board or inpaxcounter.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.
- These logs are only for debugging. You can remove them if you want.
- These logs are only for debugging. You can remove them if you want.
- 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.
src/payload.h | |
---|---|
Then you have to add your payload function to the payload.cpp
file. You can provide functions for all payload formates (see Payload Formats) or just add it for the one you are using.
Example for a custom temperature / humidity payload function
5. Sending the data
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.
- Add your custom payload function here.
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.