Skip to content

Commit

Permalink
Issue #8 - added PZEM004.setAddr[] command
Browse files Browse the repository at this point in the history
  • Loading branch information
zbx-sadman committed Apr 11, 2017
1 parent 80376f6 commit f0f246c
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 23 deletions.
15 changes: 8 additions & 7 deletions basic.h
Original file line number Diff line number Diff line change
Expand Up @@ -118,14 +118,14 @@
/=/ Enable 1-Wire processing and command:
/=/ - OW.Scan[]
/*/
//#define FEATURE_OW_ENABLE
#define FEATURE_OW_ENABLE

/*/
/=/ Enable Dallas DS18x20 sensors handling and command:
/=/ - DS18x20.Temperature[]
/*/

//#define FEATURE_DS18X20_ENABLE
#define FEATURE_DS18X20_ENABLE

/**** I2C bus ****/

Expand All @@ -140,7 +140,7 @@
/=/ Note #1: I2C library (Wire.h) takes at least 32bytes of memory for internal buffers
/=/ Note #2: I2C library (Wire.h) activate internal pullups for SDA & SCL pins when Wire.begin() called
/*/
//#define FEATURE_I2C_ENABLE
#define FEATURE_I2C_ENABLE

/*/
/=/ Enable BOSCH BMP sensors handling and commands:
Expand Down Expand Up @@ -208,8 +208,9 @@
/=/ - pzem004.voltage[]
/=/ - pzem004.power[]
/=/ - pzem004.energy[]
/=/ - pzem004.setAddr[]
/*/
//#define FEATURE_PZEM004_ENABLE
#define FEATURE_PZEM004_ENABLE

/*/
/=/ Enable APC SmartUPS protocol support and command:
Expand Down Expand Up @@ -325,8 +326,8 @@
/*/
/=/ View the more or less debug messages on the Serial Monitor. Choose one.
/*/
//#define FEATURE_DEBUG_TO_SERIAL_LOW
#define FEATURE_DEBUG_TO_SERIAL_MIDDLE
#define FEATURE_DEBUG_TO_SERIAL_LOW
//#define FEATURE_DEBUG_TO_SERIAL_MIDDLE
//#define FEATURE_DEBUG_TO_SERIAL_HIGH
//#define FEATURE_DEBUG_TO_SERIAL_DEV

Expand Down Expand Up @@ -407,6 +408,6 @@ const uint8_t constFactoryResetButtonPin = 0x08;
#define ZBX_AGENT_DEFAULT_DOMAIN ".local.net"


#define ZBX_AGENT_VERISON "Zabbuino 1.1.2"
#define ZBX_AGENT_VERISON "Zabbuino 1.1.4"
#endif // #ifndef _ZABBUINO_BASIC_CONFIG_H_

