• Home
  • pcDuino
  • WiKi
  • Store
  • Distributors
  • Home
  • pcDuino
  • WiKi
  • Store
  • Distributors
HomepcDuinoJavaOperating linker relay by Java
Previous Next

Operating linker relay by Java

Posted by: Alvin Jin , March 5, 2014

[vc_row][vc_column][vc_column_text]There are two kinds of linker relay, one is an electromagnetic relay (linker Relay), mechanical properties, and the otheris solid state relays (linker SSR), the electronic properties. Their maximum current is different. Linker Relay can control 240V AC or DC 10A, linker SSR can control 240V AC or DC 2A. Also the control state is different, there are two stares about linker Relay that are long closed and permanently attached , linker SSR only a normally closed state.

In the circuitis, Relay is mainly responsible for the security, safety, widely used in modern industrial fields.

wifi0s0-121043336713901104958533relay

Here I use the linker Relay, do the intermediate control, control the LED.Because VCC and GND interface are not enough, I used GPIO Interface Analog[/vc_column_text][/vc_column][/vc_row][vc_row][vc_column width=”1/1″][vc_tour][vc_tab title=”Parts list” tab_id=”1394008633-1-61″][vc_column_text]Button× 1

Electromagnetic relay ×1

Analog voltage electrical equipment led lights ×1

Male to Female Dupont Line×9

Female-female DuPont line×1

Pcduino ×1

图片214parts

[/vc_column_text][/vc_tab][vc_tab title=”Wiring diagram” tab_id=”1394008633-2-60″][vc_column_text]wifi0s0-125607453813901165461512wirAA

  1. GND of Linker Button -> Pin GPIO 0 of pcDuino
  2. VCC of Linker Button -> Pin GPIO 1 of pcDuino
  3. SIG of Linker Button -> Pin GPIO 2 of pcDuino
  4. GND of Linker Relay -> Pin GND of pcDuino
  5. VCC of Linker Relay -> Pin 5V of pcDuino
  6. SIG of Linker Relay -> Pin GPIO 9 of pcDuino
  7. GND of Linker LED -> Pin GND of pcDuino
  8. SIG of Linker LED -> COM port of Linker Relay
  9. NO of Linker Relay -> PIn GPIO 13 of pcDuino

[/vc_column_text][/vc_tab][vc_tab title=”Test code” tab_id=”1394010392357-2-2″][vc_column_text]GPIO Entity

package com.test;
import java.io.BufferedReader;
public class GPIO_Pin {
    private String modeURI = "/sys/devices/virtual/misc/gpio/mode/";
    private String statusURI = "/sys/devices/virtual/misc/gpio/pin/";
    private int pin = 0;
    public static final String HIGH = "1", LOW = "0", INPUT = "0", OUTPUT = "1", INPUT_PU = "8";
    public GPIO_Pin(int pin) {
        modeURI += "gpio" + pin;
        statusURI += "gpio" + pin;
        this.pin = pin;
    }
    public GPIO_Pin(String pin) {
        // Finalize file paths
        modeURI += "gpio" + pin;
        statusURI += "gpio" + pin;
        this.pin =Integer.parseInt(pin);
    }
    public int getPin() {
        return pin;
    }
    public void overrideURI(String uri) {
        modeURI = uri + "mode/gpio" + pin;
        statusURI = uri + "pin/gpio" + pin;
    }
    public void setMode(String mode) {
        writeToFile(getModeURI(), mode);
    }
    public void set(String state) {
        writeToFile(getStatusURI(), state);
    }
    public void setHIGH() {
        writeToFile(getStatusURI(), HIGH);
    }
    public void setLOW() {
        writeToFile(getStatusURI(), LOW);
    }
    public void setModeINPUT() {
        writeToFile(getModeURI(), INPUT);
    }
    public void setModeOUTPUT() {
        writeToFile(getModeURI(), OUTPUT);
    }
    public void setModeINPUT_PU() {
        writeToFile(getModeURI(), INPUT_PU);
    }
    public String getModeURI() {
        return modeURI;
    }
    public String getStatusURI() {
        return statusURI;
    }
    public String getPinMode() {
        try {
            BufferedReader reader = new BufferedReader(new FileReader(getModeURI()));
            String data = reader.readLine();
            reader.close();
            return data;
        } catch (IOException e) {
        }
        return "";
    }
    public String getPinStatus() {
        try {
            BufferedReader reader = new BufferedReader(new FileReader(getStatusURI()));
            String data = reader.readLine();
            reader.close();
            return data;
        } catch (IOException e) {
        }
        return "";
    }
    private void writeToFile(String URI, String data) {
        try {
            File file = new File(URI);
            file.delete();
            File newFile = new File(URI);
            newFile.createNewFile();
            FileWriter writer = new FileWriter(URI);
            writer.write(data);
            writer.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
Test
package com.test;
public class test {
    public static void main(String[] args) throws InterruptedException {
        // TODO Auto-generated method stub
        GPIO_Pin Relaypin=new GPIO_Pin(9);
                GPIO_Pin buttenpin=new GPIO_Pin(2);
                GPIO_Pin pin0=new GPIO_Pin(9);
                GPIO_Pin pin1=new GPIO_Pin(9);
        GPIO_Pin pin13=new GPIO_Pin(9);
                Relaypin.setModeOUTPUT();
                buttenpin.setModeINPUT();
                pin0.setModeOUTPUT();
                pin0.setLOW();
                pin1.setModeOUTPUT();
                pin1.setHIGH();
               pin13.setModeOUTPUT();
               pin13.setHIGH();
           while (true) {
              if("1".equals(buttenpin.getPinStatus())){                   
                 Relaypin.setHIGH();
              }else{
                Relaypin.setLOW();
             }                                                                   
           Thread.sleep(400);
    }
    }
}

[/vc_column_text][/vc_tab][vc_tab title=”The results” tab_id=”1394010393367-3-3″][vc_column_text]图片113#1tupi#2tup#1

[/vc_column_text][/vc_tab][/vc_tour][/vc_column][/vc_row]

Tags: Java

Share!
Tweet

Alvin Jin

About the author

Leave a Reply Cancel reply

You must be logged in to post a comment.

Category

  • Home
  • pcDuino
  • WiKi
  • Store
  • Distributors