The app does not connect to bluetooth

Continuing the discussion from Error 507 hc 05 bluetooth module:

I made this app that needs to connect to bluetooth but I don't see the hc-05 in the listpiker. I turned on the bluetooth, I saw the hc-05, I connected to it using pin 1234 and the device began to flash more slowly but still it does not flash twice (i.e. the reception and transmission status). The warning in MIT App Inventor is given because there are blocks that are not connected but which are not needed for the connection.
Then I tried to use an app from the app store (it is "Serial Bluetooth Terminal") and it connects to the hc-05 and should be able to receive and transmit characters but nothing happens. This is the program created with Arduino:

#include <SoftwareSerial.h>

const int switchPin = 12; // the switch connects to pin 12

const int ledPin = 6;     // the led connects to pin 6

const int redPin = 10;

const int greenPin = 11;

const int pinMotor1 = 8;

const int pinMotor2 = 9;

/**********************************/

int switchState = 0;    // variable for reading the pushbutton status

char on_off = '0';       // Change to character '0'

SoftwareSerial bluetooth(6, 7); // RX, TX - change these pins as needed

void color(unsigned char red, unsigned char green, unsigned char blue) // the color generating

{

  analogWrite(redPin, red);

  analogWrite(greenPin, green);

}

void setup()

{

  pinMode(switchPin, INPUT); // initialize the buttonPin as input

  pinMode(pinMotor1, OUTPUT);

  pinMode(pinMotor2, OUTPUT);

  Serial.begin(9600);

  bluetooth.begin(9600); // initialize SoftwareSerial for Bluetooth

  pinMode(3, OUTPUT);

}

/**********************************/

void loop()

{

  // read the state of the switch value

  switchState = digitalRead(switchPin);

  if (bluetooth.available() > 0)

  {

    char receivedChar = bluetooth.read();

    Serial.print("Received char: ");

    Serial.println(receivedChar);

    if (receivedChar == '1')

    {

      on_off = '1'; // Change to character '1'

      Serial.println("Acceso");//turn on

    }

    else if (receivedChar == '0')

    {

      on_off = '0'; // Change to character '0'

      Serial.println("Spento");//Turn off

    }

  }

  Serial.print("Switch state: ");

  Serial.println(switchState);

  Serial.print("on_off value: ");

  Serial.println(on_off);

  if (switchState == HIGH || on_off == '1') // if it is, the state is HIGH

  {

    color(0, 255, 0); // turn the led on

    digitalWrite(pinMotor1, HIGH);

    digitalWrite(pinMotor2, HIGH);

  }

  else

  {

    color(255, 0, 0); // turn the led off

    digitalWrite(pinMotor1, LOW);

    digitalWrite(pinMotor2, LOW);

  }

}

I attach the screen of MIT App Inventor and the circuit I created (sorry for the bad images).
So what's wrong?


Ciao @minalbero,
ti rispondo in italiano, anche se con l'inglese avresti più audience, piuttosto ritorniamo all'inglese se non risolviamo in fretta.
Quindi, se non hai già risolto da solo ecco alcuni consigli:
Nel tuo codice Arduino definisci il pin 6 come ledPin, poi lo assegni anche all'Rx della SoftwareSerial. Mi sembra che non lo usi nel resto del codice come output, ma è meglio se eviti queste "sovrapposizioni".
Nel tuo codice AI2 manca tutta la parte di richiesta di autorizzazione all'uso del BT (cosa che l'app SerialBluetoothTerminal che funziona, probabilmente fa).
Ovvero nello screen initialize devi chiedere al sistema operativo di autorizzare lo scan dei BT e l'autorizzazione alla connessione. Tipo quello che puoi trovare in questo topic:

Prova a dargli un'occhiata.
Ciao e auguri.

1 Like

Sto provando a utilizzare gli stessi blocchi che mi hai mandato ma non trovo questo: call BTstatus
trueflase
msg
Dove posso provalo?
Poi nel codice arduino ho tolto la dichiarazione del led nel pin 6 ma, utilizzando Serial Bluetooth Terminal, comunque, anche se si connette, non cambia il circuito fisico perché qualsiasi cosa mando lui riceve questo carattere: �. Sai a cosa è dovuto il problema?
Ti ringrazio anticipatamente.

From that thread

BTStatus just writes to the screen next to the button, nothing important there.

Just use a label instead

Taifun

So I don't understand why in the listpicker I see the screen all black even though I'm connected to the HC-05 but it only flashes more slowly and not "double".
Do I have to put some blocks to connect it?
What did I do wrong?

Did you ask for permission?
Taifun


