首先下载
http://get.pi4j.com/download/pi4j-1.2-SNAPSHOT.zip(使用SNAPSHOT是因为3B要用这个新版本才正常,1.1版本不能在3B上运行),解的jar文件使用eclipse进行开发,把3B作为伺服器进行socket端口监听,没有实现多线程多客户端监听,简单的点对点监听。
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import com.pi4j.io.gpio.*;
//Raspberry Server ,accept action 1/0
public class Rasp {
public static final GpioController gpio=GpioFactory.getInstance();
public static void main(String[] args) {
ServerSocket server = null;
Socket you = null;
DataOutputStream out = null;
DataInputStream in = null;
GpioPinDigitalOutput led=gpio.provisionDigitalOutputPin(RaspiPin.GPIO_29, "",PinState.LOW);
try {
server = new ServerSocket(8970);
} catch (Exception e) {
e.printStackTrace();
}
try {
System.out.println("Wait.....");
you = server.accept();
in = new DataInputStream(you.getInputStream());
out = new DataOutputStream(you.getOutputStream());
while (true) {
int i = in.readInt();
System.out.println(i);
// GPIO Control
if (i == 1) {
led.high();
out.writeUTF("LED ON");
} else if (i == 0) {
led.low();
out.writeUTF("LED OFF");
} else
out.writeUTF("Invalid");
Thread.sleep(500);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
编译好的class文件上传到3B上,3B上也需要安装pi4j(这部分网上另有教程)。使用shell命令运行(需要带类的路径,不然不能运行)
java -classpath .:classes:/opt/pi4j/lib/'*' Rasp
下面是pc端的代码实现,只有简单的界面,两个按钮。
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.util.Scanner;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
//电脑作为控制端
//实现开关界面按钮控制
class ledBut extends JFrame implements ActionListener {
JPanel pl;
JButton on, off;
Socket socket = null;
DataInputStream in = null;
DataOutputStream out = null;
String re = null;
public ledBut() {
pl = new JPanel();
on = new JButton("开");
off = new JButton("关");
on.addActionListener(this);
off.addActionListener(this);
this.setTitle("Raspberry LED Control");
this.setBounds(400, 300,320,80);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
pl.setLayout(new FlowLayout());
pl.add(on);
pl.add(off);
this.add(pl);
try {
socket = new Socket("192.168.12.107", 8970);//改成自己的3B的ip
in = new DataInputStream(socket.getInputStream());
out = new DataOutputStream(socket.getOutputStream());
} catch (Exception e) {
e.printStackTrace();
}
}
public void actionPerformed(ActionEvent arg0) {
try {
if (arg0.getSource() == on) {
out.writeInt(1);
on.setEnabled(false);
off.setEnabled(true);
} else if (arg0.getSource() == off) {
out.writeInt(0);
on.setEnabled(true);
off.setEnabled(false);
}
else
System.out.println("操作无效");
re = in.readUTF();
System.out.println(re);
Thread.sleep(100);
} catch (Exception e) {
e.printStackTrace();
}
}
}
public class pc {
public static void main(String[] args) {
new ledBut();
//旧版控制台操作
// Socket socket = null;
// DataInputStream in = null;
// DataOutputStream out = null;
// String re = null;
// Scanner read = null;
// try {
// socket = new Socket("192.168.12.107", 8970);
// in = new DataInputStream(socket.getInputStream());
// out = new DataOutputStream(socket.getOutputStream());
// read = new Scanner(System.in);
// while (true) {
// int i = read.nextInt();
// out.writeInt(i);
// re = in.readUTF();
// System.out.println(re);
// Thread.sleep(500);
// }
// } catch (Exception e) {
// e.printStackTrace();
// } finally {
// if (read != null)
// read.close();
// }
}
}
Android端的类似(IDEA上开发),也只是简单的两个按钮。activity如下
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/on_button"
android:text="开"/>
<Button android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/off_button"
android:text="关"/>
</LinearLayout>
实现的代码
package io.hyz.netled;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
public class MainActivity extends AppCompatActivity {
private static final String TAG = "MainActivity";
Socket ledSocket = null;
DataOutputStream dataOutputStream = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button on = (Button) findViewById(R.id.on_button);
Button off = (Button) findViewById(R.id.off_button);
new Thread(new Runnable() {
@Override
public void run() {
try {
ledSocket = new Socket("192.168.12.34", 8970);
dataOutputStream = new DataOutputStream(ledSocket.getOutputStream());
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();
on.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (ledSocket != null) {
new LEDThread(1).start();
}
}
});
off.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (ledSocket != null) {
new LEDThread(0).start();
}
}
});
}
class LEDThread extends Thread {
int status;
public LEDThread(int status) {
this.status = status;
}
@Override
public void run() {
Log.d(TAG, "run: " + Thread.currentThread().getId());
try {
dataOutputStream.writeInt(status);
} catch (IOException e) {
e.printStackTrace();
}
}
}
@Override
protected void onDestroy() {
super.onDestroy();
if (dataOutputStream != null) {
try {
dataOutputStream.close();
dataOutputStream = null;
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
实现的功能很简单,只是开和关,这只是自己简单记录下的,两个客户端都没有状态返回,可能后面实现