How to work with the 5GHz Wi-Fi module of the ESP32

Industrial Communications: 5GHz Wi-Fi Client-Server Example using ESP32 industrial PLC
April 8, 2021 by
How to work with the 5GHz Wi-Fi module of the ESP32
Boot & Work Corp. S.L., Quesada Dani Salvans

Introduction

The devices of the ESP32 Ethernet & WiFi & BLE PLC controller family have an integrated 5GHz Wi-Fi specific chip, called ISM43340. As you may know, the 5GHz Wi-Fi band is an improved version of the previous ones, such as the 2.4GHz band, for example.

The 5GHz technology has some advantages compared to the 2.5GHz band, such as 25 non-overlapping channels, less interference, faster connection speed, and it works with the updated IEEE standards (802.11a, 802.11n, 802.11ac).

There is also a disadvantage: shorter network range. Related to that, in this post you are going to see a couple of examples about how to use the 5GHz of the PLC to act as a Server or a Client, tested with the appropriate Client and Server JavaScript codes. 


Odoo text and image block

Requirements

The key points to consider are as follows: 

  • ESP32 PLC family

  • PC (for loading the programs into the PLC and executing the JavaScript codes)

Related links

Basics of SPIFFS File

system and Industrial PLC (ESP32)

Read >>

How to control an ESP32

industrial controller via Bluetooth

Read >>

Partition Scheme

in ESP32 PLCs

Read >>

How to find

your perfect industrial PLC

Read >>

ESP32 PLC

family products

Read >>

Server.ino - Client.js

In this first example, the PLC for industrial automation will act as the Server and the PC as the Client. 


Server.ino

Before uploading the code, remember to put your Wi-Fi SSID and your Wi-Fi password.

// WiFi5 library example
// by Industrial Shields

#include <WiFi5.h>

const char* ssid = "YOUR-WIFI-SSID-HERE";
const char* key = "YOUR-WIFI-PASSWORD-HERE";
const uint16_t serverPort = 1234;

WiFi5Server server(serverPort);
const int bufferLen = 200;
uint8_t buffer[bufferLen];

////////////////////////////////////////////////////////////////////////////////////////////////////
void setup() {
  Serial.begin(115200UL);

  if (WiFi5.begin(ssid, key) != WL_CONNECTED) {
    Serial.println("WiFi join error");
    while (true);
  }

  Serial.print("Server IP address: ");
  Serial.println(WiFi5.localIP());
    
  server.begin();

  Serial.println("WiFi5 example started");
}

////////////////////////////////////////////////////////////////////////////////////////////////////
void loop() {
  static uint32_t lastCheckTime = millis();
  if (millis() - lastCheckTime > 5000) {
    WiFi5Client client = server.available();
    if (client) {
      int rxLen = client.read(buffer, bufferLen);
      if (rxLen > 0) {
        Serial.print("RX: ");
        Serial.write(buffer, rxLen);

        client.stop();
      }
    }
    lastCheckTime = millis();
  }
}


When you execute the Server program, you are going to see the following prints on the Serial Monitor when the Client is connected (the Server IP address is to know it so this way you can use it in the client.js code to stablish the connection). And the message abcd received from the Client. 

Server IP address: 192.168.1.70
WiFi5 example started
RX: abcd


client.js

Before uploading the code, remember to put the previously seen Server IP address.

#!/usr/bin/env node

const host = process.argv[2] || '192.168.1.70';
const port = Number(process.argv[3] || 1234);

const net = require('net');

const socket = new net.Socket();

socket.on('end', () => {
	console.log('Socket end done');
});

socket.on('error', (err) => {
	console.error(`Socket error: ${err}`);
});

socket.connect(port, host, () => {
	console.log('Socket connected');
	socket.end('abcd');
});


When you execute the client with node .\client.js, you are going to see on the terminal when the client is connected:

Socket connected
Socket end done


Server.js - Client.ino

In this second example, the PC will act as the Server and the PLC as the Client. 


Server.js 

#!/usr/bin/env node

const net = require('net');

const server = net.createServer((socket) => {
	console.log(`client connected: ${socket.remoteAddress}`);

	socket.on('data', (data) => {
		console.log(`data received from ${socket.remoteAddress}: ${data}`);

		if (data.toString('utf-8') == 'get-number') {
			const number = (Date.now() * Math.random()) % 1000;
			socket.write(number.toFixed(0));

		} else {
			socket.write('unknown');
		}
	});

	socket.on('end', () => {
		console.log(`client disconnected: ${socket.remoteAddress}`);
	});

	socket.on('error', (err) => {
		console.error(`client ${socket.remoteAddress} error: ${err.message}`);
	});
});

server.on('error', (err) => {
	console.error(`server error: ${err.message}`);
});

server.listen(1234, () => {
	console.log(`server listening`);
});

When you execute the server with node .\server.js, you are going to see on the terminal when the client is connected (and the client's address):

server listening
client connected: ::ffff:192.168.1.213

client.ino

Before uploading the code, remember to put your Wi-Fi SSID and your Wi-Fi password, as well as the Server IP address. 

#include <WiFi5.h>

const char* ssid = "YOUR-WIFI-SSID-HERE";
const char* key = "YOUR-WIFI-PASSWORD-HERE";
const IPAddress serverIp(192, 168, 1, 213); const uint16_t serverPort = 1234; WiFi5Client client; uint32_t lastSendTime = 0; const int bufferLen = 200; uint8_t buffer[bufferLen]; //////////////////////////////////////////////////////////////////////////////////////////////////// void setup() { Serial.begin(115200UL); switch (WiFi5.begin(ssid, key)) { case WL_NO_SHIELD: Serial.println("WiFi error"); while (true); break; case WL_NO_SSID_AVAIL: Serial.println("No SSID available"); while (true); break; } Serial.println("WiFi5 example started"); } //////////////////////////////////////////////////////////////////////////////////////////////////// void loop() { if (!client.connected()) { Serial.println("Connecting to server..."); if (!client.connect(serverIp, serverPort)) { Serial.println("Failed. Wait some time and retry"); delay(5000); } else { Serial.println("Client connected"); } } else { if (millis() - lastSendTime > 1000) { String msg("get-number"); if (!client.write((uint8_t*) msg.c_str(), msg.length())) { Serial.println("Write failed"); client.stop(); } lastSendTime = millis(); } int rxLen = client.read(buffer, bufferLen); if (rxLen < 0) { Serial.println("Read failed"); client.stop(); } else if (rxLen > 0) { Serial.print("RX: "); Serial.print(rxLen); Serial.print(": "); Serial.write(buffer, rxLen); Serial.println(); } } }


When you execute the client program, you are going to see the following prints on the Serial Monitor when the client is connected:

WiFi5 example started
Connecting to server...
Client connected
Connecting to server...
Failed. Wait some time and retry

Do not worry about the Failed. Wait some time and retry messages as long as you have seen the Client connected message so in this moment the connection between the client and the server have been stablished. 

NOTE: Remember the code upload process with the ESP32 family PLCs. You can check it on the User Guide as well as on the Datasheet of the product. 

​Search in our Blog

How to work with the 5GHz Wi-Fi module of the ESP32
Boot & Work Corp. S.L., Quesada Dani Salvans April 8, 2021
Share this post

Looking for your ideal Programmable Logic Controller?

Take a look at this product comparison with other industrial controllers Arduino-based. 

We are comparing inputs, outputs, communications and other features with the ones of the relevant brands.


Industrial PLC comparison >>>