-
Notifications
You must be signed in to change notification settings - Fork 0
/
CurSense.ino
202 lines (154 loc) · 4.33 KB
/
CurSense.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
/*
* CurSense.ino
*
* Created on: Sep 1, 2017
* Author: mlw
*/
#include "NTPClient.h"
#include "currentSense.h"
#include <ESP8266WiFi.h>
#include <WiFiUdp.h>
#include <ESP8266WebServer.h>
#include <FS.h>
#include "EEPROM.h"
#define THRESHOLD 0
#define THRESHOLD_TIME 2
int timeHour = 0;
int off = 0;
WiFiUDP ntpUDP;
currentSense cs;
// By default 'time.nist.gov' is used with 60 seconds update interval and
// no offset
// set the timezone in the wifi.json
NTPClient *timeClient;
extern int timeZone_;
extern ESP8266WebServer server;
extern void FSBsetup(void);
uint16_t readThreshold(void)
{
return ((EEPROM.read(THRESHOLD) << 8) | EEPROM.read(THRESHOLD + 1));
}
uint16_t readThresholdTime(void)
{
return ((EEPROM.read(THRESHOLD_TIME) << 8) | EEPROM.read(THRESHOLD_TIME + 1));
}
void writeThreshold(uint16_t value)
{
EEPROM.write(THRESHOLD, (value >> 8) & 0xff);
EEPROM.write(THRESHOLD + 1, value & 0xff);
EEPROM.commit();
}
void writeThresholdTime(uint16_t value)
{
EEPROM.write(THRESHOLD_TIME, (value >> 8) & 0xff);
EEPROM.write(THRESHOLD_TIME + 1, value & 0xff);
EEPROM.commit();
}
void handleThreshold(void) {
String thresholdArg = server.arg("threshold");
uint16_t threshold;
if (thresholdArg != "")
{
threshold = atoi(thresholdArg.c_str());
writeThreshold(threshold);
cs.setThreshold(threshold);
server.send(200, "text/plain", "Threshold set");
}
else
{
// get the threshold and return it
Serial.println("We got a request for threshold");
String output = "[";
char tmpBuf[30];
sprintf(tmpBuf, "%d", cs.getThreshold());
output += "\"threshold\":\"";
output += String(tmpBuf);
output += "\"";
output += "]";
server.send(200, "text/json", output);
}
}
void handleThresholdTime(void) {
String thresholdTimeArg = server.arg("thresholdTime");
uint16_t thresholdTime;
if (thresholdTimeArg.c_str() != "")
{
thresholdTime = atoi(thresholdTimeArg.c_str());
writeThresholdTime(thresholdTime);
cs.setThresholdTime(thresholdTime);
server.send(200, "text/plain", "Threshold Time set");
}
else
{
// get the thresholdTime and return it
Serial.println("We got a request for thresholdTime");
String output = "[";
char tmpBuf[30];
sprintf(tmpBuf, "%d", cs.getThresholdTime());
output += "\"thresholdTime\":\"";
output += String(tmpBuf);
output += "\"";
output += "]";
server.send(200, "text/json", output);
}
}
void setup() {
EEPROM.begin(512);
Serial.begin(115200);
// setup the SPIFFS first so we can read our config file
SPIFFS.begin();
server.on("/heap", HTTP_GET, [](){
String json = "{";
json += "\"heap\":"+String(ESP.getFreeHeap());
json += "}";
server.send(200, "text/json", json);
json = String();
});
server.on("/current", HTTP_GET, [](){
String json = "{";
json += "\"current\":"+String(cs.getCurrent());
json += "}";
server.send(200, "text/json", json);
json = String();
});
#if 1
//get heap status, analog input value and all GPIO statuses in one json call
server.on("/all", HTTP_GET, [](){
String json = "{";
json += "\"heap\":"+String(ESP.getFreeHeap());
json += ",\"current\":"+String(cs.getCurrent());
json += ",\"threshold\":"+String(cs.getThreshold());
json += ",\"thresholdTime\":"+String(cs.getThresholdTime());
json += ",\"timeBelowThreshold\":"+String(cs.getTimeBelowThreshold());
json += ",\"output\":"+String(cs.getOutput());
json += ",\"time\":\"";
json += timeClient->getFormattedTime();
json += "\"}";
server.send(200, "text/json", json);
json = String();
});
#endif
server.on("/threshold", handleThreshold);
server.on("/thresholdTime", handleThresholdTime);
/* read values from EEPROM */
cs.setThreshold(readThreshold());
cs.setThresholdTime(readThresholdTime());
/* start my sampler */
cs.begin();
// call the browser setup first
FSBsetup();
timeClient = new NTPClient(ntpUDP, timeZone_);
}
extern void FSBloop(void);
void loop()
{
int curTime = -1;
// make sure we deal with the time
//Serial.printf("read %d\n", analogRead(A0));
if (timeClient->update())
{
curTime = (timeClient->getEpochTime() % 86400L) / 60;
}
// we are only going to use minutes to control things so get the minutes since 12:00
FSBloop();
}