How to Create an MIT APP code blocks that can read the battery percentage of the battery that is powering up our arduino nano?

Hello! Good morning everyone.

We've been stuck with this problem for two days now, I hope someone can help us solve our problem.

So, we're having a thesis entitled "Footsteps to Electrical Energy Converter".
It is made up of piezoelectrical disk that pumps electric current to our battery (lithium ion) and store them. Our project includes an app that can monitor how much battery percentage our power supply has (lithium battery) and can also monitor how many steps the user has already taken.

The code for our arduino nano is already working and we can now thankfully monitor the steps being taken and the battery percentage seen through the arduino serial monitor.

Now the problem is creating the MIT APP. The design that we tried to do cannot read the battery percentage of the power supply (lithium battery that powers up the arduino) but instead read the battery percentage of the phone that is connected to the bluetooth module. Also it doesn't read the steps being taken.

Can someone help me with this? Please. What code blocks should we use to make this work? How to make this block with the bluetooth module? Thank you in advance. Huhuhuhuhu

I'll put the arduino code that we used that can monitor the steps and battery percentage of the power supply down belooow.

#include <SoftwareSerial.h>

SoftwareSerial btSerial(2, 3); // RX, TX pins for Bluetooth module
const int piezoPin = A0;
int previousValue = 0;
int currentValue = 0;
int pumpCount = 0;

void setup() {
btSerial.begin(9600); // Start the Bluetooth serial communication
Serial.begin(9600); // Start the regular serial communication
}

void loop() {
currentValue = analogRead(piezoPin);
if (currentValue > previousValue + 250) {
pumpCount++;
}
previousValue = currentValue;
btSerial.println(pumpCount);
Serial.println(pumpCount);

int analogValue = analogRead(A1); // variable to store the analog input
float voltage = analogValue * (5.0 / 1023.0); // variable to hold the input converted to voltage
int batteryPercentage = (voltage / 5.0) * 100; // variable to hold the battery percentage
btSerial.print("Battery Percentage: ");
btSerial.println(batteryPercentage);
Serial.print("Battery Percentage: ");
Serial.println(batteryPercentage);
delay(1000); // wait 1 second before the next loop
}

Here is a simple BlueTooth text receiver sample, for single value per line:
blocks
initialize global message to

...

In your case, you could check the incoming message to see if it contains that battery label text, and steer it into a different Label.Text.

Hello, Sir! Thank you for replying to my post. I tried the blocks that you gave me but it doesn't work. I think I've done something wrong with the blocks. Is this all the blocks? Can you please explain to me how it works and what will show on the screen if this works correctly? I apologize, this is our first time doing this kind of code and we are not so familiar with the MIT APP. We would be so grateful to receive your help! Thank you!!

Maybe you need the code to select a Bluetooth device and connect to it?

(Canned Response ABG - Bluetooth non-BLE SCAN Permission Blocks)

The easiest solution, for immediate relief
(from @Barry_Meaker) ...

I had the same issue. The problem is your app does not have permission to see nearby devices. The solution is to give your app permission on your phone (no code changes in your app).

on your phone,

  • goto settings
  • search for your app
  • in App Info for your app select Permissions
  • change Nearby Devices from Not Allowed to Allowed
  • Done

By the way, the very first time you run the app, Android will ask if you want to grant the app this permission. If you say no, or ignore the pop-up, the permission will be set as Denied. Android will not ask again.

A more complex approach, for professional app development:

See Bluetooth liste of devices deosn't work anymore - #7 by Anke
Special note for Xiaomi devices:
I have an error with bluetooth on android 12, Xiaomi Poco X3 NFC - #20 by Patryk_F