Thank you very much, uskiara. I made the changes here and it helped me a lot. I set the permissions, changed the byte numbers, and removed the BLE. But even so, I couldn't connect through the app, and it was only possible to connect through the terminal. I ran a test with a JDY-31-SPP and, amazingly, it worked and sent the information satisfactorily (I even checked it on the serial monitor and saw it working). So, I don't know what's going on. The HC-05 connects through the terminal but not through the app, but the JDY-31-SPP connects to the app.
Sorry for not sending the data, I'll send the change in MIT APP INVENTOR, Arduino programming, and the circuit as well.
The change in MIT App Inventor is last
(I made a workaround for the HC-05's RXD because I didn't have a 2K Ohm resistor, using two 1K Ohm resistors instead, if it doesn't matter).
#include <BH1750.h>
#include <Wire.h>
#include <SoftwareSerial.h>
BH1750 lightMeter(0x23);
SoftwareSerial BT(5, 6); // CONNECT BT RX PIN TO ARDUINO 6 PIN | CONNECT BT TX PIN TO ARDUINO 5 PIN
int pinoR = 9; // LED vermelho
int pinoG = 10; // LED verde
int pinoB = 11; // LED azul
float Io = 0; // Referência (branco)
byte R = 0, G = 0, B = 0;
String mensagem = ""; // Mensagem recebida
// Função para converter comprimento de onda em RGB
void wavelengthToRGB(float wavelength, byte &R, byte &G, byte &B) {
float red, green, blue;
if (wavelength >= 380 && wavelength < 440) { // VIOLETA
red = -(wavelength - 440) / (440 - 380);
green = 0.0;
blue = 1.0;
} else if (wavelength >= 440 && wavelength < 485) { // AZUL
red = 0.0;
green = (wavelength - 440) / (485 - 440);
blue = 1.0;
} else if (wavelength >= 485 && wavelength < 500) { // CIANO
red = 0.0;
green = 1.0;
blue = -(wavelength - 500) / (500 - 485);
} else if (wavelength >= 500 && wavelength < 565) { // VERDE
red = (wavelength - 500) / (565 - 500);
green = 1.0;
blue = 0.0;
} else if (wavelength >= 565 && wavelength < 590) { // AMARELO
red = 1.0;
green = -(wavelength - 590) / (590 - 565);
blue = 0.0;
} else if (wavelength >= 590 && wavelength <= 625 ) { // LARANJA
red = 1.0;
green = 1.0 - (wavelength - 590) / (625 - 590); // Verde decresce
blue = 0.0;
} else if (wavelength >= 625 && wavelength <= 780) { // VERMELHO
red = 1.0;
green = -(wavelength - 780) / (780 - 625);
blue = 0.0;
} else {
red = green = blue = 0.0;
}
R = (byte) round(red * 255);
G = (byte) round(green * 255);
B = (byte) round(blue * 255);
Serial.print("R: "); Serial.print(R);
Serial.print(" G: "); Serial.print(G);
Serial.print(" B: "); Serial.println(B);
}
void ligarled() {
analogWrite(pinoR, R);
analogWrite(pinoG, G);
analogWrite(pinoB, B);
}
void desligarled() {
analogWrite(pinoR, 0);
analogWrite(pinoG, 0);
analogWrite(pinoB, 0);
}
void setup() {
Serial.begin(9600);
BT.begin(9600);
Wire.begin();
pinMode(pinoR, OUTPUT);
pinMode(pinoG, OUTPUT);
pinMode(pinoB, OUTPUT);
if (!lightMeter.begin()) {
Serial.println("Erro: BH1750 nao encontrado!");
while (1); // para execução
}
Serial.println("BH1750 iniciado com sucesso.");
}
void loop() {
while (BT.available()) {
char c = BT.read();
if (c == '\n') { // fim da mensagem
mensagem.trim(); // remove espaços e quebras
Serial.println(mensagem); // imprime mensagem completa no Serial
if (mensagem.startsWith("cor")) {
float wavelength = mensagem.substring(3).toFloat();
wavelengthToRGB(wavelength, R, G, B);
Serial.print("Wavelength: ");
Serial.println(wavelength);
}
else if (mensagem == "C") { // Calibrar
ligarled();
delay(2000);
Io = lightMeter.readLightLevel();
BT.println(Io);
delay(2000);
desligarled();
delay(2000);
Serial.print(Io);
}
else if (mensagem == "L") { // Leitura
ligarled();
delay(2000);
float I = lightMeter.readLightLevel();
float T = I / Io;
float A = -log10(T);
BT.print(A);
BT.print(",");
BT.println(T);
desligarled();
delay(2000);
Serial.print(A);
Serial.print(",");
Serial.println(T);
}
mensagem = ""; // limpa mensagem após processar
} else if (c != '\r') { // ignora retorno de carro
mensagem += c; // acumula caractere
}
}
}