Error 507 hc 05 bluetooth module

Still nothing

By sending us a photo of your sketch instead of copying the entire text onto this board, you denied us the ability to see the declaration of that state variable.

It might be important.

My bad, I tought I caught the full code in my image. Here is the code...
#define ledPin 7

int state = 0;

void setup() {

pinMode(ledPin, OUTPUT);

digitalWrite(ledPin, LOW);

Serial.begin(9600); // Default communication rate of the Bluetooth module

}

void loop() {

if(Serial.available() > 0){ // Checks whether data is comming from the serial port

state = Serial.read(); // Reads the data from the serial port

}

if (state == '0') {

digitalWrite(ledPin, LOW); // Turn LED OFF

Serial.println("status ledice: Ugašena"); // Send back, to the phone, the String "LED: ON"

state = 0;

}

else if (state == '1') {

digitalWrite(ledPin, HIGH);

Serial.println("status ledice: Upaljena");

state = 0;

}

}
do not mind the comments of the code, I configured the code by my preferences.

So the variable state is declared as int, and you read it via Serial.read(), and you compare it to char '0' and '1'.

You signed up for some type conversions here, and I don't need that trouble in Arduino code.

I am passing this off to people with more C comfort, @ChrisWard or @uskiara

I know you dont need comments , just saying, so you dont think what the hell is he talking about, I believe state should be defined as a string or char or am I wrong? Because I tried with defining it with char it didnt work

Check the tutorials, char might be your safest bet, if you are dealing with single letters.

Have you tried testing with a bluetooth terminal app, to factor out the AI2 part?

Dear @Bruno_Dragas,
as @ABG has said, the simplest and safest way to perform a communication between AI2 and Arduino is by means of text (strings or characters).
On AI2 side you have to use the block:
image
On Arduino side you have to define the receiving buffer as char; i.e.
char state = ' '; // the incoming character buffer
I se that you use the Serial.begin(9600); bysupposing that the HC05 has that baudrate set as default: please be aware that sometimes some HC05 (depending on the manufacturer) has 38400 set as default (give it a try whether nothing works at the end of the other attempts).
Be also aware that the hardware serial line of Arduino is typically used for the Serial Monitor. that is the communication between Arduino and the PC (via USB), therefore there can be a conflict between your intended use, the BT comm's, and the development system. In any case, if you don't need to echo on the PC screen what you're receiving from the BT, this is not a problem.
Be sure to reverse the Tx and Rx pins between the HC05 and the Arduino board (Tx of HC05 must be tied to the RX pin of Arduino, and Rx to Tx).
Your "basic" Arduino code should be something like:

In addition to all above, you can try with a BT serial monitor app like (free on google playstore):


This will avoid the uncertainty of one side of the communication, the Ai2 one: once you are sure that with this app your Arduino hardware and code are running fine, you only have to tune your AI2 app.
Cheers.
.

2 Likes

By using this code here i was able to send data from SBT to serial Monitor of arduino
#include "Arduino.h"

#include <SoftwareSerial.h>;

const byte rxPin = 6;

const byte txPin = 13;

SoftwareSerial BTSerial(rxPin, txPin); // RX TX

void setup() {

// define pin modes for tx, rx:

pinMode(rxPin, INPUT);

pinMode(txPin, OUTPUT);

BTSerial.begin(9600);

Serial.begin(9600);

}

String messageBuffer = "";

String message = "";

void loop() {

while (BTSerial.available() > 0) {

char data = (char) BTSerial.read();

messageBuffer += data;

if (data == ';'){

message = messageBuffer;

messageBuffer = "";

Serial.print(message); // send to serial monitor

message = "You sent " + message;

BTSerial.print(message); // send back to bluetooth terminal

}

}
I was able to talk to arduino(from SBT to serial monitor)

still nothing happens when pressing the button, only thing is that the first time it is pressed on the screen random characters appear

Ok, you have changed many things:
first of all now you are using the SoftwareSerial library, so to free the Serial Monitor to work and show what is coming in from the BT line.This is fine, provided that you have connected pin 6 to the Tx pin of HC05 and pin 13 to the Rx pin of HC05.
Let me say what I have understood: by using the app Serial Bluetooth Terminal you can send data to your Arduino, and those data are shown correctly on the Serial monitor ?
While, on the contrary, when you use your AI2 app some garbage appear on the screen.?
This is pretty weird because the garbage typically appears when there is a baudrate issue, but you say that by using the SBT all the characters are received an then shown correctly. So it seems that the baudrate between HC05 and the Arduino board is correct.

Supposing then that the problem still persists on AI2 side, can you please post here your .aia so to letus have a look into it ?

Last, but not least, pay attention to terminate the sending from Arduino to the app with a linefeed character (0x0A) or, in other words, to use, as the very last sending from Arduino, a BTserial.println();

Le's do, as a first step, this attemptl (post your raia).

you mean blocks or full app

edu.mit.appinventor.ble-20230728.aix (201.9 KB)

Dear @Bruno_Dragas , you've posted the BLE extension, not your app (file.aia). !

First of all, to export your .aia project you shall select:
image
then link it to the post by using the upload button:
image

=> BUT <=....since you are using a HC05, which is a classic BT interface, you shall not use the BLE sxtension: that's a complete different story !

Am i missing something here with BLE?

led.aia (3.1 KB),
now it should do the work... I did the same thing as first time, I don't why there was ble at first...

As I said, you shall not use BLE, the HC05 is a classic BT interface.
I asked for your app (.aia file) while you posted the BLE extension.
Please follow my instructions above.

Hiw did I post ble extension? I am not following

I did everything as you said

This post contains the extension (.aix file)

while this one the .aia
image

So the last one works good?