Facing a weird issue while reading data from esp8266.

I am trying to read dynamic data from a demo text file on test server on same network in deep sleep mode.

Case1: Joining new network

I am using WifiManager to join network and then followed by http.get() However whenever i try to join the wifi with correct credentials it does not connect sometimes. Post some time ,if i try to wake the device it automatically connects and displays the data.

Case2: Rejoining existing network

Similar issue happens when i stop the test server and update the text file contents and up the server and try to read it on esp8266. It ask me the credentials again which it should have remembered 1st time. Post some time, if i try to wake up device sometimes it automatically connnects and prints updated value.

I am unable to figure out the problem here and its solution. Any suggestion please.

#include <WiFiManager.h>
#include <ESP8266HTTPClient.h>
#include <ESP8266WiFi.h> 
#include <ESP8266WiFiMulti.h>
#include <Arduino.h>
#include <ESP8266httpUpdate.h>

ESP8266WiFiMulti WiFiMulti;
HTTPClient http;
WiFiManager manager;

  int publishPin = 0;
  int wifiPin = 5;
  unsigned int baud = 115200; 
  const char* url3 = "";
  const int httpsPort = 80;

  const String macValue = WiFi.macAddress();

 void publishData()
    String link = "/one.txt";
    Serial.print("connecting to ");


    int httpCode = http.GET();
    if (httpCode > 0) {
    if (httpCode == HTTP_CODE_OK) {
      Serial.println("This device is authorized!");

    }else {
    Serial.println("Failed to connect to API.");


void restPublish(){
  Serial.println("Rest publish");



void setup()


 Serial.println("entering deepsleep now. DND");


void loop()
  • 1. I didn't see any point where you are trying to connect to WIFI. 2. As far as I understand you had create a local network over your laptop using some APACHE or something else. As your question that sometimes you are not connecting to the server, for connection your client and server must be on the same network to connect. Please make sure that your local sever and esp device must be connected to the same network
    – Vaibhav
    Commented May 8, 2019 at 10:31
  • I would try some small delays between connecting and publishing and between publishing and deep sleep
    – Juraj
    Commented May 8, 2019 at 11:43
  • Thanks for the reply Vaibhav, Above it did mention i have the client and server running on the same network and i am using nginx as the server. Case1, is the code newly installed on ESP so it will get me webpage for 1st time configuration. Case 2 i made clear about network, it doesnot join the wifi
    – Rohit
    Commented May 8, 2019 at 12:09
  • Thanks juraj, it works i think it needed some delay. i will do some more testing on it.
    – Rohit
    Commented May 8, 2019 at 12:34
  • 1
    Could you share some links for recommendation before deep sleep. Yes i will work on check wifi connection. thanks
    – Rohit
    Commented May 8, 2019 at 15:16


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.