Introduction: Using an ESP8266 to Control Mains Sockets Using 433mhz Transmitter and Receiver

Picture of Using an ESP8266 to Control Mains Sockets Using 433mhz Transmitter and Receiver

In this Instructable I will show how to control a main socket using a 433mhz transmitter and receiver.

I started this project because I wanted to be able to switch my lamp on and off without using the remote that I already had for switching the mains sockets. So that if I was in another room and forgot to switch off the lamp I could do it from my phone using home-assistant.

Step 1: What You'll Need for This Instructable

Picture of What You'll Need for This Instructable

You can get a number of different ESP8266 boards, I'm using a NodeMCU DevKit for mine. The 433mhz transmitter and receiver are pretty standard and can be purchased from ebay.

  1. ESP8266 board
  2. 433mhz RF transmitter and receiver pair for arduino
  3. Breadboard to make the circuit
  4. Some wires to connect it up

Step 2: Arduino Sketch for Receiving the 433mhz Codes From the Existing Remote

Picture of Arduino Sketch for Receiving the 433mhz Codes From the Existing Remote

First of all you'll need the rc-switch library (thanks to sui77 for this library and example code). You can get this from https://github.com/sui77/rc-switch

Once you have this installed you can simply load the example called ReceiveDemo_Advanced. With this example uploaded to your ESP8266 you should be able to 'sniff' the 433mhz signals from the transmitter that came with your remote socket.

Open up the serial monitor in the Arduino IDE and press a button which switched on the socket on your remote that came with your remote socket, and you should see something like this:

Received 1394007 / 24bit Protocol: 1

These are the only real import parts that you need to keep an eye out for, and you'll need to put these into the sending script to send the same data to the remote socket to switch on.

Then do the same for the off button on the remote, again make a note of the code.

This library and receiver supports the following chipsets:

  • SC5262 / SC5272
  • HX2262 / HX2272
  • PT2262 / PT2272
  • EV1527 / RT1527 / FP1527 / HS1527
  • Intertechno outlets

Step 3: How to Connect Your ESP8266 to the Receiver

Picture of How to Connect Your ESP8266 to the Receiver

The 433mhz receiver only needs 3 wires, these are for power and data.

So simply connect the VCC and GND on the receiver to VIN and GND on the ESP8266 and connect the data pin of the receiver to a GPIO of choice on the ESP8266 too.

In my project I used pin D3, which is GPIO0.

Step 4: Arduino Sketch for Sending the Codes to the Mains Sockets

To send the code to your remote control main socket simply use the following sketch, changing the decimal code that you got from the receiver.

/*
Example for different sending methods
   https://github.com/sui77/rc-switch/
*/
#include <RCSwitch.h>
RCSwitch mySwitch = RCSwitch();

void setup() {
Serial.begin(9600);
  
  // Transmitter is connected to Arduino Pin #0  
  mySwitch.enableTransmit(0);  // Optional set pulse length.
  // mySwitch.setPulseLength(320);
  
  // Optional set protocol (default is 1, will work for most outlets)
  // mySwitch.setProtocol(2);
  
  // Optional set number of transmission repetitions.
  // mySwitch.setRepeatTransmit(15);
}

void loop() {
/* Same switch as above, but using decimal code */
mySwitch.send(1394007, 24);
delay(2000);
mySwitch.send(1394006, 24);
delay(2000);
}

Step 5: How to Connect Your ESP8266 to the Transmitter

Picture of How to Connect Your ESP8266 to the Transmitter

Connected the transmitter to the ESP8266 is very simple too. There are only 3 connections again, the same as the receiver, VCC, GND and Data.

So just connect VCC to VIN and GND to GND, and then connect the Data pin on the transmitter to a GPIO on the ESP8266 board.

I used GPIO0 which is pin D3.

Then once the sketch is uploaded you should see the remote socket switch on and off every 2 second. Proof it works...

Comments

JiříB17 (author)2017-04-05

I want sending temperature from Arduino from TX 433Mhz module and Dallas or DHT22 to ESP8266 devkit from RX 433mhz modul and showing temp on Nextion screen and app Blynk.

