Android Bluetooth Controlled Outlet

15K7829

Intro: Android Bluetooth Controlled Outlet

** Be careful when handling electricity, do your research to make sure you don't get hurt or burn anything

There are so many options for the microcontroller you can use for this. I've used an arduino as well as a digispark. Both work well. Just get the power supply for the arduino spliced into the main line coming in. This is just an inspirational guide


This is a fairly simple and relatively cheap project that allows you to turn on and off an outlet via Bluetooth on your android phone. The materials I used were:

5V Relay Module  -- www.amazon.com/gp/product/B0057OC6D8/ref=oh_details_o00_s00_i02  (any other relay should do, this was what I found)
Arduino R3  -- http://www.amazon.com/gp/product/B006H06TVG/ref=oh_details_o00_s00_i01
Bluetooth Shield for Arduino   --  http://www.amazon.com/gp/product/B007BYI172/ref=oh_details_o04_s00_i00  (the regular BT antennas will work too)
4 section gang box, or any other box to hold everything.
outlet, wires, extra surge protector, acrylic sheet, or any other cover.

If you were to buy everything fresh, it would probably run around 65-75 dollars depending on where and how you shop.

With the bluetooth shield I got, the printed Tx and Rx selectors are reversed, at least for the libraries i found. The relay is also opposite what you would think,  as in HIGH closes the relay and LOW opens the gate.

Split the one of the lines on your power cord,  I did the black one, attach the one going to your wall to the common terminal and the other to the normally open terminal.
more info can be found here:
http://arduino-info.wikispaces.com/ArduinoPower
all about power and the arduino as well as nice pictures and stuff.

You will need to have a separate power source for your arduino, I have a 9v running out the back.

I have found BlueTerm to be the best way of communicating from your phone to the arduino BT
https://play.google.com/store/apps/details?id=es.pymasde.blueterm&hl=en
It is free and smooth. There are other programs out there that do just the same and maybe more, But I like this one because it's simple.

**Also, check out the MIT App Inventor site, google it, create a pretty simple app to set up an alarm kind of thing to turn on a light in the morning or something. Look at the pics for an example of the code. Grab the id from your bt and write it in. 
**

Upload the code and make sure everything is plugged in and you're ready to turn off your lights!

I did try to make this outlet dim-able, but my relay didn't like it, it just stayed open when I had something plugged in.

**UPDATED CODE:

#include <SoftwareSerial.h>   //Software Serial Port
#include <MeetAndroid.h>  //library for android BT comunication

#define RxD 0//bt white
#define TxD 2//bt yellow
#define switchPin 1 //whatever the relay switch pin is connected to
#define DEBUG_ENABLED  1 //need this for stuff
SoftwareSerial blueToothSerial(RxD,TxD);

void setup()
{
Serial.begin(9600);
pinMode(RxD,INPUT);
pinMode(TxD, OUTPUT);
pinMode(switchPin,OUTPUT);
setupBlueToothConnection();
digitalWrite(switchPin,LOW); //turns relay on at startup
}

void loop()
{
  char recvChar;
  if(blueToothSerial.available())//check if there's any data sent from the remote bluetooth shield
     {
      recvChar = blueToothSerial.read();
      //blueToothSerial.print('data recieved');
      Serial.println(recvChar);
      Serial.write(recvChar);
   switch(recvChar)
    {
      case '1':
      digitalWrite(switchPin,LOW);//turn relay on
      break;
      case '0':
      digitalWrite(switchPin,HIGH);//turn off
      break;

    } //end switch statement

     }//end BT communication
}//end loop

void setupBlueToothConnection()//getting bluetooth working
{
  blueToothSerial.begin(9600); //Set BluetoothBee BaudRate to default baud rate 38400
  blueToothSerial.print("\r\n+STWMOD=0\r\n"); //set the bluetooth work in slave mode
  blueToothSerial.print("\r\n+STNA=SeeedBTSlave\r\n"); //set the bluetooth name as "SeeedBTSlave"
  blueToothSerial.print("\r\n+STOAUT=1\r\n"); // Permit Paired device to connect me
  blueToothSerial.print("\r\n+STAUTO=0\r\n"); // Auto-connection should be forbidden here
  delay(2000); // This delay is required.
  blueToothSerial.print("\r\n+INQ=1\r\n"); //make the slave bluetooth inquirable
  Serial.println("The slave bluetooth is inquirable!");
  delay(2000); // This delay is required.
  blueToothSerial.flush();
}


