-
Notifications
You must be signed in to change notification settings - Fork 7
/
script.obi
107 lines (83 loc) · 2.26 KB
/
script.obi
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
% Config params, overwrite any previous settings from the commandline
config ap_ssid MQTTbroker
config ap_password stupidPassword
config broker_user Martin
config broker_password secret
config mqtt_host test.mosquitto.org
config speed 160
% Now the initialization, this is done once after booting
on init
do
% Device number given in flash var @1 ("* 1" to make even "" a number)
setvar $device_number = @1 * 1
% MQTT prefix given in flash var @2
setvar $mqtt_prefix = @2
% Status of the relay
setvar $relay_status=0
% Command topic
setvar $command_topic =$mqtt_prefix | "/obiswitch/" | $device_number | "/command"
% Status topic
setvar $status_topic = $mqtt_prefix | "/obiswitch/" | $device_number | "/status"
publish local $status_topic $relay_status retained
% local subscriptions once in 'init'
subscribe local $command_topic
% led
setvar $wifiled = 0
gpio_out 4 $wifiled
% If we get the wificonnect let the LED blink
on wificonnect
do
settimer 2 1000
% If we get the wifidisconnect stop blinking
on wifidisconnect
do
settimer 2 0
setvar $wifiled = 0
gpio_out 4 $wifiled
% Now the MQTT client init, this is done each time the client connects
on mqttconnect
do
% remote subscriptions for each connection in 'mqttconnect'
subscribe remote $command_topic
publish remote $status_topic $relay_status retained
% Is there a remote command?
on topic remote $command_topic
do
println "Received remote command: " | $this_data
% republish this locally - this does the action
publish local $command_topic $this_data
% Is there a local command?
on topic local $command_topic
do
println "Received local command: " | $this_data
if $this_data = "1" then
setvar $relay_status = 1
gpio_out 12 0
settimer 1 150
else
if $this_data = "0" then
setvar $relay_status = 0
gpio_out 5 0
settimer 1 150
endif
endif
publish local $status_topic $relay_status retained
publish remote $status_topic $relay_status retained
% The local pushbutton
on gpio_interrupt 14 pullup
do
%println "New state GPIO 14: " | $this_gpio
if $this_gpio = 0 then
publish local $command_topic not($relay_status)
endif
% End pulse
on timer 1
do
gpio_out 5 1
gpio_out 12 1
% WifiLED blink
on timer 2
do
gpio_out 4 $wifiled
setvar $wifiled = not($wifiled)
settimer 2 1000