Virtual pins using blynk app

Hello!!

Im new to arduino and blynk app but im starting a project using rf relays that i bought on eBay, these came with code prewritten because i don’t have much coding skills and i was wondering if anyone could help adapt the code to use virtual pins instead of the serial commands form a pc

from the blynk support i got this:

"Virtual Pins
Blynk can control Digital and Analog IO Pins on you hardware directly. You don’t even need to write code for it. It’s great for blinking LEDs, but often it’s just not enough…
We designed Virtual Pins to send any data from your microcontroller to the Blynk App and back.
It opens huge opportunities for you, because anything you plug in to your hardware will be able to talk to Blynk. With Virtual Pins you can send something from the App, process it on Arduino and then send it back to the smartphone based on any logic you want. You can trigger functions, read I2C devices, convert values, control any servo motor and so on.
Virtual Pins can be used to interface with external libraries (Servo, LCD and others) and implement custom functionality. The device may send data to the Widgets to the Virtual Pin like this:

Blynk.virtualWrite(pin, "abc");
Blynk.virtualWrite(pin, 123);
Blynk.virtualWrite(pin, 12.34);

Send data from app to hardware
You can send any data from Widgets in the app to your hardware.
All Controller Widgets can send data to Virtual Pins on your hardware. For example, code below shows how to get values from the Button Widget in the App

BLYNK_WRITE(V1) //Button Widget is writing to pin V1
{
  int pinData = param.asInt(); 
}
When you press Button, Blynk App sends 1 On second click - it sends 0

and the code that i got from the relays is this:

//Author: cantone-electonics
//More information welcome to : http://www.canton-electronics.com 
//Arduino 1.0
//Arduino uno R3
//Making a wireless remote control with arduino

//const int data_out = 2;//encoder DOUT

 //LED pin,When receiving the key from the serial port, LED flash
const int ledPin =  13;    //LED pin

const int gnd_pin =  9;    //AS GND 
const int data_out =  10;    //encoder DOUT
const int vcc_pin=  11;    //AS VCC


// OSC Resistance is 3.3M
const int Osc_4xCycle = 359; //4 oscillating time periods 
const int Osc_12xCycle = 1078;//12 oscillating time periods

unsigned long Temporary[3];//Temporary storage unit


//output bit "0"
void bit_0() {
  
  digitalWrite(data_out, HIGH);
  delayMicroseconds(Osc_4xCycle);//4 oscillating time periods High level
  digitalWrite(data_out, LOW);
  delayMicroseconds(Osc_12xCycle);//12 oscillating time periods Low level
  digitalWrite(data_out, HIGH);
  delayMicroseconds(Osc_4xCycle);//4 oscillating time periods High level
  digitalWrite(data_out, LOW);
  delayMicroseconds(Osc_12xCycle);//12 oscillating time periods Low level
  
}

//output bit "1"
void bit_1() {
  
  digitalWrite(data_out, HIGH);
  delayMicroseconds(Osc_12xCycle);//12 oscillating time periods High level
  digitalWrite(data_out, LOW);
  delayMicroseconds(Osc_4xCycle);//4 oscillating time periods Low level
  digitalWrite(data_out, HIGH);
  delayMicroseconds(Osc_12xCycle);//12 oscillating time periods High level
  digitalWrite(data_out, LOW);
  delayMicroseconds(Osc_4xCycle);//4 oscillating time periods Low level
  
}

//output bit "f"
void bit_f() {
  
  digitalWrite(data_out, HIGH);
  delayMicroseconds(Osc_4xCycle);//4 oscillating time periods High level
  digitalWrite(data_out, LOW);
  delayMicroseconds(Osc_12xCycle);//12 oscillating time periods Low level
  digitalWrite(data_out, HIGH);
  delayMicroseconds(Osc_12xCycle);//12 oscillating time periods High level
  digitalWrite(data_out, LOW);
  delayMicroseconds(Osc_4xCycle);//4 oscillating time periods Low level
  
}

//output synchronous bit
void bit_syn() {
  
  digitalWrite(data_out, HIGH);
  delayMicroseconds(Osc_4xCycle);//4 oscillating time periods High level
  digitalWrite(data_out, LOW);
  delayMicroseconds(Osc_4xCycle*31);//124 oscillating time periods Low level

}

