本实验的目标是让学员掌握在Java平台使用Coap协议进行设备间通讯的方法。
实验环境准备
需要一下环境:
- JDK 8 - Java开发包
- IDEA 社区版
搭建 Coap Server
添加coap依赖库
在 IDEA 中新建一个 maven 项目,名称为: coap-server (也可用你喜欢的名字), 在建立好项目之后,修改 pom.xml 文件,加入以下内容:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| <dependencies> <dependency> <groupId>org.eclipse.californium</groupId> <artifactId>californium-core</artifactId> <version>1.0.7</version> </dependency>
</dependencies>
<build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.6.1</version> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> </plugins> </build>
|
构建 coap 资源
添加一个名为: cn.com.hohistar.iot.coap.resource 的包,在包中新建一个名为:
HelloWorldResource 的类,使该类继承于: CoapResource 类,然后修改类的代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| public class HelloWorldResource extends CoapResource {
public HelloWorldResource(String name) { super(name); }
@Override public void handleGET(CoapExchange exchange) {
System.out.println("received request: " + exchange.getSourceAddress().getHostAddress());
exchange.respond(CoAP.ResponseCode.CONTENT, "Hello World!", MediaTypeRegistry.TEXT_PLAIN); } }
|
构建 coap 服务
添加一个名为: cn.com.hohistar.iot.coap 的包,在包中新建一个名为: CoapService 的类,使该类继承于: CoapServer 类。然后在修改类的代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| public class CoapService extends CoapServer {
public static void main(String[] args) {
simpleListen(); }
public static void simpleListen() {
CoapService server = new CoapService();
HelloWorldResource helloworldRes = new HelloWorldResource("hello-world"); server.add(helloworldRes);
server.start();
System.out.println("Server startup...");
}
}
|
现在可以运行 coap 服务了。
搭建 Coap 客户端
添加coap依赖库
在 IDEA 中新建一个 maven 项目,名称为: coap-client (也可用你喜欢的名字), 在建立好项目之后,修改 pom.xml 文件,加入以下内容:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| <dependencies> <dependency> <groupId>org.eclipse.californium</groupId> <artifactId>californium-core</artifactId> <version>1.0.7</version> </dependency>
</dependencies>
<build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.6.1</version> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> </plugins> </build>
|
构建客户端
添加一个名为: cn.com.hohistar.iot.coap 的包,在包中新建一个名为: BasicClient 的类。然后在修改类的代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| public class BasicClient {
public static void main(String args[]) { start(); }
public static void start() {
CoapClient client = new CoapClient("coap://127.0.0.1:5683/hello-world");
Request request = new Request(CoAP.Code.GET);
CoapResponse coapResp = client.advanced(request);
System.out.println(Utils.prettyPrint(coapResp));
} }
|
启动运行BasicClient, 如果上面的 coap-server 已经正常启动了,你将看到类似下面的结果:
1 2 3 4 5 6 7 8 9 10
| ==[ CoAP Response ]============================================ MID : 5064 Token : ca1c424764a674c6 Type : ACK Status : 2.05 Options: {"Content-Format":"text/plain"} Payload: 12 Bytes --------------------------------------------------------------- Hello World! ===============================================================
|