User avatar
By RichardS
#43233 User
jamesengine

Description
First off – I love working with the ESP8266! Years ago – or maybe decades – using a Z80 you had an address bus and data bus and you had to build all the hardware for a simple I/O port - painful. Then came along the chips such as the ATMEL 89C2051 that had ports built in. Then the Microchip PIC chips that had built in ports, UART, I2C, etc. Now you have the ESP8266 – built in ports, UART, I2C, etc., and WIFI! IoT is now available to everyone at what I would say is stupid crazy low prices. The cost of this project was under $40 to control 5 outlets and the majority of the cost was for the 5 outlets at $30.

This project uses the ESP8255-01 to transmit on/off commands to remote outlet switches using a 433MHz transmitter and is controlled using the Blynk App on your phone – basically you can control 5 outlets pretty much from anywhere in the world! The setup is fairly simple using jumper wires and a very small breadboard (the green “Lego” in the photo).

Blynk App for cell phoneSmilies
:D :) ;) :( :o :shock: :? 8-) :lol: :x :P :oops: :cry: :evil: :twisted: :roll: :!: :?: :idea: :arrow: :| :mrgreen: :geek: :ugeek:
Arduino IDE for ESP8266
Included Arduino Sketches (see below)

The first thing to do is read the codes from the outlet transmitter. Make the following connections for the ESP8266 and the 433MHz Receiver:

ESP8266
GND --> GND
VCC --> +3.3V - NOT 5V!
TX --> To RX on computer
RX --> To TX on computer
CH_PD --> +3.3V
GPIO2 --> Data pin on 433MHz receiver

433MHz Receiver
VCC --> 3.3V
GND --> GND
DATA --> GPIO2 on ESP8266


Load the Arduino Sketch for Codes.ino. Once the program starts it will delay for 2 seconds giving you time to switch to the serial monitor. The sketch looks for a long low pulse which occurs between each code transmission. Without this you would get lots of garbage when the transmitter is not transmitting a code. By waiting for the low pulse you will miss the start bit so the code adds it back in. Each code transmission begins with a start bit and 24 data bits. The short pulses are “1”s and the long pulses are “0”s – see the O’Scope output. Press the on and off buttons on the transmitter that came with the outlets in sequence to record the codes. You will have to remember which button goes with which code. Below is an output starting with pressing On for number 1 then Off for number 1. The sequence is repeated for the remaining 4 outlets. Make sure the outlets work by using the remote that came with the outlets.

Note: make sure the outlets work with the transmitter that came with them. Place the battery in the transmitter and plug in one of the outlets. You will see a red led and hear a relay clicking as you turn the outlet on and off.

Begin of Test in 2 seconds:
Code is: 1010101111101010110011001
Code is: 1101111000011110101011111
Code is: 1010101111101010001111001
Code is: 1010101111101010001100111
Code is: 1111111111100000010101011
Code is: 1010101111111010001111001
Code is: 1011000111011010101111100
Code is: 1010101111100010111100111
Code is: 1010101111001010111111001
Code is: 1010101111001010111100111

Once you have the codes you will need to put them in the Arduino sketch “_433MHz_Blynk_5_Switches.ino”. You will also need to update 3 things in the code – your Blynk Auth code, network name and network password. Install Blynk on your phone and create an account. Download the Blynk Library from GitHub: https://github.com/blynkkk/blynk-library.

Then build a new project in Blynk. Enter in the project name and set the Hardware Model to ESP8266. When you build a new project you will receive an authentication code. Select email to receive a copy through email and copy and paste into the Arduino sketch. Select “Create project” in Blynk to add the buttons. Single tap the project and add a button. Select the button and give it a name then select what virtual pin you want to connect to – V0 – V4 are used for this project. Virtual pins are not actual pins but allow you to send data between the Blynk and the ESP8266. In this case we are using virtual pin 0 to read the status of the button and then turn the outlet on or off depending on the state of the button – a 1 or a 0. For each button you will want to set it up as a “Switch” rather than a “Push” button. The “Push” button is a momentary type switch where as “Switch” acts like an on-off switch.

Connect the ESP8266 and the 433MHz transmitter as follows:

ESP8266
GND --> GND
VCC --> +3.3V NOT 5V!
CH_PD --> +3.3V
GPIO2 --> Data pin on 433MHz transmitter



433MHz Transmitter
VCC --> 3.3V – You can supply up to 12V on the power to the transmitter. I recommend at least 5V to give you greater distance.
GND --> GND
DATA --> GPIO2 on ESP8266

Start the ESP8266 and the Blynk App. You will get an error message if they do not connect “ESP8266 is offline”. Once you have both the ESP8266 and Blynk App running you can control 5 outlets from anywhere you have cell phone service!

Code: Select all************************* Codes.ino
int val = 0;
int count;
String code;
 
 
void setup() {
Serial.begin(115200);
pinMode(2,INPUT);
Serial.println("Begin of Test in 2 seconds:");
delay(2000);
}
 
