#include <WiFi.h>
#include <WiFiClient.h>
int wifiConnect(char* ssid, char* password) {
int status = WiFi.begin(ssid, password);
while (status != WL_CONNECTED) {
switch (status) {
case WL_NO_SSID_AVAIL:
Serial.printf("No AP with name %s can be found", ssid);
return status;
case WL_CONNECT_FAILED:
Serial.printf("Connection failed");
return status;
case WL_CONNECTION_LOST:
Serial.printf("Connection lost possible security problem");
return status;
}
delay(100);
status = WiFi.status();
}
return status;
}
WiFiClient client;
void setup() {
Serial.begin(9600);
int status = wifiConnect("ssid", "password");
Serial.println(status);
client.connect("www.example.com", 80);
client.println("GET /index.html HTTP/1.1");
client.println("HOST:example.com");
client.println();
};
void loop() {
if (client.available()) {
char c = client.read();
Serial.print(c);
}
}
Of course, we can do much better than this simple example. For one thing, each operation needs to be checked for errors.
ESP32 HTTPS Client
Creating an HTTP client is easy, but creating an HTTPS client requires more processing power that most Arduino processors can muster. As a result HTTPS is only possible on more modern devices such as the ESP32. In this case there is a modified WiFiClient class that makes switching to HTTPS very easy.
The key point about HTTPS is encryption. If you connect to an HTTPS server then the data is encrypted before transmission. The key used for the encryption is exchanged using Public Key Cryptography with the help of one or more certificates. If the client and the server have certificates then it is possible for the client to confirm that the server is what it claims to be and the server can confirm that the client is what it claims to be. This can be used to restrict access to clients that have a particular certificate that proves their identity. In many cases this is unnecessary and the encryption key can be exchanged just using the server’s certificate. This is referred to as “unsafe”, but its encryption is perfectly secure, it is only the identity of the client which is not determined. This is the mode that most browsers work in, unless you upload a specific client certificate.
To create a certificate-less HTTPS client all you have to do is to make the following changes to increase security:
Apart from setting “insecure” mode you also have to change the port to 443, which is the usual port for an HTTPS connection.
The complete program is:
#include <WiFi.h>
#include <WiFiClientSecure.h>
int wifiConnect(char* ssid, char* password) {
int status = WiFi.begin(ssid, password);
while (status != WL_CONNECTED) {
switch (status) {
case WL_NO_SSID_AVAIL:
Serial.printf("No AP with name %s can be found", ssid);
return status;
case WL_CONNECT_FAILED:
Serial.printf("Connection failed");
return status;
case WL_CONNECTION_LOST:
Serial.printf("Connection lost possible security problem");
return status;
}
delay(100);
status = WiFi.status();
}
return status;
}
WiFiClientSecure client;
void setup() {
Serial.begin(9600);
int status = wifiConnect("dlink3", "hawkhawk");
Serial.println(status);
client.setInsecure();
client.connect("www.example.com", 443);
client.println("GET /index.html HTTP/1.1");
client.println("HOST:example.com");
client.println();
};
void loop() {
if (client.available()) {
char c = client.read();
Serial.print(c);
}
}
If you have a client certificate that the server needs to make the connection then you can supply it using:
client.setCACert(cert);
where cert is a C string representation of the certificate, see ESP HTTPS Server Component for an example of how to specify a certificate.
In Chapter but not in this extract
Request Methods
A Sensor Client
The HTTP Server
ESP IDF HTTPS Server Component
Summary
Connecting to a WiFi network is a matter of using the WiFi object configured as a station to connect to an access point
The ESP version of the WiFi object has extensions to make it more sophisticated, including events to allow it to work asynchronously.
You can use the IPAddress class to work with IP addresses.
Implementing error handling for a WiFi connection can be challenging.
You can scan to discover what networks are available.
An HTTP client can both send and receive data to the server depending on the request it makes.
You can create a client or a server using the WiFiClient
The most common request is GET which accepts data from the server.
Both POST and PUT can be used to send data to the server.
The only difference between a client and server is that a client can only make a connection whereas a server can accept a connection.
It is possible to avoid having to implement a server on the ESP32 by allowing a client to connect to a server running on another machine and send its data using a PUT or POST request.
The HTTP client is easy use, but in most cases you are going to want to use the HTTPS WiFiClientSecure class which doesn’t need a custom certificate to work. This is ESP-specific.
The HTTP WiFiServer server works in a very simple way and you have to manually generate all of the headers needed to implement any of the request methods,
There is no Arduino core way of implementing an HTTPS server and the best solution is to fall back to the ESP IDF