TLSR8258 SDK 如何主动采集电量
Posted: 2023年 Feb 28日 10:54
使用tuya-zigbee-sdk-tlsr8258,其中sensor例程里电量是周期性自动采集的,如何变成手动采集?
即有一个函数,主动调用后开始采集,采集完成通知回调来处理数据上报
使用tuya-zigbee-sdk-tlsr8258,其中sensor例程里电量是周期性自动采集的,如何变成手动采集?
即有一个函数,主动调用后开始采集,采集完成通知回调来处理数据上报
您可以参考以下方式:
1.sdk在进行周期上报电量前会通过回调
Code: Select all
bool_t dev_report_battery_by_user_data(dev_send_data_t OUT *send_data, uint8_t percentage, uint8_t voltage)
函数告知应用上报的数据信息,应用可以再该函数内自己采样并修改send data数据来实现上报数据的替换,您可以参考以下例程进行滤波等策略
Code: Select all
bool_t dev_report_battery_by_user_data(dev_send_data_t OUT *send_data, uint8_t percentage, uint8_t voltage)
{
const battery_table_t table[] = {BATTERY_USER_TABLE};
const uint8_t table_num = sizeof(table)/sizeof(battery_table_t);
const uint8_t t_msg_len = 10;/*数据长度*/
uint16_t t_voltage_average = 0;
uint8_t i = 0;
uint8_t t_dp = 0;
uint8_t *data = NULL;
t_voltage_average = average_filter_get_voltage_value();
if(table_num>1)
{
if(t_voltage_average < table[0].voltage)
{
percentage = 0;
}
else if (t_voltage_average >= table[table_num-1].voltage)
{
percentage = 100;
}
else
{
for(i = 0;i<table_num-1;i++)
{
if((t_voltage_average>=table[i].voltage) && (t_voltage_average<table[i+1].voltage))
{
percentage = table[i].percent + \
((t_voltage_average-table[i].voltage)*(table[i+1].percent-table[i].percent))/(table[i+1].voltage-table[i].voltage);
break;
}
}
}
}
if(0xFFFF == g_private_report_str.sequence_num)
{
g_private_report_str.sequence_num = 0;
}
else
{
g_private_report_str.sequence_num ++;
}
data = malloc(t_msg_len*sizeof(uint8_t));
if(data)
{
memset(data,0x00,t_msg_len);
data[0] = (uint8_t)(g_private_report_str.sequence_num>>8);
data[1] = (uint8_t)(g_private_report_str.sequence_num);
data[2] = BATTERY_VALUE_DP;
data[3] = TYPE_VALUE;
data[4] = 0;
data[5] = 0x04;
data[6] = 0;
data[7] = 0;
data[8] = 0;
data[9] = percentage;
memset(send_data, 0, sizeof(dev_send_data_t));
send_data->zcl_id = 0;//++zcl_seq;
send_data->qos = QOS_1;
send_data->commmand_type = ZCL_COMMAND_CLUSTER_SPEC_CMD;
send_data->direction = ZCL_DATA_DIRECTION_SERVER_TO_CLIENT;
send_data->command_id = TY_MODULE_DATA_REPORT_CMD;
send_data->addr.mode = SEND_MODE_GW;
send_data->addr.type.gw.cluster_id = CLUSTER_PRIVATE_TUYA_CLUSTER_ID;
send_data->addr.type.gw.src_ep = 1;
send_data->delay_time = 0;
send_data->random_time = 0;
send_data->data.private.len = t_msg_len;
memcpy((send_data->data.private.data),data,t_msg_len);
free(data);
}
return TRUE;
}
Code: Select all
/**
* @description: 平均值滤波读取电源电压
* @param {type} none
* @return: none
*/
uint16_t average_filter_get_voltage_value(void)
{
uint16_t t_voltage_sum = 0;
uint16_t t_voltage_average = 0;
uint16_t t_compare_num = 0;
uint16_t t_voltage[BATTERY_ALARM_FILTER_TIMES] = {0};
t_compare_num = BATTERY_ALARM_FILTER_TIMES;
memset(t_voltage,0x00,sizeof(uint16_t )*BATTERY_ALARM_FILTER_TIMES);
for(uint8_t i = 0; i < BATTERY_ALARM_FILTER_TIMES; i ++)/*平均值滤波*/
{
t_voltage[i] = hal_adc_to_voltage(hal_adc_get_value());
t_voltage_sum += t_voltage[i];
}
t_voltage_average = t_voltage_sum/BATTERY_ALARM_FILTER_TIMES;
t_voltage_sum = 0;
for(uint8_t i = 0; i < BATTERY_ALARM_FILTER_TIMES; i ++)
{
if((t_voltage[i]>(t_voltage_average+BATTERY_ALARM_FILTER_OFFSET))
||((t_voltage[i]+BATTERY_ALARM_FILTER_OFFSET)<t_voltage_average))
{
t_compare_num --;
}
else
{
t_voltage_sum += t_voltage[i];
}
}
if(t_compare_num)/*除数不能为零,为零则保留原有平均值*/
{
t_voltage_average = t_voltage_sum/t_compare_num;
}
return t_voltage_average;
}
2.若您需要单独开启采集任务并进行数据上报,可以通过应用开启定时器,并通过 hal_adc_to_voltage();hal_adc_get_value();相关函数进行采样,并通过dev_zigbee_send_data 函数自己进行采样数据上报。相关数据转载可以查看方法1进行参考使用
感谢 我试一下