//send:8 Address Bits, 4 Data Bits, Sync bit
void send_data(){
  unsigned char temp,tab,i;
  unsigned char j,k;
  for(j=0;j<4;j++)
  {
    for(k=0;k<3;k++)//send 8 Address Bits, 4 Data Bits
    {
      tab = Temporary[k];
    
      for(i=0;i<4;i++)
      {
        temp = tab;
        temp &= 0xC0;
      
        if(temp == 0xC0)//11 is bit "1"
        {
          bit_1();
        }
        else if(temp == 0x00)//00 is bit "0"
        {
          bit_0();
        }
        else //01 is bit "f"
        {
          bit_f();
         }
      
        tab =  tab << 2;
    }
  }
  
   bit_syn();//send Sync bit
  
  }
}
  

void setup() {
  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);  
   // initialize the encoder DOUT pin as an output
  pinMode(data_out, OUTPUT); 
  
  pinMode(gnd_pin, OUTPUT); 
  pinMode(vcc_pin, OUTPUT); 
  
  digitalWrite(vcc_pin, HIGH);//SET VCC
  digitalWrite(gnd_pin, LOW);//SET GND
  
  
  Serial.begin(9600);

}

void loop()
{
  int keydata = 0;
  int receive_flag = 0;
  
  Temporary[0] = 0xC5;//address A7-A4
  Temporary[1] = 0x55;//address A3-A0
  digitalWrite(ledPin, LOW);//Turn off led

  
  while(1)
  {
    //get key from pc serial port
    while (Serial.available() > 0)  
    {
        keydata = Serial.read();
        receive_flag = 1;
        delay(2);
    }
    
    if(receive_flag == 1)//if get key,send key
    {
      
    receive_flag = 0;
    
    if((keydata == 'a') || (keydata == 'A'))//trigger A channel relay.
    {
      Temporary[2] = 0xC0;// 0xC0 is A button key
      digitalWrite(ledPin, HIGH);//Turn on led
      send_data();//send code word
       Serial.println(".....trigger A channel relay.....");
    }
    else if((keydata == 'b') || (keydata == 'B'))//trigger B channel relay.
    {
      Temporary[2] = 0x30;// 0x30 is B button key
      digitalWrite(ledPin, HIGH);//Turn on led
      send_data();//send code word
      Serial.println(".....trigger B channel relay.....");
    }
     else if((keydata == 'c') || (keydata == 'C'))//trigger C channel relay.
     {
      Temporary[2] = 0x0C;// 0x0C is C button key
      digitalWrite(ledPin, HIGH);//Turn on led
      send_data();//send code word
      Serial.println(".....trigger C channel relay.....");
      
     }
     else if((keydata == 'd') || (keydata == 'D'))//trigger D channel relay.
     {
      Temporary[2] = 0x03;// 0x03 is D button key
      digitalWrite(ledPin, HIGH);//Turn on led
      send_data();//send code word
      Serial.println(".....trigger D channel relay.....");
     }
     
    keydata = 0;
    
    //When receiving the key from the serial port, LED flash
    digitalWrite(ledPin, LOW);//Turn off led
    delay(50);//delay 100ms
    digitalWrite(ledPin, HIGH);//Turn on led
    delay(50);//delay 100ms
    digitalWrite(ledPin, LOW);//Turn off led
    delay(50);//delay 100ms
    digitalWrite(ledPin, HIGH);//Turn on led
    delay(50);//delay 100ms
    digitalWrite(ledPin, LOW);//Turn off led
    
    }
   
  }
}

i would be forever thankfull if anyone could help me, i know its a bit of a stretch… thank you

Never heard of blynk. Care to provide some links?

sure, its a really great app once you get it working!

http://blynk.cc

Blynk looks interesting but I have no experience.

Nobody knows what rf relay or prewritten code you have so no one will be able to help with those just yet.

Have you ever been able to turn on/off an led with blynk yet? There is no code. So how to add a relay code?

So an rf relay is radio frequency controlled relay, most of the long code that i posted is to define the message that the arduino sends to the relays which i dont want to change. I want to change the method which i send this message, from a serial input to a blynk input via virtual pins which i posted the code also.am i being clear or to confusing?

Im still confused, sorry. I wanted you to post links to the rf module you got and the code the rf module comes with.

