Getting App Inventor to work with BLE and Seeeduino ESP32C3

To add some more specificity, the .ino displays "BLE peripheral is now active" when energized and, once the .aka connects with the .ino, the serial monitor broadcasts "Connected to central: 5d:a1:b4:07:ee:b6" and repeats the data printout11 times in rapid succession. The last line is "Disconnected from central: 5d:a1:b4:07:ee:b6."

It seems like something in the .ala is starting the .ino just fine but is not seeing the data or is disconnecting before it can get a fix.

By .aka I think you mean .apk :slight_smile:

Shouldn't be that rapid - first data set in 2 seconds and subsequent data sets every 10 seconds. Disconnection from central would occur if you tap the App's disconnect button or something happened on the hardware side - a Bluetooth interruption, power interruption, overheating etc.

The App

Each stage of the process is reported on Screen, so the App should be informing you of what is happening. For example:

The Connect Button:
If the connection is a success, you will see "Connected: Waiting for Data".

If connection fails, you will see "Status: Connection Failed".

There is also an App Inventor Error Block which reports the nature of any App error it catches, for example if the disconnect button was tapped when there wasn't a connection.

I've had an idea - I have updated the App Blocks to save to a log file. Everything that happens on the App side will be saved to the file "/Documents/psychrometer.txt" and you will be able to share that file here. I have tested it, gets as far as finding BLE devices, fails to connect to my device choice (smartwatch) - every message is stored in the file, it's similar to the way the Sketch (.ino) reports to the Arduino Serial Monitor.

Psychrometer.aia (248.7 KB)

If you can't find the log file on your phone, I suggest you install a file manager. I use the free version of File Manager Plus:

...also, on the Sketch (.ino) side of things, ensure your Arduino IDE is at least version v1.8.18. Versions lower than that are not 100% compatible.

Good Day, Chris

My Arduino IDE is version 2.3.2 so i think I am up to date. The latest occurrence was that I ran your modified program and I received a screenshot that, I think, says it would not create the file you were hoping for. The second screenshot is what is displayed for a moment after I try to connect to the chosen signal :Psychrometer."

I am attaching 2 screenshots. A view of the message and the "gray screen of death" I am taken to seconds after pressing the connect button.


What totally confuses me is how your .apk can start the .ino file printing in the serial monitor but, somehow, doesn;t see the stream.

Hi Baran

The App (.aia) had been preset to a default file scope "shared", but maybe that setting has been lost - can you check?

When you first run the App, you should be prompted to allow the App to write to media:

The error about cannot write to file is from the App, reported by the "Error Occurred" Block, and error 2104 is "Cannot write to file".

What make and model is your phone and what Android version is it?

On Screen, the App reminds us to switch on Bluetooth and Location - that has to be done before scanning.

I don't know what to make of the message "Psychrometer keeps stopping" - this pops-up after pressing the connect button? Even though I don't have your ESP setup, I do not get that external message, only the App's own messages. However, in the screenshot, no BLE devices have been found, so you are trying to connect to nothing..........

How are the buttons round in your screenshot? Have you changed anything? Obviously the button shape isn't a problem but if you have made other changes......

Hi, Chris

I am afraid that screenshot about project properties flew over my head like a Canada Goose flying home for the Spring. I am printing out the blocks from your app and the one I posted that "works" without refreshing and see if I might somehow magically combine them into something functional.

I DO have a huge question to ask of which I hope you know the answer. When I have my ESP32C3 breadboard connected to my computer and I have run and loaded the sketch and hit the hard reset button, the serial monitor basically says "I'm ready." When I press the connect button on my app, the serial monitor immediately springs into action printing data and, some seconds later, the app announces that it has connected with the psychrometer. All is well, but . . .If I power up my breadboarded ESP32C3 with a USB power supply (and the serial monitor/computer is not connected) and I hit the hard reset button, the app cannot find the .ino sketch and connect to it. Whether or not the Arduino sketch begins broadcasting seems to depend on whether or not it is connected to the serial monitor. Would you please tell me why this is so and what is causing this (assuming that this is something you are conversant in;). I obviously can't have my laptop connected outside - the idea is wireless communications.

Respectfully - Baran

Hmm - it probably isn't? The ESP collects the data and sends it out. If the App connects correctly, it has access to the data and receives it.

As I mentioned before, the Blocks have been proven in the real world, a version of the project is no doubt in use right now.

So the Object of the exercise is to get the App and the ESP working for you. It's a preliminary stage, if you test the files I send to you and errors occur, just post the errors here for me to see - but don't try to change anything unless I say it must be changed. Once the project is up and running, you have a firm base to work from. You can copy the working project and modify it to do more.

If you look at the .ino code in setup(), the line "while (!Serial);"

