├── BasicRf.ino ├── README.md └── RF.h /BasicRf.ino: -------------------------------------------------------------------------------- 1 | /* 2 | Released under Creative Commons Attribution 4.0 3 | by bitluni 2016 4 | https://creativecommons.org/licenses/by/4.0/ 5 | Attribution means you can use it however you like as long you 6 | mention that it's base on my stuff. 7 | I'll be pleased if you'd do it by sharing http://youtube.com/bitlunislab 8 | */ 9 | 10 | #include "RF.h" 11 | 12 | const int ID = 28013; //0..1048575 28013 was preprogrammed 13 | const int CHANNEL = 0; //0..1 14 | const int RF_OSC = 200; //rf signal cycle legth(µs). 200 works for me 15 | 16 | const int RF_OUT = D6; 17 | 18 | void setup() 19 | { 20 | pinMode(RF_OUT, OUTPUT); 21 | } 22 | 23 | void loop() 24 | { 25 | //turn off 26 | for(int i = 0; i < 5; i++) 27 | { 28 | rfWriteCode(RF_OUT, RF_OSC, ID, (1 << (CHANNEL + 1))); 29 | } 30 | delay(3000); 31 | //turn on 32 | for(int i = 0; i < 5; i++) 33 | { 34 | rfWriteCode(RF_OUT, RF_OSC, ID, 1 | (1 << (CHANNEL + 1))); 35 | } 36 | delay(3000); 37 | } 38 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # BasicRf 2 | Basic sketch to use an Arduino as a RF remote for chap-ass power outlets (BH9938) 3 | -------------------------------------------------------------------------------- /RF.h: -------------------------------------------------------------------------------- 1 | /* 2 | Released under Creative Commons Attribution 4.0 3 | by bitluni 2016 4 | https://creativecommons.org/licenses/by/4.0/ 5 | Attribution means you can use it however you like as long you 6 | mention that it's base on my stuff. 7 | I'll be pleased if you'd do it by sharing http://youtube.com/bitlunislab 8 | */ 9 | 10 | //Works with BH9938 with t=200 11 | 12 | inline void rfPreamble(int pin, int t) 13 | { 14 | int m = micros(); 15 | digitalWrite(pin, 1); 16 | while(micros() - m < t); 17 | digitalWrite(pin, 0); 18 | while(micros() - m < t * 32); 19 | } 20 | 21 | inline void rfWriteBit(int pin, int t, int b) 22 | { 23 | int m = micros(); 24 | if(b) 25 | { 26 | digitalWrite(pin, 1); 27 | while(micros() - m < t * 3); 28 | digitalWrite(pin, 0); 29 | while(micros() - m < t * 4); 30 | } 31 | else 32 | { 33 | digitalWrite(pin, 1); 34 | while(micros() - m < t); 35 | digitalWrite(pin, 0); 36 | while(micros() - m < t * 4); 37 | } 38 | } 39 | 40 | void rfWriteCode(int pin, int t, int code, int data) 41 | { 42 | rfPreamble(pin, t); 43 | for(int i = 0; i < 20; i++) 44 | { 45 | rfWriteBit(pin, t, code & 1); 46 | code >>= 1; 47 | } 48 | for(int i = 0; i < 4; i++) 49 | { 50 | rfWriteBit(pin, t, data & 1); 51 | data >>= 1; 52 | } 53 | } 54 | --------------------------------------------------------------------------------