├── EspRfRemote.ino ├── README.md └── RF.h /EspRfRemote.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 11 | #include 12 | #include 13 | 14 | #include "RF.h" 15 | 16 | const char* ssid = "..."; 17 | const char* password = "..."; 18 | 19 | const int RF_OSC = 200; 20 | 21 | ESP8266WebServer server(80); 22 | 23 | void handleRoot() { 24 | String message = ""; 25 | message += "Set up the RF code and press on/off to generate and call the corrensponding url:

"; 26 | message += "
"; 27 | message += "Output pin D "; 28 | message += "pulseµs "; 29 | message += "ID "; 30 | message += ""; 31 | message += "channel "; 32 | message += ""; 33 | message += ""; 34 | message += "

"; 35 | message += "URL constucted and called: ...

"; 36 | message += "have fun -bitluni"; 37 | server.send(200, "text/html", message); 38 | } 39 | 40 | void handleNotFound(){ 41 | String message = "File Not Found\n\n"; 42 | message += "URI: "; 43 | message += server.uri(); 44 | message += "\nMethod: "; 45 | message += (server.method() == HTTP_GET)?"GET":"POST"; 46 | message += "\nArguments: "; 47 | message += server.args(); 48 | message += "\n"; 49 | for (uint8_t i=0; i 0? 1: 0)); 85 | server.send(200, "text/plain", out); 86 | } 87 | 88 | void setup(void){ 89 | Serial.begin(115200); 90 | WiFi.begin(ssid, password); 91 | Serial.println(""); 92 | 93 | // Wait for connection 94 | while (WiFi.status() != WL_CONNECTED) { 95 | delay(500); 96 | Serial.print("."); 97 | } 98 | Serial.println(""); 99 | Serial.print("Connected to "); 100 | Serial.println(ssid); 101 | Serial.print("IP address: "); 102 | Serial.println(WiFi.localIP()); 103 | 104 | server.on("/", handleRoot); 105 | server.on("/rf", handleRf); 106 | server.onNotFound(handleNotFound); 107 | 108 | server.begin(); 109 | Serial.println("HTTP server started"); 110 | } 111 | 112 | void loop(void) 113 | { 114 | server.handleClient(); 115 | } 116 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # EspRfRemote 2 | Sketch to use an ESP mit Arduino as a RF remote for chap-ass power outlets (BH9938) 3 | 4 | Step 1: 5 | install Arduino IDE 6 | 7 | Step 2: 8 | Add to File -> Preferences -> Additional Boards.. the url: http://arduino.esp8266.com/stable/package_esp8266com_index.json 9 | 10 | Step 3: 11 | Tools -> Boards Manager.. install ESP8266 by esp community 12 | 13 | Step 4: 14 | Connect your device prefferably WeMos and set serial port 15 | And connect a 4433Mhs transmitter to pin D6 (or other you specify later in the web ui) 16 | 17 | Step 5: 18 | set ssid and password to your wifi crendentails 19 | 20 | Step 6: 21 | Upload sketch. Open Tools -> Serial Monitor with 115200 Baud rate 22 | 23 | Step 7: 24 | Successful connect will show ip 25 | Use your browser to open the ip 26 | http:// 27 | 28 | A simple UI will show up. You can control the parameters and the corresponding url that can be used to perform the action will be shown 29 | -------------------------------------------------------------------------------- /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 | --------------------------------------------------------------------------------