Have a code but need adaptation

Hello blynkers!

I recently have been cracking my brain trying to set up an ethernet connection, but i made it! it turns out that the store sent me an ethernet shield with w5500 instead of the supported w5100 :disappointed_relieved:

now i can proceed my project witch consists in several rf relays (4 at the time) connected wirelessly to a uno and ethernet shield with a rf receiver. since i have limited or none coding skills and ordered them with code prewritten :smile:

i was relying on some generous soul to help me adapt this using a simple ethernet example… :sweat_smile: i was thinking of replacing the command from the serial with a virtual pin form blank, can anyone help me?

//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
    
    }
   
  }
}
      

    indent preformatted text by 4 spaces

Anyone can help? Just to change the input from the serial to virtual pins…

Can you describe just in words what you want to do?
do you want to open relays through ethernet or rf?

so i bought these rf relays, transmitter and they sent me the code which i posted. when i send “a” “b” “c” or “d” on the serial monitor, the arduino which connected by usb sends a rf signal through the transmitter to the 4 relays.

i have and ethernet shield so what i wish i could do is not to use the laptop, instead using blynk. rather than typing “a” or wtvr i’d just push a button, i was thinking of replacing the part of the code which states the serial input for a virtual pin (here i think, i’m still learning code) :

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....."); 

this is what i bought:

I see why you would want rf relays. Thats a cool item. Personally I dont understand a thing of this code. The rf part is a bit complex for me. But assuming it works fr you right now with just the rf relays connected to the arduino connected to the the usb port (if it does not work, dont bother). So if you managed to make the RF part work i suggest you do the following:

Paste your code in the ethernet example sketch of the blynk librairy (in arduino ide you ll find examples - blynk - ethernet). Make sure to paste everything in the same spot (void loop of the rf code should come under void loop in the ethernet code -" void loop" and “void setup” should only be written once in the sketch etc.

Next you could simple make an if statement in the lines of: If virtual or digital pin 1 equals high, change the value for variable “keydata” (the variable that controls your RF code) to “a”. else if virtual pin 2 equals high, change the value for variable “keydata” ti “b”.

In the arduino app you would just create push buttons that control virtual (or digital) pin 1, pin2, pin3.

Its that simple on the blynk side, for the rf side you should look for help from your vendor or an rf / arduino forum .

Hope this at least pushes you in the right direction :smile:

1 Like

Yeah thats pretty great start! Thank you very much! I installed the relays on the wall, in parallel with the switchs and im gonna work on the wiring to see if it works via usb. Definetly have to work on my coding skills, they’re pretty basic lol

Cheers man!

Hi,

Seems to be a cool solution! Will have a closer look at it this weekend.
From what I quickly read in the code I would make 4 of these functions (next to the standard Blynk initation which you can find in the example sketches). It’s quick and dirty but this would do the trick I think:

BLYNK_WRITE(1) //this would be a virtual button for key/relay A
{
if(param.asInt()==1){
// same code as when a or A has been pressed via serial
Temporary[2] = 0xC0;// 0xC0 is A button key
send_data();//send code word
Serial.println(“…trigger A channel relay via Blynk App…”);
}
}

cheers!

1 Like

Oh man thank you so much, that is the way i was planning on do this ! Im a bit busy from work but gonna give it a try in the next fews! Feedback soon!

Cheers!!!