-
Notifications
You must be signed in to change notification settings - Fork 9
/
Solarmeter.ino
210 lines (192 loc) · 5.07 KB
/
Solarmeter.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
#define VERSION "V11.43"
#include <SPI.h>
#include <Ethernet.h>
#include <EthernetUdp.h>
#include <Dns.h>
#include <TimeLib.h>
#include <MsTimer2.h>
#include <avr/wdt.h>
#include <utility/w5100.h>
#include "FlashMini.h"
#include "S0Sensor.h"
#include "P1GasSensor.h"
#include "P1Power.h"
#include "AnalogSensor.h"
#include "FerrarisSensor.h"
#include "Temperature.h"
#include "userdefs.h"
//#include <SD.h>
// global variables
byte lastDayReset;
byte lastHour;
byte lastMinute;
byte iDay;
byte iHour;
byte iMinute;
int upTime; // the amount of hours the Arduino is running
EthernetServer server(555); // port changed from 80 to 555
EthernetUDP Udp;
char webData[70];
#ifdef USE_LOGGING
File logFile;
#endif
#define EE_RESETDAY 4
void setup()
{
// wait for the ethernet shield to wakeup
delay(300);
// initialize network
Ethernet.begin(mac, ip, dnsserver, gateway, subnet);
// set connect timeout parameters
W5100.setRetransmissionTime(2000); // 200ms per try
W5100.setRetransmissionCount(8);
// Try to set the time 10 times
UpdateTime();
#ifdef USE_LOGGING
// initialize SD card
SetupSD();
OpenLogFile();
#endif
// start listening
server.begin();
// initialize the sensors
for(byte i = 0; i < NUMSENSORS; i++)
{
sensors[i]->Begin(i);
}
// set a random seed
randomSeed(analogRead(0));
// restore the last day on which the counters were reset
lastDayReset = eeprom_read_byte((uint8_t*) EE_RESETDAY);
// if the eeprom contains illegal data, set it to a useful value
if(lastDayReset == 0 || lastDayReset > 31) lastDayReset = day();
lastMinute = minute();
lastHour = hour();
upTime = 0;
#ifdef USE_WD
SetupWatchdog();
#endif
// start the timer interrupt
MsTimer2::set(5, Every5ms); // 5ms period
MsTimer2::start();
}
// check and update all counters every 5ms.
void Every5ms()
{
for(byte i = 0; i < NUMSENSORS; i++)
{
sensors[i]->CheckSensor();
}
#ifdef USE_WD
CheckWatchdog();
#endif
}
void loop()
{
// get the actual time
iDay = day();
iHour = hour();
iMinute = minute();
// reset counters when todays day is different from the last day the counters were reset
if(iDay != lastDayReset)
{
busy(1);
#ifdef USE_MINDERGAS
// Calculate the new gas metervalue and start the countdown
UpdateGas();
#endif
for(byte i = 0; i < NUMSENSORS; i++)
{
sensors[i]->Reset();
}
#ifdef USE_LOGGING
// create new logfile
CloseLogFile();
OpenLogFile();
#endif
lastDayReset = iDay;
// store today as the date of the last counter reset
eeprom_write_byte((uint8_t*) EE_RESETDAY, lastDayReset);
}
// hour has changed
// cannot simply check the change of an hour because 'updatetime' can also change the hour
// therefore we also check that the minutes are 0
if(iHour != lastHour && iMinute == 0)
{
busy(2);
lastHour = iHour;
upTime++;
// save the daily values every hour
for(byte i = 0; i < NUMSENSORS; i++)
{
sensors[i]->Save();
}
// sync the time at fixed interval
if(lastHour == 2 || lastHour == 14)
{
UpdateTime();
}
#ifdef USE_MAIL
if(lastHour == MAIL_TIME)
{
SendMail();
}
#endif
}
// update every minute
if(iMinute != lastMinute)
{
busy(3);
lastMinute = iMinute;
for(byte i = 0; i < NUMSENSORS; i++)
{
sensors[i]->CalculateActuals();
}
busy(31);
#ifdef USE_MINDERGAS
// this function will not do anything until the countdown timer is finished
SendToMinderGas();
#endif
#ifdef USE_LOGGING
WriteDateToLog();
for(byte i = 0; i < NUMSENSORS; i++)
{
//sensors[i]->Status(&logFile);
logFile << sensors[i]->Today << ";" << sensors[i]->Actual << ";" << endl;
}
logFile << endl;
logFile.flush();
#endif
busy(32);
// update every 5 minutes or whatever is set in userdefs
if((lastMinute%UPDATEINTERVAL) == 0)
{
SendToPvOutput(sensors);
busy(33);
// reset the maximum for pvoutput
for(byte i = 0; i < NUMSENSORS; i++)
{
sensors[i]->ResetPeak();
}
}
busy(34);
#ifdef EXOSITE_KEY
if((lastMinute%EXOSITEUPDATEINTERVAL) == 0)
{
SendToExosite();
}
#endif
}
busy(4);
// let all sensors do other stuff
for(byte i = 0; i < NUMSENSORS; i++)
{
sensors[i]->Loop(lastMinute);
}
busy(5);
// see if there are clients to serve
ServeWebClients();
busy(0);
// give the ethernet shield some time to rest
delay(50);
}