{"id":1599,"date":"2019-06-05T15:08:43","date_gmt":"2019-06-05T07:08:43","guid":{"rendered":"https:\/\/www.techcoil.com\/blog\/?p=1599"},"modified":"2020-05-12T10:52:21","modified_gmt":"2020-05-12T02:52:21","slug":"how-to-write-a-c-program-to-communicate-with-an-esp32-development-board-via-bluetooth-serial","status":"publish","type":"post","link":"https:\/\/www.techcoil.com\/blog\/how-to-write-a-c-program-to-communicate-with-an-esp32-development-board-via-bluetooth-serial\/","title":{"rendered":"How to write a C# program to communicate with an ESP32 development board via Bluetooth Serial"},"content":{"rendered":"<p>Coupled with Bluetooth Serial and GPIO pins, <a href=\"https:\/\/www.espressif.com\/en\/products\/hardware\/esp32\/overview\" rel=\"noopener\" target=\"_blank\">ESP32<\/a> can augment a <a href=\"https:\/\/www.amazon.com\/s\/ref=as_li_ss_tl?k=Windows+computer&linkCode=ll2&tag=clivsperswebs-20&linkId=34a004d29526957f67ce4990189bae0c&language=en_US\" rel=\"noopener\" target=\"_blank\">Windows machine<\/a> with the ability to read from sensors. <\/p>\n<p>So with an <a href=\"https:\/\/www.amazon.com\/HiLetgo-ESP-WROOM-32-Development-Microcontroller-Integrated\/dp\/B0718T232Z\/ref=as_li_ss_tl?ie=UTF8&linkCode=ll1&tag=clivsperswebs-20&linkId=4f71f09042e940d42496fddf77b2b1e6&language=en_US\" rel=\"noopener\" target=\"_blank\">ESP32 development board<\/a>, you can turn your old Windows machine into an <a href=\"https:\/\/www.techcoil.com\/glossary\/iot-gateway\/\" rel=\"noopener\" target=\"_blank\">IOT gateway<\/a> that can sense its operating environment.<\/p>\n<p>Given these points, let us look at how we can write a C# program to communicate with an ESP32 development board via Bluetooth Serial.<\/p>\n<h2>Getting your ESP32 development board to communicate with another device over Bluetooth Serial<\/h2>\n<p>In order to follow this tutorial, upload the following Arduino Sketch onto your ESP32 development board: <\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\n#include &quot;BluetoothSerial.h&quot;\r\n   \r\nBluetoothSerial SerialBT;\r\n    \r\nvoid setup()\r\n{\r\n  SerialBT.begin(&quot;ESP32test&quot;);\r\n  delay(1000);\r\n}\r\n    \r\nvoid loop()\r\n{\r\n  String inputFromOtherSide;\r\n  if (SerialBT.available()) {\r\n    inputFromOtherSide = SerialBT.readString();\r\n    SerialBT.println(&quot;You had entered: &quot;);\r\n    SerialBT.println(inputFromOtherSide);\r\n  }\r\n}\r\n<\/pre>\n<p>As I had mentioned in the guide on <a href=\"https:\/\/www.techcoil.com\/blog\/how-to-get-your-esp32-development-board-to-communicate-with-another-device-over-bluetooth-serial\/\" rel=\"noopener\" target=\"_blank\">how to get your ESP32 development board to communicate with another device over Bluetooth Serial<\/a>, this Arduino Sketch will echo back whatever message that it receives from its Bluetooth connection.<\/p>\n<h2>Connecting your ESP32 development board via Bluetooth on Windows 10<\/h2>\n<p>After you had uploaded the Arduino Sketch onto your ESP development board, place it near to your Windows machine.<\/p>\n<p>Given that, your Windows 10 machine should be able to detect your ESP32 development board as a Bluetooth device. <\/p>\n<p>In order for the C# program to communicate with your ESP32 board, <a href=\"https:\/\/www.techcoil.com\/blog\/how-to-connect-to-an-esp32-development-board-via-bluetooth-on-windows-10\/\" rel=\"noopener\" target=\"_blank\">pair your ESP32 board with Windows<\/a>.<\/p>\n<p>As mentioned in the post, find the COM port that is labelled as <strong>Outgoing<\/strong> as its communication <strong>Direction<\/strong>. <\/p>\n<p>For example, you should use <strong>COM4<\/strong> in your C# program if you have the following Bluetooth Settings window screen:<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.techcoil.com\/blog\/wp-content\/uploads\/Bluetooth-settings-window-on-Windows-10-showing-COM-ports-assigned-to-ESP32test-Bluetooth-connection.jpg\" alt=\"Bluetooth settings window on Windows 10 showing COM ports assigned to ESP32test Bluetooth connection\" \/><\/p>\n<h2>Writing the C# program to communicate with your ESP32 development board via Bluetooth Serial<\/h2>\n<p>Once you had established the Bluetooth connection between your ESP32 development board and Windows 10, it is time to write the C# program. <\/p>\n<p>Since Windows had created the COM ports for you, the C# program can use the same libraries that I had mentioned in <a href=\"https:\/\/www.techcoil.com\/blog\/how-to-use-c-to-read-sensor-data-from-arduino-or-espx-via-serial-connection\/\" rel=\"noopener\" target=\"_blank\">how to use C# to read sensor data from Arduino or ESPx via serial connection<\/a>.<\/p>\n<p>With this in mind, the following C# program gets two echo messages from your ESP32 development board and display them onto the console:<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nusing System;\r\nusing System.IO.Ports;\r\n\r\nnamespace Communicate_With_ESP32_via_Bluetooth_Serial\r\n{\r\n    class MainClass\r\n    {\r\n        public static void Main(string&#x5B;] args)\r\n        {\r\n            try\r\n            {\r\n                SerialPort port = new SerialPort();\r\n                port.BaudRate = 9600;\r\n                port.PortName = &quot;COM4&quot;;\r\n                port.Open();\r\n\r\n                try\r\n                {\r\n                    port.Write(&quot;Hi, I am trying to talk to you.&quot;);\r\n                    Console.WriteLine(port.ReadLine());\r\n                    Console.WriteLine(port.ReadLine());\r\n\r\n                    port.Write(&quot;Why do you have to repeat what I say?&quot;);\r\n                    Console.WriteLine(port.ReadLine());\r\n                    Console.WriteLine(port.ReadLine());\r\n                }\r\n                catch (Exception ex)\r\n                {\r\n                    Console.WriteLine(&quot;Encountered error while writing to \/ or reading from serial port&quot;);\r\n                    Console.WriteLine(ex.ToString());\r\n                }\r\n\r\n            }\r\n            catch (Exception ex)\r\n            {\r\n                Console.WriteLine(&quot;Encountered error while opening serial port&quot;);\r\n                Console.WriteLine(ex.ToString());\r\n            }\r\n        }\r\n    }\r\n}\r\n<\/pre>\n<p>When you run the above program, you should get output similar to the following:<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.techcoil.com\/blog\/wp-content\/uploads\/Command-prompt-output-for-running-example-program-to-communicate-with-ESP32-via-Bluetooth-Serial.gif\" alt=\"Command prompt output for running example program to communicate with ESP32 via Bluetooth Serial\" \/><\/p>\n<h2>Understanding the C# code for communicating with our program running on the ESP32 development board<\/h2>\n<p>First of all, we indicate that we wish to use some classes contained in the System and System.IO.Ports namespaces:<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nusing System;\r\nusing System.IO.Ports;\r\n<\/pre>\n<p>Once we had done so, we define our own namespace and a class with a Main method.<\/p>\n<p>Inside the Main method, we try to create a <a href=\"https:\/\/docs.microsoft.com\/en-us\/dotnet\/api\/system.io.ports.serialport\" rel=\"noopener\" target=\"_blank\">SerialPort<\/a> instance to COM4 with a baud rate of 9600. When we had done so, we attempt to open the connection to our serial port:<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nSerialPort port = new SerialPort();\r\nport.BaudRate = 9600;\r\nport.PortName = &quot;COM4&quot;;\r\nport.Open();\r\n<\/pre>\n<p>After that, we are ready to communicate with the program running on our ESP32 development board:<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nport.Write(&quot;Hi, I am trying to talk to you.&quot;);\r\nConsole.WriteLine(port.ReadLine());\r\nConsole.WriteLine(port.ReadLine());\r\n \r\nport.Write(&quot;Why do you have to repeat what I say?&quot;);\r\nConsole.WriteLine(port.ReadLine());\r\nConsole.WriteLine(port.ReadLine());\r\n<\/pre>\n<p>As an illustration, our C# program send two lines of string to the ESP32 program using <a href=\"https:\/\/docs.microsoft.com\/en-us\/dotnet\/api\/system.io.ports.serialport.writeline\" rel=\"noopener\" target=\"_blank\"><code>SerialPort.Write<\/code><\/a>.<\/p>\n<p>Since we know that the ESP32 program will return us <strong>two lines<\/strong> of string for every line it receives, we use <a href=\"https:\/\/docs.microsoft.com\/en-us\/dotnet\/api\/system.io.ports.serialport.readline\" rel=\"noopener\" target=\"_blank\"><code>SerialPort.Read<\/code><\/a> <strong>twice<\/strong> <strong>right after<\/strong> we send one line of string. In addition to that, we directly output what we read with <code>SerialPort.Read<\/code> to the console. <\/p>\n<h2>Further extensions<\/h2>\n<p>At this point in time, you should be able to create a C# program that can communicate with an ESP32 program via Bluetooth Serial.<\/p>\n<p>If you have some <a href=\"https:\/\/www.amazon.com\/kuman-K5-USFor-Raspberry-Projects-Tutorials\/dp\/B016D5L5KE\/ref=as_li_ss_tl?ie=UTF8&linkCode=ll1&tag=clivsperswebs-20&linkId=c895ec1a87ab70758a39aece06b8ecc6&language=en_US\" rel=\"noopener\" target=\"_blank\">sensors<\/a> 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.  <\/p>\n<p>In case you need it, here some some ESP32 tutorials that you can refer for sensing the real world:<\/p>\n<ul>\n<li><a href=\"https:\/\/www.techcoil.com\/blog\/how-to-use-a-esp32-development-board-to-read-temperature-and-humidity-from-a-dh11-sensor\/\" rel=\"noopener\" target=\"_blank\">How to use a ESP32 development board to read temperature and humidity from a DH11 sensor<\/a><\/li>\n<li><a href=\"https:\/\/www.techcoil.com\/blog\/how-to-use-a-esp32-development-board-to-read-from-an-adxl345-accelerometer\/\" rel=\"noopener\" target=\"_blank\">How to use a ESP32 development board to read from an ADXL345 accelerometer<\/a><\/li>\n<li><a href=\"https:\/\/www.techcoil.com\/blog\/how-to-use-a-esp32-development-board-to-read-temperature-and-humidity-from-a-dht22-sensor\/\" rel=\"noopener\" target=\"_blank\">How to use a ESP32 development board to read temperature and humidity from a DHT22 sensor<\/a><\/li>\n<\/ul>\n<p><img decoding=\"async\" width=\"600\" height=\"900\" src=\"https:\/\/www.techcoil.com\/blog\/wp-content\/uploads\/How-to-write-a-C-program-to-communicate-with-an-ESP32-development-board-via-Bluetooth-Serial.jpg\" alt=\"How to write a C# program to communicate with an ESP32 development board via Bluetooth Serial\" class=\"aligncenter size-full wp-image-1942\" \/><\/p>\n\n      <ul id=\"social-sharing-buttons-list\">\n        <li class=\"facebook\">\n          <a href=\"https:\/\/www.facebook.com\/sharer\/sharer.php?u=https%3A%2F%2Fwp.me%2Fp245TQ-pN\" target=\"_blank\" role=\"button\" rel=\"nofollow\">\n            <img decoding=\"async\" src=\"\/ph\/img\/3rd-party\/social-icons\/Facebook.png\" alt=\"Facebook icon\"> Share\n          <\/a>\n        <\/li>\n        <li class=\"twitter\">\n          <a href=\"https:\/\/twitter.com\/intent\/tweet?text=&url=https%3A%2F%2Fwp.me%2Fp245TQ-pN&via=Techcoil_com\" target=\"_blank\" role=\"button\" rel=\"nofollow\">\n          <img decoding=\"async\" src=\"\/ph\/img\/3rd-party\/social-icons\/Twitter.png\" alt=\"Twitter icon\"> Tweet\n          <\/a>\n        <\/li>\n        <li class=\"linkedin\">\n          <a href=\"https:\/\/www.linkedin.com\/shareArticle?mini=1&title=&url=https%3A%2F%2Fwp.me%2Fp245TQ-pN&source=https:\/\/www.techcoil.com\" target=\"_blank\" role=\"button\" rel=\"nofollow\">\n          <img decoding=\"async\" src=\"\/ph\/img\/3rd-party\/social-icons\/linkedin.png\" alt=\"Linkedin icon\"> Share\n          <\/a>\n        <\/li>\n        <li class=\"pinterest\">\n          <a href=\"https:\/\/pinterest.com\/pin\/create\/button\/?url=https%3A%2F%2Fwww.techcoil.com%2Fblog%2Fwp-json%2Fwp%2Fv2%2Fposts%2F1599&description=\" class=\"pin-it-button\" target=\"_blank\" role=\"button\" rel=\"nofollow\" count-layout=\"horizontal\">\n          <img decoding=\"async\" src=\"\/ph\/img\/3rd-party\/social-icons\/Pinterest.png\" alt=\"Pinterest icon\"> Save\n          <\/a>\n        <\/li>\n      <\/ul>\n    ","protected":false},"excerpt":{"rendered":"<p>Coupled with Bluetooth Serial and GPIO pins, <a href=\"https:\/\/www.espressif.com\/en\/products\/hardware\/esp32\/overview\" rel=\"noopener\" target=\"_blank\">ESP32<\/a> can augment a <a href=\"https:\/\/www.amazon.com\/s\/ref=as_li_ss_tl?k=Windows+computer&#038;linkCode=ll2&#038;tag=clivsperswebs-20&#038;linkId=34a004d29526957f67ce4990189bae0c&#038;language=en_US\" rel=\"noopener\" target=\"_blank\">Windows machine<\/a> with the ability to read from sensors. <\/p>\n<p>So with an <a href=\"https:\/\/www.amazon.com\/HiLetgo-ESP-WROOM-32-Development-Microcontroller-Integrated\/dp\/B0718T232Z\/ref=as_li_ss_tl?ie=UTF8&#038;linkCode=ll1&#038;tag=clivsperswebs-20&#038;linkId=4f71f09042e940d42496fddf77b2b1e6&#038;language=en_US\" rel=\"noopener\" target=\"_blank\">ESP32 development board<\/a>, you can turn your old Windows machine into an <a href=\"https:\/\/www.techcoil.com\/glossary\/iot-gateway\/\" rel=\"noopener\" target=\"_blank\">IOT gateway<\/a> that can sense its operating environment.<\/p>\n<p>Given these points, let us look at how we can write a C# program to communicate with an ESP32 development board via Bluetooth Serial.<\/p>\n","protected":false},"author":1,"featured_media":1600,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"advanced_seo_description":"","jetpack_seo_html_title":"","jetpack_seo_noindex":false,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"footnotes":""},"categories":[375,4],"tags":[648,651,647,20,630,484,645,646,181,347],"jetpack_featured_media_url":"https:\/\/www.techcoil.com\/blog\/wp-content\/uploads\/Command-prompt-output-for-running-example-program-to-communicate-with-ESP32-via-Bluetooth-Serial.gif","jetpack_shortlink":"https:\/\/wp.me\/p245TQ-pN","jetpack-related-posts":[],"jetpack_likes_enabled":true,"jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/posts\/1599"}],"collection":[{"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/comments?post=1599"}],"version-history":[{"count":0,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/posts\/1599\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/media\/1600"}],"wp:attachment":[{"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/media?parent=1599"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/categories?post=1599"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/tags?post=1599"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}