% 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