38. Chapter WiFi Working Modes (WiFi Board)
In this chapter, we’ll focus on the WiFi infrastructure for control board.
Control board has 3 different WiFi operating modes: station mode, AP mode and AP+station mode. All WiFi programming projects must be configured with WiFi operating mode before using WiFi, otherwise WiFi cannot be used.
38.1. Project Station mode
38.1.1. Component List
Control board x1 |
|
USB cable x1 |
|
38.1.2. Component knowledge
38.1.2.1. Station mode
When control board(wifi) selects Station mode, it acts as a WiFi client. It can connect to the router network and communicate with other devices on the router via WiFi connection. As shown below, the PC is connected to the router, and if control board(wifi) wants to communicate with the PC, it needs to be connected to the router.
38.1.3. Circuit
Connect the board to the computer using the USB cable.
38.1.4. Sketch
38.1.4.1. Sketch_38.1.1
Because the names and passwords of routers in various places are different, before the Sketch runs, users need to enter the correct router’s name and password in the box as shown in the illustration above.
After making sure the router name and password are entered correctly, compile and upload codes to control board, open serial monitor and set baud rate to 115200. And then it will display as follows:
When control board successfully connects to “ssid_Router”, serial monitor will print out the IP address assigned to control board by the router.
The following is the program code:
1/**********************************************************************
2 Filename : Sketch_38.1.1_WiFi_Station
3 Description : Connect to your router using control board
4 Auther : www.freenove.com
5 Modification: 2024/08/05
6**********************************************************************/
7
8#include "WiFiS3.h"
9
10const char *ssid_Router = "********"; //Enter the router name
11const char *password_Router = "********"; //Enter the router password
12
13void setup(){
14 Serial.begin(115200);
15 delay(2000);
16 Serial.println("Setup start");
17 WiFi.begin(ssid_Router, password_Router);
18 Serial.println(String("Connecting to ")+ssid_Router);
19 while (WiFi.status() != WL_CONNECTED){
20 delay(500);
21 Serial.print(".");
22 }
23 Serial.println("\nConnected, IP address: ");
24 Serial.println(WiFi.localIP());
25 Serial.println("Setup End");
26}
27
28void loop() {
29}
Include the WiFi Library header file of control board.
1#include "WiFiS3.h"
Enter correct router name and password.
1const char *ssid_Router = "********"; //Enter the router name
2const char *password_Router = "********"; //Enter the router password
Set control board in Station mode and connect it to your router.
1WiFi.begin(ssid_Router, password_Router);
Check whether control board has connected to router successfully every 0.5s.
1while (WiFi.status() != WL_CONNECTED){
2 delay(500);
3 Serial.print(".");
4}
Serial monitor prints out the IP address assigned to control board.
1Serial.println(WiFi.localIP());
38.1.4.2. Reference
- Class Station
Every time when using WiFi, you need to include header file “WiFiS3.h.”.
begin(ssid, password,channel, bssid, connect): control board is used as Station to connect hotspot.
ssid: WiFi hotspot name
password: WiFi hotspot password
channel: WiFi hotspot channel number; communicating through specified channel; optional parameter
bssid: mac address of WiFi hotspot, optional parameter
connect: blloean optional parameter, defaulting to true. If set as false, then control board won’t connect WiFi.
config(local_ip, gateway, subnet, dns1, dns2): set static local IP address.
local_ip: station fixed IP address.
subnet: subnet mask
dns1,dns2: optional parameter. define IP address of domain name server
status: obtain the connection status of WiFI
local IP(): obtian IP address in Station mode
disconnect(): disconnect wifi
setAutoConnect(boolen): set automatic connection Every time control board is power on, it will connect WiFi aitomatically.
setAutoReconnect(boolen): set automatic reconnection Every time control board disconnects WiFi, it will reconnect to WiFi automatically.
38.2. Project AP mode
38.2.1. Component List & Circuit
Component List & Circuit are the same as in Section 30.1.
38.2.2. Component knowledge
38.2.2.1. AP mode
When control board selects AP mode, it creates a hotspot network that is separate from the Internet and waits for other WiFi devices to connect. As shown in the figure below, Control board is used as a hotspot. If a mobile phone or PC wants to communicate with control board, it must be connected to the hotspot of control board. Only after a connection is established with control board can they communicate.
38.2.3. Circuit
Connect the board to the computer using the USB cable.
38.2.4. Sketch
38.2.4.1. Sketch_38.2.1
Before the Sketch runs, you can make any changes to the AP name and password for control board in the box as shown in the illustration above. Of course, you can leave it alone by default.
Compile and upload codes to control board, open the serial monitor and set the baud rate to 115200. And then it will display as follows.
When observing the print information of the serial monitor, turn on the WiFi scanning function of your phone, and you can see the ssid_AP on control board, which is called “WiFi_Name” in this Sketch. You can enter the password “12345678” to connect it or change its AP name and password by modifying Sketch.
38.2.4.2. Sketch_37.2_AP_mode
The following is the program code:
1/**********************************************************************
2 Filename : Sketch 38.2.1_WiFi_AP
3 Description : Set control board to open an access point
4 Auther : www.freenove.com
5 Modification: 2024/08/05
6**********************************************************************/
7
8#include "WiFiS3.h"
9
10///////please enter your sensitive data in the Secret tab/arduino_secrets.h
11char ssid[] = "WiFi_Name"; // your network SSID (name)
12char pass[] = "12345678"; // your network password (use for WPA, or use as key for WEP)
13
14int status = WL_IDLE_STATUS;
15void setup() {
16 //Initialize serial and wait for port to open:
17 Serial.begin(115200);
18 delay(3000);
19 while (!Serial) {
20 ; // wait for serial port to connect. Needed for native USB port only
21 }
22 // check for the WiFi module:
23 if (WiFi.status() == WL_NO_MODULE) {
24 Serial.println("Communication with WiFi module failed!");
25 // don't continue
26 while (true);
27 }
28
29 String fv = WiFi.firmwareVersion();
30 if (fv < WIFI_FIRMWARE_LATEST_VERSION) {
31 Serial.println("Please upgrade the firmware");
32 }
33 // by default the local IP address will be 192.168.4.1
34 // you can override it with the following:
35 WiFi.config(IPAddress(192,48,56,2));
36 // print the network name (SSID);
37 Serial.print("Creating access point named: ");
38 Serial.println(ssid);
39
40 // Create open network. Change this line if you want to create an WEP network:
41 status = WiFi.beginAP(ssid, pass);
42 if (status != WL_AP_LISTENING) {
43 Serial.println("Creating access point failed");
44 // don't continue
45 while (true);
46 }
47
48 // wait 5 seconds for connection:
49 delay(5000);
50 // you're connected now, so print out the status
51 printWiFiStatus();
52}
53
54void loop() {
55}
56
57void printWiFiStatus() {
58 // print the SSID of the network you're attached to:
59 Serial.print("SSID: ");
60 Serial.println(WiFi.SSID());
61
62 // print your WiFi shield's IP address:
63 IPAddress ip = WiFi.localIP();
64 Serial.print("IP Address: ");
65 Serial.println(ip);
66
67 // print where to go in a browser:
68 Serial.print("To see this page in action, open a browser to http://");
69 Serial.println(ip);
70
71}
Include WiFi Library header file of control board.
1#include "WiFiS3.h"
Enter correct AP name and password.
1char ssid[] = "WiFi_Name"; // your network SSID (name)
2char pass[] = "12345678"; // your network password (use for WPA, or use as key for WEP)
Check whether the AP is turned on successfully. If yes, print out IP and MAC address of AP established by control board. If no, print out the failure prompt.
status = WiFi.beginAP(ssid, pass);
if (status != WL_AP_LISTENING) {
Serial.println("Creating access point failed");
// don't continue
while (true);
}
...
// wait 5 seconds for connection:
delay(5000);
// you're connected now, so print out the status
printWiFiStatus();
...
void printWiFiStatus() {
// print the SSID of the network you're attached to:
Serial.print("SSID: ");
Serial.println(WiFi.SSID());
// print your WiFi shield's IP address:
IPAddress ip = WiFi.localIP();
Serial.print("IP Address: ");
Serial.println(ip);
// print where to go in a browser:
Serial.print("To see this page in action, open a browser to http://");
Serial.println(ip);
}

