Maintain power on circuit despite switch opening


I am building a door sensor using a 3-pin switch, an ESP2866-01 (standalone) for communication to a server and a battery as power supply. (I’m using theArduino IDE for ESP 8266 to program the ESP2866 directly without the need to control it using a regular Arduino.)

Requirements of the setup are:

  • door open and close events should be send to the server. (Door open is powering on of ESP2866, door close can be detecting by change of switch state.
  • power needs to be turned off when (or shortly after) the door is closed and the events are send to server, so the battery will last long.

I have build the attached circuit and use the following code

#include <ESP8266WiFi.h>

const char* ssid     = "#####";
const char* password = "#####";
unsigned long wakeTime;

void software_Reboot(){
  //following not working yet (error code: "unknown opcode or format name 'jmp'"
  //asm volatile ("  jmp 0");         

void setup() {
  //1) set pin 0 to high, to keep power even if lever switch opens again
  pinMode (0,OUTPUT);
  //2) to watch current status of manual switch
  //3) set serial

  WiFi.begin(ssid, password);
  //for speed we set esp8266 ip, gateway and subnet manually
  while (WiFi.status() != WL_CONNECTED) {


WiFiClient client;
const String host = "##,##,##,##";
const byte hostip[]={##,##,##,##};
const String clientId="######";
int loggedOpen=false;
int loggedClose=false;
int failed=0;
void loop() {
  //if loggedClose then waiting for shutdown to finish
  if (loggedClose) return;
  //check if we logged open door and door is not yet closed
  int cStatus=digitalRead(2);
  if (loggedOpen){
    if (cStatus==LOW) { //low means door is open

  // Use WiFiClient class to create TCP connections
  const int httpPort = 80;
    if (!client.connect(hostip, httpPort)) {
      Serial.println("Connection failed");
      delay (500);
      if (failed>10){
        Serial.println("Too many connection attempts, resetting...");
      Serial.println("Connection succeeded");
  // We now create a URI for the request
  //String url = "/index.html?Wifi1";  
  String url = "/index.php?";
  if (!loggedOpen){
    Serial.println("Handle Log OPEN");
  }else{ //status of door should be closed
    Serial.println("Handle Log CLOSE");
  Serial.print("Requesting URL: ");
  client.println("GET "+url+" HTTP/1.1");
  client.println("Host: "+host);
  client.println("User-Agent: ESP8266");
  client.println("Connection: close");
  client.println("");//needed as signal that block has ended
  delay(10);//give some time to process
  //we must read data to clear buffer
  unsigned long time=0;
  Serial.println("Start Response");
  while (client.available()) {
    char c =;
  Serial.println("End Response");  
  //if door is closed we can shut down by setting pin 0 to LOW
  if (loggedClose){

As you can see I set pin 0 to high to keep power on (circumventing the switch in case door is already closed) however this does not seem to work. Am I doing something wrong?

(To be complete, I also have two other problems: the error code generated in the reset method ‘unknown opcode jmp’ but this could be specific for the ESP2866 IDE environment; Also pin 2 is not registering the closing of the door / receiving 5V )

Hope anyone can help!

thx, Nard

As I understand part of the problem is that the transistor passes a voltage equivalent to the base voltage (pin 0, initially 3.3V) minus a little bit. Because of this the voltage on de ESP2866 decreases to 3.3V minus a little bit and the voltage on pin 0 decreases and so the voltage trickles down to zero.

Of course I could put the switch/transistor part on the high voltage (9V) side. However this presents of how to measure whether the switch opens.

Am I using the wrong transistor?

Change the "quote" tags in your post, to "code".

I think the problem is due to you using an npn transistor to switch the supply voltage to the ESP8266

The base of an npn transistor needs to be around 0.6 - 0.7V more positive than the emitter for the transistor to conduct, but the base is being fed from an output of the ESP8266, which cant go higher than it’s supply voltage.

You need to use a pnp transistor or a P channel FET if you want to switch the positive supply.

You may need to use an npn transistor to switch a pnp transistor (or P channel FET).

That regulator already uses 5-10mA idle current.
Flat battery in 1-2 days, without having done anything.
Design a circuit that switches the BATTERY, e.g. with a P-fet.