rasProject_01 / weSweetHome  R. 240
process control       /     RasPi software         by   weinert-automation
Loading...
Searching...
No Matches
sweetHome2.c File Reference

Common values for an experimental smart home (lab) project. More...

#include "sweetHome2.h"
#include "weBatt.h"
#include "sweetHomeLocal.h"

Functions

void batKeepInhTimChg (state_t *const me)
 Inhibit battery keep alive loading timer state change function. More...
 
void befRiseTimChg (state_t *const me)
 Before sunrise timer state change callback. More...
 
void dawnTimChg (state_t *const me)
 Dawn timer state change callback. More...
 
void duskTimChg (state_t *const me)
 Dusk timer state change callback. More...
 
void genSunStateText (char *stateText, state_t const *const me, char const *stamp)
 Generate the sun's status text. More...
 
void logBatteryState ()
 Log battery state on outLog as line with time stamp. More...
 
void mqttClean ()
 End as MQTT client.
 
int mqttInit ()
 Initialise as MQTT client. More...
 
void mqttPlg01Set (uint8_t const on)
 Switch the plug Plug01. More...
 
void mqttPlg02Set (uint8_t const on)
 Switch the plug PLG2. More...
 
void mqttPlg03Set (uint8_t const on)
 Switch the plug PLG3. More...
 
void mqttPlg04Set (uint8_t const on)
 Switch the plug PLG4. More...
 
uint8_t pckSwPec (float power)
 Set and get package switch percentage by power. More...
 
void reduceBatLoadPWM (uint8_t const pwmRed)
 Reduce battery loader module PWM. More...
 
void setBatLoadPWM (uint8_t pwm)
 Set battery loader module PWM. More...
 
void setConsGiv (state_t *const me)
 Power consumer / producer (give away) SFC transition. More...
 
uint8_t setPckSwPerc (uint8_t perc)
 Set package switch percentage. More...
 
float setPhPckLimit (float powerLimit)
 Set and get package switch power limit. More...
 
void solPowStateText (char *stateText, state_t const *const me, char const *stamp)
 Generate solar power status text. More...
 
void sunriseTimChg (state_t *const me)
 Sunrise timer state change callback. More...
 
void sunsetTimChg (state_t *const me)
 Sunset timer state change callback. More...
 
void surplStateText (char *stateText, state_t const *const me, char const *stamp)
 Generate surplus power status text. More...
 
void switchBatToStepUp (uint8_t const on)
 Switch battery to step up converter. More...
 
void switchConsGiv (state_t *const me)
 Power consumer / producer (give away) transition. More...
 
void switchHotWpump (uint8_t const on)
 Hot water comfort pump turn on/off. More...
 
void switchInvToBatUnl (uint8_t const on)
 Switch inverter to battery unload. More...
 
void switchSolPow (state_t *const me)
 Solar power producer transition. More...
 
void switchWouldGiv250W (state_t *const me)
 Would give 250W or more away, the transition. More...
 
void switchWouldGivAway (state_t *const me)
 Would give away transition. More...
 

Variables

state_t batKeepInh
 Inhibit battery keep alive loading. More...
 
int batUnloadAllowed
 Battery unload allowed. More...
 
uint8_t batVoltValid
 The measured battery voltage is valid. More...
 
state_t befRiseTimer
 Before sunrise timer. More...
 
char clientId [38]
 MQTT client ID. More...
 
state_t consumeGiveHyst
 Give away hysteresis. More...
 
state_t consumeGiveSFC
 Give away state machine / SFC. More...
 
state_t dawnTimer
 Dawn timer. More...
 
state_t duskTimer
 Dusk timer. More...
 
uint8_t heaterAllowed
 phase packet load enabled (always)
 
int hippoSwitchMode
 Hippogreiff on/off times. More...
 
uint16_t hundredsV
 The battery voltage in 0.01V units. More...
 
char mqttHost [68]
 The MQTT broker URL or name. More...
 