18 changes: 11 additions & 7 deletions src/structs.h
Original file line number Diff line number Diff line change
Expand Up @@ -208,15 +208,15 @@ typedef union { // 4 byte
#define CMD_PZEM004_VOLTAGE 0x3D
#define CMD_PZEM004_POWER 0x3E
#define CMD_PZEM004_ENERGY 0x3F
#define CMD_PZEM004_SETADDR 0x40

#define CMD_UPS_APCSMART 0x40
#define CMD_UPS_MEGATEC 0x41
#define CMD_SYSTEM_RUN 0x42

#define CMD_NET_ENC_REINITS 0x43
#define CMD_NET_ENC_REINIT_REASON 0x44
#define CMD_NET_ENC_PKTCNT_MAX 0x45
#define CMD_UPS_APCSMART 0x41
#define CMD_UPS_MEGATEC 0x42
#define CMD_SYSTEM_RUN 0x43

#define CMD_NET_ENC_REINITS 0x44
#define CMD_NET_ENC_REINIT_REASON 0x45
#define CMD_NET_ENC_PKTCNT_MAX 0x46

// add new command as "const char command_<COMMAND_MACRO> PROGMEM". Only 'const' push string to PROGMEM. Tanx, Arduino.
// command_* values must be in lower case due analyze sub convert all chars to lower
Expand Down Expand Up @@ -309,6 +309,7 @@ const char command_CMD_PZEM004_CURRENT[] PROGMEM = "pzem0
const char command_CMD_PZEM004_VOLTAGE[] PROGMEM = "pzem004.voltage";
const char command_CMD_PZEM004_POWER[] PROGMEM = "pzem004.power";
const char command_CMD_PZEM004_ENERGY[] PROGMEM = "pzem004.energy";
const char command_CMD_PZEM004_SETADDR[] PROGMEM = "pzem004.setaddr";

const char command_CMD_UPS_APCSMART[] PROGMEM = "ups.apcsmart";
const char command_CMD_UPS_MEGATEC[] PROGMEM = "ups.megatec";
Expand Down Expand Up @@ -532,11 +533,13 @@ const char* const commands[] PROGMEM = {
command_CMD_PZEM004_VOLTAGE,
command_CMD_PZEM004_POWER,
command_CMD_PZEM004_ENERGY,
command_CMD_PZEM004_SETADDR,
#else
command_CMD_ZBX_NOPE,
command_CMD_ZBX_NOPE,
command_CMD_ZBX_NOPE,
command_CMD_ZBX_NOPE,
command_CMD_ZBX_NOPE,
#endif

#ifdef FEATURE_UPS_APCSMART_ENABLE
Expand Down Expand Up @@ -619,6 +622,7 @@ typedef enum {
#define SENS_READ_VOLTAGE 0x0B
#define SENS_READ_POWER 0x0C
#define SENS_READ_ENERGY 0x0D
#define SENS_CHANGE_ADDRESS 0x0E

#define SENS_READ_RAW 0xFF

Expand Down
19 changes: 15 additions & 4 deletions src/uart_pzem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ static uint8_t crcPZEM004(uint8_t *_data, uint8_t _size) {
* - DEVICE_ERROR_TIMEOUT if device stop talking
*
*****************************************************************************************************************************/
int8_t getPZEM004Metric(const uint8_t _rxPin, const uint8_t _txPin, uint8_t _metric, const char *_ip, uint8_t *_dst) {
int8_t getPZEM004Metric(const uint8_t _rxPin, const uint8_t _txPin, uint8_t _metric, const char *_addr, uint8_t *_dst) {
int8_t rc = RESULT_IS_FAIL;
uint8_t command, len;
int32_t result;
Expand All @@ -53,6 +53,9 @@ int8_t getPZEM004Metric(const uint8_t _rxPin, const uint8_t _txPin, uint8_t _met
case SENS_READ_ENERGY:
command = PZEM_ENERGY;
break;
case SENS_CHANGE_ADDRESS:
command = PZEM_SETADDR;
break;
default:
goto finish;
}
Expand All @@ -62,8 +65,8 @@ int8_t getPZEM004Metric(const uint8_t _rxPin, const uint8_t _txPin, uint8_t _met
// Make packet for PZEM
// 1-th byte in the packet - metric (command)
_dst[0] = command;
// 2..5 bytes - ip address. Convert its from _ip or use default (192.168.1.1) if _ip is invalid
result = hstoba(&_dst[1], _ip, 4);
// 2..5 bytes - ip address. Convert its from _addr or use default (192.168.1.1) if _addr is invalid
result = hstoba(&_dst[1], _addr, 4);
if (!result) {
_dst[1] = 0xC0; // 192
_dst[2] = 0xA8; // 168
Expand All @@ -75,7 +78,7 @@ int8_t getPZEM004Metric(const uint8_t _rxPin, const uint8_t _txPin, uint8_t _met
_dst[5] = 0x00;
// 7-th byte - CRC
_dst[6] = crcPZEM004(_dst, PZEM_PACKET_SIZE - 1);
// for(int i=0; i < sizeof(_dst); i++) { Serial.print("Byte# "); Serial.print(i); Serial.print(" => "); Serial.println(_dst[i], HEX); }
for(uint8_t i=0; i < PZEM_PACKET_SIZE; i++) { Serial.print("Byte# "); Serial.print(i); Serial.print(" => "); Serial.println(_dst[i], HEX); }
serialSend(&swSerial, _dst, PZEM_PACKET_SIZE, false);

// Recieve from PZEM004
Expand All @@ -91,6 +94,8 @@ int8_t getPZEM004Metric(const uint8_t _rxPin, const uint8_t _txPin, uint8_t _met
// Bad CRC
if (_dst[6] != crcPZEM004( _dst, len - 1)) { rc = DEVICE_ERROR_CHECKSUM; goto finish; }

for(uint8_t i=0; i < PZEM_PACKET_SIZE; i++) { Serial.print("Byte# "); Serial.print(i); Serial.print(" => "); Serial.println(_dst[i], HEX); }

// data is placed in buffer from 2-th byte, because 1-th byte is Header
switch (_metric) {
case SENS_READ_AC:
Expand All @@ -110,6 +115,12 @@ int8_t getPZEM004Metric(const uint8_t _rxPin, const uint8_t _txPin, uint8_t _met
result = ((uint32_t) _dst[1] << 16) + ((uint16_t) _dst[2] << 8) + _dst[3];
ltoa(result, (char*) _dst, 10);
break;
case SENS_CHANGE_ADDRESS:
// All returned bytes must be 0x00 on success, CRC must be equal (command - 0x10)
result = _dst[1] | _dst[2] | _dst[3] | _dst[4] | _dst[5];
rc = result ? RESULT_IS_FAIL : RESULT_IS_OK;
goto finish;
break;
}
rc = RESULT_IN_BUFFER;

Expand Down
1 change: 1 addition & 0 deletions src/uart_pzem.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ version 1.0 is used
#define PZEM_CURRENT 0xB1
#define PZEM_POWER 0xB2
#define PZEM_ENERGY 0xB3
#define PZEM_SETADDR 0xB4
#define PZEM_PACKET_SIZE 0x07
#define PZEM_DEFAULT_READ_TIMEOUT 1000L

Expand Down
17 changes: 12 additions & 5 deletions zabbuino.ino
Original file line number Diff line number Diff line change
Expand Up @@ -1421,11 +1421,11 @@ static int16_t executeCommand(char* _dst, int16_t* _argOffset)

#ifdef FEATURE_PZEM004_ENABLE
//
// 0x0101A8C0 - an IP address for PZEM (192.168.1.1)
// 0xC0A80101 - an IP address for PZEM (192.168.1.1)
//
case CMD_PZEM004_CURRENT:
/*/
/=/ pzem004.current[rxPin, txPin, ip]
/=/ pzem004.current[rxPin, txPin, addr]
/*/
if (!isSafePin(argv[0]) || !isSafePin(argv[1])) { break; }
// _dst cast to (uint8_t*) to use with subroutine math and SoftwareSerial subs, because used instead sub's internal buffer and save a little RAM size.
Expand All @@ -1434,25 +1434,32 @@ static int16_t executeCommand(char* _dst, int16_t* _argOffset)
break;
case CMD_PZEM004_VOLTAGE:
/*/
/=/ pzem004.voltage[rxPin, txPin, ip]
/=/ pzem004.voltage[rxPin, txPin, addr]
/*/
if (!isSafePin(argv[0]) || !isSafePin(argv[1])) { break; }
result = getPZEM004Metric(argv[0], argv[1], SENS_READ_VOLTAGE, &_dst[_argOffset[2]], (uint8_t*) _dst);
break;
case CMD_PZEM004_POWER:
/*/
/=/ pzem004.power[rxPin, txPin, ip]
/=/ pzem004.power[rxPin, txPin, addr]
/*/
if (!isSafePin(argv[0]) || !isSafePin(argv[1])) { break; }
result = getPZEM004Metric(argv[0], argv[1], SENS_READ_POWER, &_dst[_argOffset[2]], (uint8_t*) _dst);
break;
case CMD_PZEM004_ENERGY:
/*/
/=/ pzem004.energy[rxPin, txPin, ip]
/=/ pzem004.energy[rxPin, txPin, addr]
/*/
if (!isSafePin(argv[0]) || !isSafePin(argv[1])) { break; }
result = getPZEM004Metric(argv[0], argv[1], SENS_READ_ENERGY, &_dst[_argOffset[2]], (uint8_t*) _dst);
break;
case CMD_PZEM004_SETADDR:
/*/
/=/ pzem004.setAddr[rxPin, txPin, addr]
/*/
if (!isSafePin(argv[0]) || !isSafePin(argv[1])) { break; }
result = getPZEM004Metric(argv[0], argv[1], SENS_CHANGE_ADDRESS, &_dst[_argOffset[2]], (uint8_t*) _dst);
break;
#endif // FEATURE_PZEM004_ENABLE

#ifdef FEATURE_UPS_APCSMART_ENABLE
Expand Down

0 comments on commit f0f246c

Please sign in to comment.