проверка на отсутствие

This commit is contained in:
Dmitry Borisenko
2022-09-27 20:55:16 +02:00
parent f92291c545
commit 62ff324139
7 changed files with 55 additions and 29 deletions

View File

@@ -1,7 +1,6 @@
#pragma once
#include <Arduino.h>
#include <SoftwareSerial.h>
#define PZEM_DEFAULT_ADDR 0xF8
@@ -22,13 +21,13 @@ class PZEMSensor {
PZEMSensor(Stream *serial, uint16_t addr = PZEM_DEFAULT_ADDR);
~PZEMSensor();
PZEM_Info* values();
PZEM_Info *values(bool &online);
bool setAddress(uint8_t addr);
uint8_t getAddress();
bool setPowerAlarm(uint16_t watts);
bool getPowerAlarm();
bool reset();
void search();
bool search();
// Get most up to date values from device registers and cache them
bool refresh();

View File

@@ -43,11 +43,13 @@ PZEMSensor::PZEMSensor(Stream *port, uint16_t addr) {
init();
}
PZEM_Info *PZEMSensor::values() {
PZEM_Info *PZEMSensor::values(bool &online) {
// Update vales if necessary
if (!refresh()) {
_values = PZEM_Info();
online = false;
}
online = true;
return &_values;
}
@@ -62,7 +64,7 @@ PZEM_Info *PZEMSensor::values() {
* @param[in] check - perform a simple read check after write
*
* @return success
*/
*/
bool PZEMSensor::sendCmd8(uint8_t cmd, uint16_t rAddr, uint16_t val, bool check, uint16_t slave_addr) {
uint8_t sendBuffer[8]; // Send buffer
uint8_t respBuffer[8]; // Response buffer (only used when check is true)
@@ -295,7 +297,8 @@ uint16_t PZEMSensor::CRC16(const uint8_t *data, uint16_t len) {
return crc;
}
void PZEMSensor::search() {
bool PZEMSensor::search() {
bool ret = false;
static uint8_t response[7];
for (uint16_t addr = 0x01; addr <= 0xF8; addr++) {
sendCmd8(CMD_RIR, 0x00, 0x01, false, addr);
@@ -303,8 +306,9 @@ void PZEMSensor::search() {
// Something went wrong
continue;
} else {
Serial.print("Device on addr: ");
Serial.print(addr);
Serial.println("Pzem " + String(addr));
ret = true;
}
}
return ret;
}