Introduction: Android Bluetooth Controlled Outlet

Picture of 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

Comments

crimsonfire19 (author)2015-02-02

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

smaw51 (author)crimsonfire192015-02-02

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.

crimsonfire19 (author)smaw512015-02-03

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.

smaw51 (author)crimsonfire192015-02-03

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.

crimsonfire19 (author)smaw512015-02-14

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

crimsonfire19 (author)smaw512015-02-09

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

ache003 (author)2015-01-27

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/

smaw51 (author)ache0032015-01-27

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.

ache003 (author)2015-01-23

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

smaw51 (author)ache0032015-01-23

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.

ache003 (author)2015-01-19

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

smaw51 (author)ache0032015-01-20

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.

ache003 (author)2015-01-17

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

ache003 (author)2015-01-11

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!

smaw51 (author)ache0032015-01-11

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.

ache003 (author)2014-12-23

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

smaw51 (author)ache0032014-12-24

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();
}

Guitarknack made it! (author)2014-05-04

I have successfully made this using smaw51's instructions. I did however, have to fix the #include code but everything worked after that. I also used both of the relays to control both plugs on the outlet. I connected to the arduino using Bluetooth spp pro app. (I liked the customized buttons that the app has) I also added code to create a "wig-wag" effect. Over all, nice job smaw51. It was a great idea which is leading to more....

ache003 (author)Guitarknack2014-10-03

hey buddy, can you share with me the code you have used? and the step by step in creating this project? thanks a lot...

Guitarknack (author)ache0032014-10-03

Hi there, please take a look at sea who's instructions.

farahqasem (author)2014-01-24

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 ?

neboyntx (author)2013-05-16

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 ...

smaw51 (author)neboyntx2013-05-16

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

neboyntx (author)smaw512013-05-16

Well... I've spent a few hours today.. And I made some progress.
Went and bought a Samsung Tablet.. The tablet was able to see and connect to the device. That was a good thing.
There were some issues with the code... I think I fixed that.. but it still maybe the issue.
I have power from a wall socket to the relay and tested with a voltage meter. So that's good...
Something is not opening the relay to let the power through.
Attached is the screen shot of where the compiler barfed....
I included the following: #include
What do you think about that line?
Help
Jimbo

neboyntx (author)neboyntx2013-05-16

Sorry the copy and paste didn't work..
#include

neboyntx (author)neboyntx2013-05-16

One more try... :)

# include "softwareserial.h"

Veda88 (author)2012-07-26

What android application do you use to connect with the outlet and to turn the lights on?

drmpf (author)Veda882012-11-23

If you want a menu driven Andriod control of lights and power that you can customise from the Arduino code with NO Andriod coding required, then check out the instructable  Android controlled Lights and Power, pfodDevice for Arduino

It uses the pfodApp , which can be used to control many different microdevices via bluetooth, from KnigthRider Lights to LCD displays to Garage Doors.

The menu items and screens displayed on the Android are completely controlled by simple Arduino code.  See the pfod Specification for the details and www.pfod.com.au for numerous example projects. 


smaw51 (author)2012-07-26

blueterm, found here
https://play.google.com/store/apps/details?id=es.pymasde.blueterm&hl=en

Press menu, connect to device, select your device, default password is usually 0000 or 1234. I use 1 to turn on and 0 for off.

This app is great for things like a bluetooth controlled car as it is fast to communicate and easy to work with.

smaw51 (author)2012-07-25

Mostly, I got some stuff here and there from examples on the use of the bluetooth device I added into my code to get it to work.

nail2010 (author)2012-07-25

Did you write the code for this project yourself?

About This Instructable

14,315views

77favorites

License:

Add instructable to: