After mentioning the ESP8266WiFi class in the previous article. This time, let’s learn how to use the WiFiClient class to write programs on the client side that link to a service or server.
When the programmer is connected to a wireless network in STA mode, the microcontroller can use both internal and external Internet networks. Therefore, it can write a program to be a client to access the service through the communication port number of the service provider that wants to access with an IP number.
WiFiClient
The WiFiClient class requires programmers to create objects from this class to connect to the provider via the IP address and port number. To create a WiFiClient object, write the following command:
WiFiClient obj;
To request a connection to a service provider with the required IP address and port, use the connect() command in the following manner. The result is either true or false. Also, the IP Address variable must be created from the IPAddress class.
IPAddress ip( xx, xx, xx, xx );
con = obj.connect( ip, port );
If the connected wireless network has DNS service in the system, it can use the name or URL instead of the IP address. Therefore, the form of the connection command is as follows
con = obj.connect( URL, port )
If you want to know the status of the connection, run the connected() command to see if the result is true or false.
status = obj.connected()
Sending data to a service provider called a request, uses the println() command as follows. Requests vary depending on the service or protocol. Programmers need to study more details.
obj.println( req )
To know if the service provider has sent the information back as requested or not. The available() command must be used to check the status of the data returned from the service provider as follows.
status = obj.available()
To read the response data from the provider, use the read() command which returns the data in bytes as follows:
data = obj.read()
At the end of the connection the stop() command must be run as follows:
obj.stop()
The command to read a string from the WiFiClient until the specified character is found is
string=obj.readStringUntil( char )
Example program for connecting to AP to use the Internet. It then requests access to our server and reads the return value. Finally, turn it off as follows and the sample result is as shown in Figure 1.
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#define AP_NAME "ชื่อAP"
#define AP_PASSWD "รหัสของAP"
WiFiClient client;
void setup() {
Serial.begin(115200);
Serial.println("\n\n");
WiFi.mode(WIFI_STA);
WiFi.begin( AP_NAME, AP_PASSWD );
while (WiFi.status() != WL_CONNECTED) {
delay(250);
Serial.print(".");
}
if (client.connect("jarutex.com", 80)) {
Serial.println("Yes");
client.print(String("GET ") + "/" + " HTTP/1.1\r\n" +
"Host: " + "jarutex.com" + "\r\n" +
"Connection: close\r\n\r\n");
delay(1000);
while (client.available()) {
String line = client.readStringUntil('\r');
Serial.print(line);
}
client.stop();
} else {
Serial.println("OMG!");
}
WiFi.disconnect();
}
void loop() {
}
Conclusion
From this article, it is found that client connections can be made via the WiFiClient class, however, it is also found that when trying to connect to the web, which currently requires the use of the HTTPS protocol and using IPv6, it may cause a mistake. However, in the examples of ESP8266, there are examples of usage with HTTPS that you can study and modify later. Finally, have fun with programming.
If you want to talk with us, feel free to leave comments below!!
Referecese
- Arduino : WiFiClient()
(C) 2021, By Jarut Busarathid and Danai Jedsadathitikul
Updated 2021-11-07