-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathef0102.eub
187 lines (177 loc) · 3.77 KB
/
ef0102.eub
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
% orchis-sonoff-ebb-flow ver 01.02, GNU LGPL license, (C)2018 m.sillano
% ---- This config can be updated, don't use ["] here
% telnet: script http://192.168.178.23:85/www/sonoff/ef0102.eub
config ntp_server 1.pool.ntp.org
config ntp_timezone 2
config @6 /msillanohome/ebbflow/1
on init
do
setvar $tmp = (@8 * 1)
if $tmp = 0 then
% cool start
% @1 min counter
setvar @1 = 1
% @2 pump off (0) / on (1)
setvar @2 = 1
% config timers: never = 24x60 + 1 = 1441 min ebb
setvar @3 = "{\"time\":\"07:00\",\"days\":0,\"flow\":2,\"ebb\":58}"
setvar @4 = "{\"time\":\"20:00\",\"flow\":2,\"ebb\":298}"
% @5 timer in use D/N:3/4
setvar @5 = 3
% @7 days counter
setvar @7 = 0
endif
% hot start
setvar @8 = ($tmp + 1)
setvar $cmt = @6 | "/command"
setvar $set = @6 | "/set"
setalarm 3 json_parse("time",@3)
setalarm 4 json_parse("time",@4)
settimer 1 3001
subscribe local $cmt
subscribe local $set
% restore flow state
if (@2 * @1) > 2 then
gpio_out 12 1
gpio_out 13 0
endif
on mqttconnect
do
subscribe remote $cmt
subscribe remote $set
on topic remote $set
do
println "on Remote Set " | $this_data
publish local $set $this_data
on topic remote $cmt
do
println "on Remote Command " | $this_data
publish local $cmt $this_data
on topic local $set
do
println "on Local Set " | $this_data
setvar $tmp = json_parse("data",$this_data)
if $tmp = "setD" then
% set new timing (day)
setvar @3 = json_parse("value",$this_data)
setalarm 3 json_parse("time",@3)
endif
if $tmp = "setN" then
% set new timing (night)
setvar @4 = json_parse("value",$this_data)
setalarm 4 json_parse("time",@4)
endif
if $tmp = "timezone" then
system "set ntp_timezone " | json_parse("value",$this_data)
endif
% local commands
on topic local $cmt
do
println "on Local Command " | $this_data
if $this_data = "toggle" then
if @2 = 0 then
publish local $cmt "flow"
else
publish local $cmt "ebb"
endif
endif
if $this_data = "day" then
setvar @5 = 3
setvar @7 = json_parse("days",@3)
publish local $cmt "flow"
endif
if $this_data = "night" then
if json_parse("days",@3) = 0 then
setvar @5 = 4
publish local $cmt "ebb"
endif
endif
if $this_data = "ebb" then
setvar @2 = 0
if @5 = 3 then
setvar @1 = json_parse("ebb",@3)
else
setvar @1 = json_parse("ebb",@4)
endif
if json_parse("days",@3) = 0 then
settimer 1 5999
endif
gpio_out 12 0
gpio_out 13 1
publish local $cmt "status"
endif
if $this_data = "flow" then
setvar @2 = 1
if @5 = 3 then
setvar @1 = json_parse("flow",@3)
else
setvar @1 = json_parse("flow",@4)
endif
settimer 1 59999
gpio_out 12 1
gpio_out 13 0
publish local $cmt "status"
endif
if $this_data = "sendD" then
setvar $tmp = @6 | "/status/timeD"
publish local $tmp @3
endif
if $this_data = "sendN" then
setvar $tmp = @6 | "/status/timeN"
publish local $tmp @4
endif
if $this_data = "status" then
setvar $tmp = @6 | "/status/ebbflow"
if @2 = 0 then
if json_parse("days",@3) = 0 then
setvar $msg = " in ebb: for the next flow " | @1 | " min."
else
setvar $msg = " in ebb: for the next flow " | @7 | " day(s)."
endif
publish local $tmp $timestamp | $msg
println $timestamp | $msg
else
setvar $msg = " in flow: for the next ebb " | @1 | " min."
publish local $tmp $timestamp | $msg
println $timestamp | $msg
endif
endif
% start D timing (day)
on alarm 3
do
setvar @5 = 3
if json_parse("days",@3) = 0 then
publish local $cmt "flow"
else
if @7 > 0 then
setvar @7 = (@7 - 1)
endif
if 1 > @7 then
setvar @7 = json_parse("days",@3)
publish local $cmt "flow"
endif
endif
% start N timing (night)
on alarm 4
do
if json_parse("days",@3) = 0 then
setvar @5 = 4
publish local $cmt "ebb"
endif
% cycle counter 1 min
on timer 1
do
setvar @1 = (@1 - 1)
settimer 1 59999
if 1 > @1 then
publish local $cmt "toggle"
else
publish local $cmt "status"
endif
% sonoff local button
on gpio_interrupt 0 pullup
do
println "New state GPIO 0: " | $this_gpio
if $this_gpio = 0 then
publish local $cmt "toggle"
endif