-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathm_srv_petdoor.lua
192 lines (189 loc) · 6.85 KB
/
m_srv_petdoor.lua
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
hc_server_responce=("No command "..pl.." found\n-options are petdoor_front_[open|close|toggle],petdoor_side_[open|close|toggle]")
if string.find(pl,'petdoor_side_') then
action=string.sub(pl, 14,19)
side_door(action)
hc_server_responce=("petdoor_side_door="..m.petdoor_side_door_state)
elseif string.find(pl,'petdoor_front_') then
action=string.sub(pl, 15,20)
front_door(action)
hc_server_responce=("petdoor_front_door="..m.petdoor_front_door_state)
elseif string.find(pl,'petdoor_button_') then
action=string.sub(pl, 16,22)
button(action)
hc_server_responce=("petdoor_button="..m.petdoor_button_state)
end
if m.petdoor=="enabled" then
m.petdoor="started"
--
pwm.setup(m.petdoor_side_door, 100, m.petdoor_side_door_lock_open)
pwm.setup(m.petdoor_side_door_lock, 100, m.petdoor_side_door_open)
pwm.setup(m.petdoor_front_door, 100, m.petdoor_front_door_open)
--
side_door_open_tmr=tmr.create()
side_door_close_tmr=tmr.create()
side_door_lock_open_tmr=tmr.create()
side_door_lock_close_tmr=tmr.create()
front_door_move_tmr=tmr.create()
front_door_close_tmr=tmr.create()
--
gpio.mode(m.petdoor_button, gpio.INT, gpio.PULLUP) --button
gpio.mode(m.petdoor_side_door_rid, gpio.INPUT, gpio.PULLUP) --side rid
gpio.mode(m.petdoor_front_door_rid, gpio.INPUT, gpio.PULLUP) --front rid
--side door
side_door_open_tmr:register(m.petdoor_side_door_speed, tmr.ALARM_AUTO, function (t)
m.petdoor_side_door_position=m.petdoor_side_door_position-1
pwm.setduty(m.petdoor_side_door,m.petdoor_side_door_position)
if m.petdoor_side_door_position<=m.petdoor_side_door_open then
if retry_s >= 100 then
side_door_open_tmr:stop()
pwm.stop(m.petdoor_side_door)
m.petdoor_side_door_state="open"
return
end
retry_s=retry_s+1
m.petdoor_side_door_position=m.petdoor_side_door_open
end
end)
side_door_close_tmr:register(m.petdoor_side_door_speed, tmr.ALARM_AUTO, function (t)
m.petdoor_side_door_position=m.petdoor_side_door_position+1
pwm.setduty(m.petdoor_side_door,m.petdoor_side_door_position)
if m.petdoor_side_door_position>=m.petdoor_side_door_closed then
-- pwm.setduty(m.petdoor_side_door,m.petdoor_side_door_closed)
if gpio.read(m.petdoor_side_door_rid) == 0 then
side_door_close_tmr:stop()
side_door_lock_close_tmr:start()
else
if retry_s >= 5 then
side_door_close_tmr:stop()
pwm.stop(m.petdoor_side_door)
retry_s=0
side_door("open")
return
end
retry_s=retry_s+1
m.petdoor_side_door_position=m.petdoor_side_door_position-10
end
end
end)
--side door lock
side_door_lock_open_tmr:register(m.petdoor_side_door_lock_speed, tmr.ALARM_AUTO, function (t)
m.petdoor_side_door_lock_position=m.petdoor_side_door_lock_position-1
pwm.setduty(m.petdoor_side_door_lock,m.petdoor_side_door_lock_position)
if m.petdoor_side_door_lock_position<=m.petdoor_side_door_lock_open then
side_door_lock_open_tmr:stop()
side_door_open_tmr:start()
pwm.stop(m.petdoor_side_door_lock)
end
end)
side_door_lock_close_tmr:register(m.petdoor_side_door_lock_speed, tmr.ALARM_AUTO, function (t)
m.petdoor_side_door_lock_position=m.petdoor_side_door_lock_position+1
pwm.setduty(m.petdoor_side_door_lock,m.petdoor_side_door_lock_position)
if m.petdoor_side_door_lock_position>=m.petdoor_side_door_lock_closed then
side_door_lock_close_tmr:stop()
pwm.stop(m.petdoor_side_door_lock)
pwm.stop(m.petdoor_side_door)
m.petdoor_side_door_state="closed"
end
end)
--front door
front_door_move_tmr:register(m.petdoor_front_door_speed, tmr.ALARM_AUTO, function (t)
pwm.start(m.petdoor_front_door)
if gpio.read(m.petdoor_front_door_rid) == 0 then
retry_f=retry_f+1
if retry_f>=10 then
if m.petdoor_front_door_state=="opening" then
m.petdoor_front_door_state="open"
elseif m.petdoor_front_door_state=="closing" then
m.petdoor_front_door_state="closed"
end
front_door_move_tmr:stop()
pwm.stop(m.petdoor_front_door)
wifi.sleeptype(wifi.LIGHT_SLEEP) --wifi.NONE_SLEEP,
end
else
retry_f=7
end
end)
--
function side_door(action)
retry_s=0
side_door_open_tmr:stop()
side_door_close_tmr:stop()
side_door_lock_open_tmr:stop()
side_door_lock_close_tmr:stop()
if action=="open" then
if m.petdoor_side_door_lock_position ~= m.petdoor_side_door_lock_open then
pwm.start(m.petdoor_side_door)
side_door_lock_open_tmr:start()
else
side_door_open_tmr:start()
end
elseif action=="close" then
if m.petdoor_side_door_lock_position == m.petdoor_side_door_lock_open then
side_door_close_tmr:start()
elseif m.petdoor_side_door_lock_position ~= m.petdoor_side_door_lock_closed then
side_door_lock_close_tmr:start()
end
elseif action=="toggle" then
if m.petdoor_side_door_state~="open" and m.petdoor_side_door_state~="opening" then
m.petdoor_side_door_state="opening"
side_door("open")
else
m.petdoor_side_door_state="closing"
side_door("close")
end
end
end
--
function front_door(action)
retry_f=0
front_door_move_tmr:stop()
wifi.sleeptype(wifi.NONE_SLEEP) --wifi.NONE_SLEEP,
if action=="open" and m.petdoor_front_door_state~="open" then
m.petdoor_front_door_state="opening"
pwm.setduty(m.petdoor_front_door,m.petdoor_front_door_open)
front_door_move_tmr:start()
elseif action=="close" and m.petdoor_front_door_state~="closed" then
m.petdoor_front_door_state="closing"
pwm.setduty(m.petdoor_front_door,m.petdoor_front_door_closed)
front_door_move_tmr:start()
elseif action=="toggle" then
if m.petdoor_front_door_state~="open" and m.petdoor_front_door_state~="opening" then
front_door("open")
else
--if m.petdoor_front_door_state~="closed" and m.petdoor_front_door_state~="closing" then
front_door("close")
end
elseif action=="stop" then
pwm.stop(m.petdoor_front_door)
wifi.sleeptype(wifi.LIGHT_SLEEP)
end
end
--
function button(action)
if action=="disable" then
m.petdoor_button_state="disabled"
return
elseif action=="enable" then
m.petdoor_button_state="enabled"
return
elseif m.petdoor_button_state=="enabled" then
local delay = 100000
local now = tmr.now()
local delta = now - m.petdoor_button_lastpress
if delta < 0 then delta = delta + 2147483647 end
if delta < delay then return end
if gpio.read(m.petdoor_button) == 0 then
front_door("toggle")
m.petdoor_button_lastpress= tmr.now()
end
end
end
--
gpio.mode(m.petdoor_button, gpio.INT, gpio.PULLUP)
gpio.trig(m.petdoor_button, 'down', button)
--
end
--dofile("m_hc_server_petdoor.lua")
--node.compile("m_hc_server_petdoor.lua")
--print(node.heap())