315 MHz transmitter + Wi-Fi

Hi!

I am a total beginner with Arduino, Blynk and even coding, so I hope that you could help me out here.

I am using an Arduino Uno, an ESP8266-01 Wi-Fi module, and a 315 MHz transmitter module. We have a set of lights at home that can be controlled by a 315 MHz remote with four buttons (which I call “lock”, “square”, “up” and “down” in the following code). I want to use the Blynk app to control them instead.

The problem is that I do not know how to do it. I can connect the Arduino with the Wi-Fi shield to my router, and control a simple LED with the app. And I can connect the Arduino with the 315 MHz transmitter to send out constant codes to the lights I want to control, but I can not figure out how to use them together.

This is the code I use to connect the Arduino to my router:

#define BLYNK_PRINT Serial    
#include <ESP8266_HardSer.h>

#include <BlynkSimpleShieldEsp8266_HardSer.h>


#define EspSerial Serial

ESP8266 wifi(EspSerial);


char auth[] = "e1d820d0b5fb46ddb4XXXXXXXXXX";

void setup()
{
  
  Serial.begin(9600);
  delay(10);
  
  EspSerial.begin(115200);
  delay(10);

  Blynk.begin(auth, wifi, "Tele2GatewayXXXX", "nhdXXXXX");
}

void loop()
{
  Blynk.run();
}

And this is the code I use to send constant 315 MHz codes to the lights (from different pins):

#include <RCSwitch.h>

RCSwitch mySwitchsquare = RCSwitch();
RCSwitch mySwitchlock = RCSwitch();
RCSwitch mySwitchup = RCSwitch();
RCSwitch mySwitchdown = RCSwitch();

void setup() {

  Serial.begin(9600);
  
  
  mySwitchsquare.enableTransmit(10);
  mySwitchlock.enableTransmit(11);
  mySwitchup.enableTransmit(5);
  mySwitchdown.enableTransmit(6);

}

void loop() {

  

  
  mySwitchsquare.send("000001010101010100110000");
  delay(1);
  mySwitchlock.send("000001010101010100000011");
  delay(1);
  mySwitchup.send("000001010101010100001100");
  delay(1);
  mySwitchdown.send("000001010101010111000000");
  delay(1);
}

Does anyone know or can help me write a code to combine these two? So that for example, when I push a button for digital pin 9 in the app, the transmitter sends a 315 MHz code, and when I push a button for digital pin 10, it sends a different 315 MHz code?

What you need is called “Virtual Pins” in Blynk. If you look for that in the examples, I bet you will figure this thing out. It’s not that hard. A Virtual Pin can send a custom (or more then one) command to your hardware. I’ll give you a short example:

BLYNK_WRITE(V1)
{
  mySwitchdown.send("000001010101010111000000");
}

If you add a button in the app attached to Virtual Pin 1, Blynk will respond by executing the command defined above.

Thank you very much! But then I wonder, how do I connect the transmitter? Since I have to connect something to the DATA pin on the transmitter, which pin on the Arduino should I connect it to and how to I get it to use for example V02 to be pin D10 on the Arduino?

Well, you have to include the correct library for the Transmitter, the top part of your sketch would probably look something like this then:

#define BLYNK_PRINT Serial    
#include <ESP8266_HardSer.h>

#include <BlynkSimpleShieldEsp8266_HardSer.h>

#include <RCSwitch.h>

The library and the setup() parts probably have to be copied to. I assume the transmitter is setup in the setup() part with these:

  mySwitchsquare.enableTransmit(10);
  mySwitchlock.enableTransmit(11);
  mySwitchup.enableTransmit(5);
  mySwitchdown.enableTransmit(6);

The numbers are the pin numbers to which they are attached, right? You need to integrate all needed parts. The virtual pin and the pin for the transmitter don’t have a relationship.

Blynk handles the vPin and the RCSwitch library handles the RCSwitch parts.

I have tried to combine them like this:

#define BLYNK_PRINT Serial    
#include <ESP8266_HardSer.h>