**
-----------------------------------------------------------------------------------------------------------------------

Original code:

#include    //Software Serial Port
#define RxD 6 //oppsite what the board is lables as
#define TxD 2//oppsite what the board is lables as
#define switchPin 9 //relay connector
#define DEBUG_ENABLED  1
SoftwareSerial blueToothSerial(RxD,TxD);
char recvChar;

void setup()
{
Serial.begin(38400);
pinMode(RxD,INPUT);
pinMode(TxD, OUTPUT);
pinMode(switchPin,OUTPUT);
setupBlueToothConnection();
digitalWrite(switchPin,HIGH); //turns relay off
}

void setupBlueToothConnection()
{
  blueToothSerial.begin(38400); //Set BluetoothBee BaudRate to default baud rate 38400
  delay(1000);
  blueToothSerial.print("\r\n+STWMOD=0\r\n"); //set the bluetooth work in slave mode
  blueToothSerial.print("\r\n+STNA=SeeedBTSlave\r\n"); //set the bluetooth name as "SeeedBTSlave"
  blueToothSerial.print("\r\n+STOAUT=1\r\n"); // Permit Paired device to connect me
  blueToothSerial.print("\r\n+STAUTO=0\r\n"); // Auto-connection should be forbidden here
  delay(2000); // This delay is required.
  blueToothSerial.print("\r\n+INQ=1\r\n"); //make the slave bluetooth inquirable
  Serial.println("The slave bluetooth is ready!");
  delay(2000); // This delay is required.
  blueToothSerial.flush();
}//end setup bt connection

//Checks if the response "OK" is received
void CheckOK()
{
char a,b;
while(1)
{
if(blueToothSerial.available())
{
a = blueToothSerial.read();

if('O' == a)
{
// Wait for next character K. available() is required in some cases, as K is not immediately available.
while(blueToothSerial.available())
{
b = blueToothSerial.read();
break;
}
if('K' == b)
{
break;
}
}
}
}

while( (a = blueToothSerial.read()) != -1)
{
//Wait until all other response chars are received
}
}// end check ok

void sendBlueToothCommand(char command[])
{
blueToothSerial.print(command);
CheckOK();
}

void loop()
{
      recvChar = blueToothSerial.read();
      Serial.println(recvChar);
      Serial.write(recvChar);

   switch(recvChar)
    {
      case '1':
      digitalWrite(switchPin,LOW);//opposite to what you would think
      blueToothSerial.println("relay on");
      break;

      case '0':
      digitalWrite(switchPin,HIGH);
      blueToothSerial.println("relay off");
      break;

    }// end switch



}//end loop

28 Comments

Can I use a 12V relay Module instead of 5V? Also can it also be a password protected bluetooth?

I'd imagine you would want to stick with a 5v relay, as that is what the Arduino puts out to activate the relay, or whatever else is hooked to the pins or power out. If you have an external 12 power source for the relay, maybe, it depends on a lot of stuff, like the particular workings of the relay. Test it and se what happens. I'm assuming you have a 12v relay laying around someplace and that's why you want to use it.

For the password protected bluetooth, I don't know what that is. The one's I've messed with all have that connection code thing, the 4 digit password. Once you connect your phone to that bt id then you're set to keep open communication with it.

First of all, I'm going to ask many question to you because I'm a newbie with this kind of field. I chose this project because its very cool to control your outlet (like turning it off while your brother surfing the net all day long). Can the code works with an arduino clone(ATmega168/168P Version or ATmega328/328P version)? Did you create the apk file for this project or you created it using MIT app inventor? Thanks. This is really a cool project.

It should work with anything that uses the same type of code, may just have to do some slight tweaking to get it to work right the way you want. I've done it with arduino and digispark.

I used the mit app inventor to create the apk, it's a very simple interface and just takes some time to fiddle around with it to see what works best for you and what you want the intent to be. I wanted mine to turn my bedroom light on in the morning at a predtermined time so that's what I built mine for.

What is the signal you are referring on your reply to ache003? My relay is quite different to your relay. I think I pick the wrong relay

Is the bluetooth module a slave or a master? I'm really a newbie with this kind of thing

smaw, can you help me fix the code? i will use the bluno type of arduino. thanks

http://www.circuit-help.com.ph/product/bluno-ble-arduino-uno/

set tx and rx to whatever the pins are on the board and it should all work I'd imagine. I don't know of any other way to call the bluetooth stuff built into that board, the instructions for it probably indicate what pins they are on.

