├── README.md └── esp8266_rcswitch.ino /README.md: -------------------------------------------------------------------------------- 1 | # **ESP8266-RCSwitch** 2 | # *Updated 23-May-2015* 3 | Replaced placeholder "XXXXXX" for RCSwitch Library name with correct library on github (ninjablocks) 4 | ## *Description:* 5 | This is an Arduino IDE project that targets the cheap ESP8266 WiFi module. The example program 6 | will turn a 433MHz remote power outlet on or off from a web page served by the ESP8266. 7 | 8 | This is a very quick-and-dirty demonstration to get you started! 9 | 10 | ## *Configuration:* 11 | * Download, Install and test the IDE per instructions at http://makezine.com/2015/04/01/installing-building-arduino-sketch-5-microcontroller/ 12 | * Download and Install the ESP8266-RCSwitch from this git into your sketchbook 13 | * Add the RCSwitch Library from https://github.com/ninjablocks/arduino/tree/master/RCSwitch to your Arduino IDE 14 | * Find the codes for your RC Switch using https://github.com/ninjablocks/433Utils (RF_Sniffer.ino) 15 | * Modify the ESP8266-RCSwitch sketch, changing the on and off codes to the codes you sniffed using the RF_Sniffer and the outlet-supplied remote control 16 | * NOTE: EtekCity Remote Outlets use pulselength = 189 17 | * Modify the SSID and PASSWD values in the sketch to look for your WiFi router 18 | 19 | ## *Testing:* 20 | * Run the Serial Monitor from the new IDE to find the IP address or use your router's client tools to find it 21 | * http://"ESP8266ip"/sw1/0 = OFF; http://"ESP8266ip"/sw1/1 = ON 22 | 23 | ## *Acknowledgements:* 24 | In no particular order- 25 | * http://timleland.com/wireless-power-outlets 26 | * http://makezine.com/2015/04/01/installing-building-arduino-sketch-5-microcontroller/ 27 | * https://github.com/sandeepmistry/esp8266-Arduino 28 | 29 | -------------------------------------------------------------------------------- /esp8266_rcswitch.ino: -------------------------------------------------------------------------------- 1 | /* 2 | * This sketch demonstrates how to set up a simple HTTP-like server. 3 | * The server will set a switch depending on the request 4 | * http://server_ip/sw1/0 will turn sw1 off 5 | * http://server_ip/sw1/1 will turn sw1 on 6 | * server_ip is the IP address of the ESP8266 module, will be 7 | * printed to Serial when the module is connected. 8 | */ 9 | #include "RCSwitch.h" 10 | #include 11 | 12 | RCSwitch mySwitch = RCSwitch(); 13 | 14 | const char* ssid = "SSID"; 15 | const char* password = "PASSWORD"; 16 | 17 | // Create an instance of the server 18 | // specify the port to listen on as an argument 19 | WiFiServer server(80); 20 | 21 | void setup() { 22 | Serial.begin(115200); 23 | delay(10); 24 | 25 | // prepare GPIO2 26 | pinMode(2, OUTPUT); 27 | 28 | // Transmitter is connected to ESP8266 PIN #2 29 | mySwitch.enableTransmit(2); 30 | //EtekCity ZAP Remote Outlets use pulse of approximately 189 31 | mySwitch.setPulseLength(189); 32 | 33 | // Connect to WiFi network 34 | Serial.println(); 35 | Serial.println(); 36 | Serial.print("Connecting to "); 37 | Serial.println(ssid); 38 | 39 | WiFi.begin(ssid, password); 40 | 41 | while (WiFi.status() != WL_CONNECTED) { 42 | delay(500); 43 | Serial.print("."); 44 | } 45 | Serial.println(""); 46 | Serial.println("WiFi connected"); 47 | 48 | // Start the server 49 | server.begin(); 50 | Serial.println("Server started"); 51 | 52 | // Print the IP address 53 | Serial.println(WiFi.localIP()); 54 | } 55 | 56 | void loop() { 57 | 58 | // Check if a client has connected 59 | WiFiClient client = server.available(); 60 | if (!client) { 61 | return; 62 | } 63 | 64 | // Wait until the client sends some data 65 | Serial.println("new client"); 66 | while(!client.available()){ 67 | delay(1); 68 | } 69 | 70 | // Read the first line of the request 71 | String req = client.readStringUntil('\r'); 72 | Serial.println(req); 73 | client.flush(); 74 | 75 | // Match the request 76 | int val; 77 | if (req.indexOf("/sw1/0") != -1) { 78 | val = 0; 79 | // Insert your "OFF" code here, before ", 24)" 80 | mySwitch.send(12345, 24);} 81 | else if (req.indexOf("/sw1/1") != -1) { 82 | val = 1; 83 | // Insert your "ON" code here, before ", 24)" 84 | mySwitch.send(54321, 24); 85 | } 86 | else if (req.indexOf("favicon.ico") != -1) { 87 | // ignore favicon requests from browser 88 | val = -1; 89 | } 90 | else { 91 | val = -10; 92 | Serial.println("invalid request"); 93 | } 94 | 95 | client.flush(); 96 | 97 | // Prepare the response 98 | // Handle (ignore) favicon requests with a 404 response 99 | // https://github.com/esp8266/Arduino/issues/2080 100 | String s; 101 | if (val == -1){ 102 | s = "HTTP/1.1 404 \r\n"; 103 | } 104 | // handle invalid requests 105 | else if (val == -10){ 106 | s = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n\r\n\r\nInvalid request! "; 107 | s += "\r\nEnter URL as: \"<ip-address>/sw1/<VAL>\" where <VAL> is 0 or 1"; 108 | s += "\n"; 109 | } 110 | // correct requests 111 | else{ 112 | s = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n\r\n\r\nSwitch 1 is now "; 113 | s += (val)?"ON":"OFF"; 114 | s += "\n"; 115 | } 116 | // Send the response to the client 117 | client.print(s); 118 | delay(1); 119 | Serial.println("Client disonnected"); 120 | 121 | // The client will actually be disconnected 122 | // when the function returns and 'client' object is detroyed 123 | } 124 | --------------------------------------------------------------------------------