#include <BlynkSimpleShieldEsp8266_HardSer.h>


#define EspSerial Serial

#include <RCSwitch.h>

RCSwitch mySwitchsquare = RCSwitch();
RCSwitch mySwitchlock = RCSwitch();
RCSwitch mySwitchup = RCSwitch();
RCSwitch mySwitchdown = RCSwitch();

ESP8266 wifi(EspSerial);


char auth[] = "e1d820d0b5fb46ddb4XXXXXXXXXX";

void setup()
{
  
  Serial.begin(9600);
  delay(10);
  
  EspSerial.begin(115200);
  delay(10);

  Blynk.begin(auth, wifi, "Tele2GatewayXXXX", "nhdXXXXX");

  mySwitchsquare.enableTransmit(10);
  mySwitchlock.enableTransmit(11);
  mySwitchup.enableTransmit(5);
  mySwitchdown.enableTransmit(6);

}

void loop()
{
  Blynk.run();
  mySwitchsquare.send("000001010101010100110000");
  delay(1);
  mySwitchlock.send("000001010101010100000011");
  delay(1);
  mySwitchup.send("000001010101010100001100");
  delay(1);
  mySwitchdown.send("000001010101010111000000");
  delay(1);
}

How and where do I put in the codes for Vpins now?

You’re on the right track :slight_smile:

This is how I would go about it:

#define BLYNK_PRINT Serial    
#include <ESP8266_HardSer.h>

#include <BlynkSimpleShieldEsp8266_HardSer.h>


#define EspSerial Serial

#include <RCSwitch.h>

RCSwitch mySwitchsquare = RCSwitch();
RCSwitch mySwitchlock = RCSwitch();
RCSwitch mySwitchup = RCSwitch();
RCSwitch mySwitchdown = RCSwitch();

ESP8266 wifi(EspSerial);


char auth[] = "e1d820d0b5fb46ddb4XXXXXXXXXX";

// Insert stuff here for BLYNK_WRITE and attach a button in the App to V1
BLYNK_WRITE(V1) 
{
  mySwitchsquare.send("000001010101010100110000");
  delay(1);
  mySwitchlock.send("000001010101010100000011");
  delay(1);
  mySwitchup.send("000001010101010100001100");
  delay(1);
  mySwitchdown.send("000001010101010111000000");
  delay(1);
}

void setup()
{
  
  Serial.begin(9600);
  delay(10);
  
  EspSerial.begin(115200);
  delay(10);

  Blynk.begin(auth, wifi, "Tele2GatewayXXXX", "nhdXXXXX");

  mySwitchsquare.enableTransmit(10);
  mySwitchlock.enableTransmit(11);
  mySwitchup.enableTransmit(5);
  mySwitchdown.enableTransmit(6);

}

void loop()
{
  Blynk.run();
}

It seems the structure of your sketch is a bit outdated, it would be better if you use last Blynk library.
On the other hand…EspSerial.begin(115200)?? The Baudrate is really high…try to reduce it to 9600.

Thank you! I will try that :slight_smile:

I know, but I was not able to connect to wi-fi with the later versions of the library. And as said, I am really a beginner at this, I do not even know what the baudrate does? I know that it has something to do with the monitor but not what difference it makes?

The baudrate specifies how fast data is sent over the serial line to the ESP (in this case)
Both items (ESP and Arduino) MUST operate at the same rate (obviously)
There’re lots of posts where you will be able to find more info to change the Baudrate of your ESP. If not, let me know and I will provide some tips.

1 Like

What you are doing here is not going to work. You have to use another means of communicating with your ESP because the Arduino has only one serial port. You can either use that for debugging or for communicating with your ESP.

Now, there are a couple of options to work around that:

  1. buy an Arduino Mega, this thing has more Serial ports
  2. Attach the ESP to SoftSerial port on Arduino, not recommended
    3 Do the debugging via SoftSerial, I’d choose this option

In both cases 2 and 3 you have to make use of the SoftSerial library. At the moment I got some other things to do, but I will provide you with a basic sketch of how to accomplish this later :slight_smile:

