Information



Tags



Share

How to connect to an SSL protected server with ESP8266 (WiFiClient)

Advertisement:


read_later

Read Later

Keywords



Keywords



read_later

Read Later

Information

Tags

Share





Advertisement

Advertisement



Definition

It is not too compelling to connect to a server with ESP8266 by using WiFiClient to read the response of the web pages hosted by the server. However, if the requested server is protected with an SSL certificate, then ESP8266 cannot connect to that server without sending a secure key along with the request headers to identify itself to the page for collecting information properly. In that case, you might need to get the fingerprint(or thumbprint) of the requested web page as the secure key to connect to the server.

SSL FingerPrint works like an ID card; when you visit a web page that of an SSL protected server(website), it verifies your connection secure depending on whether you have the fingerprint or not. As a result of that, every browser has a file that contains the SSL fingerprint, mostly known as ThumbPrint.

As you elicit the fingerprint, you can send a request to the server by using WiFiClientSecure instead of WiFiClient. Do not worry, WiFiClientSecure has pretty much the same usage and syntax with WiFiClient; you only need to set an SSL fingerprint to the requested server and use 443 as the port number as depicted on the code below.

You can download libraries required for implementing WiFiClientSecure here and also inspect ESP8266 here.

Code

First of all, include the required libraries to execute WiFiClientSecure.

Define the WiFi settings – SSID and Password.

Enter the hostname(IP Address or website) and the port number – 443.

Create a WifiClientSecure object to contact with the server.

Set the fingerprint to verify the connection.

Connect to the server with ESP8266 over the Internet via WiFi.

Send a GET request to a web page hosted by the server to test the response and connection.

Also, you can execute this code with NodeMCU.

// Include required libraries to get data from your data panel connection page.
#include <ESP8266WiFi.h>
#include <WiFiClientSecure.h>
#include <ESP8266WebServer.h>
#include <ESP8266HTTPClient.h>

// Define the WiFi settings.
const char *ssid = "SUPERONLINE_WiFi_0728";
const char *password = "7J3HXPELJJLF";
// Define the hostname, the port number and the fingerprint.
const char *host = "hostname";
const char fingerprint[] PROGMEM = "fingerprint";
const int httpsPort = 443;

void setup() {
WiFi.mode(WIFI_OFF);
delay(1000);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
// Halt the code until connected to WiFi.
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print("*");
}
}

void loop() {
// Create a WiFiClientSecure object.
WiFiClientSecure client;
// Set the fingerprint to connect the server.
client.setFingerprint(fingerprint);
// If the host is not responding,return.
if(!client.connect(host, httpsPort)){
Serial.println("Connection Failed!");
return;
}

// Send a GET request to a web page hosted by the server.
client.print(String("GET ") + `webpage` + " HTTP/1.1\r\n" + "Host: " + host + "\r\n" + "Connection: close\r\n\r\n");

// Detect whether client is responding properly or not.
unsigned long timeout = millis();
while (client.available() == 0) {
if (millis() - timeout > 5000) {
Serial.println(">>> Client Timeout !");
client.stop();
return;
}
}

}