while-not-Serial - in other words, wait (forever) for the serial connection. So the subsequent BLE code lines are not reached.

If you are not connecting to the serial monitor, you can comment-out the serial code that sends to the monitor, but really it is best to work with the monitor until you are confident that everything is as required with Bluetooth results.

By the way: The USB power supply should deliver exactly the right volt-amps, you can check that in the Seeeduino ESP32C3 specifications.

What make and model is your phone and what Android version is it?

Hi, Chris

My phone is a cheap Android purchased specifically to work with MIT App Inventor projects. It is a TCL T607DL without any cell service attached. Mind you, everything works on the first .apk file I sent to you except for automatically updating the data when rebroadcast. I can disconnect and reconnect for an update but that is not efficient. Even though they are assembled differently, both apps DO activate the ESP32C3 into broadcasting - in fact, both apps "work" with the .ino called BME SensorObject except that my app doesn't refresh and "your" app doesn't show data.

The .ino will display "BLE peripheral is now active" upon reset and will sit there forever doing nothing . . . UNTIL I press the connect button on either of the .apk apps. At that point, and only then, does the .ino sketch begin printing data to the serial monitor and broadcasting the same.

Thank you, again, for your patience and assistance.

I hope that the similarities in actions combined with the difference in instructions may reveal the issue. My thought, if you believe it is
Connects_Displays_No_Refresh.aia (1.0 MB)
Connects_NoDisplay_Refreshes.aia (246.2 KB)
sound, is to create a new project and, using the backpack, import various parts of each code with modifications towards your sketches refresh rate and my sketches actual (one-shot) connection.

Oh, yes . . . thank you for referring me to the !Serial line. So obvious now.

That's perfect for the job, Android 13 and BLE 5.1. Before you start using the app, make sure Bluetooth and Location are both switched on (when the App becomes more advanced, a lot of that stuff can be automated).

There isn't any need to do that and you could end up with more complications :upside_down_face:

If you can get the log file to work, that would greatly help me to understand what happens.

Connects_Displays_No_Refresh.aia

The multi-characteristics method used only works once because it is in a Connection Block and should be in a Clock (timer) Block.

Each project needs the .ino Sketch to be coded to match the data handling method employed, so it is not a case of just taking some desirable features from one project into another - the Sketch would have to be changed too.

Here is a version of the "Connects_Displays_No_Refresh.aia" called "Connects_Displays_No_Refresh_Edit.aia".

It uses the multiple Characteristics method and regulates the incoming data with a Clock Timer. The .ino Sketch must match accordingly and you will then see the values updating.

Connects_Displays_No_Refresh_Edit.aia (1.0 MB)

The reason I expected you to persevere with streaming using a single characteristic is because ultimately you will be working with 24 values - it is necessary to regulate the sequence and flow of that much data for it to work, and working also means being able to reject the occasional blip smoothly without crashing.....

Wow, Chris . . . this is everything I dreamed of - two weeks ago;)-. It seems to me that I could play with this, slowly modifying it so that the first three data points were delivered on one UUID. I tend to lean this way because the psychrometer app that was part of the "functioning set" still does not connect and I get the same error as before when I try to run the app with the modified text file creator.

I guess the other thing I could do is recreate all of the math on the app end but that seems like a bunch of extra work, as I already have calculations working in C++.

Anyway, I thank you again for showing me that my previous app could be modified with a clock to periodically fetch the data.

I have tested Psychrometer.aia on Android 10 and Android 12. In both cases, the log file is recorded. However, on Android 12 it tells me that permission request "WRITE_EXTERNAL_STORAGE" has been denied - but the log file is still written:

psychrometer.txt (738 Bytes)

OK, that explains your genius rating! You are so right, the message said it wasn't secreted but the file folder says otherwise. I am attaching the psychrometer.txt in hopes that you will discover the issue plaguing my inoperative app.

psychrometer.txt (675 Bytes)

I do not see anything outstanding when I look at it. It basically confirms the device is found and connection has been attempted, as far as I can tell. The story ends suddenly.

Hi Baran

The last line of the log is significant - Connected: Waiting for Data

So the App is ready for the data but it is not being sent.

Two possibilities concerning Sketch BME_SensorObject.ino:

  1. The sensors are not producing values
  2. The code is not sending values

Concerning (1), do you see Temperature, Humidity and Pressure values on the Arduino Serial Monitor? If you do, then this modified Sketch might work:

BME_SensorObject2.ino (4.1 KB)
EDIT: This is the second upload of BME_SensorObject2.ino, delete the first upload if you have downloaded it.

Try the Sketch only with an APK of Psychrometer2.aia:
Psychrometer2.aia (248.8 KB)