Arduino实验:JAVA控制LED,PWM调节亮度

做这个小实验的灵感来自网友xavierchen,他用ENC28J60做了一个网络控制LED的东西,上位机用WEB方式来控制。

自已手头上正好有个W5100,本人对WEB编程不熟,所以就用了自己比较熟悉的JAVA写了一个上位机控制端。东西很简单,发出来给我像一样的初学者参考,共同学习



一、硬件准备:

1、Arduino uno r3

2、EthernetShiled W5100

3、面包板 1个

4、发光二极管 1个

5、杜邦线 两根

6、网线 1根

7、1K电阻一个

二、开发环境:

1、Arduino IDE --开发下位机程序

2、Eclipse for JAVA--开发上位机程序

3、sscom32 串口信息查看软件--查看arduino接收数据、调试

三、LED灯连线图(PS,这个面包板结构让我费解了很久,好吧,我承认现在依然费解……)

LED连接图

2013-9-21 12:50 上传
(66.97 KB)

LED连接图





四、原理

JAVA(上位机Client)——>EthernetShiled(以太网模块)——>Arduino(下位机Server,PWM)——>LED

上位机与以太网模块采用socket TCP连接通信



五、下位机--arduino程序

ARDUINO 代码复制打印

#include <SPI.h>#include <Ethernet.h>byte mac[]={0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};IPAddress serverIP(192,168,2,1);int serverPort=1981;EthernetServer server(serverPort);int whitePin=9;void setup(){Serial.begin(9600);pinMode(whitePin,OUTPUT);Ethernet.begin(mac,serverIP);server.begin();Serial.println("Server started");}void loop(){EthernetClient client=server.available();if(client){  String clientMsg="";  while(client.connected()){  if(client.available()){  int c=client.read();  analogWrite(whitePin,c);  Serial.println(c);  }  }}delay(1);client.stop();}
#include <SPI.h>

#include <Ethernet.h>

byte mac[]={0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};

IPAddress serverIP(192,168,2,1);

int serverPort=1981;

EthernetServer server(serverPort);

int whitePin=9;

void setup(){

Serial.begin(9600);

pinMode(whitePin,OUTPUT);

Ethernet.begin(mac,serverIP);

server.begin();

Serial.println("Server started");

}



void loop(){

EthernetClient client=server.available();

if(client){

String clientMsg="";

while(client.connected()){

if(client.available()){

int c=client.read();

analogWrite(whitePin,c);

Serial.println(c);

}

}

}

delay(1);

client.stop();

}


六、上位机程序
JAVA 代码复制打印
import java.beans.ExceptionListener;import java.io.DataOutputStream;import java.io.IOException;import java.net.Socket;import java.net.UnknownHostException;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JPanel;import javax.swing.JSlider;import javax.swing.JTextField;import javax.swing.event.ChangeEvent;import javax.swing.event.ChangeListener;public class ledManager extends JFrame implements ChangeListener{JTextField textField1;JLabel label1;JSlider slider1;JPanel panel;private static final String ServerIP="192.168.2.1";private static final int ServerPort=1981;private Socket client;public int str;public void init(){setTitle("LED灯网络控制端");setSize(500,80);setVisible(true);setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//setLocationRelativeTo(JFrame.CENTER_ALIGNMENT)label1=new JLabel("LED1");textField1=new JTextField(5);slider1=new JSlider(JSlider.HORIZONTAL,0,255,10);panel=new JPanel();panel.add(label1);panel.add(slider1);panel.add(textField1);add(panel);slider1.addChangeListener(this);}public void stateChanged(ChangeEvent event) {// TODO Auto-generated method stubstr=slider1.getValue();textField1.setText(""+str);try {sendSingle(str);System.out.println(str);} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}public void connServer() throws IOException{System.out.println("try to connecting Server!");client=new Socket(ServerIP,ServerPort);System.out.println("Successful to connected Arduino");}public void discConnServer() throws IOException{client.close();}public void sendSingle(int s) throws IOException{DataOutputStream send=new DataOutputStream(client.getOutputStream());send.write(s);}//public void senSingle(String s) throws Exception, IOException{//final String ServerIP="192.168.2.1";//final int ServerPort=8888;//Socket cs=new Socket(ServerIP,ServerPort);//System.out.println("connect to Server:"+ServerIP+"at port:"+ServerPort);//DataOutputStream singleToArduino=new DataOutputStream(cs.getOutputStream());//singleToArduino.writeBytes(s);//cs.close();//}public static void main(String[] args) throws IOException{ledManager ldm=new ledManager();ldm.connServer();ldm.init();}}
import java.beans.ExceptionListener;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.JTextField;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

public class ledManager extends JFrame implements ChangeListener{

JTextField textField1;
JLabel label1;
JSlider slider1;
JPanel panel;
private static final String ServerIP="192.168.2.1";
private static final int ServerPort=1981;
private Socket client;
public int str;

public void init(){
setTitle("LED灯网络控制端");
setSize(500,80);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//setLocationRelativeTo(JFrame.CENTER_ALIGNMENT)
label1=new JLabel("LED1");
textField1=new JTextField(5);
slider1=new JSlider(JSlider.HORIZONTAL,0,255,10);
panel=new JPanel();
panel.add(label1);
panel.add(slider1);
panel.add(textField1);
add(panel);
slider1.addChangeListener(this);
}
public void stateChanged(ChangeEvent event) {
// TODO Auto-generated method stub
str=slider1.getValue();
textField1.setText(""+str);
try {
sendSingle(str);
System.out.println(str);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void connServer() throws IOException{
System.out.println("try to connecting Server!");
client=new Socket(ServerIP,ServerPort);
System.out.println("Successful to connected Arduino");
}
public void discConnServer() throws IOException{
client.close();
}
public void sendSingle(int s) throws IOException{
DataOutputStream send=new DataOutputStream(client.getOutputStream());
send.write(s);
}
//public void senSingle(String s) throws Exception, IOException{
//final String ServerIP="192.168.2.1";
//final int ServerPort=8888;
//Socket cs=new Socket(ServerIP,ServerPort);
//System.out.println("connect to Server:"+ServerIP+"at port:"+ServerPort);
//DataOutputStream singleToArduino=new DataOutputStream(cs.getOutputStream());
//singleToArduino.writeBytes(s);
//cs.close();
//}
public static void main(String[] args) throws IOException{
ledManager ldm=new ledManager();
ldm.connServer();
ldm.init();
}
}


存在问题:
1、通过观察,上位机数据发送到下位机的时候,明显发现串口显示的数据有延迟,下位机是只有32K的单片机,这很正常,这个问题如何解决呢?
2、如果使用UDP的通信方式又该怎么做?
3、中午的锅碗瓢盆都没洗,一大堆衣服没洗,这才是最大的问题,肿么办啊
思考:话说这个东西没有多大实际应用价值,但对于我来说意义是重大的,基本上搞清楚了上位机通过以太网与下位机之间的通信问题,接下来可以连接更多的传感器到Arduino上,做些更好玩的东西了。

via - 极客工坊

标签: Arduino教程