Connecting Seeed studio xiao esp32c3 with MIT App Inventor via Bluetooth

Hi,
Recently I have been trying to transfer sensor data from a heat sensor (TMP36) to a Xiao esp32c3 microcontroller to MIT App Inventor via the microcontroller's Bluetooth antenna and MIT App Inventor's BluetoothLE1 Extension. However, I have been having trouble connecting the microcontroller to my app. Additionally, I cannot get the app to input the TMP36's temperature sensor data into my button labeled "Temp", as shown below.


Here is also my Arduino code for the temperature sensor and microcontroller's bluetooth connection.

The sensor works on other mobile apps such as nRF connect, so the bluetooth does work and is able to connect to devices.

I would sincerely, SINCERELY, (from the bottom of my heart) appreciate it if anyone would be willing to help me out ASAP.

Hello! I'd be happy to help you with the issues you're facing while trying to transfer sensor data from the TMP36 heat sensor to your Xiao esp32c3 microcontroller and then to MIT App Inventor via Bluetooth.

To better assist you, could you provide more specific information about the problems you are encountering? Specifically:

  1. Are you able to successfully connect your Xiao esp32c3 microcontroller to the MIT App Inventor app via BluetoothLE1 Extension, or are you facing issues during the pairing process?

  2. Are you able to read the temperature data from the TMP36 sensor using the Xiao esp32c3 microcontroller?

  3. What have you tried so far to get the app to input the TMP36's temperature sensor data into the "Temp" button?

With this additional information, I'll be better equipped to provide you with more specific guidance and help you troubleshoot the problems.

Hi Ricardo!
Thank you SO much for reaching out!

  1. No I have not been able to connect my Xiao esp32c3 to my app via bluetooth. I have tried this code and I was able to discover bluetoothLE devices, but I could not connect it to the temperature sensor. Here is my code for bluetoothLE1 connection:
  2. Yes, we are able to read the temperature data from the TMP36 sensor on the computer and on another app such as nRF connect (transferred over bluetooth), so the sensor and bluetooth work.
  3. My code above is what I have tried so far to get the app to input the TMP36's sensor data into the label. I initialized the global Received_data to be 0.0, and I set the Temp text to be the global_Received_data, but I'm not sure that's the right way.

Again, thanks so much again and I look forward to you reply.

I copied over from your sketch:

#include <BLEDevice.h>
#include <BLEUtils.h>
#include <BLEServer.h>
 
BLEServer* pServer = NULL;
BLECharacteristic* pCharacteristic = NULL;
bool deviceConnected = false;
float temperatureFahrenheit = 0.0;
 
const int analogInputPin = A0; // Pin connected to the TMP36 OUT pin
const float referenceVoltage = 3.3; // Supply voltage to the TMP36
 
// Create a BLE Server and set it up
class MyServerCallbacks : public BLEServerCallbacks {
 void onConnect(BLEServer* pServer) {
 deviceConnected = true;
 }
 
 void onDisconnect(BLEServer* pServer) {
 deviceConnected = false;
 }
};
 
void setup() {
 Serial.begin(9600);
 analogReadResolution(12);
 
 // Create the BLE Device
 BLEDevice::init("Temperature Sensor");
 
 // Create the BLE Server
 pServer = BLEDevice::createServer();
 pServer->setCallbacks(new MyServerCallbacks());
 
 // Create the BLE Service
 BLEService* pService = pServer->createService(BLEUUID("00001809-0000-1000-8000-00805f9b34fb"));
 // Use a custom UUID for the Temperature Service
 
 // Create the BLE Characteristic for temperature measurement
 pCharacteristic = pService->createCharacteristic(BLEUUID("00002a1c-0000-1000-8000-00805f9b34fb"), BLECharacteristic::PROPERTY_READ | BLECharacteristic::PROPERTY_NOTIFY);
 // Use a custom UUID for the Temperature Characteristic
 
 // Set the initial value of the temperature characteristic
 pCharacteristic->setValue("0.0"); // Start with a default value
 
 // Start the service
 pService->start();
 
 // Start advertising
 pServer->getAdvertising()->start();
}
 
void loop() {
 int rawValue = analogRead(analogInputPin);
 float voltage = (rawValue / 4095.0) * referenceVoltage;
 
 float temperatureCelsius = (voltage - 0.58) * 100.0;
 temperatureFahrenheit = (temperatureCelsius * 9.0 / 5.0) + 32.0;
 
 if (deviceConnected) {
 // Convert temperature value to a string
 String temperatureString = String(temperatureFahrenheit, 2);
 
 // Update the value of the temperature characteristic
 pCharacteristic->setValue(temperatureString.c_str());
 pCharacteristic->notify();
 }
 
 Serial.print("Temperature in Fahrenheit: ");
 Serial.println(temperatureFahrenheit);
 
 delay(1000);
}

