How to write a C# program to communicate with an ESP32 development board via Bluetooth Serial

Coupled with Bluetooth Serial and GPIO pins, ESP32 can augment a Windows machine with the ability to read from sensors.

So with an ESP32 development board, you can turn your old Windows machine into an IOT gateway that can sense its operating environment.

Given these points, let us look at how we can write a C# program to communicate with an ESP32 development board via Bluetooth Serial.

Getting your ESP32 development board to communicate with another device over Bluetooth Serial

In order to follow this tutorial, upload the following Arduino Sketch onto your ESP32 development board:

#include "BluetoothSerial.h"
   
BluetoothSerial SerialBT;
    
void setup()
{
  SerialBT.begin("ESP32test");
  delay(1000);
}
    
void loop()
{
  String inputFromOtherSide;
  if (SerialBT.available()) {
    inputFromOtherSide = SerialBT.readString();
    SerialBT.println("You had entered: ");
    SerialBT.println(inputFromOtherSide);
  }
}

As I had mentioned in the guide on how to get your ESP32 development board to communicate with another device over Bluetooth Serial, this Arduino Sketch will echo back whatever message that it receives from its Bluetooth connection.

Connecting your ESP32 development board via Bluetooth on Windows 10

After you had uploaded the Arduino Sketch onto your ESP development board, place it near to your Windows machine.

Given that, your Windows 10 machine should be able to detect your ESP32 development board as a Bluetooth device.

In order for the C# program to communicate with your ESP32 board, pair your ESP32 board with Windows.

As mentioned in the post, find the COM port that is labelled as Outgoing as its communication Direction.

For example, you should use COM4 in your C# program if you have the following Bluetooth Settings window screen:

Bluetooth settings window on Windows 10 showing COM ports assigned to ESP32test Bluetooth connection

Writing the C# program to communicate with your ESP32 development board via Bluetooth Serial

Once you had established the Bluetooth connection between your ESP32 development board and Windows 10, it is time to write the C# program.

Since Windows had created the COM ports for you, the C# program can use the same libraries that I had mentioned in how to use C# to read sensor data from Arduino or ESPx via serial connection.

With this in mind, the following C# program gets two echo messages from your ESP32 development board and display them onto the console:

using System;
using System.IO.Ports;

namespace Communicate_With_ESP32_via_Bluetooth_Serial
{
    class MainClass
    {
        public static void Main(string[] args)
        {
            try
            {
                SerialPort port = new SerialPort();
                port.BaudRate = 9600;
                port.PortName = "COM4";
                port.Open();

                try
                {
                    port.Write("Hi, I am trying to talk to you.");
                    Console.WriteLine(port.ReadLine());
                    Console.WriteLine(port.ReadLine());

                    port.Write("Why do you have to repeat what I say?");
                    Console.WriteLine(port.ReadLine());
                    Console.WriteLine(port.ReadLine());
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Encountered error while writing to / or reading from serial port");
                    Console.WriteLine(ex.ToString());
                }

            }
            catch (Exception ex)
            {
                Console.WriteLine("Encountered error while opening serial port");
                Console.WriteLine(ex.ToString());
            }
        }
    }
}

When you run the above program, you should get output similar to the following:

Command prompt output for running example program to communicate with ESP32 via Bluetooth Serial

Understanding the C# code for communicating with our program running on the ESP32 development board

First of all, we indicate that we wish to use some classes contained in the System and System.IO.Ports namespaces:

using System;
using System.IO.Ports;

Once we had done so, we define our own namespace and a class with a Main method.

Inside the Main method, we try to create a SerialPort instance to COM4 with a baud rate of 9600. When we had done so, we attempt to open the connection to our serial port:

SerialPort port = new SerialPort();
port.BaudRate = 9600;
port.PortName = "COM4";
port.Open();

After that, we are ready to communicate with the program running on our ESP32 development board:

port.Write("Hi, I am trying to talk to you.");
Console.WriteLine(port.ReadLine());
Console.WriteLine(port.ReadLine());
 
port.Write("Why do you have to repeat what I say?");
Console.WriteLine(port.ReadLine());
Console.WriteLine(port.ReadLine());

As an illustration, our C# program send two lines of string to the ESP32 program using SerialPort.Write.

Since we know that the ESP32 program will return us two lines of string for every line it receives, we use SerialPort.Read twice right after we send one line of string. In addition to that, we directly output what we read with SerialPort.Read to the console.

Further extensions

At this point in time, you should be able to create a C# program that can communicate with an ESP32 program via Bluetooth Serial.

If you have some sensors to play with, you should be able to connect them to your ESP32 board and extend the C# program to get some sensor readings via your ESP32 board.

In case you need it, here some some ESP32 tutorials that you can refer for sensing the real world:

How to write a C# program to communicate with an ESP32 development board via Bluetooth Serial

About Clivant

Clivant a.k.a Chai Heng enjoys composing software and building systems to serve people. He owns techcoil.com and hopes that whatever he had written and built so far had benefited people. All views expressed belongs to him and are not representative of the company that he works/worked for.