Bellafaire / Android-Companion-App-For-BLE-Devices

Provides an open source app which can send phone notifications and control phone functions over BLE
MIT License
43 stars 7 forks source link

Issue on uploading to ESP32 #3

Closed SpYDeR2107 closed 3 years ago

SpYDeR2107 commented 3 years ago

Hi Bellafaire! I am facing an issue uploading the sketch to ESP32. When I upload the sketch named "ESP32_BLE_Example_Code_V2" on ESP32 it doesn't show anything on serial monitor and if I upload BLEServer it shows an error in compiling. So kindly help me and please tell me what should I do. Thanks

Xasin commented 3 years ago

If you could post the error you get when compiling, that could be a lot of help! You can probably just take the last few lines of the build output and post them here directly, if they are not too long, or you could use a gist and post the entire build log.

Looking at the source files, it seems like BLEServer.ino should actually be called BLEServer.cpp, since it provides the actual implementations of the functions that ESP32_BLE_Example_Code_V2.ino references - so it's actually one single project, not two separate sketches.

SpYDeR2107 commented 3 years ago

When I upload ESP32_BLE_Example_Code_V2.ino nothing happens in serial monitor not getting Bluetooth initialising message and when try to compile BLEServer.ino go the below error. C:\Users\SpYDeR\AppData\Local\Temp\arduino_cache_751322\core\core_019ba2f18f0b849aa418375be6603a20.a(main.cpp.o):(.literal._Z8loopTaskPv+0x8): undefined reference to setup()' C:\Users\SpYDeR\AppData\Local\Temp\arduino_cache_751322\core\core_019ba2f18f0b849aa418375be6603a20.a(main.cpp.o):(.literal._Z8loopTaskPv+0xc): undefined reference toloop()' C:\Users\SpYDeR\AppData\Local\Temp\arduino_cache_751322\core\core_019ba2f18f0b849aa418375be6603a20.a(main.cpp.o): In function loopTask(void*)': C:\Users\SpYDeR\AppData\Local\Arduino15\packages\esp32\hardware\esp32\1.0.6\cores\esp32/main.cpp:18: undefined reference tosetup()' C:\Users\SpYDeR\AppData\Local\Arduino15\packages\esp32\hardware\esp32\1.0.6\cores\esp32/main.cpp:21: undefined reference to `loop()' collect2.exe: error: ld returned 1 exit status exit status 1 Error compiling for board ESP32 Dev Module. If possible kindly help me step by step make the code run successfully

Xasin commented 3 years ago

Yes, looks like the File BLEServer.ino is not supposed to be compiled on its own, as it does not define the void setup() or void loop() function that Arduino expects.

Try renaming it to BLEServer.cpp and compiling the ESP32_BLE_Example_Code_V2.ino again.

SpYDeR2107 commented 3 years ago

I renamed the BLEServer.ino to BLEServer.cpp and recieved the error on compiling ESP32_BLE_Example_Code_V2.ino

BLEServer.cpp:45:8: error: 'String' does not name a type static String currentDataField; ^ BLEServer.cpp:46:8: error: 'boolean' does not name a type static boolean blockingCommandInProgress = false; ^ BLEServer.cpp:47:8: error: 'String' does not name a type static String* bleReturnString; ^ BLEServer.cpp:56:8: error: 'boolean' does not name a type static boolean connected = false; ^ BLEServer.cpp:59:8: error: 'boolean' does not name a type static boolean operationInProgress = false; ^ BLEServer.cpp:61:14: error: variable or field 'addData' declared void void addData(String data) { ^ BLEServer.cpp:61:14: error: 'String' was not declared in this scope exit status 1 'String' does not name a type

Bellafaire commented 3 years ago

Both files need to have the .ino extension and as xasin said they must be compiled together. The example sketch will hang until the android app connects. If the connection is established you will see a persistant notification on your android device that will indicate the BLE connection is working. If it's not working then try force-stopping the android app and opening it back up again. The app only needs to be opened during the initial connection, after that as long as the persistent notification is present the phone will auto-reconnect to the ESP32 whenever it's active.

SpYDeR2107 commented 3 years ago

Hey Bellafaire, Could you please help me.I want an android studio native app that has a button to on/off esp32 on board led for my final year project and a textview that displays value send by ESP32 serial monitor using BLE.I found nothing all over the internet even I tried freelancing services but nothing works.Please help

Xasin commented 3 years ago

If I may - doing something like this should not be too hard :)

I recommend using Bellafaire's work here as basis, and then tweaking it a bit to fit your requirements. It should be easy enough to add a new BLE characteristic that acts as on/off boolean, which the app can toggle.

I also had a lot of success using Qt for this sort of stuff. It's not a truly native app, so it's not optimal, but if you are more comfortable in C++ and know the Qt framework it might be worth considering.

Other than that, I think it's a bit much to ask for help for the entire app for this. Documentation on BLE, Android app writing, and the ESP32 is all very rich and mature. There might not be a single tutorial for your exact needs, but there are plenty of tutorials on all the individual steps :)

Lastly, the ESP serial monitor redirection is tricky. I recommend using the function esp_log_set_vprintf to change the ESP_LOGx output direction. This probably doesn't change the serial monitor output, but you can use the ESP_LOGx macros to send messages to the BLE output that way, and it works quite well.

Bellafaire commented 3 years ago

I see where you're coming from, BLE isn't very beginner friendly when it comes to trying to get basic communication established. I have given thought to creating some tutorials just because of how difficult I remember it being when trying to learn to make things work. There's a lot involved in handling Bluetooth communication on android, I have used flutter in the past to make a cross-platform app that used Bluetooth through flutter_blue and overall it was much easier (and much smaller in terms of code I had to write).

Overall the tutorials for BLE generally breeze over a lot of the details which means when something goes wrong you're on your own in a pretty confusing mess. Best I could do is recommend you towards a repo other than this one, there's a lot going on in the background of this app that doesn't necessarily have to do with Bluetooth. Additionally there are a few non-standard things that I'm doing here purely for convenience in the intended application.

I do have another repo however that's more like what you're looking for https://github.com/Bellafaire/Banshee-Antweight-Battlebot here you'll find the source for an esp32-based battle Bot I've been working on as a pet project that is controlled by an android phone. It's comparatively simple and should be an easier starting point for you. Also included is the ESP32 code, all it does is send data from two joysticks and a slider plus read some data back from the ESP32.

Xasin commented 3 years ago

Also, BTW, if it does not need to be low power, I would highly advise looking into standard BT. The protocol is much easier if you just use BT SPP and wrap your own data packets, although the power drain is more.

I can't compare the native Android BLE code with Qt, but I know that the Qt BLE module was pretty easy to use - you just connect, search the GATT structure, then fetch the characteristic you want to interact with.

You could also just go for something WiFi-based, using MQTT or maybe an ESP Mesh? If you aren't comfortable with learning a complex/weird protocol like BLE for this, maybe switching to an alternative could be very worthwhile.

Also, if you intend to write your own BLE code, do not use the standard ESP Bluedroid, it's atrociously written. nimBLE is muuuuch more comfortable when setting up the GATT structure.