Version 1.0.2

Added support AHT, AM2320 and BH1750 sensors.
This commit is contained in:
2024-07-05 11:45:56 +03:00
parent ae77483afc
commit 9d83004f1a
15 changed files with 265 additions and 117 deletions

View File

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

View File

@ -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);
}

View File

@ -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;
/**