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
35
51inline void printConfiguration()
52{
53#ifndef PROJECT_PATH
54#define PROJECT_PATH (__FILE__)
55#endif
56
57#ifndef BRANCH_NAME
58#define BRANCH_NAME ("N/A")
59#endif
60#ifndef COMMIT_HASH
61#define COMMIT_HASH ("N/A")
62#endif
63
64 DBUGLN();
65 DBUGLN();
66 DBUGLN(F("----------------------------------"));
67 DBUG(F("Sketch ID: "));
69
70 DBUG(F("From branch '"));
71 DBUG(F(BRANCH_NAME));
72 DBUG(F("', commit "));
74
75 DBUG(F("Build on "));
76#ifdef CURRENT_TIME
78#else
79 DBUG(F(__DATE__));
80 DBUG(F(" "));
81 DBUGLN(F(__TIME__));
82#endif
83 DBUGLN(F("ADC mode: free-running"));
84
85 DBUGLN(F("Electrical settings"));
86
87 DBUG(F("\tf_powerCal for Grid"));
88 DBUG(F(" = "));
90 DBUG(F("\tf_powerCal for Diversion"));
91 DBUG(F(" = "));
93
94 DBUG("\tAnti-creep limit (Joules / mains cycle) = ");
96 DBUG("\tExport rate (Watts) = ");
98
100
101 DBUG(F("Datalogging capability "));
103 {
104 DBUGLN(F("in Human-readable format"));
105 }
106 else if constexpr (SERIAL_OUTPUT_TYPE == SerialOutputType::IoT)
107 {
108 DBUGLN(F("in IoT format"));
109 }
110 else if constexpr (SERIAL_OUTPUT_TYPE == SerialOutputType::JSON)
111 {
112 DBUGLN(F("in JSON format"));
113 }
114 else
115 {
116 DBUGLN(F("is NOT present"));
117 }
118}
119
134{
136 Serial.print(F(", P:"));
137 Serial.print(tx_data.powerGrid);
138
139 if constexpr (RELAY_DIVERSION)
140 {
141 Serial.print(F("/"));
142 Serial.print(relays.get_average());
143 }
144
145 Serial.print(F(", D:"));
146 Serial.print(tx_data.powerDiverted);
147
148 Serial.print(F(", E:"));
150
151 Serial.print(F(", V"));
152 Serial.print(F(":"));
153 Serial.print((float)tx_data.Vrms_L_x100 * 0.01f);
154
155 if constexpr (TEMP_SENSOR_PRESENT)
156 {
157 for (uint8_t idx = 0; idx < temperatureSensing.get_size(); ++idx)
158 {
159 if ((OUTOFRANGE_TEMPERATURE == tx_data.temperature_x100[idx])
160 || (DEVICE_DISCONNECTED_RAW == tx_data.temperature_x100[idx]))
161 {
162 continue;
163 }
164
165 Serial.print(F(", T"));
166 Serial.print(idx + 1);
167 Serial.print(F(":"));
168 Serial.print((float)tx_data.temperature_x100[idx] * 0.01f);
169 }
170 }
171
172 Serial.print(F(", (minSampleSets/MC "));
174 Serial.print(F(", #ofSampleSets "));
176
177#ifndef DUAL_TARIFF
178 if constexpr (PRIORITY_ROTATION != RotationModes::OFF)
179 {
180 Serial.print(F(", NoED "));
182 }
183#endif // DUAL_TARIFF
184
185 Serial.println(F(")"));
186}
187
204inline void printForJSON(const bool bOffPeak)
205{
206 ArduinoJson::StaticJsonDocument< 256 > doc;
207
208 doc["P"] = tx_data.powerGrid;
209
210 if constexpr (RELAY_DIVERSION)
211 {
212 doc["R"] = relays.get_average();
213 }
214
215 doc["D"] = tx_data.powerDiverted;
217 doc["V"] = (float)tx_data.Vrms_L_x100 * 0.01f;
218
219 if constexpr (TEMP_SENSOR_PRESENT)
220 {
221 for (uint8_t idx = 0; idx < temperatureSensing.get_size(); ++idx)
222 {
223 if ((OUTOFRANGE_TEMPERATURE == tx_data.temperature_x100[idx])
224 || (DEVICE_DISCONNECTED_RAW == tx_data.temperature_x100[idx]))
225 {
226 continue;
227 }
228 doc[String("T") + (idx + 1)] = (float)tx_data.temperature_x100[idx] * 0.01f;
229 }
230 }
231
233
234 serializeJson(doc, Serial);
235 Serial.println();
236}
237
262{
263 static TeleInfo teleInfo;
264
265 teleInfo.startFrame(); // Start a new telemetry frame
266
267 teleInfo.send("P", tx_data.powerGrid); // Send power grid data
268
269 if constexpr (RELAY_DIVERSION)
270 {
271 teleInfo.send("R", static_cast< int16_t >(relays.get_average())); // Send relay average if diversion is enabled
272
273 uint8_t idx = 0;
274 do
275 {
276 teleInfo.send("R", relays.get_relay(idx).isRelayON()); // Send diverted energy count for each relay
277 } while (++idx < relays.get_size());
278 }
279
280 teleInfo.send("V", tx_data.Vrms_L_x100); // Send voltage in volts
281
282 teleInfo.send("D", tx_data.powerDiverted); // Send power diverted
283 teleInfo.send("E", static_cast< int16_t >(Shared::copyOf_divertedEnergyTotal_Wh_forDL)); // Send diverted energy in Wh
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; // Skip invalid temperature readings
293 }
294 teleInfo.send("T", tx_data.temperature_x100[idx], idx + 1); // Send temperature
295 }
296 }
297
298 teleInfo.send("N", static_cast< int16_t >(Shared::absenceOfDivertedEnergyCountInSeconds)); // Send absence of diverted energy count for 50Hz
299
302
303 teleInfo.endFrame(); // Finalize and send the telemetry frame
304}
305
323inline void sendResults(bool bOffPeak)
324{
325 static bool startup{ true };
326
327 if (startup)
328 {
329 startup = false;
330 return; // reject the first datalogging which is incomplete !
331 }
332
333#ifdef RF_PRESENT
334 send_rf_data(); // *SEND RF DATA*
335#endif
336
338 {
340 }
341 else if constexpr (SERIAL_OUTPUT_TYPE == SerialOutputType::IoT)
342 {
344 }
345 else if constexpr (SERIAL_OUTPUT_TYPE == SerialOutputType::JSON)
346 {
347 printForJSON(bOffPeak);
348 }
349}
350
362inline int freeRam()
363{
364 extern int __heap_start, *__brkval;
365 int v;
366 return (int)&v - (__brkval == 0 ? (int)&__heap_start : (int)__brkval);
367}
368
369#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:155
void send(const char *tag, int16_t value, uint8_t index=0)
Sends a telemetry value as an integer.
Definition teleinfo.h:229
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:323
void printConfiguration()
Print the configuration during startup.
Definition utils.h:51
void printForJSON(const bool bOffPeak)
Write telemetry data to Serial in JSON format.
Definition utils.h:204
void printForSerialText()
Prints data logs to the Serial output in text format.
Definition utils.h:133
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:261
int freeRam()
Get the available RAM during setup.
Definition utils.h:362
PayloadTx_struct< TEMP_SENSOR_PRESENT ? temperatureSensing.get_size() :0 > tx_data
Definition utils.h:34
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