msillano
Published © LGPL

Adding a Remote Switch to Sonoff: One Pin for Switch+LED

I want to use Sonoff-basic with a special switch, because it is equipped with an LED that I want to use as a remote indicator of the relay.

IntermediateFull instructions provided5 hours2,468
Adding a Remote Switch to Sonoff: One Pin for Switch+LED

Things used in this project

Hardware components

Sonoff Basic
Itead Sonoff Basic
×1
Arduino Mini USB serial adapter
Arduino Mini USB serial adapter
Any 3.3V FTDI USB-to-Serial Converter/Programmer
×1

Software apps and online services

Arduino IDE
Arduino IDE
Using compilate firmware enough esptool.py (https://github.com/espressif/esptool)
esp_mqtt

Hand tools and fabrication machines

Soldering iron (generic)
Soldering iron (generic)

Story

Read more

Schematics

extra switch

One line for switch+led

Code

Extra switch

Plain text
Script for Sonoff esp_mqtt
% implements MQTT ON/OFF, local TOGGLE (interrupt) + remote ON/OFF (polling)
% derived by script.sonoff (https://github.com/martin-ger/esp_mqtt/blob/master/scripts/script.sonoff)
% Config params, overwrite any previous settings from the commandline

config ap_ssid      MQTT_home
config ap_password	sonoffbroker
config ntp_server	  1.it.pool.ntp.org
config ntp_timezone 1
config broker_user      none
config broker_password	none
config mqtt_host	      192.168.???.???
config speed	          160

% Now the initialization, this is done once after booting
on init
do
	% 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

	% Status of the relay
	setvar $relay_status = 0
	gpio_out 12 $relay_status
  gpio_out 13 not($relay_status)

  % Switch status
 	gpio_pinmode 14 input
  setvar  $switch_status = not(gpio_in(14))
  
	% Command topic
	setvar $command_topic = "/martinshome/switch/" | $device_number | "/command"

	% Status topic
	setvar $status_topic = "/martinshome/switch/" | $device_number | "/status"

	% local subscriptions once in 'init'
	subscribe local $command_topic
  settimer 1 100

% 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 "{Start:\"" | $timestamp | "\",Run:" | $run | "}" 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
  if $this_data = "on" then
		setvar $relay_status = 1
    endif
  if $this_data = "off" then
		setvar $relay_status = 0
    endif 
    
% 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
    println " -- do on "
	  gpio_out 12 1
 		gpio_out 13 0
    endif
    
	if $this_data = "off" then
    if $switch_status = 0 then
      println " -- do off"
	    gpio_out 12 $relay_status
		  gpio_out 13 not($relay_status)
      endif
	  endif
		
  if $this_data = "toggle" then
    if $switch_status = 0 then
      println " -- do toggle"
	  	setvar $relay_status = not($relay_status)
	  	gpio_out 12 $relay_status
 	   	gpio_out 13 not($relay_status)
      endif
	  endif
    
  publish remote $status_topic "{Time:\"" | $timestamp | "\",Power:" | ($relay_status + $switch_status) > 0 | "}" retained

% The local pushbutton, interrupt
on gpio_interrupt 0 pullup
do
	if $this_gpio = 0 then 
% toggle action only if switch off
     if $switch_status = 0 then
        publish remote $status_topic "{Time:" | $timestamp | ",TOGGLE}"  retained
 		    publish local $command_topic "toggle"
      endif
	  endif

% The remote switch, polling
on timer 1
do
	gpio_pinmode 14 input
% switch is changed?
  if $switch_status = not(gpio_in(14)) then
    setvar $switch_status = gpio_in(14)
    if $switch_status = 1 then
% publish user action and local command    
        publish remote $status_topic  "{Time:" | $timestamp | ",ON}" retained
        publish local $command_topic "on"  
    else
        publish remote $status_topic  "{Time:" | $timestamp | ",OFF}" retained
        publish local $command_topic "off"  
    endif
  endif
% restore the switch led on  
  if $switch_status = 0 then
     if $relay_status = 1 then
        gpio_out 14 1
     endif
  endif
	settimer 1 100
	

Credits

msillano

msillano

12 projects • 12 followers
Thanks to martin_ger.

Comments