esp8266 / Arduino

ESP8266 core for Arduino
GNU Lesser General Public License v2.1
15.99k stars 13.34k forks source link

WiFiClientSecure doesn't connect to HTTPS sites when a static IP is set #6957

Closed andresrcs closed 4 years ago

andresrcs commented 4 years ago

Basic Infos

Platform

Settings in IDE

Problem Description

I have a problem with WiFiClientSecure it can't make a connection to HTTPS sites when a static IP is set withWiFi.config(IP, gateway, subnet); but if I let the DHCP server to assign the IP it works as expected.

MCVE Sketch


#include <ESP8266WiFi.h>
#include <WiFiClientSecure.h>

// Network Configurations
#define WLAN_SSID     "YOUR_WIRELESS_SSID"
#define WLAN_PASSWORD "YOUR_PASSWORD"

//Connection to HTTPS sites doesn't work with static IP
IPAddress ip(192,168,0,102);
IPAddress gateway(192,168,0,1);
IPAddress subnet(255,255,255,0);

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

  // Connect to WiFi network
  Serial.println(); Serial.println();
  Serial.print("Connecting to ");
  Serial.println(WLAN_SSID);

  WiFi.config(ip, gateway, subnet);
  WiFi.mode(WIFI_STA);
  WiFi.begin(WLAN_SSID, WLAN_PASSWORD);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

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

  // connect to HTTPS site
  WiFiClientSecure client;
  client.setInsecure();
  if (client.connect("maker.ifttt.com", 443))
  {
    Serial.println("Connected To Maker");
  }
  else Serial.println("Failed To Connect To Maker!");
}

void loop() {
}
earlephilhower commented 4 years ago

You haven't set a DNS server. Add dns1 and dns2 params (can be the same) to WiFi.config and it runs fine.

WiFi.config(ip, gateway, subnet, gateway, gateway); , for example