Humidistat
Arduino firmware for a humidistat (humidity controller)
Loading...
Searching...
No Matches
SerialLogger.h
Go to the documentation of this file.
1#ifndef HUMIDISTAT_SERIALLOGGER_H
2#define HUMIDISTAT_SERIALLOGGER_H
3
4#include <stdint.h>
5#include <etl/span.h>
6
7#include "asprintf.h"
11
14template<class Humidistat_t>
16private:
17 const Humidistat_t &humidistat;
18 const etl::span<const ThermistorReader, 4> trs;
19
20 // Can't specialize constexpr...
21 static const char header[];
22
23 const uint16_t interval;
24 unsigned long lastTime = 0;
25 bool ready = false;
26
28 void log();
29
30public:
35 explicit SerialLogger(const Humidistat_t *humidistat, etl::span<const ThermistorReader, 4> trs, uint16_t interval)
37
39 static void begin(uint32_t baud) {
40 Serial.begin(baud);
41 // Indicate that we're ready
42 Serial.println("RDY");
43 }
44
46 void update() {
47 // Listen for RDY signal
48 if (Serial.available()) {
49 char buf[8];
50 size_t numRead = Serial.readBytesUntil('\r', buf, 7);
51 buf[numRead] = '\0';
52
53 if (strcmp(buf, "RDY") == 0) {
54 // Print header and set ready state
55 Serial.println(header);
56 ready = true;
57 }
58 }
59
60 if (ready) {
61 if (millis() - lastTime >= interval) {
62 lastTime = millis();
63 log();
64 }
65 }
66 }
67};
68
69template<>
70const char SerialLogger<SingleHumidistat>::header[] = "Time Humidity Setpoint Temperature ControlValue T0 T1 T2 T3 "
71 "pTerm iTerm dTerm";
72
73template<>
74const char SerialLogger<CascadeHumidistat>::header[] = "Time PV SP T CV inner0PV inner0SP inner0CV inner1PV inner1SP "
75 "inner1CV pTerm iTerm dTerm inner0pTerm inner0iTerm inner0dTerm "
76 "inner1pTerm inner1iTerm inner1dTerm";
77
78template<>
80 double pTerm, iTerm, dTerm;
81 humidistat.getTerms(pTerm, iTerm, dTerm);
82
83 char *buf = asprintf("%lu %.2f %.2f %.2f %.4f %.2f %.2f %.2f %.2f %.4f %.4f %.4f",
84 lastTime,
85 humidistat.getHumidity(),
86 humidistat.sp,
87 humidistat.getTemperature(),
88 humidistat.cv,
89 trs[0].readTemp(),
90 trs[1].readTemp(),
91 trs[2].readTemp(),
92 trs[3].readTemp(),
93 pTerm,
94 iTerm,
95 dTerm
96 );
97
98 Serial.println(buf);
99 delete buf;
100}
101
102template<>
104 double outerPTerm, outerITerm, outerDTerm;
105 humidistat.getTerms(outerPTerm, outerITerm, outerDTerm);
106
107 double innerPTerms[2], innerITerms[2], innerDTerms[2];
108 humidistat.getInner(0)->getTerms(innerPTerms[0], innerITerms[0], innerDTerms[0]);
109 humidistat.getInner(1)->getTerms(innerPTerms[1], innerITerms[1], innerDTerms[1]);
110
111 char *buf = asprintf("%lu %.2f %.2f %.2f %.4f %.3f %.4f %.3f %.4f %.4f %.4f %.4f %.4f %.4f %.4f %.4f %.4f %.4f %.4f %.4f",
112 lastTime,
113 humidistat.getHumidity(),
114 humidistat.sp,
115 humidistat.getTemperature(),
116 humidistat.cv,
117 humidistat.getInner(0)->pv,
118 humidistat.getInner(0)->sp,
119 humidistat.getInner(0)->cv,
120 humidistat.getInner(1)->pv,
121 humidistat.getInner(1)->sp,
122 humidistat.getInner(1)->cv,
123 outerPTerm,
124 outerITerm,
125 outerDTerm,
126 innerPTerms[0],
127 innerITerms[0],
128 innerDTerms[0],
129 innerPTerms[1],
130 innerITerms[1],
131 innerDTerms[1]
132 );
133
134 Serial.println(buf);
135 delete buf;
136}
137
138#endif //HUMIDISTAT_SERIALLOGGER_H
char * asprintf(const char *fmt, T... args)
Print formatted data to string. Automatically allocates string on the heap. Make sure to delete it im...
Definition asprintf.h:13
Logs humidistat data over serial.
void log()
Write a line to serial.
void update()
Log a line every interval, once data has been received.
SerialLogger(const Humidistat_t *humidistat, etl::span< const ThermistorReader, 4 > trs, uint16_t interval)
Constructor.
static const char header[]
static void begin(uint32_t baud)
Setup the serial interface.
const Humidistat_t & humidistat
const etl::span< const ThermistorReader, 4 > trs
unsigned long lastTime
Last time line was written (in millis)
const uint16_t interval
Logging interval (in millis)
double readTemp() const
Get the temperature of the thermistor.
SerialLogger< cHumidistat > serialLogger & humidistat
Definition main.cpp:84
ThermistorReader trs[]
Definition main.cpp:25