Add: 'Demo example for PZEM004t-V3 (#120)

* knx-pzem004v30

Add first draft of a pzem-004t power/current/tension/etc.. on your main line (BE CAREFUL WITH VOLTAGE!)

PZEM-004t-v30 wired on a samd21 with the use of Sercom2

Use with slight modification of: https://github.com/mandulaj/PZEM-004T-v30
View issue:  https://github.com/mandulaj/PZEM-004T-v30/issues/43

* Rename examples/knx-pzem0004t.xml to examples/knx-pzem004/knx-pzem0004t.xml

* Delete knx-pzem004t.knxprod

* Delete pzem-004t-v30.ino

* Update: 'just move to right directory'

* Update knx-pzem0004t.xml

* Add: ProgMode

* Fix/Update/Add

Add: ResetEnergy function with datetime
Update: led Class
Fix: many typo, errors
This commit is contained in:
zelogik 2021-01-21 18:41:55 +01:00 committed by GitHub
parent 742e7a1ee7
commit 58d3721571
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 515 additions and 0 deletions

View File

@ -0,0 +1,163 @@
<?xml version="1.0" encoding="utf-8"?>
<KNX xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" CreatedBy="KNX MT" ToolVersion="5.6.407.26745" xmlns="http://knx.org/xml/project/11">
<ManufacturerData>
<Manufacturer RefId="M-00FA">
<Catalog>
<CatalogSection Id="M-00FA_CS-1" Name="Geräte" Number="1" DefaultLanguage="en-US">
<CatalogItem Id="M-00FA_H-01248-1_HP-01B7-09-0000_CI-EC01-1" Name="PZEM004t" Number="1" ProductRefId="M-00FA_H-01248-1_P-EC01" Hardware2ProgramRefId="M-00FA_H-01248-1_HP-01B7-09-0000" DefaultLanguage="en-US" />
</CatalogSection>
</Catalog>
<ApplicationPrograms>
<ApplicationProgram Id="M-00FA_A-01B7-09-0000" ApplicationNumber="439" ApplicationVersion="9" ProgramType="ApplicationProgram" MaskVersion="MV-07B0" Name="SAMD-PZEM004T" LoadProcedureStyle="MergedProcedure" PeiType="0" DefaultLanguage="en-US" DynamicTableManagement="false" Linkable="false" MinEtsVersion="4.0">
<Static>
<Code>
<RelativeSegment Id="M-00FA_A-01B7-09-0000_RS-04-00000" Name="Parameters" Offset="0" Size="5" LoadStateMachine="4" />
</Code>
<ParameterTypes>
<ParameterType Id="M-00FA_A-01B7-09-0000_PT-StartupTimeout" Name="StartupTimeout">
<TypeRestriction Base="Value" SizeInBit="8">
<Enumeration Text="0 s" Value="0" Id="M-00FA_A-01B7-09-0000_PT-StartupTimeout_EN-0" />
<Enumeration Text="1 s" Value="1" Id="M-00FA_A-01B7-09-0000_PT-StartupTimeout_EN-1" />
<Enumeration Text="2 s" Value="2" Id="M-00FA_A-01B7-09-0000_PT-StartupTimeout_EN-2" />
<Enumeration Text="3 s" Value="3" Id="M-00FA_A-01B7-09-0000_PT-StartupTimeout_EN-3" />
<Enumeration Text="4 s" Value="4" Id="M-00FA_A-01B7-09-0000_PT-StartupTimeout_EN-4" />
<Enumeration Text="5 s" Value="5" Id="M-00FA_A-01B7-09-0000_PT-StartupTimeout_EN-5" />
<Enumeration Text="6 s" Value="6" Id="M-00FA_A-01B7-09-0000_PT-StartupTimeout_EN-6" />
</TypeRestriction>
</ParameterType>
<ParameterType Id="M-00FA_A-01B7-09-0000_PT-TimeCycle" Name="TimeCycle">
<TypeRestriction Base="Value" SizeInBit="8">
<Enumeration Text="Disabled" Value="0" Id="M-00FA_A-01B7-09-0000_PT-TimeCycle_EN-0" />
<Enumeration Text="1 sec" Value="1" Id="M-00FA_A-01B7-09-0000_PT-TimeCycle_EN-1" />
<Enumeration Text="5 sec" Value="2" Id="M-00FA_A-01B7-09-0000_PT-TimeCycle_EN-2" />
<Enumeration Text="15 sec" Value="3" Id="M-00FA_A-01B7-09-0000_PT-TimeCycle_EN-3" />
<Enumeration Text="1 min" Value="4" Id="M-00FA_A-01B7-09-0000_PT-TimeCycle_EN-4" />
<Enumeration Text="5 min" Value="5" Id="M-00FA_A-01B7-09-0000_PT-TimeCycle_EN-5" />
<Enumeration Text="15 min" Value="6" Id="M-00FA_A-01B7-09-0000_PT-TimeCycle_EN-6" />
</TypeRestriction>
</ParameterType>
<ParameterType Id="M-00FA_A-01B7-09-0000_PT-PercentChange" Name="PercentChange">
<TypeRestriction Base="Value" SizeInBit="8">
<Enumeration Text="Disabled" Value="0" Id="M-00FA_A-01B7-09-0000_PT-PercentChange_EN-0" />
<Enumeration Text="1 %" Value="1" Id="M-00FA_A-01B7-09-0000_PT-PercentChange_EN-1" />
<Enumeration Text="5 %" Value="2" Id="M-00FA_A-01B7-09-0000_PT-PercentChange_EN-2" />
<Enumeration Text="10 %" Value="3" Id="M-00FA_A-01B7-09-0000_PT-PercentChange_EN-3" />
<Enumeration Text="15 %" Value="4" Id="M-00FA_A-01B7-09-0000_PT-PercentChange_EN-4" />
<Enumeration Text="20 %" Value="5" Id="M-00FA_A-01B7-09-0000_PT-PercentChange_EN-5" />
<Enumeration Text="30 %" Value="6" Id="M-00FA_A-01B7-09-0000_PT-PercentChange_EN-6" />
<Enumeration Text="50 %" Value="7" Id="M-00FA_A-01B7-09-0000_PT-PercentChange_EN-7" />
</TypeRestriction>
</ParameterType>
<ParameterType Id="M-00FA_A-01B7-09-0000_PT-SwitchOnOff" Name="SwitchOnOff">
<TypeRestriction Base="Value" SizeInBit="8">
<Enumeration Text="Disabled" Value="0" Id="M-00FA_A-01B7-09-0000_PT-SwitchOnOff_EN-0" />
<Enumeration Text="Enabled" Value="1" Id="M-00FA_A-01B7-09-0000_PT-SwitchOnOff_EN-1" />
</TypeRestriction>
</ParameterType>
<ParameterType Id="M-00FA_A-01B7-09-0000_PT-EnergyReset" Name="EnergyReset">
<TypeRestriction Base="Value" SizeInBit="8">
<Enumeration Text="Disabled" Value="0" Id="M-00FA_A-01B7-09-0000_PT-EnergyReset_EN-0" />
<Enumeration Text="Daily Reset" Value="1" Id="M-00FA_A-01B7-09-0000_PT-EnergyReset_EN-1" />
<Enumeration Text="Monthly Reset" Value="2" Id="M-00FA_A-01B7-09-0000_PT-EnergyReset_EN-2" />
<Enumeration Text="Annually Reset" Value="3" Id="M-00FA_A-01B7-09-0000_PT-EnergyReset_EN-3" />
</TypeRestriction>
</ParameterType>
</ParameterTypes>
<Parameters>
<Parameter Id="M-00FA_A-01B7-09-0000_P-1" Name="startupTimeout" ParameterType="M-00FA_A-01B7-09-0000_PT-StartupTimeout" Text="Startup delaytime" Value="0">
<Memory CodeSegment="M-00FA_A-01B7-09-0000_RS-04-00000" Offset="0" BitOffset="0" />
</Parameter>
<Parameter Id="M-00FA_A-01B7-09-0000_P-2" Name="Percent Send" ParameterType="M-00FA_A-01B7-09-0000_PT-PercentChange" Text="Send actual value if changed" Value="0">
<Memory CodeSegment="M-00FA_A-01B7-09-0000_RS-04-00000" Offset="1" BitOffset="0" />
</Parameter>
<Parameter Id="M-00FA_A-01B7-09-0000_P-3" Name="Refresh Time" ParameterType="M-00FA_A-01B7-09-0000_PT-TimeCycle" Text="Send actual value cyclically" Value="0">
<Memory CodeSegment="M-00FA_A-01B7-09-0000_RS-04-00000" Offset="2" BitOffset="0" />
</Parameter>
<Parameter Id="M-00FA_A-01B7-09-0000_P-4" Name="Enable Reset" ParameterType="M-00FA_A-01B7-09-0000_PT-EnergyReset" Text="Enable daily reset (need datetime Group Address set)" Value="0">
<Memory CodeSegment="M-00FA_A-01B7-09-0000_RS-04-00000" Offset="3" BitOffset="0" />
</Parameter>
<Parameter Id="M-00FA_A-01B7-09-0000_P-5" Name="Enable ProdMode" ParameterType="M-00FA_A-01B7-09-0000_PT-SwitchOnOff" Text="Enable ProgMode at startup for 5min" Value="0">
<Memory CodeSegment="M-00FA_A-01B7-09-0000_RS-04-00000" Offset="4" BitOffset="0" />
</Parameter>
</Parameters>
<ParameterRefs>
<ParameterRef Id="M-00FA_A-01B7-09-0000_P-1_R-1" RefId="M-00FA_A-01B7-09-0000_P-1" />
<ParameterRef Id="M-00FA_A-01B7-09-0000_P-2_R-2" RefId="M-00FA_A-01B7-09-0000_P-2" />
<ParameterRef Id="M-00FA_A-01B7-09-0000_P-3_R-3" RefId="M-00FA_A-01B7-09-0000_P-3" />
<ParameterRef Id="M-00FA_A-01B7-09-0000_P-4_R-4" RefId="M-00FA_A-01B7-09-0000_P-4" />
<ParameterRef Id="M-00FA_A-01B7-09-0000_P-5_R-5" RefId="M-00FA_A-01B7-09-0000_P-5" />
</ParameterRefs>
<ComObjectTable>
<ComObject Id="M-00FA_A-01B7-09-0000_O-1" Name="Counter reset" Text="Counter reset" Number="1" FunctionText="Manual Energy counter Reset" ObjectSize="1 Bit" ReadFlag="Disabled" WriteFlag="Enabled" CommunicationFlag="Enabled" TransmitFlag="Disabled" UpdateFlag="Disabled" ReadOnInitFlag="Disabled" DatapointType="" />
<ComObject Id="M-00FA_A-01B7-09-0000_O-2" Name="Date Time" Text="Date Time" Number="2" FunctionText="dateTime" ObjectSize="8 Bytes" ReadFlag="Disabled" WriteFlag="Enabled" CommunicationFlag="Enabled" TransmitFlag="Disabled" UpdateFlag="Disabled" ReadOnInitFlag="Disabled" />
<ComObject Id="M-00FA_A-01B7-09-0000_O-3" Name="Voltage" Text="Voltage" Number="3" FunctionText="Transmit Voltage value" ObjectSize="4 Bytes" ReadFlag="Enabled" WriteFlag="Disabled" CommunicationFlag="Enabled" TransmitFlag="Enabled" UpdateFlag="Enabled" ReadOnInitFlag="Disabled" DatapointType="" />
<ComObject Id="M-00FA_A-01B7-09-0000_O-4" Name="Current" Text="Current" Number="4" FunctionText="Transmit Current value" ObjectSize="4 Bytes" ReadFlag="Enabled" WriteFlag="Disabled" CommunicationFlag="Enabled" TransmitFlag="Enabled" UpdateFlag="Enabled" ReadOnInitFlag="Disabled" DatapointType="" />
<ComObject Id="M-00FA_A-01B7-09-0000_O-5" Name="Power Factor" Text="Power Factor" Number="5" FunctionText="Transmit Power Factor" ObjectSize="4 Bytes" ReadFlag="Enabled" WriteFlag="Disabled" CommunicationFlag="Enabled" TransmitFlag="Enabled" UpdateFlag="Enabled" ReadOnInitFlag="Disabled" DatapointType="" />
<ComObject Id="M-00FA_A-01B7-09-0000_O-6" Name="Active Power" Text="Active Power" Number="6" FunctionText="Transmit Power value" ObjectSize="4 Bytes" ReadFlag="Enabled" WriteFlag="Disabled" CommunicationFlag="Enabled" TransmitFlag="Enabled" UpdateFlag="Enabled" ReadOnInitFlag="Disabled" />
<ComObject Id="M-00FA_A-01B7-09-0000_O-7" Name="Active Energy" Text="Active Energy" Number="7" FunctionText="Transmit Energy value" ObjectSize="4 Bytes" ReadFlag="Enabled" WriteFlag="Disabled" CommunicationFlag="Enabled" TransmitFlag="Enabled" UpdateFlag="Enabled" ReadOnInitFlag="Disabled" />
<ComObject Id="M-00FA_A-01B7-09-0000_O-8" Name="Frequency" Text="Frequency" Number="8" FunctionText="Transmit Frequency" ObjectSize="4 Bytes" ReadFlag="Enabled" WriteFlag="Disabled" CommunicationFlag="Enabled" TransmitFlag="Enabled" UpdateFlag="Enabled" ReadOnInitFlag="Disabled" />
<ComObject Id="M-00FA_A-01B7-09-0000_O-9" Name="ProgMode" Text="ProgMode" Number="9" FunctionText="Start device programmation mode for ETS" ObjectSize="1 Bit" ReadFlag="Disabled" WriteFlag="Enabled" CommunicationFlag="Enabled" TransmitFlag="Enabled" UpdateFlag="Disabled" ReadOnInitFlag="Disabled" />
</ComObjectTable>
<ComObjectRefs>
<ComObjectRef Id="M-00FA_A-01B7-09-0000_O-1_R-1" RefId="M-00FA_A-01B7-09-0000_O-1" />
<ComObjectRef Id="M-00FA_A-01B7-09-0000_O-2_R-2" RefId="M-00FA_A-01B7-09-0000_O-2" />
<ComObjectRef Id="M-00FA_A-01B7-09-0000_O-3_R-3" RefId="M-00FA_A-01B7-09-0000_O-3" />
<ComObjectRef Id="M-00FA_A-01B7-09-0000_O-4_R-4" RefId="M-00FA_A-01B7-09-0000_O-4" />
<ComObjectRef Id="M-00FA_A-01B7-09-0000_O-5_R-5" RefId="M-00FA_A-01B7-09-0000_O-5" />
<ComObjectRef Id="M-00FA_A-01B7-09-0000_O-6_R-6" RefId="M-00FA_A-01B7-09-0000_O-6" />
<ComObjectRef Id="M-00FA_A-01B7-09-0000_O-7_R-7" RefId="M-00FA_A-01B7-09-0000_O-7" />
<ComObjectRef Id="M-00FA_A-01B7-09-0000_O-8_R-8" RefId="M-00FA_A-01B7-09-0000_O-8" />
<ComObjectRef Id="M-00FA_A-01B7-09-0000_O-9_R-9" RefId="M-00FA_A-01B7-09-0000_O-9" />
</ComObjectRefs>
<AddressTable MaxEntries="65000" />
<AssociationTable MaxEntries="65000" />
<LoadProcedures>
<LoadProcedure MergeId="2">
<LdCtrlRelSegment LsmIdx="4" Size="5" Mode="0" Fill="0" AppliesTo="full" />
</LoadProcedure>
<LoadProcedure MergeId="4">
<LdCtrlWriteRelMem ObjIdx="4" Offset="0" Size="5" Verify="true" />
</LoadProcedure>
</LoadProcedures>
<Options />
</Static>
<Dynamic>
<ChannelIndependentBlock>
<ParameterBlock Id="M-00FA_A-01B7-09-0000_PB-1" Name="ParameterPage" Text="Common Parameters">
<ParameterRefRef RefId="M-00FA_A-01B7-09-0000_P-1_R-1" />
<ParameterRefRef RefId="M-00FA_A-01B7-09-0000_P-2_R-2" />
<ParameterRefRef RefId="M-00FA_A-01B7-09-0000_P-3_R-3" />
<ParameterRefRef RefId="M-00FA_A-01B7-09-0000_P-4_R-4" />
<ParameterRefRef RefId="M-00FA_A-01B7-09-0000_P-5_R-5" />
<ComObjectRefRef RefId="M-00FA_A-ABCE-20-0000_O-1_R-1" />
<ComObjectRefRef RefId="M-00FA_A-ABCE-20-0000_O-2_R-2" />
<ComObjectRefRef RefId="M-00FA_A-ABCE-20-0000_O-3_R-3" />
<ComObjectRefRef RefId="M-00FA_A-ABCE-20-0000_O-4_R-4" />
<ComObjectRefRef RefId="M-00FA_A-ABCE-20-0000_O-5_R-5" />
<ComObjectRefRef RefId="M-00FA_A-ABCE-20-0000_O-6_R-6" />
<ComObjectRefRef RefId="M-00FA_A-ABCE-20-0000_O-7_R-7" />
<ComObjectRefRef RefId="M-00FA_A-ABCE-20-0000_O-8_R-8" />
<ComObjectRefRef RefId="M-00FA_A-ABCE-20-0000_O-9_R-9" />
</ParameterBlock>
</ChannelIndependentBlock>
</Dynamic>
</ApplicationProgram>
</ApplicationPrograms>
<Hardware>
<Hardware Id="M-00FA_H-01248-1" Name="PZEM-004t gateway" SerialNumber="01248" VersionNumber="1" BusCurrent="10" HasIndividualAddress="true" HasApplicationProgram="true">
<Products>
<Product Id="M-00FA_H-01248-1_P-EC01" Text="PZEM004t" OrderNumber="EC01" IsRailMounted="false" DefaultLanguage="en-US">
<RegistrationInfo RegistrationStatus="Registered" />
</Product>
</Products>
<Hardware2Programs>
<Hardware2Program Id="M-00FA_H-01248-1_HP-01B7-09-0000" MediumTypes="MT-0">
<ApplicationProgramRef RefId="M-00FA_A-01B7-09-0000" />
<RegistrationInfo RegistrationStatus="Registered" RegistrationNumber="0001/19" />
</Hardware2Program>
</Hardware2Programs>
</Hardware>
</Hardware>
</Manufacturer>
</ManufacturerData>
</KNX>

