esp_mqtt/scripts/script.sonoff

109 wiersze
2.5 KiB
Plaintext

2017-08-15 20:42:41 +00:00
% Config params, overwrite any previous settings from the commandline
2017-08-22 19:46:45 +00:00
config ap_ssid MQTTbroker
2017-08-17 08:23:13 +00:00
config ap_password stupidPassword
2017-08-15 20:42:41 +00:00
config ntp_server 1.de.pool.ntp.org
2017-08-16 09:04:31 +00:00
config broker_user Martin
config broker_password secret
2017-08-15 20:42:41 +00:00
config mqtt_host martinshome.fritz.box
config speed 160
% Now the initialization, this is done once after booting
on init
do
2017-08-24 19:48:58 +00:00
% Device number ("* 1" to make even "" a number)
setvar $device_number = @1 * 1
% @<num> vars are stored in flash and are persistent even after reboot
setvar $run = @2 + 1
setvar @2 = $run
println "This is reboot no "|$run
2017-08-15 20:42:41 +00:00
2017-08-17 08:23:13 +00:00
% Status of the relay
setvar $relay_status=0
gpio_out 12 $relay_status
gpio_out 13 not ($relay_status)
2017-08-15 20:42:41 +00:00
2017-08-17 08:23:13 +00:00
% Blink flag
setvar $blink=0
2017-08-17 06:46:19 +00:00
2017-08-17 08:23:13 +00:00
% Command topic
setvar $command_topic="/martinshome/switch/" | $device_number | "/command"
2017-08-17 06:46:19 +00:00
2017-08-17 08:23:13 +00:00
% Status topic
setvar $status_topic="/martinshome/switch/" | $device_number | "/status"
publish local $status_topic $relay_status retained
2017-08-17 06:46:19 +00:00
2017-08-15 20:42:41 +00:00
% local subscriptions once in 'init'
2017-08-17 08:23:13 +00:00
subscribe local $command_topic
2017-08-15 20:42:41 +00:00
% Now the MQTT client init, this is done each time the client connects
on mqttconnect
do
% remote subscriptions for each connection in 'mqttconnect'
2017-08-17 08:23:13 +00:00
subscribe remote $command_topic
2017-08-15 20:42:41 +00:00
2017-09-17 08:34:08 +00:00
publish remote $status_topic $relay_status retained
2017-08-15 20:42:41 +00:00
% Now the events, checked whenever something happens
% Is there a remote command?
2017-08-17 08:23:13 +00:00
on topic remote $command_topic
2017-08-15 20:42:41 +00:00
do
println "Received remote command: " | $this_data
% republish this locally - this does the action
2017-08-17 08:23:13 +00:00
publish local $command_topic $this_data
2017-08-15 20:42:41 +00:00
% Is there a local command?
2017-08-17 08:23:13 +00:00
on topic local $command_topic
2017-08-15 20:42:41 +00:00
do
println "Received local command: " | $this_data
if $this_data = "on" then
2017-08-17 08:23:13 +00:00
setvar $relay_status = 1
setvar $blink = 0
gpio_out 12 $relay_status
gpio_out 13 not ($relay_status)
2017-08-21 10:00:54 +00:00
else
if $this_data = "off" then
2017-08-17 08:23:13 +00:00
setvar $relay_status = 0
setvar $blink = 0
gpio_out 12 $relay_status
gpio_out 13 not ($relay_status)
2017-08-21 10:00:54 +00:00
endif
2017-08-15 20:42:41 +00:00
endif
if $this_data = "toggle" then
2017-08-17 08:23:13 +00:00
setvar $relay_status = not ($relay_status)
gpio_out 12 $relay_status
gpio_out 13 not ($relay_status)
2017-08-15 20:42:41 +00:00
endif
if $this_data = "blink" then
2017-08-17 08:23:13 +00:00
setvar $blink = 1
2017-08-15 20:42:41 +00:00
settimer 1 500
endif
2017-08-17 08:23:13 +00:00
publish local $status_topic $relay_status retained
publish remote $status_topic $relay_status retained
2017-08-15 20:42:41 +00:00
2017-08-16 09:04:31 +00:00
% The local pushbutton
2017-08-15 20:42:41 +00:00
on gpio_interrupt 0 pullup
do
println "New state GPIO 0: " | $this_gpio
if $this_gpio = 0 then
2017-08-17 08:23:13 +00:00
setvar $blink = 0
publish local $command_topic "toggle"
2017-08-15 20:42:41 +00:00
endif
% Blinking
on timer 1
do
2017-08-17 08:23:13 +00:00
if $blink = 1 then
publish local $command_topic "toggle"
2017-08-15 20:42:41 +00:00
settimer 1 500
endif