Skip to content

HTTP GET failed... connection refused #3804

@manish-hello-gupta

Description

@manish-hello-gupta

Hardware:

Board: ESP32 Dev Module
Core Installation version: 1.0.4
IDE name: Arduino IDE
Flash Frequency: 80Mhz
PSRAM enabled: no
Upload Speed: 115200
Computer OS: Windows 10

Description:

I am trying to fetch values of columns from a table name traffic1 in database test of a phpmyadmin in localserver. I am using Xamppserver. 2-3 months before the code works perfectly. But now it shows error in connecting.The error is
HTTPCODE: -1
[HTTP] GET... failed, error: connection refused
0
0
0
0

The four zeroes are of x[4] array's elements values which do not get change

Sketch: (leave the backquotes for code formatting)

//Change the code below by your sketch
#include <Arduino.h>
#include <WiFi.h>
#include <WiFiMulti.h>
#include <HTTPClient.h>

WiFiMulti wifiMulti;
void setup() {

  Serial.begin(115200);
  Serial.println();
  for (uint8_t t = 4; t > 0; t--) {
    Serial.printf("[SETUP] WAIT %d...\n", t);
    Serial.flush();
    delay(1000);
  }

  wifiMulti.addAP("Redmi", "IDEA!23#");

}

void loop() {
  int x[4] = {0} ;
  // wait for WiFi connection
  if ((wifiMulti.run() == WL_CONNECTED)) {

    HTTPClient http;

    Serial.print("[HTTP] begin...\n");

    // configure server and url
    http.begin("http://localhost/testcode/show1.php");
    //http.begin("192.168.1.12", 80, "/test.html");
    
    Serial.print("[HTTP] GET...\n");
    // start connection and send HTTP header
    int httpCode = http.GET();
    Serial.print("HTTPCODE: ");
    Serial.println(httpCode);
    if (httpCode > 0) {
      // HTTP header has been send and Server response header has been handled
      Serial.printf("[HTTP] GET... code: %d\n", httpCode);

      // file found at server
      if (httpCode == HTTP_CODE_OK) {

        // get lenght of document (is -1 when Server sends no Content-Length header)
        int len = http.getSize();
        Serial.println(len);
        // create buffer for read
        uint8_t buff[128] = { 0 };

        // get tcp stream
        WiFiClient * stream = http.getStreamPtr();

        // read all data from server
        size_t size = stream->available();
        while (http.connected() && (len > 0 || len == -1)) {
          // get available data size
          if (size) {
            // read up to 128 byte
            int c = stream->readBytes(buff, ((size > sizeof(buff)) ? sizeof(buff) : size));

            // write it to Serial
            Serial.write(buff, c);
            if (len > 0) {
              len -= c;
            }
          }
          delay(1);
        }
        Serial.println();
        int j = 0;
        for (int i = 0; i < size; i++)
        {
          if (j == 4)
          {
            break;
          }
          if (buff[i] == ' ')
          {
            j++;
          }
          else {
            x[j] *= 10;
            x[j] += (int)buff[i];
            x[j] -= 48;
          }
        }

        Serial.print("[HTTP] connection closed or file end.\n");
      }
    }
    else {
      Serial.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str());
    }
    http.end();
  }
  for (int k = 0; k < 4; k++)
  {
    Serial.println(x[k]);
  }
  delay(10000);
}

Debug Messages:

[D][HTTPClient.cpp:276] beginInternal(): host: localhost port: 80 url: /testcode/show1.php
[E][WiFiClient.cpp:258] connect(): socket error on fd 60, errno: 104, "Connection reset by peer"
[D][HTTPClient.cpp:1018] connect(): failed connect to localhost:80
[W][HTTPClient.cpp:1318] returnError(): error(-1): connection refused
[D][HTTPClient.cpp:383] disconnect(): tcp is closed

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions