ESP01 Client timeout Problem

So I’m working with ESP01. I wired it on bread board using jumper wires as follows:

ESP VCC and CHPD → 3.3V
ESP GND → GND
ESP Rx → UNO TX with Atmega removed
ESP Tx → UNO RX with Atmega removed
ESP GPIO0 → Unconnected
ESP GPIO2 → Pulled HIGH (3.3V)

This is the code that runs on my ESP01

#include <ESP8266WiFi.h>

// WiFi credentials
char ssid[] = "";      //  your network SSID (name)
char pass[] = "";   // your network password
int status = WL_IDLE_STATUS;

// Initialize the Wifi client library
WiFiClient client;

char * thingName = "000001";

// Server
char host[] = "ec2-13-126-239-35.ap-south-1.compute.amazonaws.com";

const byte numChars = 100;
char receivedChars[numChars];
char tempChars[numChars];        // temporary array for use when parsing

// variables to hold the parsed data
char messageFromPC[numChars] = {0};
int integerFromPC = 0;
float floatFromPC = 0.0;

boolean newData = false;

void setup()
{
  Serial.begin(115200);
  delay(100);

  // We start by connecting to a WiFi network

  Serial.println();
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);
  
  WiFi.begin(ssid, pass);
  
  while (WiFi.status() != WL_CONNECTED) 
  {
    delay(500);
    Serial.print(".");
  }

  Serial.println("");
  Serial.println("WiFi connected");  
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
}

void loop() 
{
     recvWithStartEndMarkers();
    if (newData == true) {
        strcpy(tempChars, receivedChars);
            // this temporary copy is necessary to protect the original data
            //   because strtok() used in parseData() replaces the commas with \0
        parseData();
        showParsedData();
        newData = false;
    }
    Serial.print("connecting to ");
    Serial.println(host);
  
    // Use WiFiClient class to create TCP connections
    WiFiClient client;
    const int httpPort = 8080;
    if (!client.connect(host, httpPort))
    {
      Serial.println("connection failed");
      return;
    }
  
    Serial.println(F("Connected"));
    
     
    String request = "GET /put/";
    request += String(thingName);
    request += ",";
    request += String(messageFromPC);
    request += ",";
    request += String(integerFromPC);
    request += ",";
    request += String(floatFromPC);
    request += " HTTP/1.1";
    
    // Send the HTTP request:
    client.println(request);
    client.println("Host: ec2-13-126-239-35.ap-south-1.compute.amazonaws.com");
    client.println("User-Agent: ArduinoWiFi/1.1");
    client.println("Connection: close");
    client.println();

   unsigned long timeout = millis();
    while (client.available() == 0) 
    {
      if (millis() - timeout > 5000) 
      {
        Serial.println(">>> Client Timeout !");
        client.stop();
        return;
      }
    }
    
    // Read all the lines of the reply from server and print them to Serial
    while(client.available())
    {
      String line = client.readStringUntil('\r');
      Serial.print(line);
    }
    
    Serial.println();
    Serial.println("closing connection");
}


//============

void recvWithStartEndMarkers() {
    static boolean recvInProgress = false;
    static byte ndx = 0;
    char startMarker = '<';
    char endMarker = '>';
    char rc;

    while (Serial.available() > 0 && newData == false) {
        rc = Serial.read();

        if (recvInProgress == true) {
            if (rc != endMarker) {
                receivedChars[ndx] = rc;
                ndx++;
                if (ndx >= numChars) {
                    ndx = numChars - 1;
                }
            }
            else {
                receivedChars[ndx] = '\0'; // terminate the string
                recvInProgress = false;
                ndx = 0;
                newData = true;
            }
        }

        else if (rc == startMarker) {
            recvInProgress = true;
        }
    }
}

//============

void parseData() {      // split the data into its parts

    char * strtokIndx; // this is used by strtok() as an index

    strtokIndx = strtok(tempChars,",");      // get the first part - the string
    strcpy(messageFromPC, strtokIndx); // copy it to messageFromPC
 
    strtokIndx = strtok(NULL, ","); // this continues where the previous call left off
    integerFromPC = atoi(strtokIndx);     // convert this part to an integer

    strtokIndx = strtok(NULL, ",");
    floatFromPC = atof(strtokIndx);     // convert this part to a float

}

//============

void showParsedData() {
    Serial.print("Message ");
    Serial.println(messageFromPC);
    Serial.print("Integer ");
    Serial.println(integerFromPC);
    Serial.print("Float ");
    Serial.println(floatFromPC);
}

This is the output I get on my Serial Monitor

WiFi connected
IP address: 
***.***.**.**
connecting to ec2-13-126-239-35.ap-south-1.compute.amazonaws.com
Connected
>>> Client Timeout !
Message Chirantan
Integer 123
Float 0.25
connecting to ec2-13-126-239-35.ap-south-1.compute.amazonaws.com
Connected
>>> Client Timeout !
connecting to ec2-13-126-239-35.ap-south-1.compute.amazonaws.com
Connected
>>> Client Timeout !

Surprisingly the code was running completely fine few days ago and now I see this client timeout. So is this error is related to my ESP or from the server side? and what should I do to solve this?

what happens if you type this in your browser:

http://ec2-13-126-239-35.ap-south-1.compute.amazonaws.com:8080/put/000001,someText,100,22.3

do you get any answer?

J-M-L:
what happens if you type this in your browser:

http://ec2-13-126-239-35.ap-south-1.compute.amazonaws.com/put/000001,someText,100,22.3

do you get any answer?

