Table of Contents
1. Using WIFI
1. Connect to WIFI and display signal strength (STA mode)
#include “WiFi.h”
const char* ssid=”ZTE-KT”;
const char* password=”Zst666.8″;
void initWiFi(){
WiFi.mode(WIFI_STA);// Set WIFI to STA mode
WiFi.begin(ssid,password);// Connect WIFI to the corresponding device and password
Serial.print(“Connecting to WiFi”);
while(WiFi.status() !=WL_CONNECTED){
Serial.print('.');
delay(1000);
}
Serial.println(WiFi.localIP());
}
void setup() {
Serial.begin(115200);
initWiFi();
Serial.print(“RRSI:”);
Serial.println(WiFi.RSSI());
}
void loop() {
// put your main code here, to run repeatedly:
}
2. Scan WIFI and display nearby WIFI (STA mode)
#include “WiFi.h”
void setup() {
Serial.begin(115200);
WiFi.mode(WIFI_STA);// Set WIFI to STA mode
WiFi.disconnect();// Disconnect WIFI
delay(100);
}
void loop() {
Serial.println(“Starting to search for WIFI signals…”);
int n=WiFi.scanNetworks();
Serial.println(“Scan complete”);
if(n==0){
Serial.println("No WIFI devices found");
}else{
Serial.print("Number of WIFI devices:");
Serial.print(n);// Output the number of devices
Serial.println(" devices");
for(int i=0; i<n; <="" code="" delay(10);="" i++){="" serial.print("(");="" serial.print(")");="" serial.print(":");="" serial.print(i+1);="" serial.print(wifi.rssi(i));="" serial.print(wifi.ssid(i));="" serial.println((wifi.encryptiontype(i)='WIFI_AUTH_OPEN)?"":"*");' }=""></n;>
}
delay(5000);
}
3. AP Mode
#include <WiFi.h>
const char *ssid=”ESP32_WIFI”;
const char *password=”123456789″;
void setup() {
Serial.begin(115200);
WiFi.mode(WIFI_AP);
WiFi.softAP(ssid,password);
Serial.print(“Access Point IP Address:”);
Serial.println(WiFi.softAPIP());
}
void loop() {
// put your main code here, to run repeatedly:
}
4. AP Mode 1
#include <WiFi.h>
const char *ssid=”ESP32_WIFI”;
const char *password=”123456789″;
IPAddress local_IP(192,168,4,10);
IPAddress gateway(192,168,4,1);
IPAddress subnet(255,255,255,0);
void setup() {
Serial.begin(115200);
WiFi.mode(WIFI_AP);
WiFi.softAPConfig(local_IP,gateway,subnet);
WiFi.softAP(ssid,password);
WiFi.softAPsetHostname(“sxkj”);
Serial.println( WiFi.softAPgetHostname());
Serial.print(“Access Point IP Address:”);
Serial.println(WiFi.softAPIP());
}
void loop() {
Serial.println( WiFi.softAPgetStationNum());
delay(1000);
}
5. AP+STA Mode
#include <WiFi.h>
const char* wifi_network_ssid=”ZTE-KT”;
const char* wifi_network_password=”Zst666.8″;
const char *ssid=”ESP32_WIFI”;
const char *password=”123456789″;
void setup() {
Serial.begin(115200);
WiFi.mode(WIFI_MODE_APSTA);
WiFi.softAP(ssid,password);
WiFi.begin(wifi_network_ssid,wifi_network_password);
while(WiFi.status()!=WL_CONNECTED){
delay(500);
Serial.println("Waiting to connect to WIFI");
}
Serial.print(“AP Address:”);
Serial.println(WiFi.softAPIP());
Serial.print(“STA Connected IP Address:”);
Serial.println(WiFi.localIP());
}
void loop() {
}
6. WIFI TCP UART Bi-directional Communication
#include <WiFi.h>
WiFiServer server;// Set ESP32 as server
const char *ssid=”ZTE-KT”;
const char *password=”Zst666.8″;
String readTCP;// Store the string sent by the client
String readUART;// Store the string sent by the serial port
void setup() {
Serial.begin(115200);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid,password);
while(WiFi.status()!=WL_CONNECTED){
delay(500);
Serial.print(".");
}
Serial.print(“IP Address:”);
Serial.println(WiFi.localIP());
server.begin(2001);
}
void loop() {
WiFiClient client=server.available();
if(client){
Serial.print("Client connected");
while(client.connected()){
if(client.available()){
readTCP=client.readString();
Serial.println(readTCP);
readTCP="";
}
if(Serial.available()){
readUART=Serial.readString();
client.println(readUART);
readUART="";
}
}
}
client.stop();
Serial.println("Client not connected");
}
6. ESP32 Set as Client
#include <WiFi.h>
WiFiClient client;// Set ESP32 as client
const char *ssid=”ZTE-KT”;
const char *password=”Zst666.8″;
const IPAddress serverIP(192,168,5,76);// Server IP to access
uint16_t serverPort=2001;// Port number to connect to the server
String readTCP;// Store the string sent by the client
String readUART;// Store the string sent by the serial port
void setup() {
Serial.begin(115200);
WiFi.mode(WIFI_STA);
WiFi.setSleep(false);
WiFi.begin(ssid,password);
while(WiFi.status()!=WL_CONNECTED){
delay(500);
Serial.print(".");
}
Serial.print(“IP Address:”);
Serial.println(WiFi.localIP());
}
void loop() {
Serial.print(“Connecting to server”);
if(client.connect(serverIP,serverPort)){
Serial.print("Server connected successfully");
while(client.connected() || client.available()){
if(client.available()){
readTCP=client.readString();
Serial.println(readTCP);
readTCP="";
}
if(Serial.available()){
readUART=Serial.readString();
client.println(readUART);
readUART="";
}
}
Serial.println("No client connected");
client.stop();
}
else{
Serial.println("Failed to connect to server");
client.stop();
}
delay(5000);
}
7. ESP32 Set as Client, Control LED
#include <WiFi.h>
WiFiClient client;// Set ESP32 as client
const char *ssid=”ZTE-KT”;
const char *password=”Zst666.8″;
const IPAddress serverIP(192,168,5,76);// Server IP to access
uint16_t serverPort=2001;// Port number to connect to the server
const int led=2;
void setup() {
pinMode(led,OUTPUT);
Serial.begin(115200);
WiFi.mode(WIFI_STA);
WiFi.setSleep(false);
WiFi.begin(ssid,password);
while(WiFi.status()!=WL_CONNECTED){
delay(500);
Serial.print(".");
}
Serial.print(“IP Address:”);
Serial.println(WiFi.localIP());
}
void loop() {
Serial.print(“Connecting to server”);
if(client.connect(serverIP,serverPort)){
Serial.print("Server connected successfully");
while(client.connected() || client.available()){
if(client.available()){
char x=client.read();
if(x=='A'){
digitalWrite(led,HIGH);
}
else if(x=='B'){
digitalWrite(led,LOW);
}
}
}
Serial.println("No client connected");
client.stop();
}
else{
Serial.println("Failed to connect to server");
client.stop();
}
delay(5000);
}
8. WIFI_UDP Mode Set as Server
#include <WiFi.h>
#include <WiFiUdp.h>
WiFiUDP Udp;
char Rcvdata[255];
String readUART=””;
unsigned int localPort=2001;
const char *ssid=”ZTE-KT”;
const char *password=”Zst666.8″;
void setup() {
Serial.begin(115200);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid,password);
while(WiFi.status()!=WL_CONNECTED){
delay(500);
Serial.print(".");
}
if(Udp.begin(localPort)){// Start Udp listening service
Serial.println("Listening started");
Serial.printf("Now listening IP: %s, UDP port: %d\n", WiFi.localIP().toString().c_str(), localPort);// Output server IP address and port
}else{
Serial.println("Listening failed");
}
}
void loop() {
int shu= Udp.parsePacket();// Size of the received data packet
if(shu>0){
Serial.printf("Received %d bytes from %s, port %d\n", shu, Udp.remoteIP().toString().c_str(), Udp.remotePort());// Serial output received data quantity, sending client's IP and port
Udp.read(Rcvdata, 255);// Read the received data
Serial.printf(Rcvdata);// Serial output received data
}
shu=0;
if (Serial.available())// Check if there is data on the serial port
{
readUART=Serial.readString();// Read the string sent from the serial port
Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());// Prepare to send data packet, set the corresponding IP address and port of the client
Udp.print(readUART);// Send the data to the client
Udp.endPacket();// End sending
readUART = "";// Clear the received characters
}
}
9. WIFI_UDP Mode Set as Client
#include <WiFi.h>
#include <WiFiUdp.h>// Call WIFIUDP library
WiFiUDP Udp; // Create wifiUdp
char Rdata[255];// Receive the string sent by the server
String readUART=””;// String stored for serial reception
unsigned int localPort = 2002;// Communication port
const char * ssid = “ZTE-KT”;// Home router name
const char * password = “Zst666.8”;// Home router password
IPAddress ipServidor(192, 168, 5, 76); // Set the default server IP address
/*
Client’s IP address must be different from the server, otherwise it will conflict with the client connection
*/
IPAddress ipCliente(192, 168, 5, 59);// Different IP from the server
IPAddress Subnet(255, 255, 255, 0);
void setup() {
Serial.begin(115200);
WiFi.mode(WIFI_STA); // Set WiFi to STA mode
WiFi.begin(ssid, password);// Open WIFI, connect to the home router
while (WiFi.status() != WL_CONNECTED)// Wait for connection
{
delay(500);
Serial.print(".");
}
Serial.printf(“Connected”);
WiFi.config(ipCliente, ipServidor, Subnet);// Set the local IP and server IP for WIFI connection, and the network mask
Udp.begin(localPort);// Open port
}
void loop() {
int Shu = Udp.parsePacket();// Size of the received data packet
if (Shu)// Check if there is data
{
Serial.printf(“Received %d bytes from %s, port %d\n”, Shu, Udp.remoteIP().toString().c_str(), Udp.remotePort());// Serial output received data quantity, sending client’s IP and port
Udp.read(Rdata, 255);// Read the received data
Serial.printf(Rdata);// Serial output received data
}
if (Serial.available())// Check if there is data on the serial port
{
readUART=Serial.readString();// Read the string sent from the serial port
Udp.beginPacket(ipServidor,localPort);// Prepare to send data packet, set the corresponding IP address and port of the client
Udp.print(readUART);// Send the data to the client
Udp.endPacket();// End sending
readUART = "";// Clear the received characters
}
}