1-phase PV router
Loading...
Searching...
No Matches
utils.h
Go to the documentation of this file.
1
11
12#ifndef UTILS_H
13#define UTILS_H
14
15#include <Arduino.h>
16#include <ArduinoJson.h>
17
18#include "FastDivision.h"
19
20#include "calibration.h"
21#include "constants.h"
22#include "dualtariff.h"
23#include "processing.h"
24#include "teleinfo.h"
25
26#include "utils_temp.h"
27
28#include "version.h"
29
30#include "utils_temp.h"
31
32#include "version.h"
33
34#if TEMP_SENSOR_PRESENT
35inline PayloadTx_struct< temperatureSensing.get_size() > tx_data;
36#else
38#endif
39
55inline void printConfiguration()
56{
57#ifndef PROJECT_PATH
58#define PROJECT_PATH (__FILE__)
59#endif
60
61#ifndef BRANCH_NAME
62#define BRANCH_NAME ("N/A")
63#endif
64#ifndef COMMIT_HASH
65#define COMMIT_HASH ("N/A")
66#endif
67
68 DBUGLN();
69 DBUGLN();
70 DBUGLN(F("----------------------------------"));
71 DBUG(F("Sketch ID: "));
73
74 DBUG(F("From branch '"));
75 DBUG(F(BRANCH_NAME));
76 DBUG(F("', commit "));
78
79 DBUG(F("Build on "));
80#ifdef CURRENT_TIME
82#else
83 DBUG(F(__DATE__));
84 DBUG(F(" "));
85 DBUGLN(F(__TIME__));
86#endif
87 DBUGLN(F("ADC mode: free-running"));
88
89 DBUGLN(F("Electrical settings"));
90
91 DBUG(F("\tf_powerCal for Grid"));
92 DBUG(F(" = "));
94 DBUG(F("\tf_powerCal for Diversion"));
95 DBUG(F(" = "));
97
98 DBUG("\tAnti-creep limit (Joules / mains cycle) = ");
100 DBUG("\tExport rate (Watts) = ");
102
104
105 DBUG(F("Datalogging capability "));
107 {
108 DBUGLN(F("in Human-readable format"));
109 }
110 else if constexpr (SERIAL_OUTPUT_TYPE == SerialOutputType::IoT)
111 {
112 DBUGLN(F("in IoT format"));
113 }
114 else if constexpr (SERIAL_OUTPUT_TYPE == SerialOutputType::JSON)
115 {
116 DBUGLN(F("in JSON format"));
117 }
118 else
119 {
120 DBUGLN(F("is NOT present"));
121 }
122}
123
138{
140 Serial.print(F(", P:"));
141 Serial.print(tx_data.powerGrid);
142
143 if constexpr (RELAY_DIVERSION)
144 {
145 Serial.print(F("/"));
146 Serial.print(relays.get_average());
147 }
148
149 Serial.print(F(", D:"));
150 Serial.print(tx_data.powerDiverted);
151
152 Serial.print(F(", E:"));
154
155 Serial.print(F(", V"));
156 Serial.print(F(":"));
157 Serial.print((float)tx_data.Vrms_L_x100 * 0.01f);
158
159 if constexpr (TEMP_SENSOR_PRESENT)
160 {
161 for (uint8_t idx = 0; idx < temperatureSensing.get_size(); ++idx)
162 {
163 if ((OUTOFRANGE_TEMPERATURE == tx_data.temperature_x100[idx])
164 || (DEVICE_DISCONNECTED_RAW == tx_data.temperature_x100[idx]))
165 {
166 continue;
167 }
168
169 Serial.print(F(", T"));
170 Serial.print(idx + 1);
171 Serial.print(F(":"));
172 Serial.print((float)tx_data.temperature_x100[idx] * 0.01f);
173 }
174 }
175
176 Serial.print(F(", (minSampleSets/MC "));
178 Serial.print(F(", #ofSampleSets "));
180
181#ifndef DUAL_TARIFF
182 if constexpr (PRIORITY_ROTATION != RotationModes::OFF)
183 {
184 Serial.print(F(", NoED "));
186 }
187#endif // DUAL_TARIFF
188
189 Serial.println(F(")"));
190}
191
208inline void printForJSON(const bool bOffPeak)
209{
210 ArduinoJson::StaticJsonDocument< 256 > doc;
211
212 doc["P"] = tx_data.powerGrid;
213
214 if constexpr (RELAY_DIVERSION)
215 {
216 doc["R"] = relays.get_average();
217 }
218
219 doc["D"] = tx_data.powerDiverted;
221 doc["V"] = (float)tx_data.Vrms_L_x100 * 0.01f;
222
223 if constexpr (TEMP_SENSOR_PRESENT)
224 {
225 for (uint8_t idx = 0; idx < temperatureSensing.get_size(); ++idx)
226 {
227 if ((OUTOFRANGE_TEMPERATURE == tx_data.temperature_x100[idx])
228 || (DEVICE_DISCONNECTED_RAW == tx_data.temperature_x100[idx]))
229 {
230 continue;
231 }
232 doc[String("T") + (idx + 1)] = (float)tx_data.temperature_x100[idx] * 0.01f;
233 }
234 }
235
237
238 serializeJson(doc, Serial);
239 Serial.println();
240}
241
266{
267 static TeleInfo teleInfo;
268
269 teleInfo.startFrame(); // Start a new telemetry frame
270
271 teleInfo.send("P", tx_data.powerGrid); // Send power grid data
272
273 if constexpr (RELAY_DIVERSION)
274 {
275 teleInfo.send("R", static_cast< int16_t >(relays.get_average())); // Send relay average if diversion is enabled
276
277 uint8_t idx = 0;
278 do
279 {
280 teleInfo.send("R", relays.get_relay(idx).isRelayON()); // Send diverted energy count for each relay
281 } while (++idx < relays.get_size());
282 }
283
284 teleInfo.send("V", tx_data.Vrms_L_x100); // Send voltage in volts
285
286 teleInfo.send("D", tx_data.powerDiverted); // Send power diverted
287 teleInfo.send("E", static_cast< int16_t >(Shared::copyOf_divertedEnergyTotal_Wh_forDL)); // Send diverted energy in Wh
288
289 if constexpr (TEMP_SENSOR_PRESENT)
290 {
291 for (uint8_t idx = 0; idx < temperatureSensing.get_size(); ++idx)
292 {
293 if ((OUTOFRANGE_TEMPERATURE == tx_data.temperature_x100[idx])
294 || (DEVICE_DISCONNECTED_RAW == tx_data.temperature_x100[idx]))
295 {
296 continue; // Skip invalid temperature readings
297 }
298 teleInfo.send("T", tx_data.temperature_x100[idx], idx + 1); // Send temperature
299 }
300 }
301
302 teleInfo.send("N", static_cast< int16_t >(Shared::absenceOfDivertedEnergyCountInSeconds)); // Send absence of diverted energy count for 50Hz
303
306
307 teleInfo.endFrame(); // Finalize and send the telemetry frame
308}
309
327inline void sendResults(bool bOffPeak)
328{
329 static bool startup{ true };
330
331 if (startup)
332 {
333 startup = false;
334 return; // reject the first datalogging which is incomplete !
335 }
336
337#ifdef RF_PRESENT
338 send_rf_data(); // *SEND RF DATA*
339#endif
340
342 {
344 }
345 else if constexpr (SERIAL_OUTPUT_TYPE == SerialOutputType::IoT)
346 {
348 }
349 else if constexpr (SERIAL_OUTPUT_TYPE == SerialOutputType::JSON)
350 {
351 printForJSON(bOffPeak);
352 }
353}
354
366inline int freeRam()
367{
368 extern int __heap_start, *__brkval;
369 int v;
370 return (int)&v - (__brkval == 0 ? (int)&__heap_start : (int)__brkval);
371}
372
373#endif // UTILS_H
Calibration values definition.
constexpr float powerCal_grid
Definition calibration.h:34
constexpr float powerCal_diverted
Definition calibration.h:35
Container for datalogging.
Definition types.h:80
A class for managing and sending telemetry information in a structured frame format.
Definition teleinfo.h:145
void send(const char *tag, int16_t value, uint8_t index=0)
Sends a telemetry value as an integer.
Definition teleinfo.h:219
constexpr bool RELAY_DIVERSION
Definition config.h:43
constexpr TemperatureSensing temperatureSensing
Definition config.h:89
constexpr RelayEngine relays
Definition config.h:79
constexpr SerialOutputType SERIAL_OUTPUT_TYPE
Definition config.h:33
constexpr bool TEMP_SENSOR_PRESENT
Definition config.h:45
constexpr int16_t REQUIRED_EXPORT_IN_WATTS
constexpr float invSUPPLY_FREQUENCY
constexpr uint8_t ANTI_CREEP_LIMIT
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 sendResults(bool bOffPeak)
Prints or sends telemetry data logs based on the selected output format.
Definition utils.h:327
void printConfiguration()
Print the configuration during startup.
Definition utils.h:55
void printForJSON(const bool bOffPeak)
Write telemetry data to Serial in JSON format.
Definition utils.h:208
void printForSerialText()
Prints data logs to the Serial output in text format.
Definition utils.h:137
volatile uint16_t copyOf_divertedEnergyTotal_Wh_forDL
Definition shared_var.h:25
volatile int32_t copyOf_energyInBucket_long
Definition shared_var.h:27
volatile uint16_t copyOf_sampleSetsDuringThisDatalogPeriod
Definition shared_var.h:29
volatile uint8_t copyOf_lowestNoOfSampleSetsPerMainsCycle
Definition shared_var.h:28
volatile uint16_t absenceOfDivertedEnergyCountInSeconds
Definition shared_var.h:17
void printParamsForSelectedOutputMode()
Print the settings used for the selected output mode.
Public functions/variables of processing engine.
Manages telemetry data and frame formatting.
void sendTelemetryData()
Sends telemetry data using the TeleInfo class.
Definition utils.h:265
PayloadTx_struct tx_data
Definition utils.h:37
int freeRam()
Get the available RAM during setup.
Definition utils.h:366
Provides utilities for managing temperature sensors.
#define BRANCH_NAME
Definition version.h:6
#define COMMIT_HASH
Definition version.h:7
#define CURRENT_TIME
Definition version.h:5
#define PROJECT_PATH
Definition version.h:4