[SOLVED] NodeMCU giving DHCP error but gets IP address

I have a nodeMCU that I programmed from Arduino IDE. The code I used is shown below. I have changed the credentials for obvious reasons. I uploaded to the hardware. I have connected D2 to an LED via a 100 ohm resistor. I have previously tested via Lua script that I can toggle the LED from a console. I switched to Arduino IDE because of my need to interface with the Blynk app. In the app, I have a button mapped to Digital GPIO Pin2.

`
#define BLYNK_PRINT Serial // Enables Serial Monitor
#include <SPI.h>
#include <Ethernet.h>
#include <BlynkSimpleEthernet.h> // This part is for Ethernet stuff
#include <ESP8266WiFi.h>

#define WLAN_SSID       "andronicus"
#define WLAN_PASS       "mypwd_xxxx"

char auth[] = "mytoken_xxxx"; // Put your Auth Token here. (see Step 3 above)

void setup()
{
  Serial.begin(9600); // See the connection status in Serial Monitor

  Serial.print("\nConnecting to ");
  Serial.println(WLAN_SSID);

  WiFi.begin(WLAN_SSID, WLAN_PASS);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.print("\nLocal IP: ");
  Serial.println(WiFi.localIP());

  Blynk.begin(auth);  // Here your Arduino connects to the Blynk Cloud.
  
}

void loop()
{
  Blynk.run(); // All the Blynk Magic happens here...

  // You can inject your own code or combine it with other sketches.
  // Check other examples on how to communicate with Blynk. Remember
  // to avoid delay() function!
}

`

The output I get on serial console is the following:
Connecting to andronicus ..... Local IP: 192.168.1.103 [3210] Blynk v0.3.3-beta [3210] Getting IP... [3512] DHCP Failed! ��� 8Br�¤�� Connecting to andronicus ..... Local IP: 192.168.1.103 [3212] Blynk v0.3.3-beta [3212] Getting IP... [3515] DHCP Failed!

But DHCP has obviously worked because the nodeMCU gets 192.168.1.103 as IP address. I can even ping the module from another device on the LAN. But I find that the Wi-Fi gets dropped periodically. Then it scans again and reconnects, drops again. This happens in a regular loop.

I think, it’s because of this, the Blynk app says “Your ESP8266 is not in network.” So I cannot control the LED from the app. Please help. I am not using any Arduino hardware. See screenshot below.

I tried using a static IP. The behaviour is the same. Wi-Fi connects and disconnects. I can ping the hardware by the static IP when Wi-Fi is connected. The connection lasts for about 10 seconds.

Are you sure it’s not a problem in your Wifi network? All your other devices function correctly?

Yes, Wi-Fi network is okay. In fact, I loaded the original FW from https://github.com/nodemcu/nodemcu-firmware/releases. Then I wrote a Lua script. NodeMCU connects and gets an address via DHCP. Wi-Fi is thereafter always on. No problem.

It’s only when I replace the FW with the image from Arduino IDE using Blynk library that I have this problem.

The fact that it periodically prints "Connecting to " suggests something like a reset procedure since such a print is happening inside setup(). Maybe this is a clue to help me get to the root cause. Why would the board keep resetting?

You can use Blynk.config(auth) instead of Blynk.begin(auth)

Inform me if it works

I had tried Blynk.config(auth) earlier but no luck. I just deleted all the Wi-Fi code inside setup() because SSID and PWD are supposed to be passed into Blynk.begin(). I started reading the API and figured out that I might be using the wrong header files. I copied this file from an Blynk Ethernet example. I think I need to start with a Wi-Fi example.

I started looking into these instructions:

But I am getting this now:
` /root/Arduino/libraries/blynk-library-master/BlynkSimpleShieldEsp8266.h:14:2: error: #error Sorry, we changed things a little bit. Please check new examples for ESP8266. This is easy to fix :slight_smile:
#error Sorry, we changed things a little bit. Please check new examples for ESP8266. This is easy to fix :slight_smile:

`

UPDATE
Got it working! The main problem was that I started from the Ethernet example and went in the wrong direction. Final code used as is from Arduino IDE:
`
#define BLYNK_PRINT Serial // Comment this out to disable prints and save space
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "xxxx";

void setup()
{
  Serial.begin(9600);
  Blynk.begin(auth, "andronicus", "xxxx");
}

void loop()
{
  Blynk.run();
}

`

Final piece of the puzzle to solve: toggle LED from the app. LED connected to D2. In app, I used digital gp4. Works!

Why do you use Ethernet example with NodeMCU?
You should probably use ESP8266 standalone example!