(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

Thanks for the reply but I had already set all the permissions. I also tried "I have an error with bluetooth on android 12" but it doesn't work. So the problem is my phone? I have the Samsung Galaxy A21S.

It would really help if you provided a screenshot of your relevant blocks, so we can see what you are trying to do, and where the problem may be.

To get an image of your blocks, right click in the Blocks Editor and select "Download Blocks as Image". You might want to use an image editor to crop etc. if required. Then post it here in the community.

Taifun


Trying to push the limits! Snippets, Tutorials and Extensions from Pura Vida Apps by icon24 Taifun.


This is the image.

you did not use blocks to ask for permissions, see again Bluetooth liste of devices deosn't work anymore - #7 by Anke

also what is this?
Unbenannt

Taifun

1 Like

That piece is the purpose of the project. With Arduino I have to detect three objects of different shapes and send them to the application but to see a single warning I put them together. I haven't put them on yet because I haven't connected Bluetooth yet.

Now it connects to Bluetooth but the characters the Arduino receives are these: � or ….
What should I do?

Post the new code, both sending and receiving.

Dear @minalbero,
probably it's trivial, but which baudrate have you set for the Serial Monitor to the PC ?
The "weird" characters can be caused by a wrong baudrate or if you use Serial.write instead of Serial.print (i.e. numerical representation instead of ASCII). This second case should not be yours if your Arduino code is the same as you posted in your first post.
As @ABG have said, please post your last Arduino code.

PS last but not least, are you sure that the HC05 baudrate is 9600 ? I've had in few cases some of them factory set @38400 :flushed:

SECOND EDIT: but you say that the HC05 is blinking every two seconds ? If yes, this means that it is set in AT mode !! So it is in programming mode, not in communication mode :thinking:

2 Likes

Thx, now i set baudrate 38400 and work.
Thank all you :slight_smile:

Dear @minalbero,
I'm happy that it works. So your HC05 has both comm's and programming speed set @38400.
Is it one of those that remain with a steady red led ON or is it blinking slowly once (or twice) ?
(So to recognize the type and to help someone else, the next time :hugs:).
Cheers, Ugo.

The HC-05 blinking slowly twice

Now I have two other problems:

  1. now that I have the connection there is a conflict between what I transmit via bluetooth and the physical one
  2. the LCD screen doesn't work.
    To better understand this is the delivery:
    Design a device that allows you to count objects of different shapes that slide on a conveyor belt, and sort them.
    Objects with three different shapes slide on the belt.
    The shapes, seen from above and from the side, slide on the surface resting on the base, as in the lower view, but with a random angle of
    orientation.
    Device operating specifications:
    The device must recognize and count objects of each shape, and for each recognized shape a digital signal must be activated
    which will allow the object to be sorted by diverting it onto other belt paths intended for packaging, this last phase involves the collection
    of objects in packs of 10 pieces, for each type of object.
    Objects on the conveyor belt will be sensed using infrared devices.
    The quantity of objects counted and the number of packages made, for each type, must be displayed on an LCD display and also
    sent, via Bluetooth or WiFi, to a terminal (PC, smartphone, other) for viewing.
    The control device must also be equipped with a button that will allow, at any time, to start the conveyor belt or
    arrest him. The same button function must also be possible by sending a command from the remote terminal. When the tape stops they will be
    object detection sensors are also inhibited. The movement of the tape (active tape) must be indicated by the lighting of a
    Green LED. A red LED will indicate the tape stopped status. Conveyor belt motor will be driven (belt moving)
    via a low level digital signal, which will assume the high state when the motor is stopped (belt stopped).
    Technical specifications:
    The objects have homogeneous dimensions: square base with side 9cm and height 3cm; the elevated part of object B is a cube, in position
    central, with a side of 3cm, while the elevated part of object C is a parallelepiped measuring 3cm x 9cm x 3cm high placed on one side.
    The sorting signals must be of the impulsive type, active at a low level, with a duration of 20ms.
    The conveyor belt has a width of 20 cm.
    The time between the passage of two consecutive objects in front of the sensors is at least 200 ms, during which they must be
    all the actions described above are guaranteed.
    This is the Arduino program:

Blockquote#include
<SoftwareSerial.h>
#include "LCD03.h"
const int switchPin = 12; // the switch connects to pin 12
const int redPin = 10;
const int greenPin = 11;
const int pinMotor1 = 8;
const int pinMotor2 = 9;
// Configurazione dei pin per il display LCD
const int rs = A0; // Pin RS collegato ad A0
const int en = 13; // Pin E collegato al pin 13
const int d4 = 2; // Pin D4 collegato al pin 2
const int d5 = 3; // Pin D5 collegato al pin 3
const int d6 = 4; // Pin D6 collegato al pin 4
const int d7 = 5; // Pin D7 collegato al pin 5
LiquidCrystal lcd(I2C_ADDR, en, rs, d4, d5, d6, d7);
//
int switchState = 0; // variable for reading the pushbutton status
char on_off = '0'; // Change to character '0'
SoftwareSerial bluetooth(6, 7); // RX, TX - change these pins as needed
void color(unsigned char red, unsigned char green, unsigned char blue) // the color generating
{
analogWrite(redPin, red);
analogWrite(greenPin, green);
}
void setup()
{
pinMode(switchPin, INPUT); // initialize the buttonPin as input
pinMode(pinMotor1, OUTPUT);
pinMode(pinMotor2, OUTPUT);
Serial.begin(38400);
bluetooth.begin(38400); // initialize SoftwareSerial for Bluetoot
}
/
/
void loop()
{
// read the state of the switch value
switchState = digitalRead(switchPin);
if (switchState == HIGH)
{
on_off = '1';
}
else if (switchState == LOW)
{
on_off = '0';
}
if (bluetooth.available() > 0)
{
char receivedChar = bluetooth.read();
Serial.print("Received char: ");
Serial.println(receivedChar);
receivedChar = on_off;
if (receivedChar == '1')
{
on_off = '1'; // Change to character '1'
//switchState = HIGH;
Serial.println("Acceso");//turn on
}
else if (receivedChar == '0')
{
on_off = '0'; // Change to character '0'
//switchState = LOW;
Serial.println("Spento");//Turn off
}
}
//switchState = digitalRead(switchPin);
Serial.print("Switch state: ");
Serial.println(switchState);
Serial.print("on_off value: ");
Serial.println(on_off);
if (switchState == HIGH && on_off == '1') // if it is, the state is HIGH
{
color(0, 255, 0); // turn the led on
digitalWrite(pinMotor1, HIGH);
digitalWrite(pinMotor2, HIGH);
lcd.clear();
lcd.print("Oggetto ");
lcd.print(pieceType);
lcd.print(": ");
lcd.print(objectCount[pieceType - 1]);
if (shouldProducePackage()) {
for (int i = 0; i < maxObjects; i++) {
totalPackages[i] += objectCount[i];
objectCount[i] = 0;
}
displayTotalPackages();
}
else if (switchState == LOW || on_off == '0')
{
color(255, 0, 0); // turn the led off
digitalWrite(pinMotor1, LOW);
digitalWrite(pinMotor2, LOW);
}
delay (1000);
}
int countObjects() {
// Simula la lettura da un sensore infrarosso
// Puoi personalizzare questa logica in base al tuo sensore reale
return random(1, maxObjects + 1); // Aggiunto 1 per ottenere un numero tra 1 e maxObjects inclusi
}
bool shouldProducePackage() {
return (objectCount[0] + objectCount[1] + objectCount[2]) >= 10;
}
void displayTotalPackages() {
lcd.clear();
lcd.print("Confezioni totali:");
lcd.setCursor(0, 1);
for (int i = 0; i < maxObjects; i++) {
lcd.print("Oggetto ");
lcd.print(i + 1);
lcd.print(": ");
lcd.print(totalPackages[i]);
lcd.print(" ");
}
}




Dear @minalbero,
I don't understand these settings:

// Configurazione dei pin per il display LCD
const int rs = A0; // Pin RS collegato ad A0
const int en = 13; // Pin E collegato al pin 13
const int d4 = 2; // Pin D4 collegato al pin 2
const int d5 = 3; // Pin D5 collegato al pin 3
const int d6 = 4; // Pin D6 collegato al pin 4
const int d7 = 5; // Pin D7 collegato al pin 5
LiquidCrystal lcd(I2C_ADDR, en, rs, d4, d5, d6, d7);

If you use an I2C LCD, the pins to be used are only 2 (SDA and SCL) plus +5Vdc and GND. From where have you got these code lines ? What type of LCD are you using ?

On the web I've found something similar but no I2C_ADDR is used:
image
Where is lcd.begin(cols,rows) in your code ?

And what this means ?

  1. now that I have the connection there is a conflict between what I transmit via bluetooth and the physical one
    (me lo ridici in Italiano ? :grin:)

Ciao.

LCD che sto usando è 1602A.
Con:

intendo che per controllare i motori dovrebbe "prendere" l'ultimo dato che arriva, indipendentemente che sia switchState o on_off, solo che per l'accensione è cosi mentre per spegnerli ci devono essere entrambe le condizioni (switchState == LOW e on_off == '0').Non so come gestire questo a livello logico.
Spero di essere stato chiaro.