Binary file not shown.

View File

@ -0,0 +1,352 @@
#include <knx.h>
#include <PZEM004Tv30.h>
#include "wiring_private.h" // pinPeripheral() function
#include <TimeLib.h>
//Sercom Stuff
#define PIN_SERIAL2_RX (34ul) // Pin description number for PIO_SERCOM on D12 (34ul)
#define PIN_SERIAL2_TX (36ul) // Pin description number for PIO_SERCOM on D10 (36ul)
#define PAD_SERIAL2_TX (UART_TX_PAD_2) // SERCOM pad 2
#define PAD_SERIAL2_RX (SERCOM_RX_PAD_3) // SERCOM pad 3
Uart Serial2(&sercom1, PIN_SERIAL2_RX, PIN_SERIAL2_TX, PAD_SERIAL2_RX, PAD_SERIAL2_TX); //TX D10, RX D12
void SERCOM1_Handler()
{
Serial2.IrqHandler();
}
//PZEM stuff
#define PZEM004_NO_SWSERIAL
#define PZEM_DEFAULT_ADDR 0xF8
//knx stuff
#define goReset knx.getGroupObject(1)
#define goDateTime knx.getGroupObject(2)
#define goProgMode knx.getGroupObject(9)
// Global Const
const uint16_t ets_timePeriod[7] = {0, 1, 5, 15, 1 * 60, 5 * 60, 15 * 60};
const uint8_t ets_startupTimeout[7] = {0, 1, 2, 3, 4, 5, 6};
const uint8_t ets_percentCycle[6] = {0, 5, 10, 15, 20, 30}; //need knxprod update... ?
const uint8_t ledPin = LED_BUILTIN;// the number of the LED pin
const uint8_t physicalCount = 6; // voltage,current,power_factor,power,energy,frequency
// Global Variable
uint8_t percentCycle = 0; // better to define a global or read knx.paramByte each time... ?
uint32_t timePeriod = 0; // same here,
uint8_t resetPeriod = 0; //same here ...
uint8_t resetEnergy = 0; // and here... disabled/day/week/month
bool progMode = true;
// Issue on https://github.com/mandulaj/PZEM-004T-v30/issues/43
PZEM004Tv30 pzem(Serial2, PZEM_DEFAULT_ADDR);
struct Physical {
void init(uint8_t GOaddr, Dpt type_dpt){
_GOaddr = GOaddr;
_dpt = type_dpt;
}
void loop(){
// unsigned long currentMillis = millis();
// Delta Change update as defined in ETS
int32_t deltaPercent = ( 100 * ( _value - _lastValue ) / _value );
if ( percentCycle != 0 && abs(deltaPercent) >= percentCycle )
{
_trigger = true;
_lastValue = _value;
}
// Refresh groupAddress value as defined in ETS since last update
if ( timePeriod != 0 && millis() - _lastMillis >= timePeriod )
{
_trigger = true;
}
// UpdateGO but send to bus only if triggered by time or value change percentage
if (_trigger){
knx.getGroupObject(_GOaddr).value(_value, _dpt);
_lastMillis = millis();
_trigger = false;
}else{
knx.getGroupObject(_GOaddr).valueNoSend(_value, _dpt);
}
}
void setValue(float value){
if (value != _value)
{
_value = value;
}
}
private:
Dpt _dpt;
float _value = 0;
float _lastValue = 0;
uint32_t _lastMillis = 0;
uint8_t _GOaddr;
bool _trigger = false;
// bool isUpdated = false;
public:
} Physical[physicalCount];
class Blinker
{
private:
uint8_t ledPin_; // the number of the LED pin
uint32_t OnTime = 1000; // milliseconds of on-time
uint32_t OffTime = 1000; // milliseconds of off-time
bool ledState = LOW; // ledState used to set the LED
uint32_t previousMillis; // will store last time LED was updated
void setOutput(bool state_, uint32_t currentMillis_){
ledState = state_;
previousMillis = currentMillis_;
digitalWrite(ledPin_, state_);
}
public:
Blinker(uint8_t pin)
{
ledPin_ = pin;
pinMode(ledPin_, OUTPUT);
previousMillis = 0;
}
void set(uint32_t on, uint32_t off){
OnTime = on;
OffTime = off;
}
void loop(){
uint32_t currentMillis = millis();
if((ledState == HIGH) && (currentMillis - previousMillis >= OnTime))
{
setOutput(LOW, currentMillis);
}
else if ((ledState == LOW) && (currentMillis - previousMillis >= OffTime))
{
setOutput(HIGH, currentMillis);
}
}
};
Blinker led = Blinker(ledPin);
void callBackProgMode(GroupObject& go){
progMode = (bool)go.value();
}
void callBackDateTime(GroupObject& go){
static uint32_t lastUpdate = 0;
const uint32_t interval = (1000 * 60 * 60 * 24); // 1day
struct tm myTime;
myTime = go.value();
unsigned short tmp_sec = myTime.tm_sec;
unsigned short tmp_min = myTime.tm_min;
unsigned short tmp_hour = myTime.tm_hour;
unsigned short tmp_mday = myTime.tm_mday;
unsigned short tmp_month = myTime.tm_mon;
unsigned short tmp_year = myTime.tm_year;
if (millis() - lastUpdate >= interval && !timeStatus() == timeSet)
{
setTime(tmp_hour, tmp_min, tmp_sec, tmp_mday, tmp_month, tmp_year);
lastUpdate = millis();
}
}
void resetCallback(GroupObject& go)
{
if (go.value())
{
resetEnergy = true;
goReset.value(false);
}
}
void setup() {
pinPeripheral(PIN_SERIAL2_RX, PIO_SERCOM);
pinPeripheral(PIN_SERIAL2_TX, PIO_SERCOM);
SerialUSB.begin(9600);
Serial2.begin(9600);
ArduinoPlatform::SerialDebug = &SerialUSB;
randomSeed(millis());
knx.readMemory();
// led.set(5000, 5000);
if (knx.configured())
{
int confStartupTime = ets_startupTimeout[knx.paramByte(0)] * 1000;
delay(confStartupTime); // the only delay used, why make a withoutDelay function for that?
percentCycle = ets_percentCycle[knx.paramByte(1)];
timePeriod = ets_timePeriod[knx.paramByte(2)] * 1000;
resetPeriod = knx.paramByte(3);
goReset.callback(resetCallback);
goReset.dataPointType(DPT_Trigger);
goDateTime.dataPointType(DPT_DateTime);
goProgMode.dataPointType(DPT_Trigger);
goProgMode.callback(callBackProgMode);
uint8_t GOaddr = 3;
Physical[0].init(GOaddr, DPT_Value_Electric_Potential); // voltage
Physical[1].init(GOaddr += 1, DPT_Value_Electric_Current);
Physical[2].init(GOaddr += 1, DPT_Value_Power_Factor);
Physical[3].init(GOaddr += 1, DPT_Value_Power);
Physical[4].init(GOaddr += 1, DPT_Value_Energy);
Physical[5].init(GOaddr += 1, DPT_Value_Frequency);
led.set(2000, 1000);
}
// is the led active on HIGH or low? Default is LOW
knx.ledPinActiveOn(HIGH);
// pin or GPIO programming button is connected to. Default is 0
knx.ledPin(5);
knx.buttonPin(9);
knx.start();
// while (!SerialUSB) { //wait for DEBUGING
// ; // wait for serial port to connect. Needed for native USB port only
// }
}
void loop() {
knx.loop();
if (knx.configured() && !progMode)
{
refreshValueLoop();
resetEnergyLoop();
for (uint8_t i=0; i< physicalCount; i++)
{
Physical[i].loop();
}
}
else if (progMode)
{
prodModeLoop();
}
}
void refreshValueLoop(){
static const uint16_t pzemInterval = 500; // interval at which to blink (milliseconds)
static uint32_t lastPzemUpdate = 0;
if (millis() - lastPzemUpdate >= pzemInterval)
{
for (uint8_t i=0; i< physicalCount; i++)
{
float isaValue;
switch (i) { //maybe a pointer or reference could be nicer...
case 0:
isaValue = pzem.voltage();
break;
case 1:
isaValue = pzem.current();
break;
case 2:
isaValue = pzem.pf();
break;
case 3:
isaValue = pzem.power();
break;
case 4:
isaValue = pzem.energy();
break;
case 5:
isaValue = pzem.frequency();
break;
default:
break;
}
if(!isnan(isaValue))
{
Physical[i].setValue(isaValue);
}
}
}
}
void resetEnergyLoop(){
static time_t lastTime;
time_t samdTime = now();
if (timeStatus() == timeSet)
{
switch (resetPeriod)
{
case 1: //day
if (day(samdTime) != day(lastTime))
{
lastTime = samdTime;
pzem.resetEnergy();
}
break;
case 2: //week
if (weekday(samdTime) != weekday(lastTime) && weekday(samdTime) == 2) //monday
{
lastTime = samdTime;
pzem.resetEnergy();
}
break;
case 3: // month
if (month(samdTime) != month(lastTime))
{
lastTime = samdTime;
pzem.resetEnergy();
}
break;
case 4: // year
if (year(samdTime) != year(lastTime))
{
lastTime = samdTime;
pzem.resetEnergy();
}
default:
break;
}
}
}
void prodModeLoop(){ // run Only if progMode triggered ( at start or callback)
static uint32_t timerProgPrevMillis = 0;
const uint32_t timerProgMode = ( 15 * 60 * 1000 ) ; // 15min
if (!knx.progMode())
{
knx.progMode(true);
timerProgPrevMillis = millis();
led.set(500, 250);
}
else
{
if (millis() - timerProgPrevMillis > timerProgMode) {
knx.progMode(false);
goProgMode.value(false);
progMode = 0;
}
}
}