int mqttPort
 MQTT port 1883.
 
volatile uint8_t phPckCnt
 current switch state duration counter
 
volatile uint8_t phPckOff
 current phase packet switch OFF duration
 
volatile uint8_t phPckOn
 current phase packet switch ON duration
 
volatile uint8_t phPckRelOffDelay
 PPS relay off delay.
 
volatile uint8_t phPckS2
 heater 2 pps switch state: 0 off; 1 on
 
volatile uint8_t phPckSw
 phase packet switch state: 0 off; 1 on
 
volatile uint8_t phPckSwIndex
 Actual index (0..200%) determining power. More...
 
volatile uint8_t phPpow2
 heater 2 power * 50% (0:0% .. 2:100%)
 
oneWireDevice_t sensors [NUM1W_SENSORS]
 The 1-wire sensors used.
 
state_t solarPowerHyst
 Solar power producer hysteresis.
 
char subTopStPlg01 [14]
 State sub topic of S20 plug Number 01 to 09. More...
 
state_t sunriseTimer
 Sunrise timer. More...
 
state_t sunsetTimer
 Sunset timer. More...
 
state_t wouldGive250WHyst
 Would give away 250W hysteresis. More...
 
state_t wouldGiveAwayHyst
 Would give away hysteresis. More...
 

Detailed Description

Common values for an experimental smart home (lab) project.

Copyright (c) 2018 Albrecht Weinert
weinert-automation.de a-weinert.de
/ / /\
/ /___ / \ |
\ /____\ /____\ | _|__
\ /\ / \ / \| |
\/ \/ \__/ \__/|_

Revision history

Rev. 267 15.10.2024
Rev. 85 09.01.2018 : new
Rev. 99 29.01.2018 : load module voltage handling added
Rev. 148 16.06.2018 : time handling improved, Hippogreiff relay
Rev. 187 14.10.2018 : Hippogreiff off 2h before sunrise
Rev. 189 06.12.2018 : Hippo and (in between optional) Plug 3/4 back to rise
Rev. 197 03.03.2019 : Plug 2 hippo; Plug04 surplus power
Rev. 199 02.04.2019 : allow battery unload before sunrise as default
Rev. 200 16.04.2019 : battery keep inhibit until low stress
Rev. 203 30.04.2019 : timer macro change, sun times & surplus log enhanced
Rev. 205 19.05.2019 : log battery stress (%) repaired
Rev. 206 27.05.2019 : PPS pre relay off delay 5 minutes (en lieu de 3s)
Rev. 209 22.07.2019 : work around a Doxygen bug
Rev. 222 18.03.2020 : noUnloadBat as default
Rev. 232 13.12.2022 : off Hippogreiff 2h earlier
Rev. 248 29.06.2023 : relays reorg. (1. defect & prep. extra heater)
Rev. 252 17.08.2023 : ECar loading ++
Rev. 256 15.12.2023 : Hippogreiff sunset to sunrise (again) in winter
todo: implement automatic summer winter change !!
check: dif./semantics/use dawn vs. beforeSunRise !!
Rev. 256 17.03.2024 : flexible /ref Hippogreiff switching times
Rev. 260 06.08.2024 : force battery loading pwm <= BAT_LDMX_PWM

This file is the addendum to sweetHome.c and sweetHome.h containing process IO related issues, not necessary for pure HMI or logging related programs.

Function Documentation

◆ switchHotWpump()

void switchHotWpump ( uint8_t const  on)

Hot water comfort pump turn on/off.

It turns the hot water comfort circulation pump on respectively off.

Parameters
on!= 0 : on; else, ==0 : off

◆ setPhPckLimit()

float setPhPckLimit ( float  powerLimit)

Set and get package switch power limit.

This function sets the PPS power limit in the range 0.0 ... PCK_POWER_LIM_MAX.

Parameters
powerLimitPPS power limit in W
Returns
the (new) power limit

◆ pckSwPec()

uint8_t pckSwPec ( float  power)

