January 31, 2017
Updated September 26, 2017

A few days ago, I flashed some Sonoff switches with a Raspberry Pi. If I decide to use Theo Arends Sonoff-MQTT-OTA-Arduino firmware, that would be all there would be to it. But I have been investigating using NodeMCU and I have found it cumbersome to use the Raspberry Pi to upload scripts to the Sonoff. I do wish that the USB-TTL cable would arrive. In the meantime I have decided to emulate a Sonoff with a D1 mini which is connected directly to the desktop with a USB cable.

Basically, there are three things to be setup: the tactile switch, the relay and the build in LED. All that is required are a normally open push button switch, a red and green LED and a couple of resistors. Their values are not too critical. Given a voltage of 3.3V, 82 ohms resistors limit the current to 20 mA which can be handled by the LEDS. However, the ESP8266 cannot put out more than 12 mA of current, so the resistors could be dropped entirely. I used 1K ohm resistors.

The switch connects pin D3 of the D1 Mini to ground. The internal pull up resistor of the the GPIO is enabled to read a 1 from the pin when the switch is not pressed.

The red LED represents the state of the relay. Its cathode is connected to ground through a current limiting resistor, the anode is connected to pin D6 of the Wemos D1 mini. To turn the red LED on, which represents closing the relay and turning an appliance connected to the Sonoff on, a 1 must be written to D6. A 0 written to D6 turns the red LED off.

The green LED represents the built-in Sonoff LED. Its anode is connected to 3.3V, its cathode is connected to pin D7 through a current limiting resistor. Because of this configuration, a 1 written to pin D7 turns the LED off, a 0 turns the LED on. This "unnatural" connexion is what is found on the Sonoff.