-
Notifications
You must be signed in to change notification settings - Fork 0
/
Animal_Feeder.groovy
116 lines (98 loc) · 3.44 KB
/
Animal_Feeder.groovy
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
metadata {
definition (name: "Cat-dog Feeder", namespace: "sacua", author: "Samuel Cuerrier Auclair") {
capability "Actuator"
attribute "response", "string"
attribute "foodGiven", "number"
command "GiveFood", ["number"]
command "servo", ["number"]
command "setZero"
command "calibrate", ["number"]
}
preferences {
input name: "IPAddress", type: "string", title: "IP Address of the ESP device", required: true, defaultValue: "192.168.0.157"
input name: "servoclose", type: "number", title: "Close position of the servo", required: true, defaultValue: "95"
input name: "servoopen", type: "number", title: "Open position of the servo", required: true, defaultValue: "75"
}
}
def parse(String description) {
}
def GiveFood(foodWeight) {
try {
weightfood = foodWeight as float
String foodweight = (weightfood * state.calibrationFactor).toString();
def params = [uri: "http://"+ IPAddress + "/data/?food_weight=" + foodweight + "&openpos=" + servoopen.toString() + "&closepos=" + servoclose.toString()]
httpGet(params) { resp ->
if (resp.success) {
sendEvent(name: "response", value: "Success");
}
}
runIn(65,"FoodGiven")
}
catch (Exception e1) {
sendEvent(name: "response", value: "Failed");
log.error "EC - Call failed: ${e1.message}"
}
}
def servo(servoPos) {
try {
def params = [uri: "http://"+ IPAddress + "/data/?servo=" + servoPos]
httpGet(params) { resp ->
if (resp.success) {
sendEvent(name: "response", value: "Success");
}
}
}
catch (Exception e1) {
sendEvent(name: "response", value: "Failed");
log.error "EC - Call failed: ${e1.message}"
}
}
def setZero() {
try {
def params = [uri: "http://"+ IPAddress + "/data/?readweight=0"]
httpGet(params) { resp ->
if (resp.success) {
String currentValue = resp.data;
state.zero = Integer.parseInt(currentValue);
}
}
}
catch (Exception e1) {
sendEvent(name: "response", value: "Failed");
log.error "EC - Call failed: ${e1.message}"
}
}
def calibrate(weight) {
try {
def params = [uri: "http://"+ IPAddress + "/data/?readweight=0"]
httpGet(params) { resp ->
if (resp.success) {
String currentValue = resp.data;
int CurrentValue = Integer.parseInt(currentValue);
weightCal = weight as float;
state.calibrationFactor = (CurrentValue - state.zero)/weightCal as float;
}
}
}
catch (Exception e1) {
sendEvent(name: "response", value: "Failed");
log.error "EC - Call failed: ${e1.message}"
}
}
def FoodGiven() {
float foodgiven;
try {
def params = [uri: "http://"+ IPAddress + "/data/?feedSuccess=0"]
httpGet(params) { resp ->
if (resp.success) {
foodgiven = resp.data.toFloat();
foodgiven = foodgiven/state.calibrationFactor;
sendEvent(name: "foodGiven", value: foodgiven);
}
}
}
catch (Exception e1) {
sendEvent(name: "response", value: "Failed");
log.error "EC - Call failed: ${e1.message}"
}
}