New entry in the series of posts aimed at exploring ways of communication between an ESP32 and a NET6 application written in C#. This time we’ll look at communication via WebSockets.
Previously, we have already seen communication through a serial port and communication via HTTP requests. We continue today’s post by looking at communication via WebSockets.
NET6 is cross-platform and available on computers with Windows, Android, Linux, or Mac, as well as on x86/x64 and ARM7 or higher architectures. This means we can run the same program on different devices, such as desktop computers with Windows or Linux, Android mobile phones, or Raspberry Pi.
As mentioned in the previous post, and it’s not the first time on the blog, although we see one communication method after another, it does not mean that WebSocket communication is “better” than HTTP request communication. Each communication has its advantages and disadvantages.
Specifically, WebSocket communication is interesting when fully bidirectional communication between client and server is needed, and the required data transfer speed is high.
However, it’s not all advantages. It also has the downside of requiring many resources. Furthermore, it consumes a large number of ports on the server machine, forcing more complex system solutions. Therefore, it may not be advisable if we have many clients making occasional connections.
Once clarified, let’s get to it!
ESP32 Code
As in the rest of the entries in the series, I will use an M5 Stick C as a development device. However, any Wifi device similar to Arduino, such as any model from the ESP32 family, will work just as well for you.
And as usual in our blog posts, for order and cleanliness, we will separate the code by grouping it by functionality. Thus, in the APIWebSocket.hpp file, we have all the logic associated with WebSockets.
void InitWebSockets()
{
webSocket.begin(ApiHost, 81, "/API");
webSocket.onEvent(webSocketEvent);
Serial.println("Connected websockets");
}
void SendA()
{
webSocket.sendTXT("A");
}
void SendB()
{
webSocket.sendTXT("B");
}
As we can see, we only have a function to initialize WebSocket communication and two methods to send ‘A’ and ‘B’. Simple, but sufficient for our example.
On the other hand, our main sketch would look like the following.
void setup()
{
Serial.begin(115200);
WIFI_Connect();
delay(2000);
InitWebSockets();
}
bool isOn = false;
void Update()
{
if (M5.BtnA.wasPressed())
{
if (isOn == false)
{
isOn = true;
Serial.println("A");
SendA();
}
else if (isOn == true)
{
isOn = false;
Serial.println("B");
SendB();
}
}
}
void loop()
{
M5.update();
webSocket.loop();
delay(10);
Update();
}
Where, as we can see, basically in the setup we initialize WiFi and WebSocket communication. On the other hand, in loop() we update the WebSocket communication and call the update() function.
In this update() function, we simply check if the button has been pressed to alternately send ‘A’ or ‘B’. Of course, in a real example, you would put the necessary logic for your project there. For this communication example, our A/B is more than enough.
NET6 Code
On the other hand, for WebSocket communication from NET6, we will use the WebsocketSharp library that we already saw in this post.
As we have done in previous posts, we are going to create an object that simplifies the use of WebSockets in our program. In the case of WebSockets, it is very simple and could be something like the following.
public class ArduinoWebsocket : WebSocketBehavior
{
public static string LastRecieved { get; private set; } = "";
public static event EventHandler DataArrived;
protected override void OnMessage(MessageEventArgs e)
{
LastRecieved = e.Data;
DataArrived?.Invoke(this, new EventArgs());
}
}
With this object, establishing WebSocket connection from NET6 to the ESP32 would be as simple as the following.
InitWebSocketServer();
Console.ReadLine();
void InitWebSocketServer()
{
var web = new WebSocketServer(81);
web.AddWebSocketService<ArduinoWebsocket>("/API");
ArduinoWebsocket.DataArrived += DataArrived;
web.Start();
}
void DataArrived(object sender, EventArgs e)
{
if (ArduinoWebsocket.LastRecieved == "A")
{
Console.WriteLine("A");
}
else if (ArduinoWebsocket.LastRecieved == "B")
{
Console.WriteLine("B");
}
}
That easy! We really see that WebSocket communication, from a code perspective, is not complicated at all. In fact, it is very simple to connect an ESP32 with a NET6 application in C# via WebSockets. In the next post, we will see communication via MQTT. Until next time!
Download the Code
All the code from this post is available for download on Github.

