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.
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:
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?
Are you able to read the temperature data from the TMP36 sensor using the Xiao esp32c3 microcontroller?
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.
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:
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.
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.
#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.
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.
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
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?
@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?
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.
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.
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).