Introduction: 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
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