Controlling an Arduino over Bluetooth from Android using App Inventor 2
Here is an example of controlling the Arduino over Bluetooth using a HC-06 bluetooth module and an Android app. The example uses an Arduino Nano but other Arduinos will work just as well. A HC-05 module can be used stead of the HC-06.
The Android app was created in app inventor and the aia file can be downloaded at the bottom of the page.
The app has:
– buttons for turning LEDs on and off
– sliders for controlling an RGB LED
– navigation keypad
– command buttons
– buttons for turning LEDs on and off
– sliders for controlling an RGB LED
– navigation keypad
– command buttons
The app has a demo mode where the buttons work but commands are not sent via Bluetooth.
Arduino Setup
Connect the LEDs
I have used a common anode RGB LED. If you use a common cathode the common pin will need to go to ground rather than VCC and the sliders will be backwards.
I have used a common anode RGB LED. If you use a common cathode the common pin will need to go to ground rather than VCC and the sliders will be backwards.
I have used 220ohm and 330ohm resistors because these are what I had but any resistor from 220hm to around 480ohm will be OK. Just be aware that the higher the resistor value the duller the LED will be.
Connect the Bluetooth module
Arduino D2 (software serial RX pin) to BT module TX pin
Arduino D3 (software serial TX pin) to BT module RX pin via a voltage divider
Gnd to Gnd
BT VCC to the Arduinos 5v out
Arduino D2 (software serial RX pin) to BT module TX pin
Arduino D3 (software serial TX pin) to BT module RX pin via a voltage divider
Gnd to Gnd
BT VCC to the Arduinos 5v out
Although the BT module can accept an input voltage of 3.6 to 6v, the data pins are 3.3v only. This means you should not connect the Arduino D3 pin (TX) directly to the BT modules RX pin. The Arduino will read the 3.3v coming from the BT TX pin as HIGH so this can be connected directly. The Arduinos RX pin, which is 5v, needs to be brought down to 3.3v before connecting to the BT modules TX pin and a simple way of doing this is with a voltage divider mode from 2 resistors.
The BT module will take a maximum current of 40mA so it is safe to power it from the Arduinos 5v out pin.
The BT module will take a maximum current of 40mA so it is safe to power it from the Arduinos 5v out pin.
I am using the Bluetooth modules default settings:
– Baud rate = 9600
– Name = HC06
– Baud rate = 9600
– Name = HC06
Pairing
Before you can connect the app to the HC-06 you need to pair it with the Android device.
Power on the HC-06. The LED will blink rapidly.
Open Settings on the Android device and select Bluetooth.
If your device does not auto-scan you will need to manually scan for available Bluetooth devices. The HC-06 should appear in the list.
Select the HC-06. You will be asked for the pin. The default pin is “1234”.
The HC-06 modules name may include the mac address; a series of hexadecimal numbers.
Power on the HC-06. The LED will blink rapidly.
Open Settings on the Android device and select Bluetooth.
If your device does not auto-scan you will need to manually scan for available Bluetooth devices. The HC-06 should appear in the list.
Select the HC-06. You will be asked for the pin. The default pin is “1234”.
The HC-06 modules name may include the mac address; a series of hexadecimal numbers.
After pairing the HC-06 you can connect to it from within the arduinoBTcontrol app.
Connecting to the Arduino
Start the Arduino, the LED on the bluetooth module should blink rapidly.
Open the arduinoBTcontrol app on you Android device,
Go to the settings screen.
Tap the bluetooth button, this brings up a list of paired devices.
select the HC-06.
After the Arduino is connected the bluetooth button will change to say Connected and the LED on the bluetooth module will stop blinking and turn on.
Open the arduinoBTcontrol app on you Android device,
Go to the settings screen.
Tap the bluetooth button, this brings up a list of paired devices.
select the HC-06.
After the Arduino is connected the bluetooth button will change to say Connected and the LED on the bluetooth module will stop blinking and turn on.
Controlling the Arduino
After you are connected, go back to the MAIN screen and either tap a button or move the sliders.
The sliders transmit their value as soon as they start moving. Although you can move them before the Arduino is connected they will not send any data until after a connection is made.
The NAV screen contains a navigation keypad and 3 command buttons.
The direction buttons on the keypad are active while ever they are being pressed, the OK btn sends a signal as soon as it is pressed (it doesn’t wait for the user to lift their finger. The command buttons send unique commands and use normal button clicks.
The command buttons activate one of 3 sequences on the Arduino. When a command button is clicked, it will turn red and become inactive until the sequence is completed. When the sequence is finished the button will return to green and a ready sound is played. Command buttons cannot be pressed while a sequence is active. For the 3 commands buttons, the Arduino sends a finished/acknowledgement signal back to the Android app. It is this signal that triggers the command button being reset
Arduino Sketch
The Android app sends ascii codes, enclosed in start and end tags, to the Arduino. The Arduino recieves the data through the serial connection with the BT module.
The sketch and app can be downloaded at the bottom of the page.Comments is the sketch should explain what is happening.
The main parts are the recvWithStartEndMarkers() and the parseData() functions. recvWithStartEndMarkers() checks the received data for a start marker, the [ character, and when found starts to copy the received data to buffer/ variable called receivedChars[] until it finds the end marker, the ] character.
parseData() checks receivedChars[] buffer for commands and when it finds one it performs a certain action such as turn an LED on or off.
parseData() checks receivedChars[] buffer for commands and when it finds one it performs a certain action such as turn an LED on or off.
Android App
The Android app is written is MIT’s app inventor 2 and the aia file can be downloaded at the bottom of the page. Here are the key parts of the app.
When the app first starts Screen1.Initialize is called. This sets the slider value labels and changes the sliders ColorRight colours. The colours I wanted are not available in the designer and need to be set in the blocks.
Sceen1.ErrorOccurred block traps any system errors and displays then in a pop up message box.
Sceen1.ErrorOccurred block traps any system errors and displays then in a pop up message box.
The different screens, MAIN, NAV, and SETTINGS, are not really different screens. The tabs are buttons and when clicked they display or hide certain containers. When the MAIN tab is clicked, the MAIN container is displayed and the NAV and SETTINGS containers are hidden. I use this method to overcome a problem when using Bluetooth. In Android, each screen is treated like a separate app and using traditional screens means you cannot use a Bluetooth connection across different screens.
The logic is very simple. I turn off all tabs and then turn on the one I want. The code for the MAIN tab is in a procedure so that I can call it from the Screen1.Initialize function.
The Bluetooth connection is initiated when the user clicks the Bluetooth button on the SETTINGS screen. When the button is clicked the app checks that Bluetooth is turned on and then the available paired devices are copied to a list, the length of the list is then checked to make sure we have at least one device. If we have, a list picker is opened showing the paired devices. When the user selects one of the devices the BT_LP.AfterPicking function is called.
If the app is already connected the user is asked if they would like to close the connection.
If the connection is successful the button text is changed to CONNECTED and the BT_Timer is started. If the connection fails then an error message is displayed.
BT_Timer is used to check for incoming data. The timer frequency is set in the designer at 200ms, this is 5 times a second which is sufficient for this example. For more complex apps you would probably need a faster timer.
When the timer fires the BT_Timer.Timer function is called. This checks to see if there is any new data and if there is append it to a buffer. The buffer is stored in the receivedDataFromBT variable. The buffer is then checked to see if we have certain markers which in this case is a # character. If the app finds the end of data marker the updateCMDbtn function is called.
This app is a basic example and the only part that uses a call back from the Arduino are the 3 COMMAND buttons on the NAV page. Therefore using a simple end of data marker is acceptable. For more complex communication it would be better to use start and end markers. Later i will have a more advanced guide using start and end markers.
A demo mode allows the user to try the app without having a Bluetooth connection. In non demo mode the buttons do not work when there is no connection and clicking a button generates a “Bluetooth is not connected” error.
Demo mode is turned on and off on the SETTINGS page. The Demo mode button toggles between ON and OFF every time it is clicked.
Pin Buttons
On the MAIN page, the buttons correspond with pins on the Arduino. clicking one of the buttons toggles the button ON and OFF and also sends the corresponding command to the Arduino.
When a button is clicked the button property and the button number is passed to the buttonPressed function. Here the command to either turn on or turn off the LED is created and passed to the sendCommand function and the button background image is changed.
The Navigation buttons send a command as soon as they are touched (touch down) and also when the button is released (touch up). This basically sends a start and stop signal to the Arduino.
The OK button acts differently. It only sends a command on touch down. In this way the Arduino can react as soon as it is pressed and does not need to wait for the user to lift their finger.
The command buttons send a CMD command to the Arduino, turn red and then wait for the Arduino to say it is finished. While waiting the command buttons are inactive.
You can see that the 3 functions are very similar and they could easily be merged in to one function.
The BT_Timer starts running when a connection is made. Even if the command buttons are never clicked, the timer still keeps checking for data.
When the Arduino receives one of the CMD commands and has performed the corresponding function it replies with a “I am finished” message in the form of “OK1#”, “OK2#”, or “OK3#”. When the app receives one of these messages it resets the command buttons.
The final controls are the sliders. Sliders generate events when ever the slider position changes and so they can generate a lot of data. The slider command is created by joining the values from each slider together in the form Trrrgggbb. The values are converted to ascii before sending. This means the format is fixed, it is always 3 characters per colour. If speed is not that important using ascii to send data is very convenient and very robust.
Download
The zip file contains the Arduino sketch and the app inventor aia file.
댓글 없음:
댓글 쓰기