Last active
December 13, 2017 12:16
-
-
Save aaroneiche/9b6baf999467a267adfc to your computer and use it in GitHub Desktop.
Wireless outlet control for Arduino
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Wireless Outlet Control | |
* | |
* (c) 2016 Aaron Eiche, released under MIT License | |
* Use it, have fun with it. | |
* | |
* Built for my existing wireless outlets, written in Arduino | |
* Uses a common, inexpensive transmitter. 315Mhz or 433Mhz are typical. | |
* You'll need to determine codes for your own outlets. | |
* | |
* There's probably a more elegant method for doing this. | |
* | |
* Special thanks to Adam Honse, and his instructable that inspired this and | |
* gave me the approach for figuring it out. See this instructable: | |
* http://www.instructables.com/id/Cheap-Home-Automation-using-Wireless-Outlet-Module/ | |
*/ | |
// Set these according to the speed your outlets use. In microseconds. | |
// On, or 1 | |
#define HIGH_TIME_LONG 1800 | |
#define LOW_TIME_LONG 600 | |
// Off, or 0 | |
#define HIGH_TIME_SHORT 600 | |
#define LOW_TIME_SHORT 1800 | |
#define transmit_pin 4 //Pin we're sending the signal out on. | |
#define led_pin 13 //Microcontroller LED Pin, for testing. | |
void setup() { | |
// put your setup code here, to run once: | |
pinMode(transmit_pin, OUTPUT); | |
pinMode(led_pin, OUTPUT); | |
} | |
void loop() { | |
// This is a simple loop, turns lights on, turns lights off. | |
digitalWrite(led_pin,HIGH); | |
ch3on(); | |
ch2on(); | |
delay(2000); | |
digitalWrite(led_pin,LOW); | |
ch3off(); | |
ch2off(); | |
delay(2000); | |
} | |
//The common beginning 4 bits: 0110 | |
void opening(){ | |
bit_short(); | |
bit_long(); | |
bit_long(); | |
bit_short(); | |
} | |
//The common ending 4 bits: 0000 | |
void closing(){ | |
bit_short(); | |
bit_short(); | |
bit_short(); | |
bit_short(); | |
} | |
//We send the signal a few times to make sure it was received. | |
// Ch1 "on" Code: 1000 1000 | |
void ch1on(){ | |
for(int i = 0; i < 3; i++){ | |
delay(10); | |
opening(); | |
bit_long(); | |
bit_short(); | |
bit_short(); | |
bit_short(); | |
bit_long(); | |
bit_short(); | |
bit_short(); | |
bit_short(); | |
closing(); | |
} | |
} | |
// Ch1 "off" Code: 1000 0100 | |
void ch1off(){ | |
for(int i = 0; i < 3; i++){ | |
delay(10); | |
opening(); | |
bit_long(); | |
bit_short(); | |
bit_short(); | |
bit_short(); | |
bit_short(); | |
bit_long(); | |
bit_short(); | |
bit_short(); | |
closing(); | |
} | |
} | |
// Ch2 "on" Code: 1000 0010 | |
void ch2on(){ | |
for(int i = 0; i < 5; i++){ | |
delay(10); | |
opening(); | |
bit_long(); | |
bit_short(); | |
bit_short(); | |
bit_short(); | |
bit_short(); | |
bit_short(); | |
bit_long(); | |
bit_short(); | |
closing(); | |
} | |
} | |
// Ch2 "off" Code: 1000 0001 | |
void ch2off(){ | |
for(int i = 0; i < 5; i++){ | |
delay(10); | |
opening(); | |
bit_long(); | |
bit_short(); | |
bit_short(); | |
bit_short(); | |
bit_short(); | |
bit_short(); | |
bit_short(); | |
bit_long(); | |
closing(); | |
} | |
} | |
// Ch3 "on" Code: 1001 0000 | |
void ch3on(){ | |
for(int i = 0; i < 5; i++){ | |
delay(10); | |
opening(); | |
bit_long(); | |
bit_short(); | |
bit_short(); | |
bit_long(); | |
bit_short(); | |
bit_short(); | |
bit_short(); | |
bit_short(); | |
closing(); | |
} | |
} | |
// Ch3 "on" Code: 1010 0000 | |
void ch3off(){ | |
for(int i = 0; i < 5; i++){ | |
delay(10); | |
opening(); | |
bit_long(); | |
bit_short(); | |
bit_long(); | |
bit_short(); | |
bit_short(); | |
bit_short(); | |
bit_short(); | |
bit_short(); | |
closing(); | |
} | |
} | |
//Toggle durations for "1" | |
void bit_long() | |
{ | |
digitalWrite(transmit_pin,HIGH); | |
delayMicroseconds(HIGH_TIME_LONG); | |
digitalWrite(transmit_pin,LOW); | |
delayMicroseconds(LOW_TIME_LONG); | |
} | |
//Toggle durations for "0" | |
void bit_short() | |
{ | |
digitalWrite(transmit_pin,HIGH); | |
delayMicroseconds(HIGH_TIME_SHORT); | |
digitalWrite(transmit_pin,LOW); | |
delayMicroseconds(LOW_TIME_SHORT); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
"* There's probably a more elegant method for doing this." I bet there is, but... your codes just unlocked and sorted all the jumbled up research in my head regarding bit streaming RF. I'd spent days (...more like a week) trying to unravel RCSwitch and VirtualWire etc., none of which I could get to transmit correctly. Your code was so clearly written, without all the "elegant" fluff, that it is simply - BRILLIANT!