3-phase PV router
Loading...
Searching...
No Matches
utils.h
Go to the documentation of this file.
1
12#ifndef _UTILS_H
13#define _UTILS_H
14
15#include "calibration.h"
16#include "constants.h"
17#include "dualtariff.h"
18#include "processing.h"
19
20#include "utils_rf.h"
21#include "utils_temp.h"
22
27inline void printConfiguration()
28{
29#ifndef PROJECT_PATH
30#define PROJECT_PATH (__FILE__)
31#endif
32
33#ifndef BRANCH_NAME
34#define BRANCH_NAME ("N/A")
35#endif
36#ifndef COMMIT_HASH
37#define COMMIT_HASH ("N/A")
38#endif
39
40 DBUGLN();
41 DBUGLN();
42 DBUGLN(F("----------------------------------"));
43 DBUG(F("Sketch ID: "));
45
46 DBUG(F("From branch '"));
47 DBUG(F(BRANCH_NAME));
48 DBUG(F("', commit "));
50
51 DBUG(F("Build on "));
52#ifdef CURRENT_TIME
53 DBUGLN(F(CURRENT_TIME));
54#else
55 DBUG(F(__DATE__));
56 DBUG(F(" "));
57 DBUGLN(F(__TIME__));
58#endif
59 DBUGLN(F("ADC mode: free-running"));
60
61 DBUGLN(F("Electrical settings"));
62 for (uint8_t phase = 0; phase < NO_OF_PHASES; ++phase)
63 {
64 DBUG(F("\tf_powerCal for L"));
65 DBUG(phase + 1);
66 DBUG(F(" = "));
67 DBUGLN(f_powerCal[phase], 6);
68
69 DBUG(F("\tf_voltageCal, for Vrms_L"));
70 DBUG(phase + 1);
71 DBUG(F(" = "));
72 DBUGLN(f_voltageCal[phase], 5);
73 }
74
75 DBUG(F("\tf_phaseCal for all phases = "));
77
78 DBUG(F("\tExport rate (Watts) = "));
80
81 DBUG(F("\tzero-crossing persistence (sample sets) = "));
83
85
86 DBUG(F("Temperature capability "));
87 if constexpr (TEMP_SENSOR_PRESENT)
88 {
89 DBUGLN(F("is present"));
90 }
91 else
92 {
93 DBUGLN(F("is NOT present"));
94 }
95
96 DBUG(F("Dual-tariff capability "));
97 if constexpr (DUAL_TARIFF)
98 {
99 DBUGLN(F("is present"));
101 }
102 else
103 {
104 DBUGLN(F("is NOT present"));
105 }
106
107 DBUG(F("Load rotation feature "));
108 if constexpr (PRIORITY_ROTATION != RotationModes::OFF)
109 {
110 DBUGLN(F("is present"));
111 }
112 else
113 {
114 DBUGLN(F("is NOT present"));
115 }
116
117 DBUG(F("Relay diversion feature "));
118 if constexpr (RELAY_DIVERSION)
119 {
120 DBUGLN(F("is present"));
121
123 }
124 else
125 {
126 DBUGLN(F("is NOT present"));
127 }
128
129 DBUG(F("RF capability "));
130#ifdef RF_PRESENT
131 DBUG(F("IS present, Freq = "));
132 if (FREQ == RF12_433MHZ)
133 DBUGLN(F("433 MHz"));
134 else if (FREQ == RF12_868MHZ)
135 DBUGLN(F("868 MHz"));
136 rf12_initialize(nodeID, FREQ, networkGroup); // initialize RF
137#else
138 DBUGLN(F("is NOT present"));
139#endif
140
141 DBUG(F("Datalogging capability "));
142#ifdef SERIALPRINT
143 DBUGLN(F("is present"));
144#else
145 DBUGLN(F("is NOT present"));
146#endif
147}
148
154inline void printForEmonESP(const bool bOffPeak)
155{
156 uint8_t idx{ 0 };
157
158 // Total mean power over a data logging period
159 Serial.print(F("P:"));
160 Serial.print(tx_data.power);
161
162 // Mean power for each phase over a data logging period
163 for (idx = 0; idx < NO_OF_PHASES; ++idx)
164 {
165 Serial.print(F(",P"));
166 Serial.print(idx + 1);
167 Serial.print(F(":"));
168 Serial.print(tx_data.power_L[idx]);
169 }
170 // Mean power for each load over a data logging period (in %)
171 for (idx = 0; idx < NO_OF_DUMPLOADS; ++idx)
172 {
173 Serial.print(F(",L"));
174 Serial.print(idx + 1);
175 Serial.print(F(":"));
176 Serial.print((copyOf_countLoadON[idx] * 100) * invDATALOG_PERIOD_IN_MAINS_CYCLES);
177 }
178
179 if constexpr (TEMP_SENSOR_PRESENT)
180 { // Current temperature
181 for (idx = 0; idx < temperatureSensing.get_size(); ++idx)
182 {
183 if ((OUTOFRANGE_TEMPERATURE == tx_data.temperature_x100[idx])
184 || (DEVICE_DISCONNECTED_RAW == tx_data.temperature_x100[idx]))
185 {
186 continue;
187 }
188
189 Serial.print(F(",T"));
190 Serial.print(idx + 1);
191 Serial.print(F(":"));
192 Serial.print(tx_data.temperature_x100[idx] * 0.01F);
193 }
194 }
195
196 if constexpr (DUAL_TARIFF)
197 {
198 // Current tariff
199 Serial.print(F(",T:"));
200 Serial.print(bOffPeak ? "low" : "high");
201 }
202 Serial.println(F(""));
203}
204
210{
211 uint8_t phase{ 0 };
212
214 Serial.print(F(", P:"));
215 Serial.print(tx_data.power);
216
217 for (phase = 0; phase < NO_OF_PHASES; ++phase)
218 {
219 Serial.print(F(", P"));
220 Serial.print(phase + 1);
221 Serial.print(F(":"));
222 Serial.print(tx_data.power_L[phase]);
223 }
224 for (phase = 0; phase < NO_OF_PHASES; ++phase)
225 {
226 Serial.print(F(", V"));
227 Serial.print(phase + 1);
228 Serial.print(F(":"));
229 Serial.print((float)tx_data.Vrms_L_x100[phase] * 0.01F);
230 }
231
232 if constexpr (TEMP_SENSOR_PRESENT)
233 {
234 for (uint8_t idx = 0; idx < temperatureSensing.get_size(); ++idx)
235 {
236 if ((OUTOFRANGE_TEMPERATURE == tx_data.temperature_x100[idx])
237 || (DEVICE_DISCONNECTED_RAW == tx_data.temperature_x100[idx]))
238 {
239 continue;
240 }
241
242 Serial.print(F(", T"));
243 Serial.print(idx + 1);
244 Serial.print(F(":"));
245 Serial.print((float)tx_data.temperature_x100[idx] * 0.01F);
246 }
247 }
248
249 Serial.println(F(")"));
250}
251
257{
258 uint8_t phase{ 0 };
259
261 Serial.print(F(", P:"));
262 Serial.print(tx_data.power);
263
264 if constexpr (RELAY_DIVERSION)
265 {
266 Serial.print(F("/"));
267 Serial.print(relays.get_average());
268 }
269
270 for (phase = 0; phase < NO_OF_PHASES; ++phase)
271 {
272 Serial.print(F(", P"));
273 Serial.print(phase + 1);
274 Serial.print(F(":"));
275 Serial.print(tx_data.power_L[phase]);
276 }
277 for (phase = 0; phase < NO_OF_PHASES; ++phase)
278 {
279 Serial.print(F(", V"));
280 Serial.print(phase + 1);
281 Serial.print(F(":"));
282 Serial.print((float)tx_data.Vrms_L_x100[phase] * 0.01F);
283 }
284
285 if constexpr (TEMP_SENSOR_PRESENT)
286 {
287 for (uint8_t idx = 0; idx < temperatureSensing.get_size(); ++idx)
288 {
289 if ((OUTOFRANGE_TEMPERATURE == tx_data.temperature_x100[idx])
290 || (DEVICE_DISCONNECTED_RAW == tx_data.temperature_x100[idx]))
291 {
292 continue;
293 }
294
295 Serial.print(F(", T"));
296 Serial.print(idx + 1);
297 Serial.print(F(":"));
298 Serial.print((float)tx_data.temperature_x100[idx] * 0.01F);
299 }
300 }
301
302 Serial.print(F(", (minSampleSets/MC "));
304 Serial.print(F(", #ofSampleSets "));
306#ifndef DUAL_TARIFF
307 if constexpr (PRIORITY_ROTATION != RotationModes::OFF)
308 {
309 Serial.print(F(", NoED "));
310 Serial.print(absenceOfDivertedEnergyCount);
311 }
312#endif // DUAL_TARIFF
313 Serial.println(F(")"));
314}
315
321inline void sendResults(bool bOffPeak)
322{
323 static bool startup{ true };
324
325 if (startup)
326 {
327 startup = false;
328 return; // reject the first datalogging which is incomplete !
329 }
330
331#ifdef RF_PRESENT
332 send_rf_data(); // *SEND RF DATA*
333#endif
334
335#if defined SERIALOUT
337#endif // if defined SERIALOUT
338
339 if constexpr (EMONESP_CONTROL)
340 {
341 printForEmonESP(bOffPeak);
342 }
343
344#if defined SERIALPRINT && !defined EMONESP
346#endif // if defined SERIALPRINT && !defined EMONESP
347}
348
353inline void logLoadPriorities()
354{
355#ifdef ENABLE_DEBUG
356
357 DBUGLN(F("Load Priorities: "));
358 for (const auto& loadPrioAndState : loadPrioritiesAndState)
359 {
360 DBUG(F("\tload "));
361 DBUGLN(loadPrioAndState);
362 }
363
364#endif
365}
366
372inline int freeRam()
373{
374 extern int __heap_start, *__brkval;
375 int v;
376 return (int)&v - (__brkval == 0 ? (int)&__heap_start : (int)__brkval);
377}
378
379#endif // UTILS_H
Calibration values definition.
constexpr float f_phaseCal
Definition: calibration.h:46
constexpr float f_voltageCal[NO_OF_PHASES]
Definition: calibration.h:51
constexpr float f_powerCal[NO_OF_PHASES]
Definition: calibration.h:34
static auto get_average()
Get the current average.
Definition: utils_relay.h:299
void printConfiguration() const
Print the configuration of each relay.
Definition: utils_relay.h:382
constexpr auto get_size() const
Get the number of sensors.
Definition: utils_temp.h:94
#define FREQ
Definition: config.h:109
constexpr bool RELAY_DIVERSION
Definition: config.h:53
constexpr int networkGroup
Definition: config.h:112
constexpr TemperatureSensing temperatureSensing
Definition: config.h:91
constexpr RelayEngine relays
Definition: config.h:84
constexpr int nodeID
Definition: config.h:111
constexpr bool DUAL_TARIFF
Definition: config.h:54
constexpr bool EMONESP_CONTROL
Definition: config.h:46
constexpr uint8_t NO_OF_DUMPLOADS
Definition: config.h:38
constexpr int16_t REQUIRED_EXPORT_IN_WATTS
Definition: config_system.h:23
constexpr float invDATALOG_PERIOD_IN_MAINS_CYCLES
Definition: config_system.h:36
constexpr float invSUPPLY_FREQUENCY
Definition: config_system.h:35
constexpr uint8_t NO_OF_PHASES
Definition: config_system.h:19
Some constants.
constexpr int16_t DEVICE_DISCONNECTED_RAW
Definition: constants.h:25
constexpr int16_t OUTOFRANGE_TEMPERATURE
Definition: constants.h:20
#define DBUGLN(...)
Definition: debug.h:97
#define DBUG(...)
Definition: debug.h:96
Classes/types needed for dual-tariff support.
void printDualTariffConfiguration()
Print the settings for off-peak period.
Definition: dualtariff.h:71
void printParamsForSelectedOutputMode()
Print the settings used for the selected output mode.
Definition: processing.cpp:799
Public functions/variables of processing engine.
volatile uint8_t copyOf_lowestNoOfSampleSetsPerMainsCycle
Definition: processing.h:43
constexpr uint8_t PERSISTENCE_FOR_POLARITY_CHANGE
Definition: processing.h:24
uint8_t loadPrioritiesAndState[NO_OF_DUMPLOADS]
Definition: processing.h:22
volatile float copyOf_energyInBucket_main
Definition: processing.h:42
volatile uint16_t copyOf_sampleSetsDuringThisDatalogPeriod
Definition: processing.h:44
volatile uint32_t absenceOfDivertedEnergyCount
Definition: processing.h:30
PayloadTx_struct< NO_OF_PHASES > tx_data
Definition: processing.h:50
volatile uint16_t copyOf_countLoadON[NO_OF_DUMPLOADS]
Definition: processing.h:45
#define BRANCH_NAME
#define COMMIT_HASH
void printForSerialJson()
Prints data logs to the Serial output in Json format.
Definition: utils.h:209
#define PROJECT_PATH
void printConfiguration()
Print the configuration during start.
Definition: utils.h:27
int freeRam()
Get the available RAM during setup.
Definition: utils.h:372
void printForEmonESP(const bool bOffPeak)
Write on Serial in EmonESP format.
Definition: utils.h:154
void sendResults(bool bOffPeak)
Prints data logs to the Serial output in text or json format.
Definition: utils.h:321
void printForSerialText()
Prints data logs to the Serial output in text format.
Definition: utils.h:256
void logLoadPriorities()
Prints the load priorities to the Serial output.
Definition: utils.h:353
Some utility functions for the RF chip.
void send_rf_data()
Send the logging data through RF.
Definition: utils_rf.h:26
Some utility functions for temperature sensor(s)
constexpr bool TEMP_SENSOR_PRESENT
Definition: utils_temp.h:23