原文 https://hnyer.gitee.io/2020/07/09/shu-mei-pai-qing-jing-deng/
1、树莓派中搭建服务器。
2、APP和服务器处于同一个局域网,用http 通信。
3、服务器收到信息后,通过 pi4j 这个库去控制树莓派的硬件。从而控制RGB LED灯。
This project is intended to provide a friendly object-oriented I/O API and implementation libraries for Java Programmers to access the full I/O capabilities of the Raspberry Pi platform.
安装PI4J之前,你需要先在树莓派里面安装两样东西:JDK、WiringPi原生库 。
这两个东西一般都是系统自带了,如果没有请自己安装。
//简单测试:
如果你是写一个简单的Java文件来测试代码是否正确,可以在树莓派上直接运行Java文件即可。
java -classpath .:classes:/opt/pi4j/lib/'*' GpioMain
安装pi4j https://pi4j.com/1.2/download.html
下载并解压到 指定目录 sudo dpkg -i pi4j-xx.deb
安装完成后,在下面的目录中能找到lib和example文件
/opt/pi4j/lib
/opt/pi4j/examples
//服务器中调用:
服务器工程中安装 pi4j-core.jar 即可。
树莓派只有一路硬件PWM输出(GPIO1),可是要实现RGB LED的控制,需要3路PWM。
wiringPi库为我们提供了用软件多线程实现的PWM输出,我们可以利用这个库提供的函数很方便的将任意GPIO配置为PWM输出。
在本例中,我将GPIO0,GPIO1,GPIO2配置成了soft PWM输出。树莓派的引脚分配表如下图所示:
public class LedClient {
private int pinRed ;
private int pinGreen ;
private int pinBlue ;
public boolean isRunning = false ;
private static LedClient ledClient =null ;
public static LedClient getInstance(){
if(ledClient==null){
ledClient = new LedClient() ;
}
return ledClient ;
}
public LedClient( ) {
pinRed = 0 ;
pinGreen=1 ;
pinBlue=2 ;
}
public LedClient(int pinRed, int pinGreen, int pinBlue) {
this.pinRed = pinRed;
this.pinGreen = pinGreen;
this.pinBlue = pinBlue;
}
private void initLed(){
Gpio.wiringPiSetup(); // 初始化
SoftPwm.softPwmCreate(pinRed, 0, 100);
SoftPwm.softPwmCreate(pinGreen, 0, 100);
SoftPwm.softPwmCreate(pinBlue, 0, 100);
}
public String getSysInfo() throws Exception {
StringBuilder sb = new StringBuilder() ;
sb.append("\n平台:") .append(PlatformManager.getPlatform().getLabel());
sb.append("\n主板:") .append(SystemInfo.getBoardType().name());
sb.append("\ncpu类型:").append(SystemInfo.getModelName());
sb.append("\ncpu核数:").append( (Integer.valueOf(SystemInfo.getProcessor()) +1)) ;
sb.append("\nCPU温度:").append(SystemInfo.getCpuTemperature());
sb.append("\ncpu使用率:").append("不知道怎么获取") ;
String memoryAll = String.valueOf(SystemInfo.getMemoryTotal()/ (1024 *1024)) ;
sb.append("\n总共内存:") .append(memoryAll).append(" M");
String memoryFree= String.valueOf(SystemInfo.getMemoryFree() /(1024*1024)) ;
sb.append("\n剩余内存:") .append(memoryFree).append(" M");
sb.append("\n运行系统:").append(SystemInfo.getOsName()).append(" ").append(SystemInfo.getOsVersion());
return sb.toString() ;
}
public void openLed(int color){
initLed();
ledColorSet( color );// openLed
}
public void closeLed(){
isRunning =false ;
initLed();
SoftPwm.softPwmStop(pinRed);
SoftPwm.softPwmStop(pinGreen);
SoftPwm.softPwmStop(pinBlue);
}
private void ledColorSet(int color ) {
int r_val = (color & 0xFF0000) >> 16; //get red value
int g_val = (color & 0x00FF00) >> 8; //get green value
int b_val = (color & 0x0000FF) ; //get blue value (color & 0x0000FF) >> 0
r_val = map(r_val, 0, 255, 0, 100); //change a num(0~255) to 0~100
g_val = map(g_val, 0, 255, 0, 100);
b_val = map(b_val, 0, 255, 0, 100);
SoftPwm.softPwmWrite(pinRed, r_val);
SoftPwm.softPwmWrite(pinGreen, g_val);
SoftPwm.softPwmWrite(pinBlue, b_val);
}
public void ledBreath( final int ledColor , final int time1 , final int time2 ){
new Thread(new Runnable() {
@Override
public void run() {
initLed();
int r_val = (ledColor & 0xFF0000) >> 16;
int g_val = (ledColor & 0x00FF00) >> 8;
int b_val = (ledColor & 0x0000FF) ;
r_val = map(r_val, 0, 255, 0, 100);
g_val = map(g_val, 0, 255, 0, 100);
b_val = map(b_val, 0, 255, 0, 100);
while (isRunning){
// 变亮
for(int i=0 ;i< 10 ;i++){
SoftPwm.softPwmWrite(pinRed, r_val/10*i);
SoftPwm.softPwmWrite(pinGreen, g_val/10*i);
SoftPwm.softPwmWrite(pinBlue, b_val/10*i);
try {
Thread.sleep(time1);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
//变暗
for(int i=10 ;i>0 ;i--){
SoftPwm.softPwmWrite(pinRed, r_val/10*i);
SoftPwm.softPwmWrite(pinGreen, g_val/10*i);
SoftPwm.softPwmWrite(pinBlue, b_val/10*i);
try {
Thread.sleep(time2);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}).start();
}
public void ledFlicker( final int [] ledColors ,final int sleepTime ){
new Thread(new Runnable() {
@Override
public void run() {
initLed();
// 赤橙黄绿青蓝紫
int []colors = {
0xFF0000, 0xFF7F00, 0xFFFF00 , 0x00FF00,
0x00FFFF, 0x0000FF, 0x8B00FF , 0xFFFFFF
} ;
while (isRunning){
for(int color :ledColors){
ledColorSet(color ); //ledFlicker
try {
Thread.sleep(sleepTime);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}).start();
}
/**
* 将一个数从一个区间线性映射到另一个区间,比如将0~100之间的一个数映射到0~255之间
*/
private int map(int x, int in_min, int in_max, int out_min, int out_max) {
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}
}
lib和源码下载