0

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 = "192.168.43.215";
  const int httpsPort = 80;

  const String macValue = WiFi.macAddress();


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

    http.begin("http://192.168.43.215/one.txt");

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

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

  http.end();
  delay(1000);
}



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

  publishData();

}

void setup()
{
  Serial.begin(115200);
  Serial.println("setup2");

  manager.autoConnect("espdevice");
  restPublish();


 Serial.println("entering deepsleep now. DND");
 ESP.deepSleep(0);

}

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 May 8 at 10:31
  • I would try some small delays between connecting and publishing and between publishing and deep sleep – Juraj May 8 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 May 8 at 12:09
  • Thanks juraj, it works i think it needed some delay. i will do some more testing on it. – Rohit May 8 at 12:34
  • you could search for more exact solution. there are some recommendations on what to do before deepSleep. and you can test if the wifi is connected. delay() calls yield() which performs 'background' tasks... – Juraj May 8 at 12:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.