-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathArduinoMeterReader.ino
201 lines (156 loc) · 5.37 KB
/
ArduinoMeterReader.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
#include <SPI.h>
#include <Ethernet.h>
///////// CHANGEABLE VALUES /////////
const char importMultiplier[] = "1.0";
const char generationMultiplier[] = "1.0";
const char serverAddress[] = "home-monitoring.scaleys.co.uk";
const int serverPort = 80;
const int httpRequestDelay = 20;
const unsigned long millisecondsBetweenCalls = 60000L;
const char serviceEndpoint[] = "/meter";
///////// CHANGEABLE VALUES ABOVE /////////
EthernetClient ethernetClient;
byte mac[] = {0x90, 0xA2, 0xDA, 0x0F, 0xA1, 0xE6};
unsigned long importImpulseCount = 0;
unsigned long generationImpulseCount = 0;
unsigned long importImpulseCountSinceLastUpload = 0;
unsigned long generationImpulseCountSinceLastUpload = 0;
unsigned long millisecondsBetweenCallsSinceLastUpload = 0;
unsigned long lastTimeUploaded = millis();
unsigned long previousTime = 0UL;
boolean interruptsAttached = false;
// is actually pin 3...
const int importSensorPin = 1;
// is actually pin 2...
const int generationSensorPin = 0;
const int voltageSensorPin = A0;
IPAddress ip(192,168,0,12);
void setup() {
Serial.begin(9600);
connectToEthernet();
}
void connectToEthernet()
{
unsigned long millisecondsPerMinute = 60000L;
// attempt to connect to Wifi network:
// start the Ethernet connection:
bool connectedToNetwork = false;
while(!connectedToNetwork) {
Serial.println("Attempting to connect to network...");
if (Ethernet.begin(mac) == 0) {
Serial.println("Failed to connect, trying again...");
} else {
Serial.println("Connected successfully");
connectedToNetwork = true;
}
}
// give the Ethernet shield a second to initialize:
delay(1000);
Serial.println("connecting...");
Serial.print("Connected to the network IP: ");
Serial.println(Ethernet.localIP());
}
void loop() {
attachInterrupts();
if (isTimeToUploadData())
{
Serial.println("Uploading data");
sendResultsToServer();
}
}
void attachInterrupts()
{
if (!interruptsAttached)
{
interruptsAttached = true;
Serial.println("Attaching import interrupt on pin 3");
attachInterrupt(importSensorPin, flashImport, FALLING);
Serial.println("Attaching generation interrupt on pin 2");
attachInterrupt(generationSensorPin, flashGeneration, FALLING);
}
}
boolean isTimeToUploadData() {
unsigned long currentTime = millis();
if (currentTime < previousTime) {
lastTimeUploaded = currentTime;
}
previousTime = currentTime;
if ( (currentTime - lastTimeUploaded) >= millisecondsBetweenCalls) {
Serial.println("Time to upload");
lastTimeUploaded = currentTime;
return true;
}
return false;
}
/* Handles the interrupt flash logic */
void flashImport() {
importImpulseCount++;
}
void flashGeneration() {
generationImpulseCount++;
}
void sendResultsToServer() {
Serial.println("sendResultsToServer");
String postData = getPostData();
Serial.println(postData);
if (ethernetClient.connect(serverAddress, serverPort)) {
Serial.println("connected to server");
// Make a HTTP request:
ethernetClient.println("POST " + String(serviceEndpoint) + " HTTP/1.1");
ethernetClient.println("Host: " + String(serverAddress) + ":" + serverPort);
ethernetClient.println("Content-Type: application/json");
ethernetClient.println("Content-Length: " + String(postData.length()));
ethernetClient.println("Pragma: no-cache");
ethernetClient.println("Cache-Control: no-cache");
ethernetClient.println("Connection: close");
ethernetClient.println();
ethernetClient.println(postData);
ethernetClient.println();
delay(httpRequestDelay);
ethernetClient.stop();
ethernetClient.flush();
Serial.println("Called server");
uploadSuccessResetCounters();
}
}
String calculateImportWattsAndResetFlashes()
{
importImpulseCountSinceLastUpload = importImpulseCountSinceLastUpload + importImpulseCount;
importImpulseCount = 0UL;
if (importImpulseCountSinceLastUpload == 0UL) {
return "0";
}
return String(importImpulseCountSinceLastUpload);
}
String calculateGenerationWattsAndResetFlashes()
{
generationImpulseCountSinceLastUpload = generationImpulseCountSinceLastUpload + generationImpulseCount;
generationImpulseCount = 0UL;
if (generationImpulseCountSinceLastUpload == 0UL) {
return "0";
}
return String(generationImpulseCountSinceLastUpload);
}
String getPostData()
{
String importWatts = calculateImportWattsAndResetFlashes();
String generationWatts = calculateGenerationWattsAndResetFlashes();
millisecondsBetweenCallsSinceLastUpload = millisecondsBetweenCallsSinceLastUpload + millisecondsBetweenCalls;
String milliseconds = String(millisecondsBetweenCallsSinceLastUpload);
return "{\"importImpulses\":" + importWatts + ",\"generationImpulses\":" + generationWatts + ",\"msBetweenCalls\":" + milliseconds
+ ",\"importMultiplier\":" + importMultiplier + ",\"generationMultiplier\":" + generationMultiplier + ",\"mainsVoltage\":" + readMainsVoltage() + "}";
}
String readMainsVoltage()
{
double readAnalog = ((double)analogRead(voltageSensorPin) * (5.0 / 1023.0));
//Serial.println(readAnalog);
double mainsVoltage = ((readAnalog * 49.68d) + 24.04d) - 1.7d;
char tempChar[10];
dtostrf(mainsVoltage, 3, 2, tempChar);
return String(tempChar);
}
void uploadSuccessResetCounters() {
importImpulseCountSinceLastUpload = 0UL;
generationImpulseCountSinceLastUpload = 0UL;
millisecondsBetweenCallsSinceLastUpload = 0UL;
}