-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathControladorModbus.ino
More file actions
410 lines (345 loc) · 10.6 KB
/
ControladorModbus.ino
File metadata and controls
410 lines (345 loc) · 10.6 KB
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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
#include "MemoriaNoVolatil.h"
#include "Display.h"
#include "ModbusTCP.h"
// Include the libraries we need
#include <OneWire.h>
#include <DallasTemperature.h>
// Date and time functions uspiing a DS1307 RTC connected via I2C and Wire lib
#include <Wire.h>
#include "RTClib.h"
RTC_DS1307 rtc;
// Cable de datos conectado al pin 3
#define ONE_WIRE_BUS 3
// Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
OneWire oneWire(ONE_WIRE_BUS);
// Pass our oneWire reference to Dallas Temperature.
DallasTemperature sensors(&oneWire);
DateTime now;
long STATE = 1;
void inputInit();
void outputInit();
void outputTransfer();
/******* VaRIABLES DEL MAIN *******/
bool habilitados[CANT];
bool fulltime;
bool frio[CANT]; // Guardo si hay que enfriar o calentar
bool calor[CANT]; // Guardo si hay que enfriar o calentar
uint8_t salidas_frio[CANT] ; // Aca pongo un 1 un cero para prender
uint8_t salidas_calor[CANT]; // Aca pongo un 1 un cero para prender
uint8_t forced_on_hot[CANT]; // Con un uno fuerzo la salida
uint8_t forced_off_hot[CANT]; // Con un uno fuerzo la salida
uint8_t forced_on_cold[CANT]; // Con un uno fuerzo la salida
uint8_t forced_off_cold[CANT]; // Con un uno fuerzo la salida
//const uint8_t pin_salidas_frio[CANT] = {31, 33, 35, 37,39, 41,43, 45};
// DE forma temporal uso la salida 33 para la bomba.
const uint8_t pin_salidas_frio[CANT] = {31, 45, 35, 37,39, 41,43, 45};
const uint8_t pin_salidas_calor[CANT] = {29, 27, 25, 23, 26, 28, 30, 32};
const uint8_t array_pin_botones[4] = {4, 5, 12, 13};
uint8_t state_cold[CANT];
uint8_t state_hot[CANT];
long valores[CANT];
float values[CANT];
float temp[CANT+2];
float set[CANT];
int cont = 0;
int leidos = 0;
int serial_state = 0;
//long comando = 1;
uint16_t contador = 0;
int estado = 0;
unsigned long actual_time, back_time;
uint8_t estado_main;
char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
const int chipSelect = 25;
unsigned long startTime;
unsigned long elapsedTime;
/*
* The setup function. We only start the sensors here
*/
void setup(void)
{
fulltime = 0;
leidos = 0;
// Inicializo los valores.
for(int i = 0; i < CANT; i++)
{
state_cold[i] = 0;
state_hot[i] = 0;
temp[i] = 0.0;
set[i] = 18.0;
habilitados[i] = true;
frio[i] = true;
//salidas_frio[i] = 0;
//salidas_calor[i] = 0;
/**** SACO TODOS LOS FORZADOS ****/
forced_off_hot[i]=0;
forced_on_hot[i]=0;
forced_off_cold[i]=0;
forced_on_cold[i]=0;
}
inputInit();
outputInit();
debugInit(0);
DisplayInit();
set[0] = 21.0;
ModbusInit(set);
rtcInit();
// Start up the library
sensors.begin();
// void LeerEEPROM(float *sets, uint8_t *sal_f, uint8_t *sal_cal, uint8_t *ftime, bool *hab_f, bool *hab_c, bool *hab, uint8_t cant)
LeerEEPROM(set, salidas_frio, salidas_calor, &fulltime, frio, calor, habilitados, CANT_FERM );
// Los valores que se mantienen los cargo en la tabla modbus.
transferWordStates(REG_START_HABILITATIONS, habilitados, CANT_FERM);
transferWordStates(REG_START_HABCOLD, frio, CANT_FERM);
transferWordStates(REG_START_HABHOT, calor, CANT_FERM);
CopyFloatValuesToModbusRegisters(REG_START_SETPOINTS, set, CANT_FERM);
debugPrintSetpoints(set, CANT);
fulltime = 1;
// botones de entrada
/*
pinMode(Button1,INPUT_PULLUP);
pinMode(Button2,INPUT_PULLUP);
pinMode(Button3,INPUT_PULLUP);
pinMode(Button4,INPUT_PULLUP);
*/
back_time = millis();
estado_main = 0;
//LeerEEPROM();
fulltime = 1;
}
/*
* Main function, get and show the temperature
*/
void loop(void)
{
actual_time = millis();
if( (actual_time - back_time) >3000) // Ejecuto el control cada 3 segundos
{
back_time = actual_time;
now = rtc.now(); // Reloj de tiempo real!!!!!!!
mostrarHora();
updateInternalFromTable();
readTemperatures();
ControlFunction();
outputTransfer();
updateTableFromInternal();
DisplayTask(temp, set);
estado_main = 1;
if(checkSetpoints(set) > 0)
{
debugPrintln("Found diff save eeprom");
GuardarSetEEPROM(set);
}
}
ModbusTask();
}
void ControlFunction()
{
int minuto = now.minute();
//Serial.print("Funcion de control - Minuto:"); Serial.print(minuto); Serial.print(" Estado = ");Serial.println(estado);
if(minuto <=30 || fulltime)
{
for(int i =0; i < CANT; i++)
{
//Serial.print("Control temp (");Serial.print(i);Serial.println(")");
/*************************+++ VER LA FUNCION HABILITADOS, SI SE DESHABILITA estando la salida activa QUEDA activa al deshabilitar *********/
if(habilitados[i])
{
if(frio[i])
{
debugPrint(" Controlando frio = ");debugPrintln(i);
//Serial.print(" Controlando frio = ");Serial.println(i);
ControlTempFrio(i);
}
if(calor[i])
{
debugPrint(" Controlando calor = ");debugPrintln(i);
ControlTempCalor(i);
}
}
else
{
// Apago salidas
debugPrint(" Apagando salida (desactivada) = ");debugPrintln(i);
salidas_frio[i] = 0;
salidas_calor[i] = 0;
}
}
//checkWaterPumps();
}
}
void ControlBombas() /*************** REVISARRRRRR - DEFINIR SALIDA *****************/
{
if(contador > 0 )
{
//enciendo la bomba
//digitalWrite(S5,LOW);
}
else
{
//apago la bomba
//digitalWrite(S5,HIGH);
}
}
void ControlTempFrio(int i)
{
if(temp[i] > set[i] + 0.2 ) /******************** TEMP ALTA - Abrir agua fria *****************/
{
debugPrint("Enciendo agua fria=");debugPrintln(i);
salidas_frio[i] = 1;
}
else if(temp[i] < set[i] - 0.2) /******************** TEMP BAJA - Cerrar agua fria *****************/
{
debugPrint("Apago agua fria=");debugPrintln(i);
salidas_frio[i] = 0;
}
}
void ControlTempCalor(int i)
{
if(temp[i] < set[i] - 0.2 ) /******************** TEMP BAJA - Abrir agua caliente *****************/
{
debugPrint("Enciendo agua caliente=");debugPrintln(i);
salidas_calor[i] = 1;
}
else if(temp[i] > set[i] + 0.2) /******************** TEMP ALTA - Cerrar agua caliente *****************/
{
debugPrint("Apago agua caliente=");debugPrintln(i);
salidas_calor[i] = 0;
}
}
void readTemperatures() // Leo las temperaturas y las escribo en la tabla modbus
{
// call sensors.requestTemperatures() to issue a global temperature
// request to all devices on the bus
debugPrintln("Leyendo temperatura...");
sensors.requestTemperatures(); // Send the command to get temperatures
// After we got the temperatures, we can print them here.
// We use the function ByIndex, and as an example get the temperature from the first sensor only.
for(int i = 0; i < CANT_FERM; i++)
{
temp[i] = sensors.getTempCByIndex(i);
}
}
void mostrarHora()
{
#if DEBUG
Serial.print(now.year(), DEC);
Serial.print('/');
Serial.print(now.month(), DEC);
Serial.print('/');
Serial.print(now.day(), DEC);
Serial.print(" (");
Serial.print(daysOfTheWeek[now.dayOfTheWeek()]);
Serial.print(") ");
Serial.print(now.hour(), DEC);
Serial.print(':');
Serial.print(now.minute(), DEC);
Serial.print(':');
Serial.print(now.second(), DEC);
Serial.println();
#endif
}
void rtcInit()
{
if (! rtc.begin()) {
debugPrintln("Couldn't find RTC");
while (1);
}
if (! rtc.isrunning()) {
debugPrintln("RTC is NOT running!");
// following line sets the RTC to the date & time this sketch was compiled
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
// This line sets the RTC with an explicit date & time, for example to set
// January 21, 2014 at 3am you would call:
// rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));
}
}
void inputInit()
{
for(uint8_t i = 0; i<4; i++)
{
pinMode(array_pin_botones[i], INPUT);
}
}
void outputInit()
{
// set the digital pin as output:
for(uint8_t i = 0; i<CANT; i++)
{
pinMode(pin_salidas_frio[i], OUTPUT);
digitalWrite(pin_salidas_frio[i], 1); // En el arranque apagamos las salidas
}
for(uint8_t i = 0; i<CANT; i++)
{
pinMode(pin_salidas_calor[i], OUTPUT);
digitalWrite(pin_salidas_calor[i], 1); // En el arranque apagamos las salidas
}
}
void outputTransfer()
{
/****** Enciendo o apago salidas con y sin forzado -
*** Tiene prioridad el forzado de apagado por si estan ambos activos ******/
/*** Primero leo los valores de forzado desde el modbus **/
/***********++ SALIDAS FRIO **********/
for(uint8_t i = 0; i<CANT; i++)
{
/***************************************************************************** ELIMINAR UNA VEZ IMPLEMENTADO EL CONTROL DE LA BOMBA *****/
if(i==0)
{
if((salidas_frio[i] == 1 || forced_on_cold[i] == 1) && forced_off_cold[i] == 0)
{
//Prendo bomba
pinMode(33, OUTPUT);
digitalWrite(33, 0); // Prendo la bomba
}
else
{
//Apago bomba
pinMode(33, OUTPUT);
digitalWrite(33, 1); // Prendo la bomba
}
}
if((salidas_frio[i] == 1 || forced_on_cold[i] == 1) && forced_off_cold[i] == 0)
{
digitalWrite(pin_salidas_frio[i], 0); // La salida prende por cero
state_cold[i] = 1;
}
else if(salidas_frio[i] == 0 || forced_off_cold[i] == 1)
{
digitalWrite(pin_salidas_frio[i], 1); // La salida se apaga por uno
state_cold[i] = 0;
}
}
/***********++ SALIDAS CALOR **********/
for(uint8_t i = 0; i<CANT; i++)
{
if((salidas_calor[i] == 1 || forced_on_hot[i] == 1) && forced_off_hot[i] == 0)
{
digitalWrite(pin_salidas_calor[i], 0); // La salida prende por cero
state_hot[i] = 1;
}
else if(salidas_calor[i] == 0 || forced_off_hot[i] == 1)
{
digitalWrite(pin_salidas_calor[i], 1); // La salida se apaga por uno
state_hot[i] = 0;
}
}
}
void updateInternalFromTable()
{
getWordStates(REG_START_FORCE_ON_VALVE_COLD, forced_on_cold, CANT);
getWordStates(REG_START_FORCE_OFF_VALVE_COLD, forced_off_cold, CANT);
getWordStates(REG_START_FORCE_ON_VALVE_HOT, forced_on_hot, CANT);
getWordStates(REG_START_FORCE_OFF_VALVE_HOT, forced_off_hot, CANT);
getWordStates(REG_START_HABILITATIONS, habilitados, CANT_FERM);
getWordStates(REG_START_HABCOLD, frio, CANT_FERM);
getWordStates(REG_START_HABHOT, calor, CANT_FERM);
}
void updateTableFromInternal()
{
// Copio las temperaturas en la tabla modbus.
CopyFloatValuesToModbusRegisters(REG_START_TEMPS, temp, CANT_FERM);
/*** Transfiero las salidas al mapa modbus **/
transferWordStates(REG_START_VALVE_STATE_COLD, state_cold, CANT);
transferWordStates(REG_START_VALVE_STATE_HOT, state_hot, CANT);
}