-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharduino-solar-sketch.ino
326 lines (281 loc) · 11 KB
/
arduino-solar-sketch.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
#define ARDUINO_APP
#define VERSION "SOLAR-1.47"
#define BUILD_NUMBER 2
#define BUILD_DATE __DATE__
#include <EEPROM.h>
#include <Wire.h>
#include <ArduinoSTL.h>
// automation includes not specific to arduino
#include "automation/Automation.h"
#include "automation/json/JsonStreamWriter.h"
#include "automation/sensor/Sensor.h"
#include "automation/sensor/CompositeSensor.h"
#include "automation/constraint/Constraint.h"
#include "automation/constraint/NotConstraint.h"
#include "automation/constraint/ValueConstraint.h"
#include "automation/constraint/TimeRangeConstraint.h"
#include "automation/constraint/AndConstraint.h"
#include "automation/sensor/Sensor.cpp"
#include "automation/device/Device.cpp"
#include "automation/capability/Capability.cpp"
#include "automation/constraint/Constraint.cpp"
using namespace automation;
#include "arduino/Arduino.h"
#include "arduino/Dht.h"
#include "arduino/DhtHumiditySensor.h"
#include "arduino/DhtTempSensor.h"
#include "arduino/ThermistorSensor.h"
#include "arduino/LightSensor.h"
#include "arduino/VoltageSensor.h"
#include "arduino/CurrentSensor.h"
#include "arduino/PowerSensor.h"
#include "arduino/CoolingFan.h"
#include "arduino/PowerSwitch.h"
#include "arduino/CommandProcessor.h"
#include "arduino/Eeprom.h"
#include "arduino/Automation.cpp"
#include <vector>
#include <sstream>
#include <algorithm>
#include <string.h>
using namespace std;
using namespace arduino;
const uint8_t commandBuffSize = 200;
static char commandBuff[commandBuffSize+1];
// PMSTR is used to initialize names from PROGMEM to save runtime RAM space
// PROGMEM needs to be in scope of a function so using a lambda to create a local temp area
#define PMSTR(name) []() -> char* { static const char x[] PROGMEM = {name}; \
strncpy_P(commandBuff,x,commandBuffSize); commandBuff[commandBuffSize] = '\0'; \
return (char*) commandBuff; }()
ThermistorSensor charger1Temp(PMSTR("Charger 1 Temp"), 0),
charger2Temp(PMSTR("Charger 2 Temp"), 1),
//sunroomTemp(PMSTR("Sunroom Temp"), 2),
enclosureTemp(PMSTR("Enclosure Temp (thermistor)"), 3),
//atticTemp(PMSTR("Attic Temp"), 6),
inverterTemp(PMSTR("Inverter Temp"), 4);
//LightSensor lightLevel(PMSTR("Sunshine"), 5);
Dht dht(5);
DhtTempSensor enclosureTempDht(PMSTR("Enclosure Temp (DHT)"), dht);
DhtHumiditySensor enclosureHumidityDht(PMSTR("Enclosure Humidity"), dht);
VoltageSensor batteryBankVoltage(PMSTR("Battery Bank Voltage"), 9, 1016000, 101100);
VoltageSensor batteryBankBVoltage(PMSTR("Bank B Voltage"), 10, 1016000, 101100);
vector<Sensor*> mainAndBankBDelta { &batteryBankVoltage, &batteryBankBVoltage };
CompositeSensor batteryBankAVoltage(PMSTR("Bank A Voltage"), mainAndBankBDelta, Sensor::delta);
CurrentSensor batteryBankCurrent(PMSTR("Bank Current"));
PowerSensor batteryBankPower(PMSTR("Battery Bank Power"), &batteryBankVoltage, &batteryBankCurrent);
vector<Sensor*> chargerGrpSensors { &charger1Temp, &charger2Temp };
CompositeSensor chargerGroupTemp(PMSTR("Chargers Temp"), chargerGrpSensors, Sensor::maximum);
vector<Sensor*> enclosureGrpSensors { &enclosureTempDht, &enclosureTemp };
CompositeSensor enclosureGroupTemp(PMSTR("Enclosure Temp"), enclosureGrpSensors, Sensor::maximum);
vector<Sensor*> inverterGrpSensors { &enclosureTemp, &inverterTemp };
CompositeSensor inverterGroupTemp(PMSTR("Inverter and Enclosure Temp"), inverterGrpSensors, Sensor::maximum);
arduino::CoolingFan enclosureFan(PMSTR("Enclosure Fan"), 22, enclosureGroupTemp, 98, 95, LOW);
arduino::CoolingFan chargerGroupFan(PMSTR("Chargers Fan"), 23, chargerGroupTemp, 110, 105, LOW);
arduino::CoolingFan inverterFan(PMSTR("Inverter Fan"), 24, inverterGroupTemp, 103, 100, LOW);
struct MinBatteryBankVoltage : AtLeast<float, Sensor&> {
MinBatteryBankVoltage(float volts) : AtLeast(volts, batteryBankVoltage) {
}
};
struct MinBatteryBankVoltage outletsMinSteadySupplyVoltage(23);
struct MinBatteryBankVoltage outletsMinDipSupplyVoltage(21.5);
struct InverterSwitch : public arduino::PowerSwitch {
InverterSwitch() : arduino::PowerSwitch(PMSTR("Inverter Power"), 25, HIGH) { // LOW enabled RELAY wired to normally closed
}
void setup() override; //NOTE: Arduino IDE 1.8.7 or ARM G++ compiler gets confused about inline overrides
RTTI_GET_TYPE_IMPL(main, InverterSwitch)
} inverterSwitch;
void InverterSwitch::setup() {
if ( !bInitialized ) {
pinMode(relayPin, OUTPUT);
setOn(true);
bInitialized = true;
}
}
/*struct BatteryBankSwitch : public arduino::PowerSwitch {
BooleanConstraint defaultOff {false};
BatteryBankSwitch(const char* title, int pin) : arduino::PowerSwitch(title, pin, LOW) {
setConstraint(&defaultOff);
}
RTTI_GET_TYPE_IMPL(main, BatteryBankSwitch)
} batteryBankASwitch(PMSTR("Bank A Switch"), 26), batteryBankBSwitch("Bank B Switch", 27);
*/
struct OutletSwitch : public arduino::PowerSwitch {
AndConstraint constraints { {&outletsMinSteadySupplyVoltage, &outletsMinDipSupplyVoltage} };
OutletSwitch(const string& name, int pin, int onValue = LOW) : arduino::PowerSwitch(name, pin, onValue) {
setConstraint(&constraints);
outletsMinDipSupplyVoltage.setPassDelayMs(5ul * MINUTES).setFailDelayMs(5 * SECONDS).setPassMargin(2);
outletsMinSteadySupplyVoltage.setPassDelayMs(15ul * MINUTES).setFailDelayMs(1 * MINUTES).setPassMargin(2);
}
void setup() override;
};
void OutletSwitch::setup() { //default setup turns switch off
if ( !bInitialized ) {
pinMode(relayPin, OUTPUT);
setOn(true);
bInitialized = true;
}
}
struct Outlet1Switch : public OutletSwitch {
Outlet1Switch() : OutletSwitch(PMSTR("Outlet 1"), 30) {}
} outlet1Switch;
struct Outlet2Switch : public OutletSwitch {
Outlet2Switch() : OutletSwitch(PMSTR("Outlet 2"), 31) {}
} outlet2Switch;
Devices devices {{
&enclosureFan, &chargerGroupFan, &inverterFan,
&inverterSwitch,
//&batteryBankASwitch, &batteryBankBSwitch,
&outlet1Switch, &outlet2Switch
}};
Sensors sensors {{
&chargerGroupTemp,
&batteryBankVoltage, &batteryBankCurrent, &batteryBankPower,
&batteryBankAVoltage, &batteryBankBVoltage,
&enclosureTemp, &enclosureGroupTemp, //&atticTemp,
&enclosureTempDht, &enclosureHumidityDht,
&charger1Temp, &charger2Temp, &inverterTemp, &inverterGroupTemp, //&sunroomTemp,
&enclosureFan.toggleSensor, &chargerGroupFan.toggleSensor,
&inverterFan.toggleSensor, &inverterSwitch.toggleSensor,
//&batteryBankASwitch.toggleSensor, &batteryBankBSwitch.toggleSensor,
&outlet1Switch.toggleSensor, &outlet2Switch.toggleSensor//, &lightLevel
}};
void setup() {
//unsigned long serialSpeed = 38400;
unsigned long serialSpeed = 57600;
unsigned int serialConfig = SERIAL_8O1;
//unsigned int serialConfig = SERIAL_8N1;
enclosureFan.minTemp.setFailDelayMs(5*MINUTES);
inverterFan.minTemp.setFailDelayMs(3*MINUTES);
//chargerGroupFan.minTemp.setFailDelayMs(5*MINUTES);
String version;
eeprom.getVersion(version);
if ( version != VERSION )
{
gLastInfoMsg = F("SETUP: Version changed. Clearing EEPROM and saving defaults. Old: ");
gLastInfoMsg += version;
gLastInfoMsg += F(" New: ");
gLastInfoMsg += VERSION;
eeprom.setVersion(VERSION);
eeprom.setSerialSpeed(serialSpeed);
eeprom.setSerialConfig(serialConfig);
eeprom.setJsonFormat(automation::json::jsonFormat);
eeprom.setCommandCount(0);
}
else
{
gLastInfoMsg = F("SETUP: Loading EEPROM data for version ");
gLastInfoMsg += version;
json::jsonFormat = eeprom.getJsonFormat();
serialSpeed = eeprom.getSerialSpeed();
serialConfig = eeprom.getSerialConfig();
JsonStreamWriter writer(json::nullStreamPrinter);
CommandProcessor::setup(writer, sensors, devices);
}
Serial.begin(serialSpeed, serialConfig);
for (Sensor* pSensor : sensors) {
pSensor->setup();
}
for (Device* pDevice : devices) {
pDevice->setup();
}
arduino::watchdog::enable();
commandBuff[0] = '\0';
}
void loop() {
static unsigned long lastUpdateTimeMs = 0, beginCmdReadTimeMs = 0;
static unsigned int updateIntervalMs = 15000;
static size_t bytesRead = 0;
bool msgSizeExceeded = false;
bool cmdReady = false;
bool msgReadTimedOut = false;
unsigned long currentTimeMs = millis();
// read char by char to avoid issues with default 64 byte serial buffer
while (Serial.available() ) {
char c = Serial.read();
if ( bytesRead == 0 ) {
if ( c == -1 )
continue;
else
beginCmdReadTimeMs = millis();
}
commandBuff[bytesRead++] = c;
if (c == '\n') {
commandBuff[bytesRead] = '\0';
cmdReady = true;
break;
} else if ( bytesRead >= commandBuffSize ) {
commandBuff[bytesRead - 1] = '\0';
msgSizeExceeded = true;
break;
}
}
if ( (cmdReady && strlen(commandBuff)) || (currentTimeMs - lastUpdateTimeMs) > updateIntervalMs )
{
sensors.reset(); // clear cached values
sensors.getValuesBySampling(); // save time by sampling in parallel
if ( !Constraints::isPaused() ) {
for (Device* pDevice : devices) {
bool bIgnoreSameResult = false; // this will override remote changes if constraint mode is not REMOTE
pDevice->applyConstraint(bIgnoreSameResult);
}
}
lastUpdateTimeMs = millis();
} else if ( beginCmdReadTimeMs > 0 && (currentTimeMs - beginCmdReadTimeMs) > updateIntervalMs ) {
msgReadTimedOut = true;
}
if ( cmdReady || msgReadTimedOut ) {
char *pszCmd = strtok(commandBuff, "|");
char *pszRequestId = strtok(NULL, "|");
unsigned int requestId = atoi(pszRequestId);
JsonSerialWriter writer;
writer.clearByteCount();
writer.clearChecksum();
writer.implPrint(F("#BEGIN:"));
writer.implPrint(requestId);
writer.implPrintln("#");
writer.println( "[" );
writer.increaseDepth();
CommandProcessor cmdProcessor(writer, sensors, devices);
if ( msgReadTimedOut )
{
writer.println("{");
cmdProcessor.beginResp();
writer + F("Serial data read timed out. Bytes received: ") + bytesRead;
if ( bytesRead == 1 ) {
writer + F(". First byte: ") + ((int)commandBuff[0]);
}
cmdProcessor.endResp(101);
writer.println("}");
}
else if ( msgSizeExceeded )
{
writer.println("{");
cmdProcessor.beginResp();
writer + F("Request exceeded maximum size. Bytes read: ") + bytesRead;
cmdProcessor.endResp(102);
writer.println("}");
}
else
{
arduino::watchdog::keepAlive();
automation::client::watchdog::messageReceived();
cmdProcessor.execute(pszCmd);
}
writer.decreaseDepth();
writer.print("]");
writer.implPrint(F("\n#END:"));
writer.implPrint(requestId);
writer.implPrint(":");
writer.implPrint(writer.getByteCount());
writer.implPrint(":");
writer.implPrint(writer.getChecksum());
writer.implPrint(":");
writer.implPrint(automation::isTimeValid()?1:0);
writer.implPrint(":");
writer.implPrint(eeprom.getDeviceId());
writer.implPrintln("#");
bytesRead = 0; // reset commandBuff
beginCmdReadTimeMs = 0;
}
arduino::watchdog::keepAlive();
}