ESP8266 not connecting to router when not connected to PC

  • Hi all
    I have problems with my ESP8266 connecting to the router when it is connected to the external power.

    I am using the ESP8266 as a GW with sensor (temp/hum or door switch) directly connected to D3. I have several of them working but the rule, and not the exception, is that I have the following problem at setup. I have had the problem with a variety of batches of 8266’es, various power sources and a few versions of sketches(albeit very similar).

    I set up the HW ie I attach the sensor to D3 an gnd, solder a short extension antenna to the board and push the code from the Arduino IDE. When I push the code, it usually loads without problem an the LED on the 8266 is blinking. When done it runs the code and connects to the router. I look it up in the router to document the MAC address and IP address and set a fixed IP address in the router. At this stage I can also set it up in Domoticz and get sensor readings. So far so good, as long as it is booted right after pushing the code, still connected to the PC. Now, when I disconnect from the PC and connect the 8266 to external power, usually a 2.4A USB charger, it blinks once. I look it up in the router and it has not connected and obviously Domoticz doesn’t see it either.

    I cannot claim it is a good hypothesis because I have no proof, but I get a feeling it does not boot properly. Maybe it doesn’t when I crudely connect it to the charger ? Maybe the answer can be found in the sketch or in the way I set things up ? Sometimes I get it to work due to magic or the phase of the moon and then I don’t touch it. Usually it then works OK once I got it going once.

    Attached I the sketch, only edited in respect to the SSID and PW.

    I would be grateful for tips. Hopefully someone can see something in the sketch that can be improved to make it more stable and boot better. Or increase the ability to troubleshoot (blinks or whatever??)

    Glad midsommar
    Sketch below:

     * Doorswitch without NRF24L01 radio, only direct connected doorswitch
     * disabled MY_IP_GATEWAY_ADDRESS 
     * Pasted code from sketch "BinarySwitchSensor"  to get locally connected switch
     * added#include <Bounce2.h>
     * changed to #define BUTTON_PIN "D3" instead of only "3"
     * enabled led blink
     * disablat inclusion mode (don't know if I had to but I was worried it would interfere with sensor on D3
     * changed version "maindoor" in order to find the right GW in Domoticz and in the router
     * The MySensors Arduino library handles the wireless radio link and protocol
     * between your home built sensors/actuators and HA controller of choice.
     * The sensors forms a self healing radio network with optional repeaters. Each
     * repeater and gateway builds a routing tables in EEPROM which keeps track of the
     * network topology allowing messages to be routed to nodes.
     * Created by Henrik Ekblad <>
     * Copyright (C) 2013-2015 Sensnology AB
     * Full contributor list:
     * Documentation:
     * Support Forum:
     * This program is free software; you can redistribute it and/or
     * modify it under the terms of the GNU General Public License
     * version 2 as published by the Free Software Foundation.
     * Version 1.0 - Henrik EKblad
     * Contribution by a-lurker and Anticimex,
     * Contribution by Norbert Truchsess <>
     * Contribution by Ivo Pullens (ESP8266 support)
     * The EthernetGateway sends data received from sensors to the WiFi link.
     * The gateway also accepts input on ethernet interface, which is then sent out to the radio network.
     * Enter "ip-number:port" in the ip-field of the Arduino GW device. This will temporarily override any serial configuration for the Vera plugin.
     * E.g. If you want to use the defualt values in this sketch enter:
     * LED purposes:
     * - To use the feature, uncomment any of the MY_DEFAULT_xx_LED_PINs in your sketch, only the LEDs that is defined is used.
     * - RX (green) - blink fast on radio message recieved. In inclusion mode will blink fast only on presentation recieved
     * - TX (yellow) - blink fast on radio message transmitted. In inclusion mode will blink slowly
     * - ERR (red) - fast blink on error during transmission error or recieve crc error
     * See for wiring instructions.
     * nRF24L01+  ESP8266
     * VCC        VCC
     * CE         GPIO4
     * CSN/CS     GPIO15
     * SCK        GPIO14
     * MISO       GPIO12
     * MOSI       GPIO13
     * GND        GND
     * Not all ESP8266 modules have all pins available on their external interface.
     * This code has been tested on an ESP-12 module.
     * The ESP8266 requires a certain pin configuration to download code, and another one to run code:
     * - Connect REST (reset) via 10K pullup resistor to VCC, and via switch to GND ('reset switch')
     * - Connect GPIO15 via 10K pulldown resistor to GND
     * - Connect CH_PD via 10K resistor to VCC
     * - Connect GPIO2 via 10K resistor to VCC
     * - Connect GPIO0 via 10K resistor to VCC, and via switch to GND ('bootload switch')
      * Inclusion mode button:
     * - Connect GPIO5 via switch to GND ('inclusion switch')
     * Hardware SHA204 signing is currently not supported!
     * Make sure to fill in your ssid and WiFi password below for ssid & pass.
    // Enable debug prints to serial monitor
    #define MY_DEBUG
    // Use a bit lower baudrate for serial prints on ESP8266 than default in MyConfig.h
    #define MY_BAUD_RATE 9600
    // Enables and select radio type (if attached)
    //#define MY_RADIO_NRF24
    //#define MY_RADIO_RFM69
    //#define MY_RF24_CHANNEL 14
    #define MY_GATEWAY_ESP8266
    // Hsand
    #define MY_ESP8266_SSID "DOVADO-XXXX"
    #define MY_ESP8266_PASSWORD "YYYY"
    // Enable UDP communication
    //#define MY_USE_UDP
    // Set the hostname for the WiFi Client. This is the hostname
    // it will pass to the DHCP server if not static.
    #define MY_ESP8266_HOSTNAME "MAindoor_GW_Hsand"
    // Enable MY_IP_ADDRESS here if you want a static ip address (no DHCP)
    //#define MY_IP_ADDRESS 192,168,178,87
    // If using static ip you need to define Gateway and Subnet address as well
    // #define MY_IP_GATEWAY_ADDRESS 192,168,178,1
    // #define MY_IP_SUBNET_ADDRESS 255,255,255,0
    // The port to keep open on node server mode
    #define MY_PORT 5003
    // How many clients should be able to connect to this gateway (default 1)
    // Controller ip address. Enables client mode (default is "server" mode).
    // Also enable this if MY_USE_UDP is used and you want sensor data sent somewhere.
    //#define MY_CONTROLLER_IP_ADDRESS 192, 168, 178, 68
    // Enable inclusion mode
    // Enable Inclusion mode button on gateway
    // Set inclusion mode duration (in seconds)
    // Digital pin used for inclusion mode button
    // Set blinking period
    // Flash leds on rx/tx/err
    // Led pins used if blinking feature is enabled above
    #define MY_DEFAULT_ERR_LED_PIN 16  // Error led pin
    #define MY_DEFAULT_RX_LED_PIN  16  // Receive led pin
    #define MY_DEFAULT_TX_LED_PIN  16  // the PCB, on board LED
    #if defined(MY_USE_UDP)
    #include <WiFiUdp.h>
    #include <ESP8266WiFi.h>
    #include <MySensors.h>
    #include <SPI.h>
    // added from BinarySwitchSensor
    #include <Bounce2.h>
    #define CHILD_ID 3
    #define BUTTON_PIN  D3  // Arduino Digital I/O pin for button/reed switch changed from 3 to D3
    Bounce debouncer = Bounce(); 
    int oldValue=-1;
    // Change to V_LIGHT if you use S_LIGHT in presentation below
    MyMessage msg(CHILD_ID,V_TRIPPED);
    void setup()
      // Setup the button
      // Activate internal pull-up
      // After setting up the button, setup debouncer
    void presentation()
    	// Present locally attached sensors here
      sendSketchInfo("Binary switch Sensor", "1.0 maindoor_Hsand");
       // Register binary input sensor to gw (they will be created as child devices)
      // You can use S_DOOR, S_MOTION or S_LIGHT here depending on your usage. 
      // If S_LIGHT is used, remember to update variable type you send in. See "msg" above.
      present(CHILD_ID, S_DOOR);  
    //  Check if digital input has changed and send in new value
    void loop()
    	// Send locally attached sensors data here
      // Get the update value
      int value =;
      if (value != oldValue) {
        // Send in the new value
         send(msg.set(value==HIGH ? 1 : 0));
         oldValue = value;

  • Mod

    @beersmith D3 (GPIO0) controls how the esp8266 boots. See

    The ESP8266 will enter the serial bootloader when GPIO0 is held low on reset. Otherwise it will run the program in flash.

    Try connecting the button to a different pin. D1, D2, D5, D6 and D7 are usually safe to use (depending on which esp8266 board you are using).

  • Man ska inte ropa Hej...
    But it seems your tip was really working out.
    I tried D1 and D2 but had some other issues so I went back to D3 but made sure the doorswitch was not connected on boot, in order not to interfere. Then it boots, normally, connects to the router and subsequently to Domoticz.
    Thanx a lot ! // Beersmith

Log in to reply

Suggested Topics

  • 3
  • 1
  • 2
  • 15
  • 2
  • 24