Great idea, it seems like an IC version of essentially what I've implemented with the LED. I'll order one and give it a shot. I'll let you know how it turns out.
As the problem seems to be related to the port pin being low on startup, maybe connect the optoisolator with the LED Anode to +V and the Cathode to the resistor from the port pin, then the motor will turn on when the port is driven low.
So I am now beyond confused. I got in some PNP transistors and figured I'd give them a shot. I replaced the motor with an LED for now just to see if I could get it turning on and off. My schematic is this:
For some reason, the LED is always on, no matter whether the GPIO2 goes high or low, it just always stays on. Also, this circuit keeps burning out PNP transistors; I've already gotten the magic smoke twice. Anyone have ideas on what I'm doing wrong?
EDIT: I figured out the problem. I have 12v at the emitter and only 3.3v at the base, so the transistor is always turned on (base always has a lower voltage than emitter). Any ideas how to fix this? I can connect the emitter to 3.3v instead of 12v, but then there isn't enough voltage/current to drive the motor (though it works with the diode).
The LED is always on as the Emitter of the transistor is connected to +9V and the maxV for the base is 3.3V so the transistor is biased on. You need to connect the E to 3.3V for this test. The way you have it may be applying excess volts to the ESP8266 and damaging it.
Try an optoisolator as per the subjection earlier.
Finally got it all working. I'm still waiting for my optoisolator to arrive, but in the mean time I thought I'd try a PNP / NPN combo. I implemented the below circuit, and it works flawlessly.
I intend to try the optoisolator approach when I receive my optoisolator, but for those of you out there that don't have one, this should do the trick for you.