HTTPS requet from ESP8266

Bhumil Depani
3 min readMay 23, 2021

Want to communicate with the internet from your tiny NodeMCU ESP8266?

HTTPS

If yes then follow the steps.

For that we need two libraries included in the Arduino IDE.

  • ESP8266WiFi.h
  • WiFiClientSecure.h

First fetch the code “HTTPS_request” from the below link:

https://github.com/BhumilDepani/ESP8266.git

Let’s discuss what this code is about,

  • First ESP8266 will connect to the router, after that it will establish connection with Github server via HTTPS protocol.
  • After that ESP8266 will go to the address described in the code and will fetch data from there and will print it on the Serial Monitor.

Copy paste that code in Arduino IDE and try to run.

Most probably you will get an error indicating “ESP8266WiFi.h file not found”.The reason for the error is straightforward that the 2 libraries specified above are not included in your Arduino environment.

So, let’s include both libraries.

Open your GIT GUI. Select “Clone Existing Repository”. Fill required fields as below:

So, now you are ready with an Arduino environment. Now compile and deploy your code into ESP8266 Board with USB-type B cable.

After successful deployment, open your Serial monitor. You will get output. If you won’t get output then press the RST push button on the ESP8266 Board, now ESP will reset and will run code from the start.

Will get output as below:

Output of mentioned code

Happy?

But, want to know about code? Then follow me.

Write your WiFi SSID name and password (in double quotes) in 5th and 6th line.

We are trying to get data from Github server, as indicated in line 8.

HTTPS uses port number 443 by default on the server

  • Port is a gate through which a request can enter into the server.
  • Port 80 is dedicated to HTTP and 443 is for HTTPS.
Schematic presentation of how HTTPS works

HTTPS works as above.

Now, we need to find the public key of api.github.com, it isn’t static so whenever you write your code you need to find it separately and paste in line 13. Write “api.github.com” in any browser. Make a right click and go to the Inspect menu. Go to “Security” tab and click “View Certificate”. Go to the “Details” tab and find the “Thumbprint” field. Its value is the Public Key of “api.github.com”. Copy it and paste it in the code.

Now begins Serial communication between ESP8266 and PC.

WiFi.begin() function will take WiFi name and password to establish connection. Then check the connection in the loop. WiFi.localIP() function will give the IP address of ESP8266.

Then create an object of WiFiClientSecure class and name it client.

Use setFingerprint() function to send the public key of the api.github.com.

Now, establish connection with api.github.com host using connect() function.

We are fetching data from the address specified in the 51th line. In the 55th line we are using the client.print() function to send headers with GET request.

In the 67th line we are checking whether ESP8266 is connected with a specified address or not, and if not then recheck and if yes then read the response using client.readStringUntil() function.

In line 75th, we are just confirming the correctness of the response, line 75 to 79 are optionals.

At last we are writing the response back to Serial Monitor. We have successfully fetch data from the internet using HTTPS protocol and print it in the serial monitor.

Thank you very much for reading and helping Earth to embed. If you have any doubt in any step write out to us at “embedtheearth@gmail.com”. We would more than happy to help you.

--

--