Set and get package switch percentage by power.

Besides determining an returning the percentage(power), this function sets the process control values via setPckSwPerc() returns its value.

Parameters
powerin W
Returns
the phase packet percentage 0..100

◆ setPckSwPerc()

uint8_t setPckSwPerc ( uint8_t  perc)

Set package switch percentage.

This function sets phPckSwIndex by the parameter value in the range 0..200; respectively PCK_POWER_IND_MAX. Additionally it adjusts the current phPckCnt should its value be higher than by the new setting.

On transitions from respectively to 0 the control relay (PCK_POWER_REL) is actuated before respectively after actuating the electronic switch.

Parameters
perc0..200; values above PCK_POWER_IND_MAX will have no effect and return the current setting un-altered
Returns
the actual (new) phase packet percentage 0..200

◆ logBatteryState()

void logBatteryState ( )

Log battery state on outLog as line with time stamp.

Logs the battery voltage and ... .

◆ switchInvToBatUnl()

void switchInvToBatUnl ( uint8_t  on)

Switch inverter to battery unload.

When the parameter is !=0 respectively ON, this function switches the (small) inverter from panel to the battery unload step up converter and turns the extra Ferraris energy meter off by REL2. Note: Said Ferraris is now unused (but still mounted).

When the parameter is off it does the other way round.

Parameters
ontrue: switch inverter to step up converter (battery unload); 0, false: switch inverter to panel.

◆ switchBatToStepUp()

void switchBatToStepUp ( uint8_t  on)

Switch battery to step up converter.

When the parameter is !=0 respectively ON, this function switches the battery to the step up converter.

When the parameter is off, this function switches the battery to the (max. 20 A) battery loader / keep alive module .

Parameters
ontrue: switch battery to step up converter; 0, false: switch battery to load modul.

◆ batKeepInhTimChg()

void batKeepInhTimChg ( state_t *const  me)

Inhibit battery keep alive loading timer state change function.

It turns the battery keep alive loading on when ending.

Parameters
mepointer to the inhibit battery keep alive loading timer

◆ setBatLoadPWM()

void setBatLoadPWM ( uint8_t  pwm)

Set battery loader module PWM.

This function sets the battery load module power PWM signal and hence its output voltage. 0 is the lowest and 255 the highest possible setting. While this function allows setting to 0, the highest value is limited to BAT_LDMX_PWM.

Parameters
pwmthe pwm ratio 0: 0%; 255: 100%

◆ reduceBatLoadPWM()

void reduceBatLoadPWM ( uint8_t const  pwmRed)

Reduce battery loader module PWM.

This function reduces the battery load module power PWM signal and hence its output voltage by the parameter value. The reduction is limited to BAT_NOLD_PWM.

See also
setBatLoadPWM()
Parameters
pwmRedthe reduction of the pwm ratio

◆ befRiseTimChg()

void befRiseTimChg ( state_t *const  me)

Before sunrise timer state change callback.

On timer end .....

Parameters
mepointer to the sunset timer

◆ dawnTimChg()

void dawnTimChg ( state_t *const  me)

Dawn timer state change callback.

On timer end .....

Parameters
mepointer to the dawn timer

◆ sunriseTimChg()

void sunriseTimChg ( state_t *const  me)

Sunrise timer state change callback.

On timer end .....

Parameters
mepointer to the sunrise timer

◆ sunsetTimChg()

void sunsetTimChg ( state_t *const  me)

Sunset timer state change callback.

On timer end .....

Parameters
mepointer to the sunset timer

◆ duskTimChg()

void duskTimChg ( state_t *const  me)

Dusk timer state change callback.

On timer end .....

Parameters
mepointer to the sunset timer

◆ genSunStateText()

void genSunStateText ( char *  stateText,
state_t const *const  me,
char const *  stamp 
)

Generate the sun's status text.

This function makes the special log text for sun's times: set, rise etc..