Ideally, the values get posted on the link:
http://http://ec2-13-126-239-35.ap-south-1.compute.amazonaws.com:8080/get
and it was going like that from past few days. I had some other work on my desk so I had to clear it off my desk. So I just unplugged my bread board keeping rest of everything every damn connection as it is. Completed my other work and got back on to this project now, plugged it in and opened the serial monitor and now its not working.

I tried to type that in my browser but it says "Hmm....can't reach this page". So is it the problem of my site?

I tried to type that in my browser but it says "Hmm....can't reach this page". So is it the problem of my site?

No I actually forgot the port you use, should have been

http://ec2-13-126-239-35.ap-south-1.compute.amazonaws.com:8080/put/000001,someText,100,22.3

which seems to work OK

J-M-L:
No I actually forgot the port you use, should have been

http://ec2-13-126-239-35.ap-south-1.compute.amazonaws.com:8080/put/000001,someText,100,22.3

which seems to work OK

Umm...yeah this thing works

So where's the problem? Client is my ESP01, right? Now, I'm getting bit confused, sorry if the question is dumb.

The UNO is a 5V device and your ESP a 3.3V device. Do you have something that adapt the voltage between Arduino Tx and ESP Rx ?

J-M-L:
The UNO is a 5V device and your ESP a 3.3V device. Do you have something that adapt the voltage between Arduino Tx and ESP Rx ?

No, I don't. But that doesn't seem to be a problem. It has worked before completely fine and as they say it doesn't really damage the ESP for sure. IDK how but it doesn't. This project was working fine like four days ago. And my other projects even worked completely fine without including those level shifting. I never encountered the problem.

The entire scenario seems like I’m not getting anything in response. I tried the code which I used very first time in this project.

#include <ArduinoJson.h>
#include <ESP8266WiFi.h>

 const char* ssid = "";
 const char* password = "";

 const char* host = "ec2-13-126-239-35.ap-south-1.compute.amazonaws.com";//:8080/put/value-to-be-inserted

String path = "/put/";
String sensorData = "Chirantan";

void setup()
{
 
Serial.begin(115200);
delay(100);

 Serial.println();
 Serial.println();
 Serial.print("Connecting to ");
 Serial.println(ssid);

 WiFi.begin(ssid, password);

 while (WiFi.status() != WL_CONNECTED) {
 delay(500);
 Serial.print(".");
 }

 Serial.println("");
 Serial.println("WiFi connected");
 Serial.println("IP address: ");
 Serial.println(WiFi.localIP());
 }

 int value = 0;
 
void loop() 
{
  Serial.print("connecting to ");
  Serial.println(host);
  WiFiClient client;
  const int httpPort = 8080;

  if (!client.connect(host, httpPort))
  {
    Serial.println("connection failed");
    return;
  }
  client.println("GET /put/" +
String(sensorData) + " HTTP/1.1");
 client.println("Host: ec2-13-126-239-35.ap-south-1.compute.amazonaws.com");
 client.println("User-Agent: ArduinoESP/1.1");
 client.println("Connection: close");
 client.println();
 /* client.print(String("GET ") + path + data + " HTTP/1.1\r\n" +
               "Host: " + host + "\r\n" + 
               "Connection: keep‐alive\r\n\r\n");*/
  delay(500); // wait for server to respond
  // read response
  int dir = 0; 
  while(client.available()){
    String line = client.readStringUntil('\r');
    Serial.print(line);
    /////////////////////////////////////////////////////////////////////
    // NOTE: we’ll replace the line above with something to parse the 
    // JSON response here!
    /////////////////////////////////////////////////////////////////////
  }
  Serial.print("closing connection. ");
}

This is the response I get

closing connection. connecting to ec2-13-126-239-35.ap-south-1.compute.amazonaws.com
closing connection. connecting to ec2-13-126-239-35.ap-south-1.compute.amazonaws.com
closing connection. connecting to ec2-13-126-239-35.ap-south-1.compute.amazonaws.com
closing connection. connecting to ec2-13-126-239-35.ap-south-1.compute.amazonaws.com
closing connection. connecting to ec2-13-126-239-35.ap-south-1.compute.amazonaws.com
closing connection. connecting to ec2-13-126-239-35.ap-south-1.compute.amazonaws.com
closing connection. connecting to ec2-13-126-239-35.ap-south-1.compute.amazonaws.com

with no reply from the site

OK - some are sensitive to voltage

have you tried having a longer timeout?

   if (millis() - timeout > 5000)

or in the previous code the  delay(500); // wait for server to respond

give it 15 or 20 seconds just for the sake of it ?

J-M-L:
OK - some are sensitive to voltage

have you tried having a longer timeout?

   if (millis() - timeout > 5000)

or in the previous code the

  delay(500); // wait for server to respond

give it 15 or 20 seconds just for the sake of it ?

I'll try that and get back here

And Now, The code is not uploading. It may take lil time for me to get back on this

GPIO0 controls the module mode (programming or normal operation). You need to connect GPIO0 to GND and reset/powercycle the ESP01 before trying to upload your code. Then unplug.

J-M-L:
GPIO0 controls the module mode (programming or normal operation). You need to connect GPIO0 to GND and reset/powercycle the ESP01 before trying to upload your code. Then unplug.

Okay, IDK what I was missing or if I was even really missing anything or not, I rewired everything like five times…for the first four times I didn’t got any success. But for the fifth time as soon as I wired up everything I was able to upload the code and then when I opened the serial monitor everything started replying!
IDK how, why but for now everything is working fine again without changing a single line in the code. But the earlier failure of esp01 is giving me chills what if!!

If it encounters again, I’ll have to seek your help again…

Esp-01 are buggy and somewhat outdated

For a somewhat similar price you could go for a better chip.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.