Version 1.0.2
Added support AHT, AM2320 and BH1750 sensors.
This commit is contained in:
parent
ae77483afc
commit
9d83004f1a
8
.gitmodules
vendored
8
.gitmodules
vendored
@ -18,4 +18,10 @@
|
||||
url = https://github.com/aZholtikov/zh_onewire
|
||||
[submodule "components/zh_vector"]
|
||||
path = components/zh_vector
|
||||
url = https://github.com/aZholtikov/zh_vector
|
||||
url = https://github.com/aZholtikov/zh_vector
|
||||
[submodule "components/zh_bh1750"]
|
||||
path = components/zh_bh1750
|
||||
url = https://github.com/aZholtikov/zh_bh1750
|
||||
[submodule "components/zh_aht"]
|
||||
path = components/zh_aht
|
||||
url = https://github.com/aZholtikov/zh_aht
|
25
README.md
25
README.md
@ -9,14 +9,18 @@ ESP-NOW based sensor for ESP32 ESP-IDF and ESP8266 RTOS SDK.
|
||||
|
||||
## Features
|
||||
|
||||
1. Supported sensors:
|
||||
1. DS18B20
|
||||
2. DHT11/22
|
||||
2. Support sensor power management.
|
||||
3. Automatically adds sensor configuration to Home Assistan via MQTT discovery as a sensor.
|
||||
4. Update firmware from HTTPS server via ESP-NOW.
|
||||
5. Direct or mesh work mode.
|
||||
6. Support of external or battery powering.
|
||||
1. Supported 1-wire sensors:
|
||||
1. [DS18B20](https://github.com/aZholtikov/zh_ds18b20)
|
||||
2. [DHT11/DHT22/AM2302/AM2320](https://github.com/aZholtikov/zh_dht)
|
||||
2. Supported I2C sensors:
|
||||
1. [AM2320](https://github.com/aZholtikov/zh_dht)
|
||||
2. [BH1750](https://github.com/aZholtikov/zh_bh1750)
|
||||
3. [AHT20/AHT21](https://github.com/aZholtikov/zh_aht)
|
||||
3. Optional support sensor power management (for 1-wire sensors only).
|
||||
4. Automatically adds sensor configuration to Home Assistan via MQTT discovery as a sensor.
|
||||
5. Update firmware from HTTPS server via ESP-NOW.
|
||||
6. Direct or mesh work mode.
|
||||
7. External or battery powering.
|
||||
|
||||
## Notes
|
||||
|
||||
@ -30,7 +34,7 @@ ESP-NOW based sensor for ESP32 ESP-IDF and ESP8266 RTOS SDK.
|
||||
|
||||
MQTT configuration message should filled according to the template "X1,X2,X3,X4,X5,X6". Where:
|
||||
|
||||
1. X1 - Sensor type. 1 for DS18B20, 2 for DHT11, 3 for DHT22.
|
||||
1. X1 - Sensor type. 1 for DS18B20, 2 for AHT, 8 for DHT, 9 for BH1750.
|
||||
2. X2 - Sensor GPIO number 1 (main pin for 1-wire sensors, SDA pin for I2C sensors). 0 - 48 (according to the module used), 255 if not used.
|
||||
3. X3 - Sensor GPIO number 2 (SCL pin for I2C sensors). 0 - 48 (according to the module used), 255 if not used.
|
||||
4. X4 - Power GPIO number (if using sensor power control). 0 - 48 (according to the module used), 255 if not used.
|
||||
@ -69,8 +73,7 @@ make flash
|
||||
1. A gateway is required. For details see [zh_gateway](https://github.com/aZholtikov/zh_gateway).
|
||||
2. Use the "make ota" command instead of "make" to prepare 2 bin files for OTA update (for ESP8266).
|
||||
3. For ESP8266 battery powering mode GPIO16 must be connected to RST.
|
||||
4. A battery powering mode sensor power management is highly recommended for greater stability.
|
||||
5. Updates and setting changes are only available in external power mode. To temporarily disable battery mode, enable "menuconfig -> ZH ESP-NOW Sensor Configuration -> Enable power mode selection at startup" and set the "Power selection GPIO number". Connecting this GPIO to GND during startup will temporarily disable the battery mode.
|
||||
4. Updates and setting changes are only available in external power mode. To temporarily disable battery mode, enable "menuconfig -> ZH ESP-NOW Sensor Configuration -> Enable power mode selection at startup" and set the "Power selection GPIO number". Connecting this GPIO to GND during startup will temporarily disable the battery mode.
|
||||
|
||||
Thanks to [Rajeev Tandon](https://github.com/rajtan) for some good advice.
|
||||
|
||||
|
1
components/zh_aht
Submodule
1
components/zh_aht
Submodule
@ -0,0 +1 @@
|
||||
Subproject commit 735dda94d3cd552b5b22792362dc8bc95c413aba
|
1
components/zh_bh1750
Submodule
1
components/zh_bh1750
Submodule
@ -0,0 +1 @@
|
||||
Subproject commit 54c3b1669749458d1ab33aa27ac2afcea6fb9a31
|
@ -1 +1 @@
|
||||
Subproject commit ec60c4d5596150a139e7237c7a685e5b2d2df8fb
|
||||
Subproject commit 49a4564b99119d9713bb60790bbf341782c03647
|
@ -1 +1 @@
|
||||
Subproject commit 7339f1544b59184ee3e77e1266ce08910b392768
|
||||
Subproject commit 7662593a30319809aa5160fa382689b675615f15
|
@ -1 +1 @@
|
||||
Subproject commit 88bf4433203939a411a5ca3232955e5cdb8698ad
|
||||
Subproject commit cbf2c69e4452378681d77e7f273b0489a8ffaa56
|
@ -1 +1 @@
|
||||
Subproject commit 10a5837c8fffd027d2d6a0709644f69a0bc85be1
|
||||
Subproject commit 742c807c416d9f80c0571f8daea85d275e8d4c93
|
@ -1 +1 @@
|
||||
Subproject commit 76281e85be8bd7570c5a0d5d49626242c0c798c3
|
||||
Subproject commit 783b44fbca0ed467a868b4420f191c6d9f6caea5
|
@ -1 +1 @@
|
||||
Subproject commit c2993a50590d397fbfb9de19419ce80d905b70b3
|
||||
Subproject commit d006e1e40c44ac847c373f8458983031123bca90
|
@ -1 +1 @@
|
||||
Subproject commit 899c3feed8df95f937c4f802ba880adf5e6140da
|
||||
Subproject commit 53cdc7687388840c04470fb35e84e83b27175ae5
|
@ -28,41 +28,63 @@ menu "ZH ESP-NOW Sensor Configuration"
|
||||
bool "N"
|
||||
config SENSOR_TYPE_DS18B20
|
||||
bool "DS18B20"
|
||||
config SENSOR_TYPE_DHT11
|
||||
bool "DHT11"
|
||||
config SENSOR_TYPE_DHT22
|
||||
bool "DHT22"
|
||||
config SENSOR_TYPE_DHT
|
||||
bool "DHT"
|
||||
config SENSOR_TYPE_AHT
|
||||
bool "AHT"
|
||||
config SENSOR_TYPE_BH1750
|
||||
bool "BH1750"
|
||||
endchoice
|
||||
|
||||
choice CONNECTION_TYPE
|
||||
depends on SENSOR_TYPE_DS18B20 || SENSOR_TYPE_DHT || SENSOR_TYPE_BH1750 || SENSOR_TYPE_AHT
|
||||
prompt "Connection type"
|
||||
default CONNECTION_TYPE_ONEWIRE
|
||||
config CONNECTION_TYPE_ONEWIRE
|
||||
depends on SENSOR_TYPE_DS18B20 || SENSOR_TYPE_DHT
|
||||
bool "ONE WIRE"
|
||||
config CONNECTION_TYPE_I2C
|
||||
depends on SENSOR_TYPE_DHT || SENSOR_TYPE_BH1750 || SENSOR_TYPE_AHT
|
||||
bool "I2C"
|
||||
endchoice
|
||||
|
||||
config SENSOR_PIN_1
|
||||
depends on CONNECTION_TYPE_ONEWIRE || CONNECTION_TYPE_I2C
|
||||
int "Sensor GPIO number (main pin for 1-wire sensors, SDA pin for I2C sensors)"
|
||||
range 0 GPIO_RANGE_MAX
|
||||
default 5
|
||||
help
|
||||
Sensor GPIO (main pin for 1-wire sensors, SDA pin for I2C sensors).
|
||||
|
||||
config SENSOR_PIN_2
|
||||
depends on CONNECTION_TYPE_I2C
|
||||
int "Sensor GPIO number (SCL pin for I2C sensors)"
|
||||
range 0 GPIO_RANGE_MAX
|
||||
default 14
|
||||
help
|
||||
Sensor GPIO (SCL pin for I2C sensors).
|
||||
|
||||
config MEASUREMENT_FREQUENCY
|
||||
depends on CONNECTION_TYPE_ONEWIRE || CONNECTION_TYPE_I2C
|
||||
int "Measurement frequency"
|
||||
range 1 65536
|
||||
default 300
|
||||
help
|
||||
Measurement frequency (sleep time on battery power).
|
||||
|
||||
config BATTERY_POWERED
|
||||
depends on SENSOR_TYPE_DS18B20 || SENSOR_TYPE_DHT11 || SENSOR_TYPE_DHT22
|
||||
depends on CONNECTION_TYPE_ONEWIRE || CONNECTION_TYPE_I2C
|
||||
bool "Battery powered"
|
||||
default false
|
||||
help
|
||||
Battery powered.
|
||||
|
||||
config MEASUREMENT_FREQUENCY
|
||||
depends on SENSOR_TYPE_DS18B20 || SENSOR_TYPE_DHT11 || SENSOR_TYPE_DHT22
|
||||
int "Measurement frequency"
|
||||
range 1 65536
|
||||
default 300
|
||||
help
|
||||
Measurement frequency.
|
||||
|
||||
config SENSOR_PIN_1
|
||||
depends on SENSOR_TYPE_DS18B20 || SENSOR_TYPE_DHT11 || SENSOR_TYPE_DHT22
|
||||
int "Sensor GPIO number"
|
||||
range 0 GPIO_RANGE_MAX
|
||||
default 5
|
||||
help
|
||||
Sensor GPIO.
|
||||
|
||||
config POWER_CONTROL_USING
|
||||
depends on SENSOR_TYPE_DS18B20 || SENSOR_TYPE_DHT11 || SENSOR_TYPE_DHT22
|
||||
depends on CONNECTION_TYPE_ONEWIRE
|
||||
bool "Enable using sensor power control"
|
||||
default true
|
||||
help
|
||||
Enable using sensor power control.
|
||||
Enable using sensor power control (for 1-wire sensors only).
|
||||
|
||||
config POWER_CONTROL_PIN
|
||||
depends on POWER_CONTROL_USING
|
||||
|
@ -74,17 +74,19 @@ void zh_load_config(sensor_config_t *sensor_config)
|
||||
nvs_close(nvs_handle);
|
||||
#ifdef CONFIG_SENSOR_TYPE_DS18B20
|
||||
sensor_config->hardware_config.sensor_type = HAST_DS18B20;
|
||||
#elif CONFIG_SENSOR_TYPE_DHT11
|
||||
sensor_config->hardware_config.sensor_type = HAST_DHT11;
|
||||
#elif CONFIG_SENSOR_TYPE_DHT22
|
||||
sensor_config->hardware_config.sensor_type = HAST_DHT22;
|
||||
#elif CONFIG_SENSOR_TYPE_DHT
|
||||
sensor_config->hardware_config.sensor_type = HAST_DHT;
|
||||
#elif CONFIG_SENSOR_TYPE_AHT
|
||||
sensor_config->hardware_config.sensor_type = HAST_AHT;
|
||||
#elif CONFIG_SENSOR_TYPE_BH1750
|
||||
sensor_config->hardware_config.sensor_type = HAST_BH1750;
|
||||
#else
|
||||
sensor_config->hardware_config.sensor_type = HAST_NONE;
|
||||
#endif
|
||||
#ifdef CONFIG_MEASUREMENT_FREQUENCY
|
||||
sensor_config->hardware_config.measurement_frequency = CONFIG_MEASUREMENT_FREQUENCY;
|
||||
#else
|
||||
sensor_config->hardware_config.measurement_frequency = 0;
|
||||
sensor_config->hardware_config.measurement_frequency = 300;
|
||||
#endif
|
||||
#ifdef CONFIG_SENSOR_PIN_1
|
||||
sensor_config->hardware_config.sensor_pin_1 = CONFIG_SENSOR_PIN_1;
|
||||
@ -113,9 +115,7 @@ void zh_load_config(sensor_config_t *sensor_config)
|
||||
nvs_get_u8(nvs_handle, "sensor_pin_1", &sensor_config->hardware_config.sensor_pin_1);
|
||||
nvs_get_u8(nvs_handle, "sensor_pin_2", &sensor_config->hardware_config.sensor_pin_2);
|
||||
nvs_get_u8(nvs_handle, "power_pin", &sensor_config->hardware_config.power_pin);
|
||||
uint16_t measurement_frequency = {0};
|
||||
nvs_get_u16(nvs_handle, "frequency", &measurement_frequency); // Just to prevent a compiler warning.
|
||||
sensor_config->hardware_config.measurement_frequency = measurement_frequency;
|
||||
nvs_get_u16(nvs_handle, "frequency", &sensor_config->hardware_config.measurement_frequency);
|
||||
nvs_get_u8(nvs_handle, "battery_power", (uint8_t *)&sensor_config->hardware_config.battery_power);
|
||||
nvs_close(nvs_handle);
|
||||
}
|
||||
@ -184,6 +184,30 @@ void zh_sensor_init(sensor_config_t *sensor_config)
|
||||
gpio_set_level(sensor_config->hardware_config.power_pin, 0);
|
||||
}
|
||||
}
|
||||
if (sensor_config->hardware_config.sensor_type != HAST_NONE && sensor_config->hardware_config.sensor_pin_1 != ZH_NOT_USED && sensor_config->hardware_config.sensor_pin_2 != ZH_NOT_USED)
|
||||
{
|
||||
#ifdef CONFIG_IDF_TARGET_ESP8266
|
||||
i2c_config_t i2c_config = {
|
||||
.mode = I2C_MODE_MASTER,
|
||||
.sda_io_num = sensor_config->hardware_config.sensor_pin_1,
|
||||
.sda_pullup_en = GPIO_PULLUP_ENABLE,
|
||||
.scl_io_num = sensor_config->hardware_config.sensor_pin_2,
|
||||
.scl_pullup_en = GPIO_PULLUP_ENABLE,
|
||||
};
|
||||
i2c_driver_install(I2C_PORT, i2c_config.mode);
|
||||
i2c_param_config(I2C_PORT, &i2c_config);
|
||||
#else
|
||||
i2c_master_bus_config_t i2c_bus_config = {
|
||||
.clk_source = I2C_CLK_SRC_DEFAULT,
|
||||
.i2c_port = I2C_PORT,
|
||||
.scl_io_num = sensor_config->hardware_config.sensor_pin_2,
|
||||
.sda_io_num = sensor_config->hardware_config.sensor_pin_1,
|
||||
.glitch_ignore_cnt = 7,
|
||||
.flags.enable_internal_pullup = true,
|
||||
};
|
||||
i2c_new_master_bus(&i2c_bus_config, &sensor_config->i2c_bus_handle);
|
||||
#endif
|
||||
}
|
||||
if (sensor_config->hardware_config.sensor_pin_1 != ZH_NOT_USED)
|
||||
{
|
||||
switch (sensor_config->hardware_config.sensor_type)
|
||||
@ -191,18 +215,70 @@ void zh_sensor_init(sensor_config_t *sensor_config)
|
||||
case HAST_DS18B20:
|
||||
if (zh_onewire_init(sensor_config->hardware_config.sensor_pin_1) != ESP_OK)
|
||||
{
|
||||
sensor_config->hardware_config.sensor_pin_1 = ZH_NOT_USED;
|
||||
goto ZH_SENSOR_ERROR;
|
||||
}
|
||||
break;
|
||||
case HAST_DHT11:
|
||||
case HAST_DHT22:;
|
||||
zh_dht_sensor_type_t sensor_type = (sensor_config->hardware_config.sensor_type == HAST_DHT11) ? ZH_DHT11 : ZH_DHT22;
|
||||
sensor_config->dht_handle = zh_dht_init(sensor_type, sensor_config->hardware_config.sensor_pin_1);
|
||||
sensor_config->hardware_config.sensor_pin_1 = sensor_config->dht_handle.sensor_pin;
|
||||
case HAST_DHT:;
|
||||
zh_dht_init_config_t dht_init_config = ZH_DHT_INIT_CONFIG_DEFAULT();
|
||||
if (sensor_config->hardware_config.sensor_pin_2 == ZH_NOT_USED)
|
||||
{
|
||||
dht_init_config.sensor_pin = sensor_config->hardware_config.sensor_pin_1;
|
||||
}
|
||||
else
|
||||
{
|
||||
#ifdef CONFIG_IDF_TARGET_ESP8266
|
||||
dht_init_config.i2c_port = I2C_PORT;
|
||||
#else
|
||||
dht_init_config.i2c_handle = sensor_config->i2c_bus_handle;
|
||||
#endif
|
||||
}
|
||||
if (zh_dht_init(&dht_init_config) != ESP_OK)
|
||||
{
|
||||
goto ZH_SENSOR_ERROR;
|
||||
}
|
||||
break;
|
||||
case HAST_BH1750:;
|
||||
zh_bh1750_init_config_t bh1750_init_config = ZH_BH1750_INIT_CONFIG_DEFAULT();
|
||||
bh1750_init_config.auto_adjust = true;
|
||||
#ifdef CONFIG_IDF_TARGET_ESP8266
|
||||
bh1750_init_config.i2c_port = I2C_PORT;
|
||||
#else
|
||||
bh1750_init_config.i2c_handle = sensor_config->i2c_bus_handle;
|
||||
#endif
|
||||
if (zh_bh1750_init(&bh1750_init_config) != ESP_OK)
|
||||
{
|
||||
goto ZH_SENSOR_ERROR;
|
||||
}
|
||||
break;
|
||||
case HAST_BMP280: // For future development.
|
||||
break;
|
||||
case HAST_BME280: // For future development.
|
||||
break;
|
||||
case HAST_BME680: // For future development.
|
||||
break;
|
||||
case HAST_AHT:;
|
||||
zh_aht_init_config_t aht_init_config = ZH_AHT_INIT_CONFIG_DEFAULT();
|
||||
#ifdef CONFIG_IDF_TARGET_ESP8266
|
||||
aht_init_config.i2c_port = I2C_PORT;
|
||||
#else
|
||||
aht_init_config.i2c_handle = sensor_config->i2c_bus_handle;
|
||||
#endif
|
||||
if (zh_aht_init(&aht_init_config) != ESP_OK)
|
||||
{
|
||||
goto ZH_SENSOR_ERROR;
|
||||
}
|
||||
break;
|
||||
case HAST_SHT: // For future development.
|
||||
break;
|
||||
case HAST_HTU: // For future development.
|
||||
break;
|
||||
case HAST_HDC1080: // For future development.
|
||||
break;
|
||||
default:
|
||||
ZH_SENSOR_ERROR:
|
||||
sensor_config->hardware_config.sensor_type = HAST_NONE;
|
||||
sensor_config->hardware_config.sensor_pin_1 = ZH_NOT_USED;
|
||||
sensor_config->hardware_config.sensor_pin_2 = ZH_NOT_USED;
|
||||
break;
|
||||
}
|
||||
}
|
||||
@ -290,31 +366,54 @@ uint8_t zh_send_sensor_config_message(const sensor_config_t *sensor_config)
|
||||
data.payload_data.config_message.sensor_config_message.qos = 2;
|
||||
data.payload_data.config_message.sensor_config_message.retain = true;
|
||||
char *unit_of_measurement = NULL;
|
||||
data.payload_data.config_message.sensor_config_message.unique_id = 1;
|
||||
data.payload_data.config_message.sensor_config_message.sensor_device_class = HASDC_VOLTAGE;
|
||||
unit_of_measurement = "V";
|
||||
strcpy(data.payload_data.config_message.sensor_config_message.unit_of_measurement, unit_of_measurement);
|
||||
zh_send_message(sensor_config->gateway_mac, (uint8_t *)&data, sizeof(zh_espnow_data_t));
|
||||
++messages_quantity;
|
||||
switch (sensor_config->hardware_config.sensor_type)
|
||||
{
|
||||
case HAST_DS18B20:
|
||||
data.payload_data.config_message.sensor_config_message.unique_id = 1;
|
||||
data.payload_data.config_message.sensor_config_message.unique_id = 2;
|
||||
data.payload_data.config_message.sensor_config_message.sensor_device_class = HASDC_TEMPERATURE;
|
||||
unit_of_measurement = "°C";
|
||||
strcpy(data.payload_data.config_message.sensor_config_message.unit_of_measurement, unit_of_measurement);
|
||||
zh_send_message(sensor_config->gateway_mac, (uint8_t *)&data, sizeof(zh_espnow_data_t));
|
||||
++messages_quantity;
|
||||
break;
|
||||
case HAST_DHT11:
|
||||
case HAST_DHT22:
|
||||
data.payload_data.config_message.sensor_config_message.unique_id = 1;
|
||||
case HAST_DHT:
|
||||
case HAST_AHT:
|
||||
case HAST_SHT: // For future development.
|
||||
case HAST_HTU: // For future development.
|
||||
case HAST_HDC1080: // For future development.
|
||||
data.payload_data.config_message.sensor_config_message.unique_id = 2;
|
||||
data.payload_data.config_message.sensor_config_message.sensor_device_class = HASDC_TEMPERATURE;
|
||||
unit_of_measurement = "°C";
|
||||
strcpy(data.payload_data.config_message.sensor_config_message.unit_of_measurement, unit_of_measurement);
|
||||
zh_send_message(sensor_config->gateway_mac, (uint8_t *)&data, sizeof(zh_espnow_data_t));
|
||||
++messages_quantity;
|
||||
data.payload_data.config_message.sensor_config_message.unique_id = 2;
|
||||
data.payload_data.config_message.sensor_config_message.unique_id = 3;
|
||||
data.payload_data.config_message.sensor_config_message.sensor_device_class = HASDC_HUMIDITY;
|
||||
unit_of_measurement = "%";
|
||||
strcpy(data.payload_data.config_message.sensor_config_message.unit_of_measurement, unit_of_measurement);
|
||||
zh_send_message(sensor_config->gateway_mac, (uint8_t *)&data, sizeof(zh_espnow_data_t));
|
||||
++messages_quantity;
|
||||
break;
|
||||
case HAST_BH1750:
|
||||
data.payload_data.config_message.sensor_config_message.unique_id = 2;
|
||||
data.payload_data.config_message.sensor_config_message.sensor_device_class = HASDC_ILLUMINANCE;
|
||||
unit_of_measurement = "lx";
|
||||
strcpy(data.payload_data.config_message.sensor_config_message.unit_of_measurement, unit_of_measurement);
|
||||
zh_send_message(sensor_config->gateway_mac, (uint8_t *)&data, sizeof(zh_espnow_data_t));
|
||||
++messages_quantity;
|
||||
break;
|
||||
case HAST_BMP280: // For future development.
|
||||
break;
|
||||
case HAST_BME280: // For future development.
|
||||
break;
|
||||
case HAST_BME680: // For future development.
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
@ -326,83 +425,90 @@ void zh_send_sensor_status_message_task(void *pvParameter)
|
||||
sensor_config_t *sensor_config = pvParameter;
|
||||
float humidity = 0.0;
|
||||
float temperature = 0.0;
|
||||
float illuminance = 0.0;
|
||||
zh_espnow_data_t data = {0};
|
||||
data.device_type = ZHDT_SENSOR;
|
||||
data.payload_type = ZHPT_STATE;
|
||||
data.payload_data.status_message.sensor_status_message.sensor_type = sensor_config->hardware_config.sensor_type;
|
||||
for (;;)
|
||||
{
|
||||
if (sensor_config->hardware_config.power_pin != ZH_NOT_USED && sensor_config->hardware_config.sensor_pin_1 != ZH_NOT_USED)
|
||||
if (sensor_config->hardware_config.power_pin != ZH_NOT_USED && sensor_config->hardware_config.sensor_pin_1 != ZH_NOT_USED && sensor_config->hardware_config.sensor_pin_2 == ZH_NOT_USED)
|
||||
{
|
||||
gpio_set_level(sensor_config->hardware_config.power_pin, 1);
|
||||
vTaskDelay(1000 / portTICK_PERIOD_MS); // Power stabilization period after the sensor is turned on. The value is selected experimentally. DHT11/22 requires 1 second.
|
||||
switch (sensor_config->hardware_config.sensor_type)
|
||||
{
|
||||
case HAST_DS18B20:
|
||||
vTaskDelay(DS18B20_POWER_STABILIZATION_PERIOD / portTICK_PERIOD_MS);
|
||||
break;
|
||||
case HAST_DHT:
|
||||
vTaskDelay(DHT_POWER_STABILIZATION_PERIOD / portTICK_PERIOD_MS);
|
||||
break;
|
||||
default:
|
||||
gpio_set_level(sensor_config->hardware_config.power_pin, 0);
|
||||
break;
|
||||
}
|
||||
}
|
||||
esp_err_t err = ESP_OK;
|
||||
switch (sensor_config->hardware_config.sensor_type)
|
||||
{
|
||||
case HAST_DS18B20:
|
||||
ZH_DS18B20_READ:
|
||||
switch (zh_ds18b20_read(NULL, &temperature))
|
||||
err = zh_ds18b20_read(NULL, &temperature);
|
||||
if (err == ESP_OK)
|
||||
{
|
||||
case ESP_OK:
|
||||
data.payload_data.status_message.sensor_status_message.temperature = temperature;
|
||||
break;
|
||||
case ESP_FAIL:
|
||||
if (sensor_config->hardware_config.battery_power == false)
|
||||
{
|
||||
vTaskDelay(10000 / portTICK_PERIOD_MS);
|
||||
goto ZH_DS18B20_READ;
|
||||
}
|
||||
break;
|
||||
case ESP_ERR_INVALID_CRC:
|
||||
if (sensor_config->hardware_config.battery_power == false)
|
||||
{
|
||||
vTaskDelay(1000 / portTICK_PERIOD_MS);
|
||||
goto ZH_DS18B20_READ;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
data.payload_data.status_message.sensor_status_message.voltage = 3.3; // For future development.
|
||||
}
|
||||
break;
|
||||
case HAST_DHT11:
|
||||
case HAST_DHT22:
|
||||
ZH_DHT_READ:
|
||||
switch (zh_dht_read(&sensor_config->dht_handle, &humidity, &temperature))
|
||||
case HAST_DHT:
|
||||
err = zh_dht_read(&humidity, &temperature);
|
||||
if (err == ESP_OK)
|
||||
{
|
||||
case ESP_OK:
|
||||
data.payload_data.status_message.sensor_status_message.humidity = humidity;
|
||||
data.payload_data.status_message.sensor_status_message.temperature = temperature;
|
||||
break;
|
||||
case ESP_ERR_INVALID_RESPONSE:
|
||||
if (sensor_config->hardware_config.battery_power == false)
|
||||
{
|
||||
vTaskDelay(10000 / portTICK_PERIOD_MS);
|
||||
goto ZH_DHT_READ;
|
||||
}
|
||||
break;
|
||||
case ESP_ERR_TIMEOUT:
|
||||
if (sensor_config->hardware_config.battery_power == false)
|
||||
{
|
||||
vTaskDelay(10000 / portTICK_PERIOD_MS);
|
||||
goto ZH_DHT_READ;
|
||||
}
|
||||
break;
|
||||
case ESP_ERR_INVALID_CRC:
|
||||
if (sensor_config->hardware_config.battery_power == false)
|
||||
{
|
||||
vTaskDelay(3000 / portTICK_PERIOD_MS);
|
||||
goto ZH_DHT_READ;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
data.payload_data.status_message.sensor_status_message.voltage = 3.3; // For future development.
|
||||
}
|
||||
break;
|
||||
case HAST_BH1750:
|
||||
err = zh_bh1750_read(&illuminance);
|
||||
if (err == ESP_OK)
|
||||
{
|
||||
data.payload_data.status_message.sensor_status_message.illuminance = illuminance;
|
||||
data.payload_data.status_message.sensor_status_message.voltage = 3.3; // For future development.
|
||||
}
|
||||
break;
|
||||
case HAST_BMP280: // For future development.
|
||||
break;
|
||||
case HAST_BME280: // For future development.
|
||||
break;
|
||||
case HAST_BME680: // For future development.
|
||||
break;
|
||||
case HAST_AHT:
|
||||
err = zh_aht_read(&humidity, &temperature);
|
||||
if (err == ESP_OK)
|
||||
{
|
||||
data.payload_data.status_message.sensor_status_message.humidity = humidity;
|
||||
data.payload_data.status_message.sensor_status_message.temperature = temperature;
|
||||
data.payload_data.status_message.sensor_status_message.voltage = 3.3; // For future development.
|
||||
}
|
||||
break;
|
||||
case HAST_SHT: // For future development.
|
||||
break;
|
||||
case HAST_HTU: // For future development.
|
||||
break;
|
||||
case HAST_HDC1080: // For future development.
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
zh_send_message(sensor_config->gateway_mac, (uint8_t *)&data, sizeof(zh_espnow_data_t));
|
||||
if (sensor_config->hardware_config.power_pin != ZH_NOT_USED && sensor_config->hardware_config.sensor_pin_1 != ZH_NOT_USED)
|
||||
if (err == ESP_OK)
|
||||
{
|
||||
zh_send_message(sensor_config->gateway_mac, (uint8_t *)&data, sizeof(zh_espnow_data_t));
|
||||
}
|
||||
else
|
||||
{
|
||||
zh_send_message(sensor_config->gateway_mac, (uint8_t *)&data, sizeof(zh_espnow_data_t)); // For future development. Will be changed for sensor read error message.
|
||||
}
|
||||
if (gpio_get_level(sensor_config->hardware_config.power_pin) == 1)
|
||||
{
|
||||
gpio_set_level(sensor_config->hardware_config.power_pin, 0);
|
||||
}
|
||||
|
@ -11,6 +11,8 @@
|
||||
#include "esp_sleep.h"
|
||||
#include "zh_ds18b20.h"
|
||||
#include "zh_dht.h"
|
||||
#include "zh_bh1750.h"
|
||||
#include "zh_aht.h"
|
||||
#include "zh_config.h"
|
||||
|
||||
#ifdef CONFIG_NETWORK_TYPE_DIRECT
|
||||
@ -47,6 +49,11 @@
|
||||
#define get_app_description() esp_app_get_description()
|
||||
#endif
|
||||
|
||||
#define I2C_PORT (I2C_NUM_MAX - 1)
|
||||
|
||||
#define DS18B20_POWER_STABILIZATION_PERIOD 500 // Power stabilization period after the sensor is turned on. The value is selected experimentally.
|
||||
#define DHT_POWER_STABILIZATION_PERIOD 2000 // Power stabilization period after the sensor is turned on. The value is selected experimentally.
|
||||
|
||||
#define ZH_SENSOR_ATTRIBUTES_MESSAGE_FREQUENCY 60 // Frequency of transmission of keep alive messages to the gateway (in seconds).
|
||||
|
||||
#define ZH_MESSAGE_TASK_PRIORITY 2 // Prioritize the task of sending messages to the gateway.
|
||||
@ -56,7 +63,7 @@ typedef struct // Structure of data exchange between tasks, functions and event
|
||||
{
|
||||
struct // Storage structure of sensor hardware configuration data.
|
||||
{
|
||||
ha_sensor_type_t sensor_type; // Sensor types.
|
||||
ha_sensor_type_t sensor_type; // Sensor type.
|
||||
uint8_t sensor_pin_1; // Sensor GPIO number 1. @note Main pin for 1-wire sensors, SDA pin for I2C sensors.
|
||||
uint8_t sensor_pin_2; // Sensor GPIO number 2. @note SCL pin for I2C sensors.
|
||||
uint8_t power_pin; // Power GPIO number (if used sensor power control).
|
||||
@ -66,12 +73,14 @@ typedef struct // Structure of data exchange between tasks, functions and event
|
||||
volatile bool gateway_is_available; // Gateway availability status flag. @note Used to control the tasks when the gateway connection is established / lost. Used only when external powered.
|
||||
uint8_t gateway_mac[6]; // Gateway MAC address. @note Used only when external powered.
|
||||
uint8_t sent_message_quantity; // System counter for the number of sended messages. @note Used only when powered by battery.
|
||||
zh_dht_handle_t dht_handle; // Unique DTH11/22 sensor handle.
|
||||
TaskHandle_t attributes_message_task; // Unique task handle for zh_send_sensor_attributes_message_task(). @note Used only when external powered.
|
||||
TaskHandle_t status_message_task; // Unique task handle for zh_send_sensor_status_message_task(). @note Used only when external powered.
|
||||
const esp_partition_t *update_partition; // Unique handle for next OTA update partition. @note Used only when external powered.
|
||||
esp_ota_handle_t update_handle; // Unique handle for OTA functions. @note Used only when external powered.
|
||||
uint16_t ota_message_part_number; // System counter for the number of received OTA messages. @note Used only when external powered.
|
||||
#ifndef CONFIG_IDF_TARGET_ESP8266
|
||||
i2c_master_bus_handle_t i2c_bus_handle; // Unique I2C bus handle.
|
||||
#endif
|
||||
} sensor_config_t;
|
||||
|
||||
/**
|
||||
|
@ -1 +1 @@
|
||||
1.0.1
|
||||
1.0.2
|
Loading…
x
Reference in New Issue
Block a user