Arduino
Contents
Bluetooth 220 V voltage switch
Control a household lamp with your smartphone.
Needed:
- Arduino board
- 5v relay for arduino (http://www.play-zone.ch/de/2-kanal-relais-modul-karte.html)
- bluetooth modem (https://www.sparkfun.com/products/12577)
- android sdk
- android phone
- jumper wires
connecting blue tooth modem to arduino
Important thing is that your modem supports 5v power. Many boards are built for 3v. Only four wires needed:
- Connect grd on arduino to grd on modem
- Connect 5v of arduio to vcc on modem
- Connect arduino digital pin 2 to tx on modem
- Connect arduino digital pin 3 to rx on modem
Do not use pins on arduino that have a tx and rx printed on them. They are used to communicate with your pc while connected by usb.
connecting relay to your arduino
This is straight forward too:
- Connect grd on arduino to grd on relay module.
- Connect 5v of arduino to vcc on relay module.
- Connect digital pin 13 to Ch1 or In1 on relay module.
- Connect digital pin 12 to Ch2 or In2 on relay module.
- e.c.t.
arduino code
Whenever a 40 (decimal) byte value is sent from smartphone, then the relay1 switches state. Whenever a 41 (decimal) byte value is sent from smartphone, then the relay2 switches state.
#include <SoftwareSerial.h>
int relay1 = 13;
int relay2 = 12;
int bluetoothTx = 2;
int bluetoothRx = 3;
int relay1on = 0;
int relay2on = 0;
SoftwareSerial bluetooth(bluetoothTx, bluetoothRx);
// the setup routine runs once when you press reset:
void setup() {
pinMode(relay1, OUTPUT);
pinMode(relay2, OUTPUT);
Serial.begin(9600); // Begin the serial monitor§§§§9600bps
bluetooth.begin(115200); // The Bluetooth Mate defaults to 115200bps
}
// the loop routine runs over and over again forever:
void loop() {
if(bluetooth.available()) // If the bluetooth sent any characters
{
byte btInput = bluetooth.read();
Serial.print((char)btInput);
if(btInput == 41)
{
if(relay1on == 0)
{
digitalWrite(relay1, HIGH);
bluetooth.print("A1 ON");
relay1on = 1;
}
else
{
digitalWrite(relay1, LOW);
bluetooth.print("A1 OFF");
relay1on = 0;
}
}
if(btInput == 42)
{
if(relay2on == 0)
{
digitalWrite(relay2, HIGH);
bluetooth.print("A2 ON");
relay2on = 1;
}
else
{
digitalWrite(relay2, LOW);
bluetooth.print("A2 OFF");
relay2on = 0;
}
}
}
if(Serial.available()) // If stuff was typed in the serial monitor, for testing purposes.
{
// Send any characters the Serial monitor prints to the bluetooth
bluetooth.print((char)Serial.read());
}
}
android code
There are plenty of tutorials on the web.
http://developer.android.com/guide/topics/connectivity/bluetooth.html
Pair devices first. The bt modem is usually visible to other devices.
package ch.arduinobt;
public interface BlueToothListener {
void bytesReceived(byte[] bytes);
void connected(String deviceName);
void connecting();
void disconnected();
void error(String error);
}
package ch.arduinobt;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.UUID;
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.content.Intent;
public class BlueTooth {
public static BlueTooth current = new BlueTooth();
private final static int REQUEST_ENABLE_BT = 1;
private Set<BluetoothDevice> pairedDevices;
private BluetoothAdapter mBluetoothAdapter;
private UUID MY_UUID = UUID
.fromString("00001101-0000-1000-8000-00805F9B34FB");
List<BlueToothListener> listeners = new ArrayList<BlueToothListener>();
ConnectThread thread;
private class ConnectThread extends Thread {
private final BluetoothSocket mmSocket;
private final BluetoothDevice mmDevice;
public ConnectThread(BluetoothDevice device) {
// Use a temporary object that is later assigned to mmSocket,
// because mmSocket is final
BluetoothSocket tmp = null;
mmDevice = device;
// Get a BluetoothSocket to connect with the given BluetoothDevice
try {
// MY_UUID is the app's UUID string, also used by the server
// code
tmp = device.createRfcommSocketToServiceRecord(MY_UUID);
} catch (IOException e) {
error(e);
}
mmSocket = tmp;
}
public void run() {
// Cancel discovery because it will slow down the connection
mBluetoothAdapter.cancelDiscovery();
try {
// Connect the device through the socket. This will block
// until it succeeds or throws an exception
mmSocket.connect();
// Listeners
for (BlueToothListener listener : listeners) {
listener.connected(mmDevice.getName());
}
} catch (IOException connectException) {
error(connectException);
// Unable to connect; close the socket and get out
try {
mmSocket.close();
} catch (IOException closeException) {
}
return;
}
// Do work to manage the connection (in a separate thread)
try {
manageConnectedSocket(mmSocket);
} catch (Exception e) {
e.printStackTrace();
}
finally
{
}
}
/** Will cancel an in-progress connection, and close the socket */
public void cancel() {
if (mmSocket != null) {
try {
mmSocket.close();
} catch (IOException e) {
}
}
}
/* Call this from the main activity to send data to the remote device */
public void write(byte[] bytes) throws IOException {
mmSocket.getOutputStream().write(bytes);
}
}
public void addBtListener(BlueToothListener listener) {
listeners.add(listener);
}
public void removeBtListener(BlueToothListener listener) {
listeners.remove(listener);
}
public void startSocket(String deviceName) {
for(BlueToothListener listener : listeners)
{
listener.connecting();
}
cancel();
BluetoothDevice device = null;
for (BluetoothDevice dev : pairedDevices) {
String name = dev.getName();
if (name.equalsIgnoreCase(deviceName)) {
device = dev;
break;
}
}
if (device != null) {
thread = new ConnectThread(device);
thread.start();
} else {
error(new IOException("BT device not found"));
}
}
private void error(Exception ex) {
String message = ex.getMessage();
for (BlueToothListener listener : listeners) {
listener.error(message);
}
}
public void cancel() {
if (thread != null) {
thread.cancel();
}
}
/* Call this from the main activity to send data to the remote device */
public void write(byte[] bytes) throws IOException {
if (thread != null) {
thread.write(bytes);
}
else
{
throw new RuntimeException("not conneted to bt device");
}
}
private void manageConnectedSocket(BluetoothSocket socket) throws Exception {
byte[] buffer = new byte[1024]; // buffer store for the stream
int bytes; // bytes returned from read()
while (true) {
bytes = socket.getInputStream().read(buffer);
if (bytes > 0) {
sendBytesToReceivers(buffer, bytes);
}
}
}
private void sendBytesToReceivers(byte[] buffer, int length) {
byte[] toSend = new byte[length];
System.arraycopy(buffer, 0, toSend, 0, length);
for (BlueToothListener listener : listeners) {
listener.bytesReceived(toSend);
}
}
public List<String> getBoundDevices() {
List<String> deviceNames = new ArrayList<String>();
try {
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if (mBluetoothAdapter == null) {
throw new RuntimeException("Does not support bt");
}
if (!mBluetoothAdapter.isEnabled()) {
BlueToothNotEnabledException exception = new BlueToothNotEnabledException();
throw exception;
}
pairedDevices = mBluetoothAdapter.getBondedDevices();
// If there are paired devices
if (pairedDevices.size() > 0) {
// Loop through paired devices
for (BluetoothDevice device : pairedDevices) {
String name = device.getName();
deviceNames.add(name);
}
}
} catch (Exception ex) {
error(ex);
}
return deviceNames;
}
}