No worries, thanks for trying to help! These are the rf relays:

http://m.ebay.com/itm/271551913413

The code that they is in the first post, the long code that i posted

google blynk arduino
watch the blynk tutorials on yourtube.

Ive used blynk, its not hard for simple things, my problem ks that i dont have enough coding skills to change the code itself to change from a serial input to a blynk input or virtual pins. I know how to use virtual pins in a simple project...

my idea was to replace this:

** **if((keydata == 'a') || (keydata == 'A'))//trigger A channel relay.[/b]     {       Temporary[2] = 0xC0;// 0xC0 is A button key       digitalWrite(ledPin, HIGH);//Turn on led       send_data();//send code word       Serial.println(".....trigger A channel relay.....");** **
which i think is the equivalent as typing "a" on my laptop to control relay "a" with a virtual pin input which is stated with:
BLYNK_WRITE(V1) //Button Widget is writing to pin V1
{
** int pinData = param.asInt();**
}
When you press Button, Blynk App sends 1 On second click - it sends 0

instead of telling us HOW you want to do this, please tell us WHAT you want to do.

use blynk on my iphone to turn on relays...

relays are connected to my Arduino UNO, R3 clone
the relays I am using are http://m.ebay.com/itm/271551913413

i’m sorry, i thought i explained everything, maybe in a confusing way.

I have 4 rf relays http://m.ebay.com/itm/271551913413

one arduino uno rev.3
ethernet shield w5100
blynk app

The project is to install several rf relays in parallel with the wall switches controlled wireless by blink. the relays receive inputs from a transmitter connected to uno which is connected to my router using an ethernet shield.

The uno is connected to blynk’s cloud server which communicates with the app that uses buttons,sliders, etc by selecting digital,analog or virtual pins.

the code that the vendor sent to upload to arduino is written to use serial commands from a laptop connected to uno via usb. what i wanted to do is to use the blynk app to send those inputs instead of the serial:

blynk’s tutorial:

vendor’s code:

//Author: cantone-electonics
//More information welcome to : http://www.canton-electronics.com 
//Arduino 1.0
//Arduino uno R3
//Making a wireless remote control with arduino

//const int data_out = 2;//encoder DOUT

 //LED pin,When receiving the key from the serial port, LED flash
const int ledPin =  13;    //LED pin

const int gnd_pin =  9;    //AS GND 
const int data_out =  10;    //encoder DOUT
const int vcc_pin=  11;    //AS VCC


// OSC Resistance is 3.3M
const int Osc_4xCycle = 359; //4 oscillating time periods 
const int Osc_12xCycle = 1078;//12 oscillating time periods

unsigned long Temporary[3];//Temporary storage unit


//output bit "0"
void bit_0() {
  
  digitalWrite(data_out, HIGH);
  delayMicroseconds(Osc_4xCycle);//4 oscillating time periods High level
  digitalWrite(data_out, LOW);
  delayMicroseconds(Osc_12xCycle);//12 oscillating time periods Low level
  digitalWrite(data_out, HIGH);
  delayMicroseconds(Osc_4xCycle);//4 oscillating time periods High level
  digitalWrite(data_out, LOW);
  delayMicroseconds(Osc_12xCycle);//12 oscillating time periods Low level
  
}

//output bit "1"
void bit_1() {
  
  digitalWrite(data_out, HIGH);
  delayMicroseconds(Osc_12xCycle);//12 oscillating time periods High level
  digitalWrite(data_out, LOW);
  delayMicroseconds(Osc_4xCycle);//4 oscillating time periods Low level
  digitalWrite(data_out, HIGH);
  delayMicroseconds(Osc_12xCycle);//12 oscillating time periods High level
  digitalWrite(data_out, LOW);
  delayMicroseconds(Osc_4xCycle);//4 oscillating time periods Low level
  
}

//output bit "f"
void bit_f() {
  
  digitalWrite(data_out, HIGH);
  delayMicroseconds(Osc_4xCycle);//4 oscillating time periods High level
  digitalWrite(data_out, LOW);
  delayMicroseconds(Osc_12xCycle);//12 oscillating time periods Low level
  digitalWrite(data_out, HIGH);
  delayMicroseconds(Osc_12xCycle);//12 oscillating time periods High level
  digitalWrite(data_out, LOW);
  delayMicroseconds(Osc_4xCycle);//4 oscillating time periods Low level
  
}

