简体   繁体   English

Arduino客户端通过套接字将数据发送到Java Server

[英]Arduino Client send data to Java Server through Sockets

I'm starting a new project where I need to send data from an Arduino to a Java Server. 我正在启动一个新项目,我需要将数据从Arduino发送到Java服务器。 I would like to use the Arduino Wifi shield. 我想使用Arduino Wifi防护罩。 Afterwards the java server will send the received data to a web service trough a 3G Router. 之后,java服务器将通过3G路由器将接收到的数据发送到Web服务。

Is this setup possible? 这种设置可行吗? Does there exist an API to set up a socket connection between an Arduino and a Java Server? 是否存在用于在Arduino和Java服务器之间建立套接字连接的API?

If not which possibilities do i have? 如果没有,我有哪些可能性? Thank in advanced 感谢进阶

There is certainly a number of ways you can do this. 当然,您可以通过多种方法来执行此操作。 You can use the Arduino Ethernet library to make connections to your Java server. 您可以使用Arduino以太网库建立与Java服务器的连接。 Here's a nice little program called ClientConnect which uses the ethernet device to make a connection to a server. 这是一个名为ClientConnect的不错的小程序,它使用以太网设备建立与服务器的连接。 This little program sends a web request to the server with the following lines: 这个小程序通过以下几行向服务器发送Web请求:

client.println("GET /search?q=arduino HTTP/1.0");
client.println();

On the server side it would be easiest to use some sort of simple servlet and utilize a Java web framework such as Tomcat . 在服务器端,使用某种简单的servlet并利用Java Web框架(例如Tomcat)将是最容易的。

Your Arduino transactions would then just look like simple web transactions: 然后,您的Arduino交易将看起来像简单的Web交易:

GET /your-path-to-your-servley?field1=value1&field2=value2 HTTP/1.0
[[ empty line here ]]

This will send a set of field/value pairs to your Java webserver which will call your servlet. 这会将一组字段/值对发送到Java Web服务器,该Web服务器将调用servlet。 In the example at the top, "q" is the field name and "arduino" is the value. 在顶部的示例中, "q"是字段名称, "arduino"是值。

Hope this helps. 希望这可以帮助。

Similar to the answer by @gray which is a "push" solution, is to have the java server query the Arduino at some interval ("pull" model). 与@gray的答案类似,这是一个“推送”解决方案,是让Java服务器以一定的时间间隔查询Arduino(“拉”模型)。 To do this, just see the Web hosting example in Arduino's ethernet samnple library. 为此,只需查看Arduino的以太网samnple库中的Web托管示例。 You can modify it, to have the arduino return the value of its sensors as part of its http response. 您可以对其进行修改,以使arduino作为其http响应的一部分返回其传感器的值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM