Energy Metering

Last Updated on : 2022-11-24 09:20:26download

The energy metering feature can address the needs of energy data collection and processing for IoT products. It enables the system to collect energy data such as operating power, voltage, and current, and energy consumption, and then report the data to the Tuya IoT Cloud.

The energy metering feature requires app_uf_db components to process the energy data.

API overview

The following table lists the main APIs included in the energy metering component. For more information, see tuya_measure_com.h.

API Description
tuya_dev_measure_reg Register parameters of energy metering.
tuya_dev_measure_start Enable the energy metering feature.
tuya_get_measure_rms Get the current voltage, current, and power.
tuya_get_emasure_energy Get the energy usage during a specified period that is set on the mobile app. Once the meter is read, the usage data will be zeroed out to start a new measuring cycle.
tuya_get_measure_rslt Get the result of the production test.
tuya_get_calibration_para Get the calibration factors including voltage, current, power, and consumption.
tuya_clear_calibration_date Clear calibration data.

How it works

Data collection

We provide two methods for data collection.

  • Pulse count: The energy metering IC outputs a certain amount of pulses over a specified time period to obtain the energy data.

  • UART transmission: The energy metering IC transmits energy data through the UART.

Energy metering IC

The following four energy metering ICs are supported. You can select desired IC based on your circuit design.

  • BL0937: supports pulse count and suits 1R or 2R resistors.

  • HLW8012: supports pulse count and suits 1R or 2R resistors.

  • HLW8032: supports UART transmission and suits 1R resistors.

  • BL0942: calibration-free IC that supports UART transmission and suits 1R resistors. If you want to calibrate the accuracy of the metering IC, make sure the supply voltage is the same as the default voltage and get the IC connected to AC power.

The logic of energy metering

Energy Metering

Tuya’s solution is recommended. If you intend to design the logic yourself, you can use the APIs tuya_get_measure_rms and tuya_get_emasure_energy to get the collected data.

Feature

  • Supports four types of energy metering ICs.
  • Supports energy data collection when the energy metering IC is supplied with 220V or 120V input voltage.
  • Provides calibrated accuracy of 3%.