//output synchronous bit
void bit_syn() {
  
  digitalWrite(data_out, HIGH);
  delayMicroseconds(Osc_4xCycle);//4 oscillating time periods High level
  digitalWrite(data_out, LOW);
  delayMicroseconds(Osc_4xCycle*31);//124 oscillating time periods Low level

}

//send:8 Address Bits, 4 Data Bits, Sync bit
void send_data(){
  unsigned char temp,tab,i;
  unsigned char j,k;
  for(j=0;j<4;j++)
  {
    for(k=0;k<3;k++)//send 8 Address Bits, 4 Data Bits
    {
      tab = Temporary[k];
    
      for(i=0;i<4;i++)
      {
        temp = tab;
        temp &= 0xC0;
      
        if(temp == 0xC0)//11 is bit "1"
        {
          bit_1();
        }
        else if(temp == 0x00)//00 is bit "0"
        {
          bit_0();
        }
        else //01 is bit "f"
        {
          bit_f();
         }
      
        tab =  tab << 2;
    }
  }
  
   bit_syn();//send Sync bit
  
  }
}
  

void setup() {
  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);  
   // initialize the encoder DOUT pin as an output
  pinMode(data_out, OUTPUT); 
  
  pinMode(gnd_pin, OUTPUT); 
  pinMode(vcc_pin, OUTPUT); 
  
  digitalWrite(vcc_pin, HIGH);//SET VCC
  digitalWrite(gnd_pin, LOW);//SET GND
  
  
  Serial.begin(9600);

}

void loop()
{
  int keydata = 0;
  int receive_flag = 0;
  
  Temporary[0] = 0xC5;//address A7-A4
  Temporary[1] = 0x55;//address A3-A0
  digitalWrite(ledPin, LOW);//Turn off led

  
  while(1)
  {
    //get key from pc serial port
    while (Serial.available() > 0)  
    {
        keydata = Serial.read();
        receive_flag = 1;
        delay(2);
    }
    
    if(receive_flag == 1)//if get key,send key
    {
      
    receive_flag = 0;
    
    if((keydata == 'a') || (keydata == 'A'))//trigger A channel relay.
    {
      Temporary[2] = 0xC0;// 0xC0 is A button key
      digitalWrite(ledPin, HIGH);//Turn on led
      send_data();//send code word
       Serial.println(".....trigger A channel relay.....");
    }
    else if((keydata == 'b') || (keydata == 'B'))//trigger B channel relay.
    {
      Temporary[2] = 0x30;// 0x30 is B button key
      digitalWrite(ledPin, HIGH);//Turn on led
      send_data();//send code word
      Serial.println(".....trigger B channel relay.....");
    }
     else if((keydata == 'c') || (keydata == 'C'))//trigger C channel relay.
     {
      Temporary[2] = 0x0C;// 0x0C is C button key
      digitalWrite(ledPin, HIGH);//Turn on led
      send_data();//send code word
      Serial.println(".....trigger C channel relay.....");
      
     }
     else if((keydata == 'd') || (keydata == 'D'))//trigger D channel relay.
     {
      Temporary[2] = 0x03;// 0x03 is D button key
      digitalWrite(ledPin, HIGH);//Turn on led
      send_data();//send code word
      Serial.println(".....trigger D channel relay.....");
     }
     
    keydata = 0;
    
    //When receiving the key from the serial port, LED flash
    digitalWrite(ledPin, LOW);//Turn off led
    delay(50);//delay 100ms
    digitalWrite(ledPin, HIGH);//Turn on led
    delay(50);//delay 100ms
    digitalWrite(ledPin, LOW);//Turn off led
    delay(50);//delay 100ms
    digitalWrite(ledPin, HIGH);//Turn on led
    delay(50);//delay 100ms
    digitalWrite(ledPin, LOW);//Turn off led
    
    }
   
  }
}

Have you resolved your issue? I have used Blynk to toggle the Sainsmart relays (not RF) simply by uploading the Arduino_Ethernet sketch in the BoardsAndShields folder in the Blynk directories. I also want to learn to use virtual pins to allow me to incorporate reading temp&humidity values or clock readings from my RTC, but simple relay toggles are nothing different than power cycling LEDs on a breadboard.