Hi. I need help with the code below. The code supposes to send the value of variables i and x to an MIT App Inventor app. However, it will not send the data. If I remove the "if (SerialBT.available)", then it will send data and the app will receive the data. What am I doing wrong here? Thank in advance for your help.
#include "BluetoothSerial.h"
#if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BLUEDROID_ENABLED) #error Bluetooth is not enabled! Please run make menuconfig to and enable it #endif
BluetoothSerial SerialBT;
int i=1;
int x=1;
void setup(){
btStart();
Serial.begin(9600);
SerialBT.begin("ESP32test"); //Bluetooth device name //Serial.println("The device started, now you can pair it with bluetooth!");
}
void loop(){
if (SerialBT.available()){
SerialBT.print(i);
SerialBT.print("|");
SerialBT.println(x);
SerialBT.print("|");
Quite evidently, nothing - now that you have fixed it. The programme was waiting for something from the receiver and nothing was forthcoming, so nothing was sent from the transmitter.
You could have sent a prearranged signal from the receiver as a prompt, but you can be sure there isn't any point in that. It would just be adding an unwarranted extra complexity.
Im sorry. But i dont understand what you are saying. I wanna only send this data when the bluetooth is connected. I dont want to remove the SerialBT.available. However, if i put it, it does not send the data although the bluetooth is already connected.
Whenever the module is paired to a bluetooth-partner (= you smartphone) and has received data it will send the received data through the serial interface to your microcontroller.
And the command Serial.available() returns the number of bytes that are inside the receive-buffer of your microcontroller.
Your microocntrolle has NO idea that his serial partner is a bluetooth-module.
The only thing the microcontroller knows "my own serial interface is configured with baudrate given in SerialBT.begin() (and some other standard parameters)
I haven't worked with blue-tooth-modules yet. But it is very likely that you can send some special status-commands to the bluetooth-module to get an answer if a bluetooth-partner is connected.
The other way would be to send a short message to the smartphone and the smartphone is programmed to answer with a certain character-sequence which you would process inside the microcontroller.
It would help very much if you would describe the complete thing that you want to do.
Sending two values I and x to a smartphone is not a selfpurpose.
If you really want to do that, even after serious examination of the logic, you need a hardware solution, which may or may not exist. The first thing you need to to do is be more forthcoming on why you "wanna" do it, and I'm betting you have no good reason. As things stood, and as explained, your code shows that you want to do some serialBT printing IF there has been an incoming signal. If there is no incoming signal, nothing happens, quelle surprise(!). If you remove the "if", it works - as you have already found, and as everybody else does in normal practice.
I submit the problems you have at the moment are probably
that you refuse to accept the fact,
you are expecting both Bluetooth and Arduino to be smarter than they really are - psychic in fact, and
you are just making things more difficult for yourself by engaging on a pointless exercise in the face of an existing solution.
I would also point out that your MIT app apparently serves as no more than a plain-vanilla terminal. It would be a lot better if you used a standard Bluetooth terminal, thereby avoiding any hidden nasties at the other end, which just might be where some misunderstandings lie. Feel free to re-invent the wheel, but do it later - after the problem in hand is solved.
While I don't know anything about ESP32 Bluetooth, the fact that you get a result after you remove the redundant command confirms that you do, and all your configuration etc. is kosher. This may be down to the library you are using. Just make sure that any standard terminal is compatible with the Bluetooth service you are currently using. I believe that by Morich is the most useful.
That's good. It is also important that you understand the logic.
More to the point, I believe that what you have produced, or are just about to, is a standard procedure for using ESP32 to send plain data. It might be ratty, but it already works, and probably heralds the inevitable. It could be that the magic that sets this apart from Classic Bluetooth SPP is all in the first five lines, none of which I understand. This is the first time I have actually seen that and, in the light of the grief suffered by others, I believe it is a significant contribution to the ongoing saga of Arduino Bluetooth. I would therefore like to see your final code, and any prior configuration that you might have had to do on Bluetooth. This is for adding to some notes on simple Bluetooth that I have published. I have a Heltec ESP32 development board but I have never used it and, while I might claim some expertise with plain vanilla HC-05 Bluetooth, I know nothing about the Bluetooth in ESP32, And there are a lot of people like me....
The ESP32 classic bluetooth has some additional functionality to the HC05, but is quite simple to use. It certainly works well as a slave device to an Android phone using Kai Morich's Serial Bluetooth Terminal.
There is a function .connected() which returns the connected state of the BT module inside the esp. It is similar to reading the state pin on an HC05.
Here is a demonstration using the red and green led on my ESP32 development module (EzSBC). Other modules may have different indicator possibilities.
//This example code is in the Public Domain (or CC0 licensed, at your option.)
//By Evandro Copercini - 2018
//
//This example creates a bridge between Serial and Classical Bluetooth (SPP)
//and also demonstrate that SerialBT have the same functionalities of a normal Serial
#include "BluetoothSerial.h"
#if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BLUEDROID_ENABLED)
#error Bluetooth is not enabled! Please run `make menuconfig` to and enable it
#endif
BluetoothSerial SerialBT;
void setup() {
Serial.begin(115200);
pinMode(16, OUTPUT); //red dual color led
pinMode(17, OUTPUT); //green dual color led
SerialBT.begin("ESP32test"); //Bluetooth device name
Serial.println("The device started, now you can pair/connect it with phone!");
}
void loop() {
if (Serial.available()) {
SerialBT.write(Serial.read());
}
if (SerialBT.available()) {
Serial.write(SerialBT.read());
//SerialBT.write(SerialBT.read());//echo back
}
delay(20);
if (SerialBT.connected())
{
digitalWrite(17, LOW);//turn on
digitalWrite(16, HIGH);//turn off
}
else
{
digitalWrite(17, HIGH);//turn off
digitalWrite(16, LOW); //turn on
}
}
the library-name BluetoothSerial.h tells that you are using a ESP32.
I was assuming you are using some kind of bluetooth extra-module.
So to a ESP32 which has bluetooth included on the system more diectly. It may not apply what I have written in post #7.
Esspecially an ESP32 is aware of having a bluetooth because it is a SystemOnChip ( in short SoC)
Do I understand correctly that the Bluetooth on ESP32 is a BLE that can be configured as a classic Bluetooth then? The OP's code rather suggests that. This is the first time I have seen ESP bluetooth code that does something. I keep feeling that HC-0x will fall by the wayside but it has been a long wait.
I will have to get the Heltec board out of the bottom drawer.....
I will have to get the Heltc out of the bottom drawer.....
The ESP32 development boards are inexpensive and powerful and there's plenty to learn about them. I have mostly used the Serial Bluetooth with a phone, I haven't really worked through much of the library and the use of callbacks, and the use of two units as master and slave.
The use of master mode is shown in the BluetoothSerial.h library example SerialToSerialBTM.ino
I am also new to using Bluetooth with esp32. it took me a while to learn how to send data from esp32 to an android app. This is the first coding I have done with esp32 Bluetooth. This coding was just me testing to see if it could send the data. This is because I am currently trying to create a car battery level indicator that can be monitored through wifi and Bluetooth.
Thanks. I have downloaded this and now realise that evo69 is using a standard example SerialToSerialBT more or less verbatim. I understand that there is no preliminary config required to do this, evo used it as it came out of the box, and it is all handled by the library.
I note with despair the dates on the stuff in GitHub. I guess this is just another story about how far one has to walk along the road to Damascus. I have a Heltec WiFi kit 32 somewhere. I don't think I ever knew it was good for classic Bluetooth.