|
本帖最后由 kevinzhang19701 于 2014-1-13 17:23 编辑
代码在我家里电脑上,这里我先简单说一下吧。
项目的大致结构如上图。Arduino终端通过Ethernet Shield(扮演Web Client)向服务器传递请求,传递参数。这个可以在官网找到sample,稍微改一下就可以了。
#include <Ethernet.h>
#include <SPI.h>
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; // 这里随便写
byte ip[] = { 10, 0, 0, 177 }; // 分配给ethernet shield的IP
byte server[] = { 64, 233, 187, 99 }; // 服务器的IP
EthernetClient client;
void setup()
{
Ethernet.begin(mac, ip);
Serial.begin(9600);
delay(1000);
Serial.println("connecting...");
if (client.connect(server, 8080)) // 用Tomcat的默认8080端口
{
Serial.println("connected");
client.println("GET /search?q=arduino HTTP/1.0"); // 要传递的数据放在?后面,等号左边是变量名,
// 等号右边是数值(一般都是字符型)
client.println();
} else {
Serial.println("connection failed");
}
}
void loop()
{
if (client.available())
{
char c = client.read();
Serial.print(c);
}
if (!client.connected())
{
Serial.println();
Serial.println("disconnecting.");
client.stop();
for(;;)
;
}
}
上传的参数由Java写的服务器端的Servlet来控制,数据最后写入数据库一个表内保存,便于以后进行统计。
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.ResultSet;
public class myServlet extends HttpServlet
{
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
request.setCharacterEncoding("UTF-8");
String myPara = request.getParameter("q"); //要取的变量名
myImport(myPara); // 写入数据库的函数
try
{
/* TODO output your page here. You may use following sample code. */
out.println("<html>");
out.println("<head>");
out.println("<title>myServlet</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>更新完毕...</h1>");
out.println("</body>");
out.println("</html>");
} finally
{
out.close();
}
}
public void myImport(String aPara)
{
Connection conn = null;
Statement stmt = null;
ResultSet rs1 = null;
int oR = 0;
String mySQL01 = "INSERT INTO newcon (c1) value ('" + aPara + "');"; // 将参数写入数据的SQL语句
try
{
conn = DriverManager.getConnection("jdbc:mysql://localhost/myproject06?" +
"user=root&password=15J053436"); //连接数据库
stmt = conn.createStatement();
oR = stmt.executeUpdate(mySQL01);
}
catch (SQLException ex)
{
System.out.println("Error in connection: " + ex.toString());
System.out.println("SQLException: " + ex.getMessage());
System.out.println("SQLState: " + ex.getSQLState());
System.out.println("VendorError: " + ex.getErrorCode());
}
System.out.println("");
System.out.println(oR);
System.out.println("原始数据写入newcon表完毕...");
}
// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
/**
* Handles the HTTP
* <code>GET</code> method.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
/**
* Handles the HTTP
* <code>OST</code> method.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
/**
* Returns a short description of the servlet.
*
* @return a String containing servlet description
*/
@Override
public String getServletInfo() {
return "Short description";
}// </editor-fold>
}
原始代码,我回家找了再发。
|
评分
-
查看全部评分
|