-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathPoolMCU_CONFIGURE.ino
270 lines (247 loc) · 6.62 KB
/
PoolMCU_CONFIGURE.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
#include <SimpleTimer.h> //https://github.com/jfturcot/SimpleTimer
#include <ESP8266WiFi.h>
#include <PubSubClient.h> //https://github.com/knolleary/pubsubclient
#include <ESP8266mDNS.h>
#include <WiFiUdp.h>
#include <ArduinoOTA.h> //https://github.com/esp8266/Arduino/tree/master/libraries/ArduinoOTA
//USER CONFIGURED SECTION START//
const char* ssid = "YOUR_WIRELESS_SSID";
const char* password = "YOUR_WIRELESS_SSID";
const char* mqtt_server = "YOUR_MQTT_SERVER_ADDRESS";
const int mqtt_port = 1883;
const char *mqtt_user = "YOUR_MQTT_USERNAME";
const char *mqtt_pass = "YOUR_MQTT_PASSWORD";
const char *mqtt_client_name = "PoolMCU"; // Client connections can't have the same connection name
//USER CONFIGURED SECTION END//
WiFiClient espClient;
PubSubClient client(espClient);
SimpleTimer timer;
// Pins
const int cleanerPin = 15; //marked as D8 on the board
const int spaLightPin = 13; //marked as D7 on the board
const int poolLightPin = 12; //marked as D6 on the board
const int tempPin = A0;//marked as A0 on the board
const int modePin = 4; //marked as D2 on the board
const int modeSensePin = 14; //marked as D5 on the board
// Variables
String currentStatus = "POOL";
String newStatus = "POOL";
int gateOldStatus = 1;
int oldTemp = 0;
String oldStatus = "0";
char temperature[50];
char poolMode[50];
bool boot = true;
bool checkDebounce = false;
int newGate = 1;
bool connectWifi();
//Functions
void setup_wifi()
{
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
void reconnect()
{
int retries = 0;
while (!client.connected()) {
if(retries < 5)
{
Serial.print("Attempting MQTT connection...");
if (client.connect(mqtt_client_name, mqtt_user, mqtt_pass))
{
Serial.println("connected");
if(boot == false)
{
client.publish("checkIn/poolMCU", "Reconnected");
}
if(boot == true)
{
client.publish("checkIn/poolMCU", "Rebooted");
boot = false;
}
client.subscribe("commands/pool");
}
else
{
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
retries++;
delay(5000);
}
}
if(retries > 5)
{
ESP.restart();
}
}
}
void callback(char* topic, byte* payload, unsigned int length)
{
Serial.print("Message arrived [");
String newTopic = topic;
Serial.print(topic);
Serial.print("] ");
payload[length] = '\0';
String newPayload = String((char *)payload);
Serial.println(newPayload);
Serial.println();
if (newTopic == "commands/pool")
{
if (currentStatus == "SPA" && newPayload == "POOL")
{
digitalWrite(modePin, HIGH);
delay(800);
digitalWrite(modePin, LOW);
delay(2000);
currentStatus = "POOL";
}
if (currentStatus == "POOL" && newPayload == "SPA")
{
digitalWrite(modePin, HIGH);
delay(800);
digitalWrite(modePin, LOW);
delay(2000);
currentStatus = "SPA";
}
if(newPayload == "Pool_Light")
{
digitalWrite(poolLightPin, HIGH);
Serial.println("Sending Pool Light");
delay(800);
digitalWrite(poolLightPin, LOW);
}
if(newPayload == "Spa_Light")
{
digitalWrite(spaLightPin, HIGH);
Serial.println("Sending Spa Light");
delay(800);
digitalWrite(spaLightPin, LOW);
}
if(newPayload == "Cleaner")
{
digitalWrite(cleanerPin, HIGH);
Serial.println("Sending Cleaner");
delay(800);
digitalWrite(cleanerPin, LOW);
}
}
}
void getPoolMode()
{
if (pulseIn(modeSensePin, HIGH, 3000000) > 100)
{
currentStatus = "SPA";
if(currentStatus != oldStatus)
{
client.publish("pool/mode","SPA", true);
oldStatus = currentStatus;
}
}
else if(digitalRead(modeSensePin) == HIGH)
{
currentStatus = "SPA";
if(currentStatus != oldStatus)
{
client.publish("pool/mode","SPA", true);
oldStatus = currentStatus;
}
}
else
{
currentStatus = "POOL";
if(currentStatus != oldStatus)
{
client.publish("pool/mode","POOL", true);
oldStatus = currentStatus;
}
}
}
void checkIn()
{
client.publish("checkIn/poolMCU","OK");
}
void getTemperature() {
if(checkDebounce == false)
{
uint8_t i;
float average;
int samples[50];
float numerator = 1;
float denominator = 1;
// take 50 samples in a row, with a slight delay
for (i=0; i< 50; i++) {
samples[i] = analogRead(tempPin);
delay(10);
}
// average all the samples out
average = 0;
for (i=0; i< 50; i++) {
average += samples[i];
}
average /= 50;
// convert the value to resistance
numerator = -31000 * average;
denominator = (3.3 * average) -5155; // might need to change -5155 to 5*max ADC value
average = numerator / denominator;
Serial.print("Thermistor resistance ");
Serial.println(average);
// convert to temperature in F
float steinhart;
steinhart = average / 10000; // (R/Ro)
steinhart = log(steinhart); // ln(R/Ro)
steinhart /= 3915; // 1/B * ln(R/Ro)
steinhart += 1.0 / (25 + 273.15); // + (1/To)
steinhart = 1.0 / steinhart; // Invert
steinhart -= 273.15; // convert to C
int tempcalc = steinhart * 9 / 5 + 32; // convert to F
String temp_str = String(tempcalc);
temp_str.toCharArray(temperature, temp_str.length() + 1);
client.publish("pool/temperature", temperature);
}
}
void setup()
{
Serial.begin(115200);
// GPIO Pin Setup
pinMode(tempPin, INPUT);
pinMode(modeSensePin, INPUT);
pinMode(modePin, OUTPUT);
pinMode(spaLightPin, OUTPUT);
pinMode(poolLightPin, OUTPUT);
pinMode(cleanerPin, OUTPUT);
digitalWrite(cleanerPin, LOW);
digitalWrite(modePin, LOW);
digitalWrite(spaLightPin, LOW);
digitalWrite(poolLightPin, LOW);
setup_wifi();
client.setServer(mqtt_server, mqtt_port);
client.setCallback(callback);
ArduinoOTA.setHostname("PoolMCU");
ArduinoOTA.begin();
timer.setInterval(30000, getTemperature);
timer.setInterval(3000, getPoolMode);
timer.setInterval(120000, checkIn);
}
void loop()
{
if (!client.connected())
{
reconnect();
}
client.loop();
ArduinoOTA.handle();
timer.run();
}