Example

  1. Initialize the energy metering feature in device_init.

    OPERATE_RET device_init(VOID)
    {
    	OPERATE_RET op_ret = OPRT_OK;
    
    	/*
    	Initializes other features
    	...
    	*/
    
    	// Register parameters of energy metering.
    	op_ret = user_dltj_params_register();
    	if (OPRT_OK != op_ret) {
    		return op_ret;
    	}
    	// Registration is successful. Energy metering is enabled in non-calibration mode.
    	op_ret = tuya_dev_measure_start(FALSE);
    	if (OPRT_OK != op_ret) {
    		return op_ret;
    	}
      /*[Adopt Tuya's solution for energy data reporting and storage. For more information, see the diagram in the section on the logic of energy metering. Initialize the interface tuya_dltj_init, as shown below:]*/
    	COE_DLTJ_TYPE_S dp_info;
    	dp_info.vol_dp = DPID_VOLT;
    	dp_info.cur_dp = DPID_CURR;
    	dp_info.pow_dp = DPID_POWER;
    	dp_info.energy_dp = DPID_ADD_ELE;
    
    	op_ret = tuya_dltj_init(dp_info, tuya_get_measure_rms, tuya_get_emasure_energy);
    	if (OPRT_OK != op_ret) {
    		PR_ERR("dltj unit init err:%d", op_ret);
    		return op_ret;
    	}
    /*[If you intend to design the logic yourself, you can use the APIs tuya_get_measure_rms and tuya_get_emasure_energy to get the collected data.]*/
    
    	/*
    	...
    	*/
    
    return op_ret;
    }
    
    /**
    * @brief user_dltj_params_register
    *        Register parameters of energy metering.
    * @param[in] VOID_T
    * 
    * @return VOID_T 
    */
    OPERATE_RET user_dltj_params_register(VOID_T)
    {
    	TY_DEV_MEASURE_PAPR_S devinfo;
    
    	extern UCHAR_T get_dltj_ele_pin(VOID_T);
    	extern UCHAR_T get_dltj_vi_pin(VOID_T);
    	extern UCHAR_T get_dltj_sel_pin(VOID_T);
    
    	memset(&devinfo, 0, SIZEOF(devinfo));
    	if (0 == MEASURE_DEF_VLOT_TYPE) {
    		devinfo.v_ref       = V_DEF_220; // 220V voltage.
    		devinfo.i_ref       = I_DEF_220; // Reference current for 560Ω resistors on 220V voltage.
    		devinfo.p_ref       = P_DEF_220; // Reference power for 560Ω resistors on 220V voltage.
    		PR_DEBUG("dev vlot type 220v");
    	} else {
    		devinfo.v_ref       = V_DEF_120; // 120V voltage.
    		devinfo.i_ref       = I_DEF_120; // 120V
    		devinfo.p_ref       = P_DEF_120; // 120v
    		PR_DEBUG("dev vlot type 110v");
    	}
    	devinfo.type        = MEASURE_CHIP; // Energy metering ICs. 0: DEV_BL0937. 1: DEV_HLW8012. 2: DEV_HLW8032. 3:DEV_BL0942.
    	devinfo.uart_no     = TY_UART0; // The UART used for serial communication.
    	devinfo.timer_no    = TUYA_TIMER0; // Hardware timer.
    	devinfo.resval      = MEASURE_RES;
    	devinfo.epin        = get_dltj_ele_pin();
    	devinfo.ivpin       = get_dltj_vi_pin();
    	devinfo.ivcpin.pin  = get_dltj_sel_pin();
    	devinfo.ivcpin.type = MEASURE_SEL_PIN_TYPE;
    	devinfo.if_measure  = TRUE; // With energy metering feature.
    
    	return tuya_dev_measure_reg(devinfo);
    }
    
  2. In the authorization callback mf_user_callback, add the functions for clearing saved energy data and calibration parameters.

    VOID mf_user_callback(VOID)
    {
    	/*
    	...
    	*/
    	tuya_clear_calibration_date(); // Clear calibration parameters.
    	tuya_clear_all_energy(); // Clear energy data.
    }
    
  3. In the gateway reset function gw_reset_cb, add the function for clear saved energy data.

    VOID gw_reset_cb(IN CONST GW_RESET_TYPE_E type)
    {
    	/*
    	...
    	*/
    	if (GW_REMOTE_RESET_FACTORY == type) {
    		tuya_clear_all_energy(); // Clear energy data.
    		return;
    	}
    }
    
  4. In the app_init, add a beacon for repeated production tests.

    VOID app_init(VOID)
    {
    	/*
    	...
    	*/
    
    	if (tuya_get_measure_rslt()) {
    		set_prod_ssid("tuya_mdev_test2");
    	}
    }
    
  5. In the status_changed_cb, add a function for calibration parameter reporting.

    VOID status_changed_cb(IN CONST GW_STATUS_E status)
    {
    	if (GW_NORMAL == status) {
    		/*
    		...
    		*/
    	user_dltj_upload_coe_params(); // Report calibration parameters.
    	}
    	/*
    	...
    	*/
    }
    
    /**
    * @brief user_dltj_upload_coe_params
    *        Report calibration parameters
    * @param[in] VOID_T
    * 
    * @return OPERATE_RET 
    */
    OPERATE_RET user_dltj_upload_coe_params(VOID_T)
    {
    	OPERATE_RET ret = OPRT_OK;
    	PROD_MEASURE_COE_S *date = tuya_get_calibration_para();
    
    	if(NULL == date) {
    		return OPRT_INVALID_PARM;
    	}
    
    	TY_OBJ_DP_S dp_pvi[5] = {
    		{
    			.dpid = DPID_PT_FLAG,
    			.type = PROP_VALUE,
    			.time_stamp = 0,
    			.value.dp_value = date->prod_result
    		},
    
    		{
    			.dpid = DPID_VOLT_COE,
    			.type = PROP_VALUE,
    			.time_stamp = 0,
    			.value.dp_value = ((GENERAL_COE_S *)(date->prod_date_struct))->v_ref
    		},
    
    		{
    			.dpid = DPID_CURR_COE,
    			.type = PROP_VALUE,
    			.time_stamp = 0,
    			.value.dp_value = ((GENERAL_COE_S *)(date->prod_date_struct))->i_ref
    		},
    
    		{
    			.dpid = DPID_POWER_COE,
    			.type = PROP_VALUE,
    			.time_stamp = 0,
    			.value.dp_value = ((GENERAL_COE_S *)(date->prod_date_struct))->p_ref
    		},
    
    		{
    			.dpid = DPID_ELE_COE,
    			.type = PROP_VALUE,
    			.time_stamp = 0,
    			.value.dp_value = ((GENERAL_COE_S *)(date->prod_date_struct))->e_ref
    		}
    	};
    
    	ret = dev_report_dp_json_async(get_gw_cntl()->gw_if.id, dp_pvi, 5);
    	if (OPRT_OK != ret) {
    		PR_ERR("upload_dltj_coe_param ret:%d", ret);
    	}
    
    	return ret;
    }
    
  6. In the beacon test function tuya_app_prod_enter, add the function for calibrating the energy metering IC.

    VOID tuya_app_prod_enter(IN BOOL_T flag, IN CHAR_T rssi)
    {
    	/*
    	Disable the relay control, such as control over the button, and do not trigger any button press event during calibration.
    	*/
    	key_ctrl_en = FALSE;
    
    	/*Register parameters of energy metering*/
    	op_ret = user_dltj_params_register(); // Find the function definition in device_init.
    	if(OPRT_OK != op_ret) {
    		return;
    	}
    
    	/*Enable the realy*/
    	app_set_channel_stat(TRUE);
    	/*Wait for three seconds*/
    	tuya_hal_system_sleep(3000);
    	/*Enable calibration test*/
    	ret = tuya_dev_measure_start(TRUE);
    	/*Disable the relay after calibration*/
    	app_set_channel_stat(FALSE);
    
    	if (OPRT_OK == ret) {
    		/*
    		Calibration is successful. You can use a LED to indicate a successful action.
    		*/
    	} else {
    		/*
    		Calibration failed. You can use a LED to indicate a failure.
    		*/
    	return; // Stop test and return.
    	}
    
    	/*Enable button test…*/
    	key_ctrl_en = TRUE;
    
    	/*
    	...
    	*/
    }
    

Production test process

In mass production, Tuya’s standard automated test system can help you perform the test of the energy metering feature efficiently. For more information, see Production Test on Wi-Fi Energy Monitoring Socket.