This is the part where you format and transmit the temperature:

 String temperatureString = String(temperatureFahrenheit, 2);
 
 // Update the value of the temperature characteristic
 pCharacteristic->setValue(temperatureString.c_str());
 pCharacteristic->notify();
 }
 

Though I am unfamiliar with the pCharacteristic notify process, it looks like the sketch is sending text strings, so you should have a BLE Strings Received event to display the incoming strings.

Your first post had one, with a harmless bug that compared a global variable against itself before doing the display.
8b8c8c215a798e66f4a5d5aa2c0cb7ba43e69dcf_2_431x500

P.S. I don't see in your code any permission handling or mention of the versions of your BLE extension or your Android phone.

Here are stock references for those:

Hi ABG,
Thanks so much for helping out. I deleted that block in my code and added the characteristics code in my arduino. I noticed that you mentioned asking for permission for the code, and was wondering if you could take a look at this code to see if it does the trick. As for version, I did download the version 20230223-beta BLE extension. As for my Android phone, it is an A14?

Would there still be a reason why this code does not successfully connect to my temperature sensor via the xiao esp32 and send its data via bluetooth? Thanks so much again.

My BLE experience has fallen so far behind the other PUs that you need a specialist.

@Anke ?

Hi @ABG with your advice I got this code to run and successfully connect to the sensor via the xiao esp32. Thanks so so much!
It's just that for some reason when I press the disconnect button error 3300 pops up.
Here is my code


Here is the error

I just think it's strange because I checked the data that's changing on the screen with another bluetooth app on my phone and it was relaying the same data, so I assumed that the device was connected, but the error says that the device isn't. Any thoughts?

Check to see if there is a BLE event block to catch errors.

If there is one, have it notify you gracefully.

You might have already been disconnected by an error.

Sounds good.

@ABG, in addition to my code listed above, I have been trying to include a notifier that will send a notification if the sensor data gets too high/too low. Here is my code:

However, when I run the app, it doesn't seem to work, maybe because the tempOutput text is displyaed in quotation marks and brackets. Is there a workaround this?

image

Thanks so much

stringValues is a list, so you have to select item 1 from that list.

Showing an entire list causes AI2 to wrap all the items in quotes, to keep them separate.

(I have yet to see a stringValues come back with more than 1 item. But I am not a BLE expert.)

Hi @ABG,
Thanks so much for helping me so much. It worked. I really truly appreciate it and it has officially made my weekend.
I have altered my code to look like this:

What I intended for was for the app to show an alert if the sensor picked on too high temperatures and also send a text message to the contact number selected from the contact list under user information.

But after running the app and whenever the temperature surpasses the range, nothing happens. There is no alert either. I was wondering (again), if you happened to have any feedback? Thanks again.

Those free floating if/then tests need to be at the bottom of that BLE strings received event.

All work in AI2 needs to be triggered by an event.

Hi @ABG, Thanks again.
I changed the blocks and I found out that the notifier was able to send a warning and the phone was able to vibrate. However, that was only after I took out the blocks that sent the messages.

What happened was that I would get this warning on the phone:

Is it because the text procedure cannot send text? And if so, is there any way around it? Thanks Abraham.

I have no experience with the phone component.

I do remember that if you are testing with the Companion, you need to use the version ending in 'u', available from MIT, not from the Play store.

Here is mistake. There you should enter the "Text" from the TextBox and not the "NumbersOnly" property.

1 Like

I should have seen that error.

Goes to show, sometimes an error message can send you down a rabbit hole.
I was out in the wilds, overwhelmed by wondering about

  • phone service
  • phone number formats
  • input errors.

It used up my energy, which should have been given to reading the code closely.

It's all good. Thanks @ABG and @Patryk_F

I have recently been trying to change my code to accomodate for the temperature and heart rate readings for a max30102 transferred via bluetooth to the mit app inventor. My sketch (I will upload it here as soon as I can) can read the readings simultaneously and (should) have different service and characteristic UUIDs for each of the sensor data.
Here is a picture of my block code.

However, in the circled red part, I came across a road block and I was wondering, how can the mit app inventor differentiate the different string datas so that they can be inputed into their respective labels' texts (tempOutput and HROutput).

Thanks!

It depends on how your sketch packages the values.

There are three different approaches in BLE:

  • Use separate characteristicUuids for the different measurements, and test the received characteristicUuids in the BLE StringsReceived event
  • (non-BLE) Package the pair temp,HR as text values separated by a comma and terminated by \n
  • (non-BLE) Send each reading separately, with a text prefix ("temp:", "hr:",...), and parse each incoming message using Text blocks.

But the sending sketch has to agree to follow what you chose.

Thanks @ABG,
I'm still writing the code but each sensor data will have its own service and characteristic UUids.

What blocks do you recommend I use to test the received characteristicUUIDs?

Thanks