Hi all. I've come across a weird and frustrating issue. I have a simple reed switch attached to a NodeMCU and the code uploads and executes with no problems UNLESS the reed switch is activated (ie the presence of a magnet means the switch is closed at the time of the upload). If I open the switch by moving the magnet away, no problem. If I try to upload with the switch closed the upload fails every time. Once the code loads, the reed switch and the code work perfectly until the device is rebooted. Again, if the magnet has closed the reed switch when the device boots, it fails to load any of its code.
I thought this was a quirk of the reed sensor I was using until I built another project that uses a 3.3v relay. If the relay is powered from the NodeMCU's 3.3v pin when the device boots or an upload is attempted, it fails in exactly the same way that the reed switch module does.
To get the code successfully uploaded, I have to disconnect the relay power then reconnect it again after the upload has completed and the device has rebooted. Then it all works fine ... until the device reboots if it loses power for any reason. Again, it will not start at all if the relay is being powered from its 3.3v pin at the time of rebooting. It acts exactly the same as the reed switch does if it is in a closed state.
My initial thoughts were that the computer couldn't provide enough power, but if the device is connected to an external 2 amp USB charger it behaves the same on startup.
I can think of two things to try. One is to have a separate power supply altogether for the reed switch and the relay, except this is really undesirable. The second idea was to look into whether a capacitor could be used that delays the relay from starting until after the device has started. The problem with that is that it probably won't help with uploads.
OTA uploads fail in exactly the same way as USB uploads, and of course the device fails to reboot properly anyway after an OTA update.
Both modules have a buzzer, which makes a very high-pitched but low volume noise during both upload failure and reboot failure. The buzzer makes the same noise during a successful upload and immediately stops when the upload completes. I don't know if that is significant or not.
The Wemos D1 mini behaves exactly the same as the NodeMCU. I've tried multiple MCUs and D1s and they all act the same.
I'm using a Mac with Arduino IDE 1.8.5. I haven't tried the sketches out on an Uno or Mega yet to see if it is just ESP8266 related. Before I go through a lengthy testing process I thought I'd reach out to the community to see if anyone has had the same problem and solved it, or if there is an obvious cause that I've missed. Thank you in advance if you can help!