I am working on a room automation project. I connected an ESP8266 board with WiFi through my home router.I make an HTTPS request from ESP8266. It returns a good response 200
when connected using DHCP's dynamically assigned IP, but returns -1
response when a static IP is configured in it.
My code:
Rui Santos
Complete project details at https://randomnerdtutorials.com
*********/
// Load Wi-Fi library
#include<ESP8266WiFi.h>
#include<ESP8266HTTPClient.h>
#include<WiFiClientSecureBearSSL.h>
using namespace std;
// Replace with your network credentials
const char* ssid = "<router_ssid>";
const char* password = "<router_password>";
void setup() {
Serial.begin(9600);
Serial.println("started");
// //Configure static IP
// IPAddress staticIP(192,168,10,55);
// IPAddress gateway(192,168,10,1);
// IPAddress subnet(255,255,255,0);
// if(!WiFi.config(staticIP,gateway,subnet)){
// // Serial.println("IP Configuration failed");
// }
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
// Serial.print(".");
digitalWrite(LED_BUILTIN,HIGH);
delay(500);
// Serial.print(".");
digitalWrite(LED_BUILTIN,LOW);
}
Serial.println("connected");
digitalWrite(LED_BUILTIN,LOW);
// set the http client
BearSSL::WiFiClientSecure client;
client.setInsecure();
HTTPClient https;
while(https.begin(client,"yandex.com",443)){
Serial.println("client began");
int code = https.GET();
if(code == 200){
Serial.println("code 200");
}
else{
Serial.println("failed");
Serial.println(code);
}
delay(5000);
}
}
void loop(){
}
When I un-comment the above lines, for static IP, the status comes back to -1
.
Please guide me what could be the problem?
Note: I have applied port forwarding on my router for the same static IP, with port 5555. Might it be creating the hurdle in HTTPS communication?
-
2you didn't specify a DNS server IP so it can't evaluate the server's nameJuraj– Juraj ♦2024年08月22日 10:03:10 +00:00Commented Aug 22, 2024 at 10:03
1 Answer 1
You are missing the configuration for the DNS servers. This information is provided by DHCP and tells the ESP how to resolve domain names into IP addresses.
IPAddress staticIP(192,168,10,55);
IPAddress gateway(192,168,10,1);
IPAddress subnet(255,255,255,0);
IPAddress primaryDNS(8, 8, 8, 8);
IPAddress secondaryDNS(8, 8, 4, 4);
if(!WiFi.config(staticIP,gateway,subnet, primaryDNS,secondaryDNS )){
// Serial.println("IP Configuration failed");
}
This uses the Google DNS servers. Note that it is typical for your router to also provide DNS services, you can pick your router as the IP for the DNS, which increases speed as it has a lower latency for things in the DNS cache
-
i did the additions, but still it is failing connection and returning -1.Ammar Mujtaba Tariq– Ammar Mujtaba Tariq2024年08月24日 06:04:38 +00:00Commented Aug 24, 2024 at 6:04