[SOLVED]"[HTTP] GET... failed, error: connection refused" when using Static IP

GeorgeFlorian1
Posts: 160
Joined: Thu Jan 31, 2019 2:32 pm

[SOLVED]"[HTTP] GET... failed, error: connection refused" when using Static IP

Postby GeorgeFlorian1 » Mon Mar 11, 2019 2:41 pm

### Hardware:

Board: ESP32 DEVKIT1
Core Installation version: 1.0.1-git -this one-
IDE name: Arduino IDE
Flash Frequency: 80Mhz
PSRAM enabled: no
Upload Speed: 921600
Computer OS: Linux Mint 19.1 Mate

Hello there !

I'm having troubles receiving the response from a URL when using Static IP. All works perfectly fine using DHCP IP.
I assume the ESP32 connects to the internet because I can access the webpage (which has a HTML input form where one can enter the URL) using the Static IP.

Long story short I am reading lines from a SPIFFS file and storing them in an String array x[].

Code: Select all

void setup () {
  if(x[0] != NULL &&      // SSID
     x[0].length() != 0 &&
     x[1] != NULL &&      // Password
     x[1].length() != 0 &&
     x[2] != NULL &&      //Local IP
     x[2].length() != 0 &&
     x[3] != NULL &&      // Gateway
     x[3].length() != 0 &&
     x[4] != NULL &&      // Subnet
     x[4].length() != 0) {
       IPAddress local_IP_STA, gateway_STA, subnet_STA;
       local_IP_STA.fromString(x[2]);
       gateway_STA.fromString(x[3]);
       subnet_STA.fromString(x[4]);
       IPAddress primaryDNS(8, 8, 8, 8);
       IPAddress secondaryDNS(8, 8, 4, 4);
       WiFi.begin(x[0].c_str(),x[1].c_str());
       delay(50);
       if(!WiFi.config(local_IP_STA, gateway_STA, subnet_STA))
       Serial.println("Couldn't configure STATIC IP ! Starting DHCP IP !");
       delay(50);
}
void loop() {
if ((WiFi.status() == WL_CONNECTED)) {
HTTPClient http;
http.begin(URL);
      int httpCode = http.GET(); //Make the request
  
      if (httpCode > 0) { //Check for the returning code
          Serial.printf("[HTTP] GET... code: %d\n", httpCode);
          if(httpCode == HTTP_CODE_OK) {
              String payload = http.getString();
              Serial.println(payload);
            }
        } else {
            Serial.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str());
          }
  
      http.end();
   } // while
delay(10000);
}
WiFi.config(local_IP_STA, gateway_STA, subnet_STA, primaryDNS) or
WiFi.config(local_IP_STA, gateway_STA, subnet_STA, primaryDNS, secondaryDNS) returns

Code: Select all

[E][WiFiGeneric.cpp:658] hostByName(): DNS Failed for jsonplaceholder.typicode.com
[HTTP] GET... failed, error: connection refused
I've also moved WiFi.config() before and after the WiFi.begin() but nothing changed.

What do you think the problem is ?
Last edited by GeorgeFlorian1 on Wed Mar 13, 2019 2:02 pm, edited 3 times in total.

ESP_Sprite
Posts: 9727
Joined: Thu Nov 26, 2015 4:08 am

Re: "[HTTP] GET... failed, error: connection refused" when using Static IP

Postby ESP_Sprite » Tue Mar 12, 2019 7:38 am

Are you sure your gateway and netmask are right? If these were set wrong, it would allow local connections (eg your laptop to your ESP32) but not remote ones (eg your ESP32 to a server on the Internet).

GeorgeFlorian1
Posts: 160
Joined: Thu Jan 31, 2019 2:32 pm

Re: "[HTTP] GET... failed, error: connection refused" when using Static IP

Postby GeorgeFlorian1 » Tue Mar 12, 2019 9:30 am

ESP_Sprite wrote:
Tue Mar 12, 2019 7:38 am
Are you sure your gateway and netmask are right? If these were set wrong, it would allow local connections (eg your laptop to your ESP32) but not remote ones (eg your ESP32 to a server on the Internet).
I'm beginning to think that I haven't done a proper subnetting. Using the DHCP local IP, gateway and subnet on the WiFi.config() works just fine.

joglz8
Posts: 20
Joined: Wed Jan 09, 2019 9:46 am

Re: "[HTTP] GET... failed, error: connection refused" when using Static IP

Postby joglz8 » Tue Mar 12, 2019 11:27 am

Are you connecting to a wireless router or to an access point on your LAN?

I have noticed that connecting to an access point I don't need the password but connecting directly to my wireless router I do. That is with an IP address reserved by DHCP server / router.
Old controls guy, SW Ontario, Canada .

GeorgeFlorian1
Posts: 160
Joined: Thu Jan 31, 2019 2:32 pm

Re: "[HTTP] GET... failed, error: connection refused" when using Static IP

Postby GeorgeFlorian1 » Wed Mar 13, 2019 9:06 am

joglz8 wrote:
Tue Mar 12, 2019 11:27 am
Are you connecting to a wireless router or to an access point on your LAN?

I have noticed that connecting to an access point I don't need the password but connecting directly to my wireless router I do. That is with an IP address reserved by DHCP server / router.
I've always connected the ESP to a Wireless Router, I don't have any experience with an access point.
That being said, I've always needed SSID and Password when the ESP was in Station Mode and it tried connecting to a Wireless Router. I've thought of it like being natural, because that's how I connect my phone to my home's WiFi network.
This is what is written in WiFiSTA.cpp

Code: Select all

/**
 * Start Wifi connection
 * if passphrase is set the most secure supported mode will be automatically selected
 * @param ssid const char*          Pointer to the SSID string.
 * @param passphrase const char *   Optional. Passphrase. Valid characters in a passphrase must be between ASCII 32-126 (decimal).
 * @param bssid uint8_t[6]          Optional. BSSID / MAC of AP
 * @param channel                   Optional. Channel of AP
 * @param connect                   Optional. call connect
 * @return
 */
wl_status_t WiFiSTAClass::begin(const char* ssid, const char *passphrase, int32_t channel, const uint8_t* bssid, bool connect)

Who is online

Users browsing this forum: No registered users and 75 guests