Parameters
stateTexta character array supplied to hold the state text to be generated; minimal length 80.
mepointer to own state; never null
stamp(time) stamp to be prepended (max. length 23); default " - "

◆ switchSolPow()

void switchSolPow ( state_t *const  me)

Solar power producer transition.

At the moment just switch the relays and set a flag. Note: trigger with pSolar

Parameters
mepointer to the give away hysteresis

◆ solPowStateText()

void solPowStateText ( char *  stateText,
state_t const *const  me,
char const *  stamp 
)

Generate solar power status text.

This function generates the log text for the solar power on/off discriminator.

Parameters
stateTexta character array supplied to hold the state text to be generated; minimal length 80.
mepointer to own state; never null
stamp(time) stamp to be prepended (max. length 23); default " - "

◆ switchConsGiv()

void switchConsGiv ( state_t *const  me)

Power consumer / producer (give away) transition.

Triggered with pHome, at transitions relay and plug are switched for signalling via the switchConsGiv() SFC to have minimal on and off times.

At transitions to OFF, i.e. producer role imminent, battery ballast loading will be started (if not yet ON).

Parameters
mepointer to the give away hysteresis

◆ setConsGiv()

void setConsGiv ( state_t *const  me)

Power consumer / producer (give away) SFC transition.

At the moment just switch the relays and set a flag. Note: trigger with pHome (hyst)

Parameters
mepointer to consumeGiveSFC

◆ surplStateText()

void surplStateText ( char *  stateText,
state_t const *const  me,
char const *  stamp 
)

Generate surplus power status text.

This function generates the log text for the surplus power status machine.

Parameters
stateTexta character array supplied to hold the state text to be generated; minimal length 80.
mepointer to own state; never null
stamp(time) stamp to be prepended (max. length 23); default " - "

◆ switchWouldGivAway()

void switchWouldGivAway ( state_t *const  me)

Would give away transition.

At the moment just switch the relays and set a flag. Note: trigger with pGiveAway

Parameters
mepointer to the give away hysteresis

◆ switchWouldGiv250W()

void switchWouldGiv250W ( state_t *const  me)

Would give 250W or more away, the transition.

At the moment just switch the relays and set a flag. Note: trigger with pGiveAway

Parameters
mepointer to the give away hysteresis

◆ mqttInit()

int mqttInit ( )

Initialise as MQTT client.

On success only: subscribe, loop and publish.

Returns
0: success the common mosq is set and usable; else: errno

◆ mqttPlg01Set()

void mqttPlg01Set ( uint8_t const  on)

Switch the plug Plug01.

This function publishes the switch command via MQTT to the relay device Plug01, usually a Sonoff S20 with Tasmota.

Parameters
onswitch on when true, else off

◆ mqttPlg02Set()

void mqttPlg02Set ( uint8_t const  on)

Switch the plug PLG2.

See mqttPlg01Set

◆ mqttPlg03Set()

void mqttPlg03Set ( uint8_t const  on)

Switch the plug PLG3.

See mqttPlg01Set

◆ mqttPlg04Set()

void mqttPlg04Set ( uint8_t const  on)

Switch the plug PLG4.

See mqttPlg01Set

Variable Documentation

◆ phPckSwIndex

volatile uint8_t phPckSwIndex

Actual index (0..200%) determining power.

This is the control variable for the electric heater elements in the hot water tank. Until June 2023 there was one such element of PCK100PERC_POWER W and this variable had a range of 0..100%. Since July 2023 there are two heater elements of equal power. The range of this control variable was extended to 0..200%. The power distribution between the two heater elements is done by software; see setPckSwPerc(uint8_t const perc) and PCK_POWER_IND_MAX

◆ batVoltValid

uint8_t batVoltValid

The measured battery voltage is valid.

When not 0 the last MQTT battery voltage measurement respectively message is not older than 2.4s and hence considered the actual valid value.

◆ batUnloadAllowed

int batUnloadAllowed

Battery unload allowed.

