Blynk + OTA 433 Mhz RF HUB TX RX with Relay Control
Tested Working
Needed Items:
Esp8266 regulated 3.3v
433mhz TX
433Mhz RX
Power
Relay (optional)
Blynk App
Connections :
Power Everything Vcc to Vcc Gnd to Gnd.
Tx Data Pin to 4 and Rx Data Pin to 5 on ESP8266
Relay to pin 13
Software:
//######################################################
//######################################################
//Blynk Setup: All Virtual Pins
//Required Widgets in Blynk:
//RTC Widget in blynk for getting time from blynk servers
//V0(OUTPUT Text)=Send Tx Send Custom Tx Command
//V1(OUTPUT Button)= 1:motor on 0:motor off My Custom Command TX (500 to on 400 to off)
//V3(OUTPUT Button)= 1:Relay on 0:Relay Off Relay On Off
//V4(INPUT TextBox)=Rx Input Shows Last Rx Value
//V8(INPUT Table)=Table of 2 Cols (Date Time, Value) Shows Incoming Rx Values With Time Date
//####################################################################################
//some included .h files could be removed
#include <Time.h>
#include <RCSwitch.h>
#include <ESP8266WiFi.h>
#include <DNSServer.h>
#include <ESP8266WebServer.h>
#include <WiFiManager.h>
#include <BlynkSimpleEsp8266.h>
#include <TimeLib.h>
#include <WidgetRTC.h>
#include <ESP8266mDNS.h>
#include <WiFiUdp.h>
#include <FS.h>
#include <ArduinoOTA.h>
//Define Relay (Used For Some Switch Relay )
#define relay 13
#define HOSTNAME "RF-HUB-WITH-RELAY" //Define Your OTA Name
RCSwitch mySwitch = RCSwitch();
RCSwitch mySwitch1 = RCSwitch();
//Blynk Channel Auth
char auth[] = "<FILL HERE>";
const char* ssid = "<SSID HERE>";
const char* password = "<PASSWORD HERE>";
int rowIndex = 0;
int state = 0;
unsigned long timer1 = 0;
unsigned long timer2 = 0;
unsigned long timer3 = 0;
WidgetRTC rtc;
void setup()
{
Serial.begin(9600);
pinMode(relay, OUTPUT);
digitalWrite(relay, LOW);
delay(20);
//Connect Wifi
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
Serial.println("Connecting to wifi");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("Connected. Open Blynk App");
//Start OTA
String hostname(HOSTNAME);
WiFi.hostname(hostname);
ArduinoOTA.setHostname((const char *)hostname.c_str());
ArduinoOTA.begin();
//Pin 5 for RX Data
//Pin 4 for Tx data
//Set RX TX
mySwitch.enableReceive(5);
mySwitch1.enableTransmit(4);
//Start Blynk
Blynk.begin(auth, "", "");
// mySwitch.setPulseLength(320);
// mySwitch.setProtocol(2);
mySwitch.setRepeatTransmit(5);
//RTC
setSyncInterval(10 * 60);
timer3 = millis();
timer2 = millis();
timer1 = millis();
}
void loop()
{
Blynk.run();
ArduinoOTA.handle();
yield();
//####################################################################
if (mySwitch.available()) {
int value = mySwitch.getReceivedValue();
if (value > 0) {
String currentTime = String(hour()) + ":" + minute() + ":" + second();
String currentDate = String(day()) + " " + month() + " " + year();
// adding 1 row to table every second
Blynk.virtualWrite(V8, "add", rowIndex, currentTime + " " + currentDate, value);
//highlighting latest added row in table
Blynk.virtualWrite(V8, "pick", rowIndex);
Blynk.virtualWrite(V4,value );
rowIndex++;
}
mySwitch.resetAvailable();
}
//####################################################################
//1 sec timer
if (((millis() - timer1) / 100) > 10) {
//#################################
//Relay off after 25 seconds
if (digitalRead(relay) == HIGH) {
if (((millis() - timer3) / 1000) > 25) {
digitalWrite(relay, LOW);
delay(100);
Blynk.virtualWrite(V3, 0);
}
}
//################################
timer1 = millis();
}
//####################################################################
//1 min timer
if (((millis() - timer2) / 100) > 10) {
//If Wifi Disconnected, Retry
if (WiFi.status() != WL_CONNECTED)
{
setup();
}
timer2 = millis();
}
}
BLYNK_CONNECTED() {
// Synchronize time on connection
rtc.begin();
}
//####################################################
//send tx of value
BLYNK_WRITE(V0)
{
int v = param.asInt();
mySwitch1.send(v, 24);
delay(100);
}
// motor on off
BLYNK_WRITE(V1)
{
int v = param.asInt();
//Serial.println("virtual button event");
if (v == 1) {
for(int i=0;i<10;i++){
mySwitch1.send(500, 24);
delay(100);
}
//Serial.println("virtual button on");
} else {
for(int i=0;i<10;i++){
mySwitch1.send(400, 24);
delay(100);
}
//Serial.println("virtual button off");
}
}
//Relay ON / OFF Set
BLYNK_WRITE(V3)
{
int v = param.asInt();
if (v == 1) {
timer3 = millis();
digitalWrite(relay, HIGH);
delay(100);
Blynk.virtualWrite(V3, 1);
} else {
digitalWrite(relay, LOW);
delay(100);
Blynk.virtualWrite(V3, 0);
}
}