Controlling the Arduino LED via Bluetooth
First I connected up the HC-06 with the Arduino. The obvious way to do this is to use the Rx and Tx pins on the Arduino and to connect them the way we discussed before: HC-06 Tx -> Arduino Rx, Arduino Tx -> HC-06 Rx via a voltage divider bringing the Arduino's 5V level Tx signal down to 3.3V required by the HC-06. However, I found out that this has a big disadvantage: The hardware serial port is also used for USB and therefore during download of the sketch into the Arduino flash. When the HC-06 is connected to the serial port, then the Arduino programming does not work any more because the serial port is then used for two different tasks and the HC-06 will respond and mess up the downloaded data.
However, there is also a library for a "soft serial connection" (
SoftSerial). There you use 2 GPIO (general purpose I/O) pins and the software drives these pins high and low in a way that simulates a serial port. Therefore: "soft serial". Even though this library is less powerful than the standard
Serial library using the hardware port, it has all I need to communicate with the HC-06. The voltage divider for the Arduino Tx signal is of course also needed in order to avoid damage to the HC-06.
Now I wrote an Arduino sketch that sends "Hello World!" once per second to this SoftSerial port and therefore to its Tx line and the Rx line of the HC-06. These data are then sent via Bluetooth to the PC.
minicom is a program that takes characters typed on the keyboard and sends them to a serial port and it takes data from the serial port and prints them on its output window. As explained in my previous mail the serial port associated with Bluetooth is
/dev/rfcomm0 and therefore I start
minicom -D /dev/rfcomm0, such that minicom connects to this port. Now everything that comes in on /dev/rfcomm0 will be displayed on minicom's window and in fact I see a new "Hello World!" pop up every second on this window. This tells me that sending data from the Arduino to the PC works fine. Btw: In the sketch I print the data I send to the softserial port also to the standard serial port such that I can see what is happening on the console (on the Arduino SDK: Tools -> Serial Monitor brings up this console). There is no conflict with downloading here because when re-programming the flash, the program will be stopped anyhow.
Now I modify my Arduino sketch such that I check if serial data have come in and if yes I read them. If the text is "on", I switch the LED on, if it is "of", I switch it off (I use only 2 characters and you could use 0 and 1 or whatever you prefer. You must just write the sketch correspondingly). The incoming characters I also print on the serial console. This means that I simply have to type "on" when the keyboard focus is on minicom on the PC. This "on" will travel through Bluetooth to the softserial input of the Arduino, will be read by the sketch and reacted upon (
https://iotworkshop.africa/pub/Embedded_Systems/SketchForBluetoothControl/hc06Test.ino) .
--
Uli Raich - 2017-04-27
Comments
Topic revision: r2 - 2017-10-30
- uli