Bit 0 (1): allow after sunset
Bit 1 (2): allow before sunrise
start value: 8 set default value (usually 2 in winter and 3 in summer) Note: Made int en lieu de uint_8 for use in getopt_long.

See also
BAT_UNL_SUMM
BAT_UNL_WINT

◆ hippoSwitchMode

int hippoSwitchMode

Hippogreiff on/off times.

Bit 0,1 ( 3): switch at sunset resp. sunrise (winter)
Bit 2,3 (12): switch at dusk resp. dawn (summer)
start value: 12 (/ref Hippogreiff summer)

◆ hundredsV

uint16_t hundredsV

The battery voltage in 0.01V units.

This is just an integer value consistent to the last valid battery voltage measurement valFilVal.batVolt. Contrary to valFilVal.batVolt which is set to -0.9 to indicate invalidity after 2.4s without new (MQTT) measurements, this value will be kept (forever).
It is preset with 1289 (12.89 V) lest have battery low before the first valid MQTT measurement / message.
Receiving a good MQTT message from the battery voltmeter will set valFilVal.batVolt, batVoltValid and this value.
Use formFixed16(&textStart, hundredsV, 2) to format as "12.89" without trailing zero.

See also
formFixed16 valFilVal batVoltValid

◆ batKeepInh

state_t batKeepInh

Inhibit battery keep alive loading.

While running disallow battery keep alive with BAT_KEEP_PWM. This timer is initially OFF and will be stopped in advance by low battery voltage stress and battery keep command via HMI/GUI.
It will be started after battery unload, after battery ballast load and by battery off command via GUI.

◆ sunriseTimer

state_t sunriseTimer

Sunrise timer.

This timer will run out every day at (approximated) sunrise. For the "every day" behaviour, its state change function will — after all due actions — restart this timer for the next 24h to hit (very approximately) the next sunrise. This acceptable guess will be adjusted at day change or at program start.

◆ sunsetTimer

state_t sunsetTimer

Sunset timer.

This timer will run out every day at (approximated) sunset. See also sunriseTimer

◆ befRiseTimer

state_t befRiseTimer

Before sunrise timer.

This timer will run out every day at about 180 min before (approximated) sunrise. The offset must be sufficient time to unload the battery before sunrise.
See also: sunriseTimer BEFORE_RISE

◆ dawnTimer

state_t dawnTimer

Dawn timer.

This timer will run out every day at (approximated) dawn, meaning the beginning of civil twilight. See also sunriseTimer and duskTimer

◆ duskTimer

state_t duskTimer

Dusk timer.

This timer will run out every day at (approximated) dusk, meaning the end of civil twilight. About this time street lamps and position lights may be lit. See also sunriseTimer

◆ consumeGiveHyst

state_t consumeGiveHyst

Give away hysteresis.

thresholds: PDEL_SGGIVE, PDEL_SGCONS

◆ consumeGiveSFC

state_t consumeGiveSFC

Give away state machine / SFC.

Status ON means consumer: OK.
Status OFF means very low power consumption: Inhibit power delivery.

◆ wouldGiveAwayHyst

state_t wouldGiveAwayHyst

Would give away hysteresis.

thresholds (04.03.18): -/+ 7W

◆ wouldGive250WHyst

state_t wouldGive250WHyst

Would give away 250W hysteresis.

thresholds (04.03.18): -250 / -178W

◆ mqttHost

char mqttHost[68]

The MQTT broker URL or name.

May be set by option –mqttHost meterPi or –mqttBroker 192.168.178.87

default: localhost MQTTBroker (currently localhost !)

◆ subTopStPlg01

char subTopStPlg01[14]

State sub topic of S20 plug Number 01 to 09.

It's preset as plug01/POWER for 01, but the digit at index [5] will be set before each use accordingly.

◆ clientId

char clientId[38]

MQTT client ID.

default value: sweetHomeControl; length: 15; max. length: 36
May be changed before mqttInit().

MQTT client ID.