But OneWire.h library no working with ESP8266.. Or must I use RCSwitch.h library?
Thanks.

FrankS122 (author)2017-01-12

I don't understand how this works. How do you power the 433mhz module on the receiver with VIN form the nodemcu .

RussellG41 (author)FrankS1222017-03-06

The VIN is for input power up to 5v to the NodeMCU. (Not to be used when powered from the mini USB port. It will fry the board if you have both connected at the same time!) Look for the screen printed lettering on the NodeMCU labled 3.3 to power the rf receiver. I was able to receive codes while powered from this pin and using D3 for the data in.

AndrásT5 (author)2017-02-11

Be careful! The sample code above is using the setProtocol() and setPulseLength() in the wrong order !

setProtocol() sets the pulse length to the default value!

WizziL (author)2016-12-14

Do you need a logic level shifter for this project? I'm asking because ESP8266 is 3.3V and these RF modules are 5V

If you are using RF transmitter as transmitter only you can power it with 3.3V and in most cases it should work fine. Some of them out there need 3.5V to work correctly. If you would like to use RF module to recive data then you would have to power it with 5V and in turn you would have to use level shifter.

twotones (author)WizziL2017-01-14

Trying this myself, I can power a 433mhz with the 3.3V pin from the ESP8266 (12-E) without any logic shifter.

ajaxharg (author)2016-10-21

Very useful. I am writing a small program to sense water in case my UV water filter quartz glass shatters and floods the house. If water is detected then the plug to the water pump turns off. Your clear example got me up to speed very quickly. Thanks

dick55 (author)2016-04-11

What was the reason for using a NodeMCU, normally used for its built-in 2.4GHz WiFi capability?

padeath (author)dick552016-04-11

I used it so that I can control the GPIO pin to send the signal to the remote controlled power socket remotely :) as over a network connection using home-assistant. I can switch it on and off on a timer too, so that I can switch on a light in the evening even when I'm not at home.

dick55 (author)padeath2016-04-11

Can't this be done with an arduino board?

padeath (author)dick552016-04-12

Yeah it can, you're right, but you would have to use a ethernet shield to be able to connect to it using MQTT to get it to switch the socket on and off using a network connection.

dick55 (author)padeath2016-04-12

I have been confused about your project because there was no mention of any code for NodeMCU Wifi capability (and perhaps because I didn't study your write-up carefully enough). To do the remote capability you want you are apparently using some type of WiFi code. Am I supposed to infer that you are using Home Assist code? (I'm not familiar with Home Assist). If that had been clear I would not have made my first comment, and if you had made it clear in the reply that would have cleared up my confusion. Not trying to be mean, just noting that all of us need to be careful to not assume too much knowledge from our readers.

I do appreciate your replies. Thanks.

padeath (author)dick552016-04-12

Thanks for your comment, I does make me think that this wasn't complete in some ways. I write about being able to use this circuit to switch the socket on and off remotely but dont say how I'm doing that exactly. Mainly because at the I was writing this and creating the project for myself I hadn't actually done that part either.

I did write at the very end of the Instructable that you can find details on how to hook this up with home-assistant, but I remove that in the end because I haven't written an Instructable about that.

If you would like a write up on how to do this I can do one, not a problem.

dick55 (author)padeath2016-04-12

No need to do write-up for me now. I was just trying to understand your interesting project. However, for the Instructable to be useful to a person that wants to build this and doesn't have the necessary background, the write-up would be necessary. Maybe you should wait for one or more readers to request it. This exchange we've had should prompt them.

Best regards,

John R. Anderson

diy_bloke (author)dick552016-09-12

I have an ESP8266 code that allows you to switch RC switches on and off from a webpage. I will publish it soon in an ibble, but anyone interested and not able to wait can drop me a message

Jasea (author)2016-09-01

Hi,
When you connected the receiver to get the code from the remote, and used pin D3 for the data input, did you make any changes to the ReceiveDemo_Advanced program? For example did you change the Interupt number on the following line

mySwitch.enableReceive(0); // Receiver on interrupt 0 => that is pin #2

Regards

John