-
Notifications
You must be signed in to change notification settings - Fork 0
/
UH-Control.ino
290 lines (246 loc) · 7.78 KB
/
UH-Control.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
#include <DS18B20.h>
//#include <OneWire.h>
//#include <DallasTemperature.h>
#include <WiFi.h>
#include "PCF8574.h"
#include "Adafruit_MQTT.h"
#include "Adafruit_MQTT_Client.h"
/************************* WiFi Access Point *********************************/
#define WLAN_SSID "SSID_NAME" // enter your ssid
#define WLAN_PASS "123456789" //enter your wifi password
// Adafruit IO
#define AIO_SERVER ""
#define AIO_SERVERPORT 1883
#define AIO_USERNAME ""
#define AIO_KEY ""
/************************* PCF8574 *********************************/
//#define I2C_RELAYS_ADR 0x24
//#define I2C_DIGITAL_ADR 0x22
PCF8574 pcf1(0x24, 4, 15);
PCF8574 pcf(0x22, 4, 15);
unsigned long current = 0;
unsigned long i = 0;
/************************* ds18b20 *********************************/
//#define ONE_WIRE_BUS 32
//OneWire oneWire(ONE_WIRE_BUS);
//DallasTemperature sensors(&oneWire);
DS18B20 ds1(32); //channel-1-DS18b20
DS18B20 ds2(33); //channel-2-DS18b20
int deviceCount = 0;
float tempC;
/************ Global State ******************/
// Create an ESP8266 WiFiClient class to connect to the MQTT server.
WiFiClient client;
// Setup the MQTT client class by passing in the WiFi client and MQTT server and login details.
/****************************** Feeds ***************************************/
Adafruit_MQTT_Client mqtt(&client, AIO_SERVER, AIO_SERVERPORT, AIO_USERNAME, AIO_KEY);
Adafruit_MQTT_Publish setvalue = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME "/feeds/setvalue");
Adafruit_MQTT_Publish sensor1 = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME "/feeds/sensor1");
Adafruit_MQTT_Publish sensor2 = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME "/feeds/sensor2");
Adafruit_MQTT_Publish waterpump = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME "/feeds/waterpump");
Adafruit_MQTT_Publish heatpump = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME "/feeds/heatpump");
Adafruit_MQTT_Publish boiler = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME "/feeds/boiler");
Adafruit_MQTT_Subscribe setpoint = Adafruit_MQTT_Subscribe(&mqtt, AIO_USERNAME "/feeds/setpoint", MQTT_QOS_1); //set point
/*************************** Sketch Code ************************************/
// Bug workaround for Arduino 1.6.6, it seems to need a function declaration
// for some reason (only affects ESP8266, likely an arduino-builder bug).
void MQTT_connect();
uint16_t save;
float a = 0;
float b = 0;
int f, g, h = 0;
//DS18B20 ds(32);
//DS18B20 ds1(33);
void setup() {
Serial.begin(115200);
delay(10);
pcf.pinMode(P0, INPUT_PULLUP); //ThemoState Signal
pcf.pinMode(P1, INPUT_PULLUP); //Defrost Signal
pcf.pinMode(P2, INPUT_PULLUP); //Alarm Signal
pcf1.pinMode(P0, OUTPUT); //Heat Pump (not used)
pcf1.pinMode(P1, OUTPUT); //Boiler (not used)
pcf1.pinMode(P2, OUTPUT); //Water Pump (not used)
pcf1.pinMode(P3, OUTPUT); //Heat Pump
pcf1.pinMode(P4, OUTPUT); //Boiler
pcf1.pinMode(P5, OUTPUT); //Water Pump
pcf1.digitalWrite(P0, HIGH);
pcf1.digitalWrite(P1, HIGH);
pcf1.digitalWrite(P2, HIGH);
pcf1.digitalWrite(P3, HIGH);
pcf1.digitalWrite(P4, HIGH);
pcf1.digitalWrite(P5, HIGH);
if (pcf.begin() && pcf1.begin() )
{
Serial.println(F("Ok pcf"));
}
else
{
Serial.println(F("Error"));
}
//deviceCount = sensors.getDeviceCount();
//Serial.println("Devices on one wire");
//Serial.println(deviceCount);
//EEPROM.begin(save);
Serial.println(F("Adafruit MQTT Connection"));
// Connect to WiFi access point.
Serial.println(); Serial.println();
Serial.print("Connecting to ");
Serial.println(WLAN_SSID);
WiFi.begin(WLAN_SSID, WLAN_PASS);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println();
Serial.println("WiFi connected");
Serial.println("IP address: "); Serial.println(WiFi.localIP());
// Setup MQTT subscription for onoff feed.
mqtt.subscribe(&setpoint);
}
float sliderval;
void loop() {
// Ensure the connection to the MQTT server is alive (this will make the first
// connection and automatically reconnect when disconnected). See the MQTT_connect
// function definition further below.
MQTT_connect();
//sliderval = EEPROM.read(save);
a = ds1.getTempC(); //OUTSIDE TEMP
b = ds2.getTempC() ; //Water TEMP
Serial.println("temp 1");
Serial.println(a);
Serial.println("temp 2");
Serial.println(b);
// this is our 'wait for incoming subscription packets' busy subloop
// try to spend your time here
Adafruit_MQTT_Subscribe *subscription;
while ((subscription = mqtt.readSubscription(6000))) {
if (subscription == &setpoint) {
Serial.print(F("Got: "));
Serial.println((char *)setpoint.lastread);
sliderval = atoi((char *)setpoint.lastread);
}
}
//EEPROM.write(save, sliderval);
// Now we can publish stuff!
Serial.print(F("\nSending "));
Serial.print("...");
if (! sensor1.publish(a)) {
Serial.println(F("Failed"));
} else {
Serial.println(F("OK!"));
delay(3000);
}
if (! setvalue.publish(sliderval)) {
Serial.println(F("Failed"));
} else {
Serial.println(F("OK!"));
delay(3000);
}
if (!sensor2.publish(b)) {
Serial.println(F("Failed"));
} else {
Serial.println(F("OK!"));
delay(5000);
}
// ping the server to keep the mqtt connection alive
// NOT required if you are publishing once every KEEPALIVE seconds
if (! mqtt.ping()) {
mqtt.disconnect();
}
uint8_t val1 = pcf.digitalRead(P0);
uint8_t val2 = pcf.digitalRead(P1);
uint8_t val3 = pcf.digitalRead(P2);
Serial.println("D1 Input");
Serial.println(val1);
Serial.println("D2 Input");
Serial.println(val2);
Serial.println("D3 Input");
Serial.println(val3);
int k = 5; //Water Temp Value can be change from here:
if (val1 == LOW) {
pcf1.digitalWrite(P5, LOW); //WaterPump ON
f = 1;
if (sliderval < a && k <= b)
{
pcf1.digitalWrite(P3, LOW); //Heatpump
g = 1;
} else
{
pcf1.digitalWrite(P3, HIGH);
g = 0;
}
if (sliderval >= a || val2 == LOW || val3 == LOW || b <= 5 )
{
current = 1;
i = 0;
h = 1;
pcf1.digitalWrite(P4, LOW); //boiler
}
else {
if (current == 1)
{
pcf1.digitalWrite(P4, LOW);
i++;
}
}
if (i >= 3) { // for delay off time of boiler can be change here 3 to 4,5,6 etc its mean 15 sec will be add
pcf1.digitalWrite(P4, HIGH);
h = 0;
i = 0;
current = 0;
}
} else if (val1 == HIGH)
{
f = 0; g = 0; h = 0;
pcf1.digitalWrite(P3, HIGH);
pcf1.digitalWrite(P4, HIGH);
pcf1.digitalWrite(P5, HIGH);
}
if (! waterpump.publish(f)) {
Serial.println(F("Failed"));
} else {
Serial.println(F("OK!"));
delay(2000);
}
if (! heatpump.publish(g)) {
Serial.println(F("Failed"));
} else {
Serial.println(F("OK!"));
delay(2000);
}
if (! boiler.publish(h)) {
Serial.println(F("Failed"));
} else {
Serial.println(F("OK!"));
Serial.println("Free heap:");
Serial.println(ESP.getFreeHeap());
Serial.print("RSSI: ");
Serial.println(WiFi.RSSI());
Serial.print("Wifi status: ");
Serial.println(WiFi.status());
delay(2000);
}
}
// Function to connect and reconnect as necessary to the MQTT server.
// Should be called in the loop function and it will take care if connecting.
void MQTT_connect() {
int8_t ret;
// Stop if already connected.
if (mqtt.connected()) {
return;
}
Serial.print("Connecting to MQTT... ");
uint8_t retries = 10;
while ((ret = mqtt.connect()) != 0) { // connect will return 0 for connected
Serial.println(mqtt.connectErrorString(ret));
Serial.println("Retrying MQTT connection in 30 seconds...");
mqtt.disconnect();
delay(30000); // wait 30 seconds
retries--;
if (retries == 0) {
// basically die and wait for WDT to reset me
while (1);
}
}
Serial.println("MQTT Connected!");
}