2016-09-11 17:21:15 +02:00
|
|
|
/* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
/** library to query measurements from peacefair PZEM-004 and PZEM-004T electricity meter (API)
|
|
|
|
* @file sensor_pzem.h
|
|
|
|
* @author King Kévin <kingkevin@cuvoodoo.info>
|
|
|
|
* @date 2016
|
|
|
|
* @note peripherals used: USART @ref sensor_pzem_usart
|
|
|
|
*/
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
/** a measurement response has been received */
|
|
|
|
extern volatile bool sensor_pzem_measurement_received;
|
|
|
|
|
2016-09-12 21:35:15 +02:00
|
|
|
/** measurements offered by electricity meter */
|
2016-09-11 17:21:15 +02:00
|
|
|
enum sensor_pzem_measurement_type_t {
|
|
|
|
VOLTAGE = 0,
|
|
|
|
CURRENT = 1,
|
|
|
|
POWER = 2,
|
|
|
|
ENERGY = 3,
|
|
|
|
MAX
|
|
|
|
};
|
|
|
|
|
2016-09-12 21:35:15 +02:00
|
|
|
/** measurement returned by electricity meter */
|
2016-09-11 17:21:15 +02:00
|
|
|
struct sensor_pzem_measurement_t {
|
|
|
|
enum sensor_pzem_measurement_type_t type; /**< measurement type */
|
|
|
|
bool valid; /**< is the measurement valid (e.g. format and checksum are correct) */
|
2016-09-11 22:44:27 +02:00
|
|
|
/** possible measurement values */
|
|
|
|
union measurement_t {
|
2016-09-11 17:21:15 +02:00
|
|
|
float voltage; /**< measured voltage in volts */
|
|
|
|
float current; /**< measured current in amperes */
|
|
|
|
float power; /**< measured power in watts */
|
|
|
|
uint32_t energy; /**< measured energy in watts/hour (24 bits) */
|
2016-09-11 22:44:27 +02:00
|
|
|
} value; /**< measurement value */
|
2016-09-11 17:21:15 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/** setup peripherals to communicate with electricity meter */
|
|
|
|
void sensor_pzem_setup(void);
|
|
|
|
/** request measurement from electricity meter
|
|
|
|
* @param[in] address electricity meter device address
|
2016-09-11 22:44:27 +02:00
|
|
|
* @param[in] type measurement type to request
|
2016-09-11 17:21:15 +02:00
|
|
|
*/
|
|
|
|
void sensor_pzem_measurement_request(uint32_t address, enum sensor_pzem_measurement_type_t type);
|
|
|
|
/** decode received measurement
|
|
|
|
* @return decoded measurement (invalid if no new measurement has been received)
|
|
|
|
*/
|
|
|
|
struct sensor_pzem_measurement_t sensor_pzem_measurement_decode(void);
|