if/then blocks.
Hi @ABG, so sorry for spamming you this Monday
I tried this code with if/then statements and it came up with this issue:
This is my Arduino code:
#include <Wire.h>
#include <BLEDevice.h>
#include <BLEUtils.h>
#include <BLEServer.h>
#include "MAX30105.h"
#include "heartRate.h"
BLEServer *pServer;
BLEService *pService;
BLECharacteristic *pTemperatureCharacteristic;
BLECharacteristic *pHeartRateCharacteristic;
MAX30105 particleSensor;
long lastBeat = 0;
int beatAvg;
int counter = 0;
int beatDetected = 0;
void setup()
{
Serial.begin(115200);
Serial.println("Initializing...");
// Initialize BLE
BLEDevice::init("mlex");
pServer = BLEDevice::createServer();
pService = pServer->createService(BLEUUID("0000AAAA-0000-1000-8000-00805F9B34FB")); // Custom Service UUID
pTemperatureCharacteristic = pService->createCharacteristic(
BLEUUID("0000AAAA-0001-1000-8000-00805F9B34FB"),
BLECharacteristic::PROPERTY_READ |
BLECharacteristic::PROPERTY_NOTIFY
);
pHeartRateCharacteristic = pService->createCharacteristic(
BLEUUID("0000AAAA-0002-1000-8000-00805F9B34FB"),
BLECharacteristic::PROPERTY_READ |
BLECharacteristic::PROPERTY_NOTIFY
);
// Initialize sensor
if (particleSensor.begin(Wire, I2C_SPEED_FAST) == false)
{
Serial.println("MAX30105 was not found. Please check wiring/power. ");
while (1);
}
particleSensor.setup();
particleSensor.setPulseAmplitudeRed(0x0A);
particleSensor.setPulseAmplitudeGreen(0);
particleSensor.enableDIETEMPRDY();
// Start the BLE server
pService->start();
BLEAdvertising *pAdvertising = pServer->getAdvertising();
pAdvertising->start();
}
void printStuff(float tempC, float tempF, long irValue, float bpm, int avgBpm)
{
// Convert float values to strings
String temperatureStr = String(tempF, 4);
String bpmStr = String(avgBpm, 2);
String avgBpmStr = String(avgBpm);
Serial.print("Temp F=");
Serial.print(temperatureStr);
Serial.print(", BPM=");
Serial.print(bpmStr);
Serial.print(", Avg BPM=");
Serial.print(avgBpmStr);
if (irValue < 50000)
Serial.print(" No finger?");
Serial.println();
// Update BLE characteristics with the new values
pTemperatureCharacteristic->setValue(temperatureStr.c_str()); // Use .c_str() to convert String to const char*
pTemperatureCharacteristic->notify();
pHeartRateCharacteristic->setValue(bpmStr.c_str()); // Use .c_str() to convert String to const char*
pHeartRateCharacteristic->notify();
}
int calculate_average_bpm(float bpm)
{
const byte SAMPLE_SIZE = 4;
static byte bpm_collection[SAMPLE_SIZE];
static byte nextSpot = 0;
int avgBpm;
if (bpm < 255 && bpm > 20)
{
bpm_collection[nextSpot] = (byte)bpm;
nextSpot = nextSpot + 1;
nextSpot = (nextSpot % SAMPLE_SIZE);
avgBpm = 0;
for (byte x = 0 ; x < SAMPLE_SIZE ; x++)
avgBpm += bpm_collection[x];
avgBpm /= SAMPLE_SIZE;
}
return avgBpm;
}
void loop()
{
float beatsPerMinute;
long irValue = particleSensor.getIR();
counter = counter + 1;
if (checkForBeat(irValue) == true)
{
long delta = millis() - lastBeat;
lastBeat = millis();
beatsPerMinute = 60 / (delta / 1000.0);
beatAvg = calculate_average_bpm(beatsPerMinute);
beatDetected = 1;
}
if (beatDetected > 0 || counter > 25)
{
float temperature = particleSensor.readTemperature();
float temperatureF = particleSensor.readTemperatureF();
printStuff(temperature, temperatureF, irValue, beatsPerMinute, beatAvg);
counter = 0;
beatDetected = 0;
}
}
I did copy the right UUIDs over:
But it still seems to not work.
In your strings received event you use the strings received value.
Did you notice the other two values that the event gives you?
They tell you more about where the string values came from.
Compare them against your global uuids to see what arrived, a temperature or the other one.
Do I have to show you how to compare two values?
Hi @ABG,
I'm sorry for the relentless messages. This is what I edited so far for the app:
I have two characteristicUuids for each sensor data (one for temp, one for heart rate) and one service uuid overall. However, this code didn't work and what happened was that the temperature and heart rate label texts did not change at all.
As for stringValues, how would it be possible to compare them?
Sorry for the bother.
Hi @ABG, Thanks for responding. I applied the corrections but the data still won't be displayed as text. Any other suggestions?
@Patryk_F
Thanks Patryk,
I changed the block circled to PhoneNumberPicker1.PhoneNumber to send it to the close contact of the user selected from their contacts.
However, an issue I ran into is that if this action was initialized, the app would continuously pull up the messages app and close out of it and then pull it back up again, not giving me the chance to send the warning message from mit app inventor. I have to leave mit app inventor and physically go to the messages app to manually send the warning message.
Here is my code:
I was wondering: is there a way to send the message directly from the mit app inventor? And is the "glitch" because of the possibility of the sensor (continuously) reading the temperature to be over 90 degrees? How could I use a clock timer to make sending the text message a one-time case and stop it there?
If you register for strings on a uuids, as far as I know, you should not be reading it any more.
The strings should arrive on their own.
Look in the tutorials section of this board for BLE samples.
How do you know what type of measurement you received?
P.S. Try searching this board for 'multiple BLE'
Hi @ABG,
This is another variation of code that I used:
and I came up with this error:
I’ve been looking around for projects but the projects on the product page scarcely deal with BLE and do not have the functions for the xiao esp32c3 microcontroller (there’s not much online at all I’ll keep looking). I know for a fact though, that the data is transferred to the app as a string.
Any thoughts? Thanks for all the help.
If it's not text, what does it look like?
Why are you using the ReadString method in the StringReceived event? What happens if you just remove the ReadString blocks?
There are two ways to read the data. You register the BLE component for automatic reading of data using the RegisterForString block, which you did in the Connected event, or you use ReadString e.g. in the clock. Your code with ESP seems to use notifications, so just registering string readings should be enough, and ReadString blocks are unlikely to be needed.
It just looks like their default text, sorry for the confusing phrasing.
Essentially, the data is not displayed in the labels, and the labels stay as they are as they have in default. The text for the label for the temp data is tempOutput, and it just stays that when the app runs (even though the device is connected).
Thanks @Patryk,
I took out the ReadString block and just set the code to be this:
The UUIDs are copied from the arduino code, which is listed above in this conversation:
pServer = BLEDevice::createServer();
pService = pServer->createService(BLEUUID("0000AAAA-0000-1000-8000-00805F9B34FB")); // Custom Service UUID
pTemperatureCharacteristic = pService->createCharacteristic(
BLEUUID("0000AAAA-0001-1000-8000-00805F9B34FB"),
BLECharacteristic::PROPERTY_READ |
BLECharacteristic::PROPERTY_NOTIFY
);
pHeartRateCharacteristic = pService->createCharacteristic(
BLEUUID("0000AAAA-0002-1000-8000-00805F9B34FB"),
BLECharacteristic::PROPERTY_READ |
BLECharacteristic::PROPERTY_NOTIFY
);
I copied them over directly from the code, but it just doesn't seem to work? If I take all the code out that reads the heart rate sensor data, the data for temperature shows up in the label fine, but the two sensor data just can't seem to be displayed together on the app.
Also, thank you Patryk and Abraham for helping so much. It means a lot.
It worked for him, so I don't know why the problem is here.
Edit:
You have a bug here. It should be ...Char2.
You can also try using green logical or blue math blocks instead of pink text comparison blocks.
For diagnostic purposes, add multi-line Labels to show a log of the incoming serviceUuid and characteristicUuid.
It's hard to debug a comparison without seeing what's on both sides
set LblCharacteristicUuid.Text to JOIN(characteristicUuid, '\n', LblCharacteristicUuid.Text)
The project you shared is also not working? Are there any errors or just no response? Here I added labels to see the raw data if any.
MLEX_vital_reader_3.aia (256.0 KB)
Hi @Patryk, There's just no response
There are no errors.