This guide intended to be for somewhat advanced users of Arduino and Windows 10. We are using ESP32 setup with Arduino IDE in this mentioned way. We need PuTTY as extra software. Unfortunately, ESP32 with Arduino libraries and Windows 10 is far from being perfect. The original target of this guide is to complete these steps :
- ESP32 will remain connected with the Windows 10 PC via USB for code uploading purpose
- ESP32 will pair with the Windows 10 PC via Bluetooth
- We will send message to the ESP32 from Windows 10 PC over Bluetooth
- We will retrive the sent message on Windows 10 PC over Bluetooth
Unfortunately, step 4 is not easy to complete. If you somehow use two Arduino IDEs to send and receive the message then they will become unresponsive. Arduino IDE is too simple to guess any error. Using PuTTY to communicate over the COM port of Windows 10 makes the sending message part easy. Espressif themselves has example code to use the Bluetooth serial :
1 2 3 | # https://github.com/espressif/arduino-esp32/blob/master/libraries/BluetoothSerial/examples/SerialToSerialBT/SerialToSerialBT.ino # |
Below is the code from above link :
---
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | #include "BluetoothSerial.h" #if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BLUEDROID_ENABLED) #error Bluetooth is not enabled! Please run `make menuconfig` to and enable it #endif BluetoothSerial SerialBT; void setup() { Serial.begin(115200); SerialBT.begin("ESP32test"); //Bluetooth device name Serial.println("The device started, now you can pair it with bluetooth!"); } void loop() { if (Serial.available()) { SerialBT.write(Serial.read()); } if (SerialBT.available()) { Serial.write(SerialBT.read()); } delay(20); } |
The BluetoothSerial.h
code uses a FreeRTOS queue. Thus the uxQueueMessagesWaiting
function is called which returns several messages available on the queue. But that had minor glitch :
1 2 3 | # https://github.com/espressif/arduino-esp32/issues/1207 # |
Go ahead and compile the code and upload it to ESP32 using the Arduino IDE. How open complete the pairing from Windows 10 PC. By going to the Bluetooth settings and Device Manager (on Windows 10 PC) you’ll get the COM port numbers of incoming and outgoing Bluetooth Connection. Usually, COM3 is outgoing and COM5 is incoming. I will completely avoid Arduino IDE, instead, use multiple instances of PuTTY.
To find the Bluetooth COM port number we need to open the Device Manager. Go down the list until you see Ports (COM & LPT). Click “Ports (COM & LPT)” and you will see what ports are in use by Bluetooth.
Now open the first instance of putty. Select Serial in the connection type option, write the correct COM port of USB (COM6 in our example), select a speed of 115200. Click the Terminal tab option on the left-hand side and select Force On both for Local echo and Local line editing options. Save this setting with some name. Launch the terminal session. You’ll get this kind of output :
1 2 3 4 5 6 7 8 9 10 11 12 | ets Jun 8 2016 00:22:57 rst:0x1 (POWERON_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT) configsip: 0, SPIWP:0xee clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00 mode:DIO, clock div:1 load:0x3fff0018,len:4 load:0x3fff001c,len:1100 load:0x40078000,len:10208 load:0x40080400,len:6460 entry 0x400806a4 The device started, now you can pair it with bluetooth! |
Now open the second instance of putty. Select Serial in the connection type option, write the correct COM port of outgoing Bluetooth (COM3 in our example). You can load the previously saved value or do manually – select a speed of 115200, click the Terminal tab option on the left-hand side and select Force On both for Local echo and Local line editing options. Launch the terminal session. This will be an empty terminal where you can type, hit enter and send a message.
Now open the third instance of putty. Select Serial in the connection type option, write the correct COM port of incoming Bluetooth (COM5 in our example). You can load the previously saved value or do manually – select a speed of 115200, click the Terminal tab option on the left-hand side and select Force On both for Local echo and Local line editing options. Launch the terminal session. This will be an empty terminal where your typed message will arrive over Bluetooth.
I have the below code which turns ON and OFF the on-board blue LED upon pressing 1 and 0 respectively. Also, it echoes a useful message. Usually, peoples use these kinds of codes to control ESP32 with smartphone apps. But, in our case, we are using one Windows 10 PC.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | #include "BluetoothSerial.h" BluetoothSerial ESP_BT; int incoming; int LED_BUILTIN = 2; // on-board LED pin number void callback(esp_spp_cb_event_t event, esp_spp_cb_param_t *param){ if(event == ESP_SPP_SRV_OPEN_EVT){ Serial.println("Client Connected"); } } void setup() { Serial.begin(115200); //serial monitor at 115200 ESP_BT.register_callback(callback); ESP_BT.begin("ESP32"); //name of Bluetooth Serial.println("Bluetooth Device is Ready to Pair"); pinMode (LED_BUILTIN, OUTPUT); } void loop() { if (ESP_BT.available()) // checking if we receive data from Bluetooth { incoming = ESP_BT.read(); // check what we recevived Serial.write(incoming); // print on Bluetooth serial Serial.print("Received:"); Serial.println(incoming); if (incoming == 49) { digitalWrite(LED_BUILTIN, HIGH); ESP_BT.println("LED turned ON"); } if (incoming == 48) { digitalWrite(LED_BUILTIN, LOW); ESP_BT.println("LED turned OFF"); } } delay(20); } |
End-result in my case – no print on PuTTY terminal attached to the incoming Bluetooth port. That is probably out of bug of Arduino libraries. You can install the Android App named “Bluetooth Serial Terminal” (by Kai) to test the code.
I have kept the code on my GitHub repo for your usage and edit.
Tagged With https://thecustomizewindows com/2020/01/send-receive-message-on-pc-over-bluetooth-from-esp32-arduino/ , how to test esp32 bluetooth with pc , esp32 serial print bluetooth receive , esp32 putty , esp32 Bluetooth send and receive message , esp32 Bluetooth pc , esp32 bluetooth data transfer to windows 10 , esp32 bluetooth communicating with windows system , esp32 a2dp source to pc , enable bluetooth COM port windows 10