void loop() {
code = ("Code is: ");                               //Build the code string to send
val = pulseIn(2,LOW);                               //Measure the low pulses and wait for a long low pulse signaling the start of the code transmission
if (val > 2000){                                    //wait for long low pulse to start capturing code
    code = code + "1";                              //write a 1 for the start bit
    for (int count = 1;  count <=24; count ++){     //capture next 24 bits
      val = pulseIn(2,HIGH);
      if (val < 300) code = code + "1";             //short pulses are 1
      if (val > 300) code = code + "0";             //long pulses are 0
    }
    Serial.println(code);                           //serial print code
    delay(400);                                     //delay for awhile
}
 
}
 
**********END
 
**********_433MHz_Blynk_5_Switche.ino
 
 
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>               //The Blynk Library
 
String code;
String codeFunction;
int val;
 
 
// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "PUT YOUR BLYNK AUTH CODE HERE";
 
BLYNK_WRITE(V0)                     // This will connect the button in Blynk tied to the virtual pin 0 (V0)
{
  int value = param.asInt();        // Set value based on the value of the button in Blynk - 1 for on 0 for off
  if (value == 1 )  {
      code  = ("1010101111101010110011001");  //Transmit on code for outlet number 1
      for (int i=0; i<=5; i++){               //Transmit the code 5 times
      trans433(code);                         //Call function to transmit code
      }
  }
  if (value == 0)  {
      code  = ("1010101111101010110000111");  //Transmit off code for outlet number 1
      for (int i=0; i<=5; i++){
      trans433(code);
     }
  }
}
 
BLYNK_WRITE(V1)         // This will connect the button in Blynk tied to the virtual pin 1 (V1)
{
  int value = param.asInt();
  if (value == 1 )  {
      code  = ("1010101111101010001111001");
      for (int i=0; i<=5; i++){
      trans433(code);
      }
  }
  if (value == 0)  {
      code  = ("1010101111101010001100111");
      for (int i=0; i<=5; i++){
      trans433(code);
     }
  }
}
 
BLYNK_WRITE(V2)        // This will connect the button in Blynk tied to the virtual pin 2 (V2)
{
  int value = param.asInt();
  if (value == 1 )  {
      code  = ("1010101111101000111111001");
      for (int i=0; i<=5; i++){
      trans433(code);
      }
  }
  if (value == 0)  {
      code  = ("1010101111101000111100111");
      for (int i=0; i<=5; i++){
      trans433(code);
     }
  }
}
 
BLYNK_WRITE(V3)           // This will connect the button in Blynk tied to the virtual pin 3 (V3)
{
  int value = param.asInt();
  if (value == 1 )  {
      code  = ("1010101111100010111111001");
      for (int i=0; i<=5; i++){
      trans433(code);
      }
  }
  if (value == 0)  {
      code  = ("1010101111100010111100111");
      for (int i=0; i<=5; i++){
      trans433(code);
     }
  }
}
 
BLYNK_WRITE(V4)           // This will connect the button in Blynk tied to the virtual pin 4 (V4)
{
  int value = param.asInt();
  if (value == 1 )  {
      code  = ("1010101111001010111111001");
      for (int i=0; i<=5; i++){
      trans433(code);
      }
  }
  if (value == 0)  {
      code  = ("1010101111001010111100111");
      for (int i=0; i<=5; i++){
      trans433(code);
     }
  }
}
 
 
void setup()
{
  Blynk.begin(auth,"PUT YOUR NETWORK NAME HERE","PUT YOUR NETWORK PASSWORD HERE");
  while (Blynk.connect() == false) {                // Wait until connected
  }
  pinMode(2,OUTPUT);                                //Set GPIO2 as output mode
}
 
void loop()
{
  Blynk.run();                                      //Blynk takes care of the connection between the Blynk App and the ESP8266
}
 
 
 
void trans433(String codeFunction) {                // This function transmits the code to the outlets through the 433MHz Transmitter
 
for (int count = 0; count <=24; count ++){          // Read the start bit and 24 data bits one at a time and transmit a short or long pulse
    val=codeFunction.charAt(count);
    if (val == 48)                                  //48 is a 0  49 is a 1 in ASCI Code
    {
    digitalWrite(2,HIGH);                           //Connect GPIO2 of the ESP8266 to the data pin on the 433MHz transmitter
    delayMicroseconds(512);                         //long pulse for a zero
    digitalWrite(2,LOW);
    delayMicroseconds(200);
    }
    else
    {
    digitalWrite(2,HIGH);
    delayMicroseconds(152);                       //short pulse for a one
    digitalWrite(2,LOW);
    delayMicroseconds(568);
    }
    }
delay(5);                                         //short delay between each transmission
}



Parts
1 Power Supply – GND, 3.3V and 5V to 12V – the 433MHz has further range with more voltage
1 ESP8266-01
1 433MHz Transmitter
1 433MHz Receiver
1 Set of Etekcity Wireless Remote Control Electrical Outlet Switch for Household Appliances

Links
Where to buy plug
Where to buy RXTX
Blynk App

Video

Images
Attachments
image001.png
image005.jpg
image005.jpg (14.28 KiB) Viewed 5720 times
image007.jpg
image007.jpg (10.31 KiB) Viewed 5720 times
image009.jpg
unnamed.jpg
unnamed.jpg (11.16 KiB) Viewed 5720 times