device.ino
4.31 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
#include "device.h"
void insertSequence(int device, int start_value, int end_value, double duration, struct action *action) {
struct sequence *ptr = (struct sequence*) malloc(sizeof(struct sequence));
ptr->device = device;
ptr->start_value = start_value;
ptr->end_value = end_value;
ptr->action = action;
ptr->next = NULL;
Serial.println("is0");
if ( sequence_tail == NULL ) {
Serial.println("is1");
sequence_head = ptr;
set_relay(device, start_value);
ptr->end = action->start + duration;
} else {
Serial.println("is2");
sequence_tail->next = ptr;
ptr->start = sequence_tail->end;
ptr->end = sequence_tail->end + duration;
}
Serial.println("is3");
sequence_tail=ptr;
}
void loop_device() {
if ( sequence_tail != NULL ) { // there's something to do
monitor_sequence_duration();
next_sequence_step();
}
}
void next_sequence_step() {
unsigned int ut = time(NULL);
if ( ut >= sequence_head->end ) {
// set relay to end values of current step and start values of next step. afterwards remove the completed step;
if ( sequence_head->start_value != sequence_head->end_value ) {
set_relay(sequence_head->device, sequence_head->end_value);
}
struct sequence *tmp = (struct sequence*) malloc(sizeof(struct sequence));
tmp = sequence_head;
sequence_head = tmp->next;
if ( sequence_head != NULL ) {
set_relay(sequence_head->device, sequence_head->start_value);
next_sequence_step(); // in case the sequence duration is 0
} else {
sequence_tail = NULL;
}
free(tmp);
}
}
void monitor_sequence_duration() {
String pre = "home/compost/device/";
int device = sequence_head->device;
String duration(pre + device + "/duration");
unsigned int ut = time(NULL);
double remaining = sequence_head->end - ut;
String remainingStr(remaining);
Serial.println("--------------- Duration");
Serial.println(duration);
Serial.println(remainingStr);
Serial.println("---------------");
mqttClient.publish(duration, remainingStr );
}
int fill_start(struct action *ptr) {
if ( sequence_head == NULL ) {
insertSequence( dst_valve_rotate, 0, 0, 0, ptr );
insertSequence( dst_valve_watersource, 1, 1, 0, ptr );
insertSequence( src_valve_pump, 0, 0, 0, ptr );
insertSequence( peristaltic_reverse, 1, 0, 5, ptr ); // fill hose for 5 seconds
insertSequence( src_valve_pump, 1, 0, ptr->data, ptr ); // let gravity do its work
insertSequence( peristaltic, 1, 0, 5, ptr ); // vent hose for 5 seconds
insertSequence( dst_valve_watersource, 0, 0, 0, ptr );
return 1;
} else {
return 0;
}
}
int rotate_start(struct action *ptr) {
if ( sequence_head == NULL ) {
insertSequence( dst_valve_watersource, 0, 0, 0, ptr );
insertSequence( dst_valve_rotate, 1, 1, 0, ptr );
insertSequence( src_valve_pump, 1, 1, 0, ptr );
insertSequence( dive, 1, 0, ptr->data, ptr );
insertSequence( src_valve_pump, 0, 0, 0, ptr );
insertSequence( dst_valve_rotate, 0, 0, 0, ptr );
return 1;
} else {
return 0;
}
}
int rotate_start(struct button *ptr){
return 1;
}
int fertilize_start(struct action *ptr) {
if ( sequence_head == NULL ) {
insertSequence( dst_valve_watersource, 1, 1, 0, ptr );
insertSequence( dst_valve_rotate, 0, 0, 0, ptr );
insertSequence( src_valve_pump, 1, 1, 0, ptr );
insertSequence( dive, 1, 0, ptr->data, ptr );
insertSequence( src_valve_pump, 0, 0, 0, ptr );
insertSequence( dst_valve_rotate, 0, 0, 0, ptr );
insertSequence( dst_valve_watersource, 0, 0, 0, ptr );
return 1;
} else {
return 0;
}
}
void set_relay(int device, int value) {
String pre = "home/compost/device/";
/* int device = sequence_head->device; */
String state(pre + device + "/state");
String valueStr( value );
mqttClient.publish(state, valueStr );
Serial.println("--------------- Relay");
Serial.println(state);
Serial.println(valueStr);
Serial.println("---------------");
int pin = 1;
while ( pin < 7 ) {
if ( device == pin
/* || step->src_valve == pin */
/* || step->dst_valve == pin */
) {
/* pcf8574.digitalWrite(pin,0); */
} else {
/* pcf8574.digitalWrite(pin,1); */
}
pin++;
}
}