This project allows Arduino Uno to receive the text sent from Android to it via Bluetooth connection and display the received text on the LCD attached to Arduino Uno. In addition, Arduino Uno will periodically send text to Android via the same Bluetooth connection and the received text will be display on Android.
1. Wiring Diagram
2. Arduino Code
#include <SoftwareSerial.h>
#include <LiquidCrystal.h>
// initialize the LCD library with the numbers of the Arduino digital pins above
LiquidCrystal lcd(9, 8, 7, 6, 5, 4);
SoftwareSerial nss(2, 3); // (RX, TX)
int const chatInterval = 30000; // after 30 seconds, send a message to the connected Android device.
int numberOfTimesToSend = 2; // the number of times to send the message (spaced out by number of chatInterval above)
int lastChatSendTime = 0; //the last time - in millis() - that we sent a message to the Droid
String chatMessage = "Hello from Arduino"; // change this to anything you want to send to the Android device
int chatSendCounter = 0; //the number of times we've sent the message
void setup()
{
// set up the LCD's number of columns and rows:
lcd.begin(16, 2);
// serial port communications from Arduino to Bluetooth Firefly module (BlueSMiRF Gold)
// BlueSMiRF Gold defaults to 115.2k BAUD. Change to 57.6k BAUD.
// Don't ever use low BAUD like 4800 and 9600.
nss.begin(19200); // was 57600
//clear parallel LCD and set the cursor to the first row, first column.
lcd.clear();
lcd.setCursor(0, 0);
//reset the timer so we can send a message to the Droid in "chatInterval" seconds from now (30 seconds unless you change it in initialization above)
lastChatSendTime = millis();
}
void loop()
{
//*************receiving a message from Android and printing on Arduino*************************
if (nss.available())
{
lcd.write(nss.read()); //get message from Android device on the Arduino NewSoftSerial serial port and print it out on the LCD
}
//*************sending a message from Arduino to Android *************************
if (((millis() - lastChatSendTime) > chatInterval) && (chatSendCounter < numberOfTimesToSend))
//send a predefined message to the Android device after 'chatInterval' seconds and do this 'numberOfTimesToSend' times.
{
lastChatSendTime = millis();
chatSendCounter++; //increment chat counter
nss.print(chatMessage); //print out the message on the NewSoftSerial serial port which then travels via Bluetooth to the Android device
}
}
3. Android Code
a. Download the source code from the link below.
https://developer.android.com/samples/BluetoothChat/index.html
b. Locate “BluetoothChatService.java” and find the below lines of code.
private static final UUID MY_UUID_SECURE = UUID.fromString("fa87c0d0-afac-11de-8a39-0800200c9a66");
private static final UUID MY_UUID_INSECURE = UUID.fromString("8ce255c0-200a-11e0-ac64-0800200c9a66");
c. Replace the above lines with the following lines.
private static final UUID MY_UUID_SECURE = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
private static final UUID MY_UUID_INSECURE = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
private static final UUID MY_UUID_INSECURE = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
d. Save the file and run the modified BluetoothChat App on a real Android phone (the App won’t work on emulator).
4. The Result
Note,
- Arduino version: 1.6.1
- Android Studio version: 1.2 Beta
No comments:
Post a Comment