From 8c61d1f4dc71c4b0bd35dd3ec810796e5ea5b8cc Mon Sep 17 00:00:00 2001 From: Amber Elferink Date: Tue, 18 Jan 2022 15:57:52 +0100 Subject: [PATCH 1/7] fixed linker errors and updated Readme with steps to build. added more links in readme --- README.md | 11 +++++++++++ .../WindowsBtWithEsp32/WindowsBtWithEsp32.cpp | 3 +++ 2 files changed, 14 insertions(+) diff --git a/README.md b/README.md index 314bf06..df641c8 100644 --- a/README.md +++ b/README.md @@ -2,6 +2,17 @@ This repository is a sample on how to connect from a Windows 10 PC to an ESP32 via bluetooth and windows sockets. You should pair your PC with the ESP32 first. The console application will loop through the BT devices connected to the PC and find the ESP32, connect to it, send a message, and recieve a message. The Arduino.ino file should be loaded onto the ESP32. The Visual Studio project should be built (I used Visual Studio 2019). The actual source code is just contained in the .ino file and single .cpp file. The code should be simple to follow, unlike most of the examples and documentation I came across. +# Steps to build: +- Download the zip with the code (or clone it with git). +- Download Visual Studio Community (tested with 2019). https://visualstudio.microsoft.com/downloads/ +- During that installation, you only need the .NET desktop development kit. +- Download the Arduino IDE. https://www.arduino.cc/en/software +- Open the Arduino.ino file, and click Upload with your ESP32 plugged in. (Make sure you installed [ESP32 libraries first](https://randomnerdtutorials.com/installing-the-esp32-board-in-arduino-ide-windows-instructions/). + +- Open the .sln file in WindowsBTWithEsp32 with Visual Studio Community. +- Click play (Local Windows Debugger). +- You should now receive messages via bluetooth in the terminal that opened: "Message from ESP32". + ## References diff --git a/WindowsBtWithEsp32/WindowsBtWithEsp32/WindowsBtWithEsp32.cpp b/WindowsBtWithEsp32/WindowsBtWithEsp32/WindowsBtWithEsp32.cpp index d4b8317..7fc031a 100644 --- a/WindowsBtWithEsp32/WindowsBtWithEsp32/WindowsBtWithEsp32.cpp +++ b/WindowsBtWithEsp32/WindowsBtWithEsp32/WindowsBtWithEsp32.cpp @@ -6,6 +6,8 @@ Microsoft example - https://github.com/microsoftarchive/msdn-code-gallery-micros Bluetooth programming with Windows sockets - https://docs.microsoft.com/en-us/windows/win32/bluetooth/bluetooth-programming-with-windows-sockets **/ +#pragma comment(lib, "Bthprops.lib") +#pragma comment(lib, "Ws2_32.lib") #include #include @@ -139,6 +141,7 @@ bool recieveMessageFromEsp32() printf("%c", recievedMessage[i]); } printf("\r\n"); + sendMessageToEsp32(); } } From 380a2b0a74de70ff32fb09afe506ef93a5e1c849 Mon Sep 17 00:00:00 2001 From: AmberElferink <32518317+AmberElferink@users.noreply.github.com> Date: Tue, 18 Jan 2022 16:23:08 +0100 Subject: [PATCH 2/7] Updated Readme --- README.md | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index df641c8..58d5c62 100644 --- a/README.md +++ b/README.md @@ -4,10 +4,11 @@ This repository is a sample on how to connect from a Windows 10 PC to an ESP32 v # Steps to build: - Download the zip with the code (or clone it with git). -- Download Visual Studio Community (tested with 2019). https://visualstudio.microsoft.com/downloads/ +- Download [Visual Studio Community] (https://visualstudio.microsoft.com/downloads/) (tested with 2019). - During that installation, you only need the .NET desktop development kit. -- Download the Arduino IDE. https://www.arduino.cc/en/software +- Download the [Arduino IDE](https://www.arduino.cc/en/software). - Open the Arduino.ino file, and click Upload with your ESP32 plugged in. (Make sure you installed [ESP32 libraries first](https://randomnerdtutorials.com/installing-the-esp32-board-in-arduino-ide-windows-instructions/). +- Open the Bluetooth menu on your PC (you should have a dongle or Bluetooth integrated). Connect to Esp32Test. - Open the .sln file in WindowsBTWithEsp32 with Visual Studio Community. - Click play (Local Windows Debugger). @@ -19,4 +20,4 @@ This repository is a sample on how to connect from a Windows 10 PC to an ESP32 v * Helpful examples on getting connected devices - https://www.winsocketdotnetworkprogramming.com/winsock2programming/winsock2advancedotherprotocol4k.html * Bluetooth APIs - https://docs.microsoft.com/en-us/windows/win32/api/bluetoothapis/ * Microsoft example - https://github.com/microsoftarchive/msdn-code-gallery-microsoft/blob/master/Official%20Windows%20Platform%20Sample/Bluetooth%20connection%20sample/%5BC%2B%2B%5D-Bluetooth%20connection%20sample/C%2B%2B/bthcxn.cpp -* Bluetooth programming with Windows sockets - https://docs.microsoft.com/en-us/windows/win32/bluetooth/bluetooth-programming-with-windows-sockets \ No newline at end of file +* Bluetooth programming with Windows sockets - https://docs.microsoft.com/en-us/windows/win32/bluetooth/bluetooth-programming-with-windows-sockets From 02b36d35a055914070c08d53418dc73310331a9e Mon Sep 17 00:00:00 2001 From: AmberElferink <32518317+AmberElferink@users.noreply.github.com> Date: Tue, 18 Jan 2022 16:25:02 +0100 Subject: [PATCH 3/7] Readme changed packages to install --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 58d5c62..594292d 100644 --- a/README.md +++ b/README.md @@ -5,7 +5,7 @@ This repository is a sample on how to connect from a Windows 10 PC to an ESP32 v # Steps to build: - Download the zip with the code (or clone it with git). - Download [Visual Studio Community] (https://visualstudio.microsoft.com/downloads/) (tested with 2019). -- During that installation, you only need the .NET desktop development kit. +- During that installation, you only need the Desktop development with C++. - Download the [Arduino IDE](https://www.arduino.cc/en/software). - Open the Arduino.ino file, and click Upload with your ESP32 plugged in. (Make sure you installed [ESP32 libraries first](https://randomnerdtutorials.com/installing-the-esp32-board-in-arduino-ide-windows-instructions/). - Open the Bluetooth menu on your PC (you should have a dongle or Bluetooth integrated). Connect to Esp32Test. From 89a61a62eb20f8198994e7c7f96e5adb4b509493 Mon Sep 17 00:00:00 2001 From: Amber Elferink Date: Tue, 18 Jan 2022 17:09:01 +0100 Subject: [PATCH 4/7] rewrote receive message to work with strings --- .../WindowsBtWithEsp32/WindowsBtWithEsp32.cpp | 41 ++++++++++++------- 1 file changed, 26 insertions(+), 15 deletions(-) diff --git a/WindowsBtWithEsp32/WindowsBtWithEsp32/WindowsBtWithEsp32.cpp b/WindowsBtWithEsp32/WindowsBtWithEsp32/WindowsBtWithEsp32.cpp index 7fc031a..8f360e3 100644 --- a/WindowsBtWithEsp32/WindowsBtWithEsp32/WindowsBtWithEsp32.cpp +++ b/WindowsBtWithEsp32/WindowsBtWithEsp32/WindowsBtWithEsp32.cpp @@ -14,6 +14,8 @@ Bluetooth programming with Windows sockets - https://docs.microsoft.com/en-us/wi #include #include #include +#include +#include BTH_ADDR esp32BtAddress; SOCKADDR_BTH btSocketAddress; @@ -111,7 +113,7 @@ bool connectToEsp32() bool sendMessageToEsp32() { - const char* message = "Message from Windows\r\n"; + const char* message = "\n"; int sendResult = send(btClientSocket, message, (int)strlen(message), 0); //send your message to the BT device if (sendResult == SOCKET_ERROR) { @@ -128,20 +130,29 @@ bool recieveMessageFromEsp32() printf("Waiting to recieve a message\r\n"); while (true) { - char recievedMessage[512]; //make sure buffer is big enough for any messages your receiving - int recievedMessageLength = 512; - int recieveResult = recv(btClientSocket, recievedMessage, recievedMessageLength, 0); //if your socket is blocking, this will block until a - if (recieveResult < 0) //a message is recieved. If not, it will return right - { //away - continue; - } - printf("Message recieved - \r\n"); - for (int i = 0; i < recieveResult; i++) - { - printf("%c", recievedMessage[i]); - } - printf("\r\n"); - sendMessageToEsp32(); + char buffer[50000]; + int nrRecievedBytes = recv(btClientSocket, buffer, sizeof(buffer), 0); //if your socket is blocking, this will block until a + if (nrRecievedBytes < 0) { continue; } //a message is recieved. If not, it will return right away + std::string message(buffer, nrRecievedBytes); + std::cout << "Message recieved: " << message << std::endl; + + sendMessageToEsp32(); // send message to unblock the ESP32 from waiting on a message from the PC. + } +} + +bool countReceivedValidMessage() +{ + printf("Waiting to recieve a message\r\n"); + + while (true) + { + char buffer[50000]; + int nrRecievedBytes = recv(btClientSocket, buffer, sizeof(buffer), 0); //if your socket is blocking, this will block until a + if (nrRecievedBytes < 0) {continue;} //a message is recieved. If not, it will return right away + std::string message(buffer, nrRecievedBytes); + std::cout << "Message recieved: " << message << std::endl; + + sendMessageToEsp32(); // send message to unblock the ESP32 from waiting on a message from the PC. } } From 42137a39d16ec4d81a1747abb41ab6ea61b07600 Mon Sep 17 00:00:00 2001 From: Amber Elferink Date: Tue, 18 Jan 2022 17:30:17 +0100 Subject: [PATCH 5/7] rewrote to buffers with strings --- .../WindowsBtWithEsp32/WindowsBtWithEsp32.cpp | 45 ++++++++++-------- firmware/Bluetooth_serial_test/.gitignore | 5 ++ .../.vscode/extensions.json | 7 +++ firmware/Bluetooth_serial_test/include/README | 39 ++++++++++++++++ firmware/Bluetooth_serial_test/lib/README | 46 +++++++++++++++++++ firmware/Bluetooth_serial_test/platformio.ini | 14 ++++++ firmware/Bluetooth_serial_test/src/main.cpp | 25 ++++++++++ firmware/Bluetooth_serial_test/test/README | 11 +++++ 8 files changed, 174 insertions(+), 18 deletions(-) create mode 100644 firmware/Bluetooth_serial_test/.gitignore create mode 100644 firmware/Bluetooth_serial_test/.vscode/extensions.json create mode 100644 firmware/Bluetooth_serial_test/include/README create mode 100644 firmware/Bluetooth_serial_test/lib/README create mode 100644 firmware/Bluetooth_serial_test/platformio.ini create mode 100644 firmware/Bluetooth_serial_test/src/main.cpp create mode 100644 firmware/Bluetooth_serial_test/test/README diff --git a/WindowsBtWithEsp32/WindowsBtWithEsp32/WindowsBtWithEsp32.cpp b/WindowsBtWithEsp32/WindowsBtWithEsp32/WindowsBtWithEsp32.cpp index 8f360e3..abdb821 100644 --- a/WindowsBtWithEsp32/WindowsBtWithEsp32/WindowsBtWithEsp32.cpp +++ b/WindowsBtWithEsp32/WindowsBtWithEsp32/WindowsBtWithEsp32.cpp @@ -111,10 +111,9 @@ bool connectToEsp32() return true; } -bool sendMessageToEsp32() +bool sendMessageToEsp32(std::string message) { - const char* message = "\n"; - int sendResult = send(btClientSocket, message, (int)strlen(message), 0); //send your message to the BT device + int sendResult = send(btClientSocket, message.c_str(), sizeof(message.c_str()), 0); //send your message to the BT device if (sendResult == SOCKET_ERROR) { wprintf(L"Sending to ESP32 failed. Error code %d\r\n", WSAGetLastError()); @@ -125,19 +124,23 @@ bool sendMessageToEsp32() return true; } -bool recieveMessageFromEsp32() +std::string recieveMessageFromEsp32(std::string send_reply = "none" ) { - printf("Waiting to recieve a message\r\n"); - while (true) - { - char buffer[50000]; - int nrRecievedBytes = recv(btClientSocket, buffer, sizeof(buffer), 0); //if your socket is blocking, this will block until a - if (nrRecievedBytes < 0) { continue; } //a message is recieved. If not, it will return right away + + char* buffer = new char[50000]; + int nrRecievedBytes = recv(btClientSocket, buffer, 50000, 0); //if your socket is blocking, this will block until a + if (nrRecievedBytes < 0) { + delete(buffer); //make sure to clean up things on the heap made with "new" + return ""; + } + std::string message(buffer, nrRecievedBytes); - std::cout << "Message recieved: " << message << std::endl; + std::cout << "Message recieved: " < THIS FILE +| +|- platformio.ini +|--src + |- main.c + +and a contents of `src/main.c`: +``` +#include +#include + +int main (void) +{ + ... +} + +``` + +PlatformIO Library Dependency Finder will find automatically dependent +libraries scanning project source files. + +More information about PlatformIO Library Dependency Finder +- https://docs.platformio.org/page/librarymanager/ldf.html diff --git a/firmware/Bluetooth_serial_test/platformio.ini b/firmware/Bluetooth_serial_test/platformio.ini new file mode 100644 index 0000000..2fedd0b --- /dev/null +++ b/firmware/Bluetooth_serial_test/platformio.ini @@ -0,0 +1,14 @@ +; PlatformIO Project Configuration File +; +; Build options: build flags, source filter +; Upload options: custom upload port, speed and extra flags +; Library options: dependencies, extra library storages +; Advanced options: extra scripting +; +; Please visit documentation for the other options and examples +; https://docs.platformio.org/page/projectconf.html + +[env:featheresp32] +platform = espressif32 +board = featheresp32 +framework = arduino diff --git a/firmware/Bluetooth_serial_test/src/main.cpp b/firmware/Bluetooth_serial_test/src/main.cpp new file mode 100644 index 0000000..50ef7d8 --- /dev/null +++ b/firmware/Bluetooth_serial_test/src/main.cpp @@ -0,0 +1,25 @@ +#include +#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() { + // put your setup code here, to run once: + Serial.begin(9600); + SerialBT.begin("ESP32test"); //Bluetooth device name + Serial.println("The device started, now you can pair it with bluetooth!"); +} + +void loop() { + if (SerialBT.available()) + { + String message = SerialBT.readStringUntil('\n'); + Serial.println(message); + SerialBT.println("<970,400,500-500,500,100-1,0>"); + } + delay(10); +} \ No newline at end of file diff --git a/firmware/Bluetooth_serial_test/test/README b/firmware/Bluetooth_serial_test/test/README new file mode 100644 index 0000000..b94d089 --- /dev/null +++ b/firmware/Bluetooth_serial_test/test/README @@ -0,0 +1,11 @@ + +This directory is intended for PlatformIO Unit Testing and project tests. + +Unit Testing is a software testing method by which individual units of +source code, sets of one or more MCU program modules together with associated +control data, usage procedures, and operating procedures, are tested to +determine whether they are fit for use. Unit testing finds problems early +in the development cycle. + +More information about PlatformIO Unit Testing: +- https://docs.platformio.org/page/plus/unit-testing.html From 5319aca0fd830ef52dfca8e8621e8499440f1f59 Mon Sep 17 00:00:00 2001 From: Amber Elferink Date: Tue, 18 Jan 2022 17:58:43 +0100 Subject: [PATCH 6/7] rewrote to fast string message implementations --- .../WindowsBtWithEsp32/WindowsBtWithEsp32.cpp | 68 ++++++++++++------- 1 file changed, 42 insertions(+), 26 deletions(-) diff --git a/WindowsBtWithEsp32/WindowsBtWithEsp32/WindowsBtWithEsp32.cpp b/WindowsBtWithEsp32/WindowsBtWithEsp32/WindowsBtWithEsp32.cpp index abdb821..6911d47 100644 --- a/WindowsBtWithEsp32/WindowsBtWithEsp32/WindowsBtWithEsp32.cpp +++ b/WindowsBtWithEsp32/WindowsBtWithEsp32/WindowsBtWithEsp32.cpp @@ -16,6 +16,7 @@ Bluetooth programming with Windows sockets - https://docs.microsoft.com/en-us/wi #include #include #include +#include BTH_ADDR esp32BtAddress; SOCKADDR_BTH btSocketAddress; @@ -111,9 +112,9 @@ bool connectToEsp32() return true; } -bool sendMessageToEsp32(std::string message) +bool sendMessageToEsp32(const char* message) { - int sendResult = send(btClientSocket, message.c_str(), sizeof(message.c_str()), 0); //send your message to the BT device + int sendResult = send(btClientSocket, message, (int)strlen(message), 0); //send your message to the BT device if (sendResult == SOCKET_ERROR) { wprintf(L"Sending to ESP32 failed. Error code %d\r\n", WSAGetLastError()); @@ -124,41 +125,56 @@ bool sendMessageToEsp32(std::string message) return true; } -std::string recieveMessageFromEsp32(std::string send_reply = "none" ) +bool sendMessageToEsp32(std::string message) +{ + const char* msg = message.c_str(); + std::cout << strlen(msg); + return sendMessageToEsp32(msg); + +} + + + + +bool sendMessageToEsp32() { + const char* message = "\n"; + int sendResult = send(btClientSocket, message, (int)strlen(message), 0); //send your message to the BT device + if (sendResult == SOCKET_ERROR) + { + wprintf(L"Sending to ESP32 failed. Error code %d\r\n", WSAGetLastError()); + closesocket(btClientSocket); + WSACleanup(); + return false; + } + return true; +} +//receive a message and send a message back. +//to not send a message back use send_reply="-". (This will block the esp32 if it expects a message if you don't send anywhere else!) +//always terminate the reply with \n. \n will send the minimum info to not block the esp32. +std::string recieveStringMessageFromEsp32(std::string send_reply = "\n") +{ char* buffer = new char[50000]; - int nrRecievedBytes = recv(btClientSocket, buffer, 50000, 0); //if your socket is blocking, this will block until a - if (nrRecievedBytes < 0) { - delete(buffer); //make sure to clean up things on the heap made with "new" + int nrReceivedBytes = recv(btClientSocket, buffer, 50000, 0); //if your socket is blocking, this will block until a message comes in + if (nrReceivedBytes < 0) { + delete[] buffer; //make sure to clean up things on the heap made with "new" return ""; } - std::string message(buffer, nrRecievedBytes); + std::string message(buffer, nrReceivedBytes); std::cout << "Message recieved: " < Date: Tue, 18 Jan 2022 18:57:47 +0100 Subject: [PATCH 7/7] benchmark code written --- .../WindowsBtWithEsp32/WindowsBtWithEsp32.cpp | 87 ++++++++++++++----- firmware/Bluetooth_serial_test/src/main.cpp | 2 +- 2 files changed, 65 insertions(+), 24 deletions(-) diff --git a/WindowsBtWithEsp32/WindowsBtWithEsp32/WindowsBtWithEsp32.cpp b/WindowsBtWithEsp32/WindowsBtWithEsp32/WindowsBtWithEsp32.cpp index 6911d47..6b607e1 100644 --- a/WindowsBtWithEsp32/WindowsBtWithEsp32/WindowsBtWithEsp32.cpp +++ b/WindowsBtWithEsp32/WindowsBtWithEsp32/WindowsBtWithEsp32.cpp @@ -17,6 +17,7 @@ Bluetooth programming with Windows sockets - https://docs.microsoft.com/en-us/wi #include #include #include +#include BTH_ADDR esp32BtAddress; SOCKADDR_BTH btSocketAddress; @@ -128,32 +129,15 @@ bool sendMessageToEsp32(const char* message) bool sendMessageToEsp32(std::string message) { const char* msg = message.c_str(); - std::cout << strlen(msg); return sendMessageToEsp32(msg); } - - -bool sendMessageToEsp32() -{ - const char* message = "\n"; - int sendResult = send(btClientSocket, message, (int)strlen(message), 0); //send your message to the BT device - if (sendResult == SOCKET_ERROR) - { - wprintf(L"Sending to ESP32 failed. Error code %d\r\n", WSAGetLastError()); - closesocket(btClientSocket); - WSACleanup(); - return false; - } - return true; -} - //receive a message and send a message back. //to not send a message back use send_reply="-". (This will block the esp32 if it expects a message if you don't send anywhere else!) //always terminate the reply with \n. \n will send the minimum info to not block the esp32. -std::string recieveStringMessageFromEsp32(std::string send_reply = "\n") +std::string recieveStringMessageFromEsp32(std::string send_reply = "\n", bool debug = true) { char* buffer = new char[50000]; int nrReceivedBytes = recv(btClientSocket, buffer, 50000, 0); //if your socket is blocking, this will block until a message comes in @@ -163,7 +147,9 @@ std::string recieveStringMessageFromEsp32(std::string send_reply = "\n") } std::string message(buffer, nrReceivedBytes); - std::cout << "Message recieved: " < measured. +//you can give up an expected message to see if your data is validated. MAKE SURE TO MATCH THE WHITESPACE (so terminate with \r\n). +void benchmarkMessagesPerSecond(std::string expectedMessage ="-", float measureXseconds = 10.0f ) +{ + clock_t start, time = 0; + start = clock(); + + int validMessagesCounter = 0; + int invalidMessagesCounter = 0; + + std::cout << "Benchmark started, wait " << measureXseconds << " seconds" << std::endl; + + if (expectedMessage != "-") + { + while (time < measureXseconds) + { + std::string msg = recieveStringMessageFromEsp32("\n", false); + + if (msg == "") + { + // do nothing, no message was received in the time period + } + else if (msg == expectedMessage) + { + validMessagesCounter++; + } + else + { + invalidMessagesCounter++; + } + time = (clock() - start) / CLOCKS_PER_SEC; + } + } + else + { + while (time < measureXseconds) + { + std::string msg = recieveStringMessageFromEsp32("\n", false); + if (msg == "") + { + // do nothing, no message was received in the time period + } + else + { + validMessagesCounter++; + } + time = (clock() - start) / CLOCKS_PER_SEC; + } + } + std::cout << "Benchmark completed" << std::endl; + std::cout << "Measured for " << time << " seconds" << std::endl; + std::cout << "Received " << validMessagesCounter << " valid messages and " << invalidMessagesCounter << " invalid messages" << std::endl; + std::cout << "Total: " << validMessagesCounter / time << " messages per second." << std::endl; +} @@ -196,10 +236,11 @@ int main() return 0; } std::cout <<"Waiting to recieve a message\r\n"; - while (true) - { - recieveStringMessageFromEsp32(); //receive messages from ESP32 - } + //while (true) + //{ + // recieveStringMessageFromEsp32(); //receive messages from ESP32 + //} + benchmarkMessagesPerSecond("<970,400,500-500,500,100-1,0>\r\n", 10); return 0; } \ No newline at end of file diff --git a/firmware/Bluetooth_serial_test/src/main.cpp b/firmware/Bluetooth_serial_test/src/main.cpp index 50ef7d8..b9ca389 100644 --- a/firmware/Bluetooth_serial_test/src/main.cpp +++ b/firmware/Bluetooth_serial_test/src/main.cpp @@ -21,5 +21,5 @@ void loop() { Serial.println(message); SerialBT.println("<970,400,500-500,500,100-1,0>"); } - delay(10); + // delay is only required if you do non-blocking or one way messages only. } \ No newline at end of file