I see! Thank you very much, that would be very helpful :smiley:

The goal is to just have four buttons in Blynk, and each button is supposed to send one of the 315MHz codes when you press them.

Anyway, softserial debugging example:

#include <SoftwareSerial.h>
SoftwareSerial mySerial(10, 11); // tx / rx

#define BLYNK_PRINT mySerial    // Comment this out to disable prints and save space
#include <ESP8266_Lib.h>
#include <BlynkSimpleShieldEsp8266.h>

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "YourAuthToken";


// Hardware Serial on Mega, Leonardo, Micro...
#define EspSerial Serial

// or Software Serial on Uno, Nano...
//#include <SoftwareSerial.h>
//SoftwareSerial EspSerial(2, 3); // RX, TX

// Your ESP8266 baud rate:
#define ESP8266_BAUD 9600

ESP8266 wifi(&EspSerial);

void setup()
{
  // Set console baud rate
  Serial.begin(9600);
  delay(10);
  // Set ESP8266 baud rate
  EspSerial.begin(ESP8266_BAUD);
  delay(10);

  Blynk.begin(auth, wifi, "ssid", "pass");
}

void loop()
{
  Blynk.run()
}

I think this should be it, but I get an error compiling, missing ESP8266_Lib.h … when I comment the include out in the BlynkSimpleShieldEsp8266.h it compiles fine though.

nevermind that last bit, I just had an outdated library :slight_smile: This code should do the trick and have you output serial debugging to pins 10/11 as being tx/rx.

As said, I am a total beginner. I do not really understand where to fit in the 315MHz codes in this. And also, how I should connect the transmitter now. Before, when I was not supposed to use Blynk and WiFi, I connected D5, D6, D10 and D11 to the data pin on the transmitter. (As seen in the second embedded code in my first post). Which pin/pins do I connect now to the data pin? How do I write the code for them? And how do I put in Vpins as well, so that I can get four buttons in Blynk, and each of them is supposed to send out a specific 315MHz code when pressed?

Thanks a lot for your help! :smiley:

#include <SoftwareSerial.h>
SoftwareSerial mySerial(10, 11); // tx / rx

#define BLYNK_PRINT mySerial    // Comment this out to disable prints and save space
#include <ESP8266_Lib.h>
#include <BlynkSimpleShieldEsp8266.h>

#include <RCSwitch.h>

RCSwitch mySwitchsquare = RCSwitch();
RCSwitch mySwitchlock = RCSwitch();
RCSwitch mySwitchup = RCSwitch();
RCSwitch mySwitchdown = RCSwitch();

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "YourAuthToken";


// Hardware Serial on Mega, Leonardo, Micro...
#define EspSerial Serial

// or Software Serial on Uno, Nano...
//#include <SoftwareSerial.h>
//SoftwareSerial EspSerial(2, 3); // RX, TX

// Your ESP8266 baud rate:
#define ESP8266_BAUD 9600

ESP8266 wifi(&EspSerial);

BLYNK_WRITE(V0)
{
  mySwitchsquare.send("000001010101010100110000");
}

BLYNK_WRITE(V1)
{
  mySwitchup.send("000001010101010100001100");
}

void setup()
{
  // Set console baud rate
  Serial.begin(9600);
  delay(10);
  // Set ESP8266 baud rate
  EspSerial.begin(ESP8266_BAUD);
  delay(10);

  Blynk.begin(auth, wifi, "ssid", "pass");

  mySwitchsquare.enableTransmit(10);
  mySwitchlock.enableTransmit(11);
  mySwitchup.enableTransmit(5);
  mySwitchdown.enableTransmit(6);
}

void loop()
{
  Blynk.run()
}

This probably works.

Thanks! I will try that later tonight :slight_smile:

I only get “error compiling for board” :confused:

@emigor which Arduino are you using and can you confirm you have installed the RCSwitch library?

Uno R3, and yes I have! I got it to upload now, but when I look in the serial monitor it does not seem to connect to my wifi, it only says something about “blynk.cloud…”.