is it okay if i use the BLUNO type of arduino? the one kind of arduino with built in bluetooth, do you think this will affect the code? thanks

The code will change, and I don't know how, but it would just be finding the pins needed for that and making the changes.

smaw51, can i buy your finished product? i'm very interested on this. is it possible? i'm based on the Philippines.

I could build you one, but it would cost you $100 not including shipping for my time and materials, so probably more worth it to just buy one. There are several different microcontrollers you could use, cheapest would be a digispark, but getting the power to work right is tough with the bluetooth needing 3.3v so a regular arduino, or even a knock off arduino is your best bet with the dedicated 3.3v out and prefered tx and rx pins, but they're big and bulky for something just controlling a relay.

thanks smaw!
in iploading the code, i will just copy your code and paste it to the arduino? will it work? thanks

Can someone show me a schematic diagram on how to connect the arduino, relay and blueetooth? also the power outlet? i cant catch up to the tutorial, newbie here.. thanks!

Hopefully this helps, using the white or black lines from the main power for the relay won't really matter. I'm sure there is a right way to do that, but it more than likely won't make any difference. Getting power to the arduino will mean having your adapter tied into the main 120v power line coming in too. There's a lot of way to do all of this, and each have their own pros and cons.

The pins you use to control the relay and bluetooth depend on whatever ones you want to use, tx and rx on the arduino are the ones I used.

I'm working on this project, can someone upload the correct working code? Thanks. HAppy holidays :)

This worked for me just recently with a little digispark thing. Stuff might have to change for you, depending on your particular hardware and what not. Different bluetooth dongles are different about how particular they are about the voltage they need to work right.

I uploaded some screenshots from the MIT beta app inventor thing online, google it, for making a quick and easy app for your phone to turn the lights on in the morning.

#include <SoftwareSerial.h>   //Software Serial Port
#include <MeetAndroid.h>  //library for android BT comunication

#define RxD 0//white
#define TxD 2//yellow
#define switchPin 1
#define DEBUG_ENABLED  1
SoftwareSerial blueToothSerial(RxD,TxD);

void setup()
{
Serial.begin(9600);
pinMode(RxD,INPUT);
pinMode(TxD, OUTPUT);
pinMode(switchPin,OUTPUT);
setupBlueToothConnection();
digitalWrite(switchPin,LOW); //turns relay on
}

void loop()
{
  char recvChar;
  if(blueToothSerial.available())//check if there's any data sent from the remote bluetooth shield
     {
      recvChar = blueToothSerial.read();
      //blueToothSerial.print('data recieved');
      Serial.println(recvChar);
      Serial.write(recvChar);
   switch(recvChar)
    {
      case '1':
      digitalWrite(switchPin,LOW);//opposite to what you would think
      break;
      case '0':
      digitalWrite(switchPin,HIGH);
      break;

    } //end switch statement

     }//end BT communication
}//end loop

void setupBlueToothConnection()
{
  blueToothSerial.begin(9600); //Set BluetoothBee BaudRate to default baud rate 38400
  blueToothSerial.print("\r\n+STWMOD=0\r\n"); //set the bluetooth work in slave mode
  blueToothSerial.print("\r\n+STNA=SeeedBTSlave\r\n"); //set the bluetooth name as "SeeedBTSlave"
  blueToothSerial.print("\r\n+STOAUT=1\r\n"); // Permit Paired device to connect me
  blueToothSerial.print("\r\n+STAUTO=0\r\n"); // Auto-connection should be forbidden here
  delay(2000); // This delay is required.
  blueToothSerial.print("\r\n+INQ=1\r\n"); //make the slave bluetooth inquirable
  Serial.println("The slave bluetooth is inquirable!");
  delay(2000); // This delay is required.
  blueToothSerial.flush();
}

hello

i'm working on an android application that can control the appliance on a surge protector via arduino and other component as you see in the pic, but I HAVE A PROBLEM in the code of the android it seems that their is a class missing ,if you please please can help me ?

Greetings,
I'm working on building this project with a Mentee at school to illustrate how this device could save the family money by using a smart phone to control the lights/stereo/xbox automatically as you walk through the typical home.
The issue we are having right now, I've used a Samsung Android and Iphone, and neither device can see the Arduio.
Help... looking for ideas to check?
Jim Downing
Science Project is due Monday night ...
and these devices can connect to other bluetooth stuff? Is the bluetooth device on your arduino lighting up and looking for a connection? make sure it is all connectected properly
More Comments