-
Notifications
You must be signed in to change notification settings - Fork 0
/
test1.c
130 lines (88 loc) · 2.49 KB
/
test1.c
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
% Config params, overwrite any previous settings from the commandline
config speed 80
config ap_on 0
config ap_open 1
config auto_connect 1
config dns dhcp
config netmask 255.255.255.0
config gw 192.168.12.1
config ntp_server 1.pool.ntp.org
config ssid [email protected]
config password 83622365
config mqtt_host 192.168.10.125
config mqtt_user Sebastien
config mqtt_password 83622365
config mqtt_port 1883
config mqtt_ssl 1
%ESP8266 broker function, not used now.
%config broker_port 1883
%config broker_access 3
%config broker_user Sebastien
%config broker_password 83622365
% Now the initialization, this is done once after booting
on init
do
% Device number
setvar $device_number = 1
setvar $timer_toggle_flag = 0
% @<num> vars are stored in flash and are persistent even after reboot
setvar $run = @1 + 1
setvar @1 = $run
println "This is boot no "|$run
% Status of the relay
setvar $LEDR_status=0
gpio_out 4 $LEDR_status
% Command topic
setvar $command_topic="/home/LEDR/" | $device_number | "/command"
% Status topic
setvar $status_topic="/home/LEDR/" | $device_number | "/status"
publish local $status_topic $LEDR_status retained
% local subscriptions once in 'init'
subscribe local $command_topic
% 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 $LEDR_status retained
% Now the events, checked whenever something happens
% 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 = "on" then
setvar $LEDR_status = 1
gpio_out 14 $LEDR_status
else
if $this_data = "off" then
setvar $LEDR_status = 0
gpio_out 14 $LEDR_status
endif
endif
publish local $status_topic $LEDR_status retained
publish remote $status_topic $LEDR_status retained
% The local pushbutton
on gpio_interrupt 4 pullup
do
println "New state GPIO 4: " | $this_gpio
gpio_out 2 $this_gpio
endif
% Blinking
on timer 1
do
%timer reloading
settimer 1 500
if $blink = 1 then
if $timer_toggle_flag = 0 then
$timer_toggle_flag = 1
else
$timer_toggle_flag = 0
endif
gpio_out 12 $timer_toggle_flag
endif