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

109,744

78

31

Introduction: 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

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

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

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

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...

2 People Made This Project!

Recommendations

  • First Time Author Contest

    First Time Author Contest
  • Fruit and Veggies Speed Challenge

    Fruit and Veggies Speed Challenge
  • Fandom Contest

    Fandom Contest

31 Comments

0
ob2s
ob2s

Question 4 months ago on Step 5

This is very helpful. The version of receiver you have has a very short range even with an antenna added. Digikey sells one that has excellent range (Picture: digikey left, original right). Anyway, I was able to receive the codes from my outlet remote. On and Off codes. And then when I send them via the transmitter, I see them on the receiver exactly like the remote sent them, but it doesn't switch the outlet at all. Distance doesn't matter. Is it encoding or something ? the "24" ? My codes are 7 digits as well. I also tried to have the outlet relearn sending the code in desperation. Thanks

capo678.jpg
0
svatos.jirka
svatos.jirka

Question 1 year ago

Hi,
The greate article! Thanks.
I used NodeMcu and the same receiver. I Didnt change code above (receiver); enableReceiver(0), pin D3. I used external power supply (USB didnt want to work). I used logic analyzer to see data from receiver (D3). If I press key on switch (433Mhz) then I see data on D3 (via logic analyzer), but "mySwitch.available()" always returns 0. It make me crazy, please help.

PS: I used also another receiver.
PS: The receiver should be 433 Mhz. Please check dimension of green cube (4.7 mm x 4.7m)

please help, with best regards, jiri

0
mssib28
mssib28

Answer 10 months ago

Hi,
if not solved --> use enableReceiver(D3), whenn connected at pin D3 on ESP8266 Node MCU.
Best regards

Martin

0
sanjayb48
sanjayb48

Answer 1 year ago

Hello, I have the same problem and wondering if you have found a resolution. I have used pin D3 (GPIO 0) on ESP8266 for data and tried powering the receiver with the nodemcu's 3.3 V as well as external power supply (upto 5 V). There is no reception at the antenna. If anybody has gotten receiver to work, please tell about your approach. (PS - I have both transmitter and receiver working on Arduino Uno, using the RCSwitch library as shown in the sketch.)

0
sui777
sui777

11 months ago

You're welcome :)

0
doncel
doncel

1 year ago

Hi, I did the exact circuit as described but all I get in serial monitor is random symbols when I press nodemcu reset button. Would you mind showing me where the mistake is pls?

20200416_114633.jpgprs.png
0
luke_2700k
luke_2700k

1 year ago

Do you know how we could set it up to transmit multiple codes? I have a fan that has a 433Mhz RF remote and would like to be able to set fan speed high/meduim/low from home assistant as well as set up automations to turn it off again after a certain amount of time. Thanks in advance :)

0
Brl1nG
Brl1nG

2 years ago

On wich Pin of the nodemcu 1.0 do you connect the data pin of the receiver?

0
Bartmimore
Bartmimore

Question 3 years ago on Step 1

Can you help me understand why the IDE isn't finding the RCSwitch..h file when it shows as a tab in the sketch? I'm trying to compile for the NodeMCU, but can't get it to load.
Many Thanks, Roger

RFSwitch.jpg
0
ThongrobK
ThongrobK

Question 3 years ago on Introduction

Hello
This project can use Magnetic sensor (433Mhz) to connect esp8266

0
Bartmimore
Bartmimore

Question 3 years ago on Introduction

I've downloaded and unzipped the RCSwitch.h file and loaded it in the IDE. When I try to compile, I get this error - Arduino: 1.8.5 (Windows 10), Board: "NodeMCU 0.9 (ESP-12 Module), 80 MHz, 115200, 4M (3M SPIFFS)"
C:\Users\Roger\Documents\Arduino\rc-switch-master\examples\SendDemo\SendDemo.ino:8:22: fatal error: RCSwitch.h: No such file or directory
#include

^

compilation terminated.

exit status 1
Error compiling for board NodeMCU 0.9 (ESP-12 Module).

Looking at the IDE, I see the tab with "RCSwitch.h" and a tab for "Output".
Why can't the IDE find the right files to compile for he NodeMCU?

0
Jasea
Jasea

4 years ago

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

0
georgek52
georgek52

Reply 3 years ago

Hello Jasea,

Did you figure out this issue with the pins?

Br,

George

0
Jasea
Jasea

Reply 3 years ago

Hi George,

I never did manage to decode my remote controller, and in the end used a Sonoff switch that I reprogrammed.

John

0
JiříB17
JiříB17

4 years ago

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.

0
FrankS122
FrankS122

4 years ago

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

0
RussellG41
RussellG41

Reply 4 years ago

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.

0
AndrásT5
AndrásT5

4 years ago

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!

0
WizziL
WizziL

4 years ago

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