Something is wrong UV sensor, Bluetooth ardiuno , and Mit app

Screenshot 2024-05-07 165626|690x334

#include <SoftwareSerial.h>
#include <LiquidCrystal_I2C.h>

// Pin assignments
#define UV_SENSOR_PIN A0 // Analog pin for UV sensor

// Bluetooth module pins
#define BT_RX_PIN 2
#define BT_TX_PIN 3

SoftwareSerial BT(BT_RX_PIN, BT_TX_PIN); // RX, TX pins for Bluetooth module

LiquidCrystal_I2C lcd(0x27,16,2);

void setup() {
Serial.begin(9600); // Initialize serial communication for debugging
BT.begin(9600); // Initialize Bluetooth serial communication

lcd.init();
lcd.backlight();

}

void loop() {
// Read analog values from UV and IR sensors
int uvValue = analogRead(UV_SENSOR_PIN);

// Convert analog values to UV intensity (mW/cm^2)
float uvIntensity = map(uvValue, 0, 1023, 0, 15); // Assuming UV sensor range is 0-15 mW/cm^2

// Estimate wavelength based on intensity (replace with your calibration)
float uv_wavelength = estimate_wavelength(uvIntensity);

int light = analogRead(UV_SENSOR_PIN);
Serial.println();

lcd.clear();

lcd.setCursor(0,0);
lcd.print("UV I:");
lcd.print(uvIntensity);
lcd.print(" mW/cm^2 ");
lcd.setCursor(0,1);
lcd.print("UV W:");
lcd.print(uv_wavelength);
lcd.print(" nm");
delay(1000);

// Send sensor readings via Bluetooth
BT.print(uvIntensity);
BT.print("|");
BT.print(uv_wavelength);
BT.print("|");
//delay(1000);

// Print sensor readings to serial monitor for debugging
Serial.print("UV Intensity: ");
Serial.print(uvIntensity);
Serial.print(" mW/cm^2 ");
Serial.print("UV Wavelength: ");
Serial.print(uv_wavelength);
Serial.println(" nm");

delay(1000); // Delay for stability
}

// Function to estimate wavelength based on intensity (replace with your calibration)
float estimate_wavelength(float intensity_voltage) {
// This is a placeholder function for demonstration purposes
// You need to replace it with your actual calibration algorithm
// that maps intensity to wavelength
// For simplicity, assume a linear relationship between intensity and wavelength
return intensity_voltage * 81; // Example calibration factor
}

Be sure to use println() at the end of each message to send from the sending device, to signal end of message.

Only use print() in the middle of a message.

Be sure not to println() in the middle of a message, or you will break it into two short messages and mess up the item count after you split the message in AI2.

Do not rely on timing for this, which is unreliable.

In the AI2 Designer, set the Delimiter attribute of the BlueTooth Client component to 10 to recognize the End of Line character.
BlueToothClient1_Properties
Also, return data is not immediately available after sending a request,
you have to start a Clock Timer repeating and watch for its arrival in the Clock Timer event. The repeat rate of the Clock Timer should be faster than the transmission rate in the sending device, to not flood the AI2 buffers.

In your Clock Timer, you should check

  Is the BlueTooth Client still Connected?
  Is Bytes Available > 0?
     IF Bytes Available > 0 THEN
       set message var  to BT.ReceiveText(-1) 

This takes advantage of a special case in the ReceiveText block:

ReceiveText(numberOfBytes)
Receive text from the connected Bluetooth device. If numberOfBytes is less than 0, read until a delimiter byte value is received.

If you are sending multiple data values per message separated by | or comma, have your message split into a local or global variable for inspection before trying to select list items from it. Test if (length of list(split list result) >= expected list length) before doing any select list item operations, to avoid taking a long walk on a short pier. This bulletproofing is necessary in case your sending device sneaks in some commentary messages with the data values.

Some people send temperature and humidity in separate messages with distinctive prefixes like "t:" (for temperature) and "h:" (for humidity).
(That's YAML format.)

The AI2 Charts component can recognize these and graph them. See Bluetooth Client Polling Rate - #12 by ABG

To receive YAML format messages, test if the incoming message contains ':' . If true, split it at ':' into a list variable, and find the prefix in item 1 and the value in item 2.

(Canned Reply: ABG- Export & Upload .aia)
Export your .aia file and upload it here.
export_and_upload_aia

Thank you so muchhhh for your response great sir.

SPCmeter (1).aia (1.7 MB)

This should be

Notice how I dropped that trailing '|' and
changed the last print() to a println() to add a Line Feed at the end of the message.

Also change
image
to
image

in order to turn on end of line sensing, then change


to

(3 corrections)

  • bytes available
  • bytes requested
  • if/then list length check
1 Like

my app does not show any data
even though i followed the steps correctly

Does your app connect?

(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

...

Can you connect and receive data using a general purpose BlueTooth terminal app from the Play Store?

1 Like

Add this:

1 Like

that doesn't seem to be the case
every time the app is downloaded permission is always changed

:question:

What I mean is the app has permission to see nearby devices

Don't forget to test the sketch with a third party BlueTooth terminal app, to avoid wild goose chases on the app side.

Be sure the sketch is actually sending data.

the sketch sends data but the app doesn't seem to receive it

Try communicating using this app:

1 Like

I also tried but it seems like the data is not received ,there is no data

I am still finding alternative solution
and I am very confuse of what part did I go wrong