|
这是UNO代码:[mw_shl_code=cpp,true] #include <SPI.h>
#include <Ethernet.h>
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress dnserver(192, 168, 0,105);
IPAddress ip(192, 168,0,111);
EthernetClient client;
char c;
String result;
void setup() {
Serial.begin(9600);
Ethernet.begin(mac, ip);
delay(3000);
pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
Serial.println("Ethernet connecting...");
}
void loop() {
if (client.connect(dnserver, 80))
{
Serial.println("Connected");
client.println("POST http://192.168.0.105:80/remoteControlByYuancong/ResponseServlet HTTP/1.1");
client.println("Host:192.168.0.105");
client.println();
Serial.println(client.available());
while(client.available())
{
c = client.read();
result += c;
}
Serial.println("==");
Serial.println(result.substring(0,8));
if (result.substring(0,3).equals("A2=1"))
{
digitalWrite(2, HIGH);
}
else
{
digitalWrite(2, LOW);
}
if (result.substring(5,8).equals("A3=1"))
{
digitalWrite(3, HIGH);
}
else
{
digitalWrite(3, LOW);
}
Serial.println("end");
client.stop();
result = "";
}
else
{
Serial.println("connection failed");
}
}
[/mw_shl_code]
这是web页面的:
[mw_shl_code=html,true]<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>遥控</title>
<style type="text/css">
body{
margin:0px;
text-align:center; /*居中*/
background-color:#3d465e;
}
div{
box-sizing:border-box; /*盒子宽高设置以border为标准*/
}
#top{
height:50px;
margin:0 auto ;
background-color:#f78d12;<!--守望黄-->
}
.container{
width:270px;
height:480px;
margin:0 auto;
background-color:#FFFFFF;
background-image:url("images/76.jpg");
<!--border-radius:10px;-->
border-style:solid;
border-width:2px;
border-color:gray;
box-shadow:5px 5px 5px gray;
}
.className{
line-height:50px;
height:50px;
width:180px;
color:#ffffff;
background-color:#f78d12;
font-size:17px;
font-weight:normal;
font-family:Arial;
border:3px solid #3d465e;
-webkit-border-top-left-radius:40px;
-moz-border-radius-topleft:40px;
border-top-left-radius:40px;
-webkit-border-top-right-radius:40px;
-moz-border-radius-topright:40px;
border-top-right-radius:40px;
-webkit-border-bottom-left-radius:40px;
-moz-border-radius-bottomleft:40px;
border-bottom-left-radius:40px;
-webkit-border-bottom-right-radius:40px;
-moz-border-radius-bottomright:40px;
border-bottom-right-radius:40px;
-moz-box-shadow: inset 0px 0px 0px 0px #ffffff;
-webkit-box-shadow: inset 0px 0px 0px 0px #ffffff;
box-shadow: inset 0px 0px 0px 0px #ffffff;
text-align:center;
display:inline-block;
text-decoration:none;
}.className:hover{
background-color:#f8cd04;
}
#login{
width:270px;
height:202px;
background-color:;
}
#twopart{
width:360;
height:16px;
background-color:;
}
input.ys{
color: #ffffff;
height: 30px;
background-color: #f78d12;
}
</style>
<link rel="shortcut icon" href="xiaoTuBiao.ico" type="image/x-icon" />
</head>
<body>
<script LANGUAGE = "JavaScript" >
function checkvalue()
{
document.form1.action="addinone.htm";
document.form1.submit();
alert('已提交');
return true;
} </script>
<div id="top"></div>
<div class="container" >
<p style="color:#ffffff;font-size:20px"></p>
<br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/>
<div id="login">
<form name='form1'action="http://15440a9z84.iask.in:41498/remoteControlByYuancong/ControlServlet?p=1" method="post" >
<a href='javascript:document.form1.submit();' class='className'>开启遥控器</a>
</form>
</div>
<a href="index.html">返回主页</a>
</div>
</body>
</html>[/mw_shl_code]
因为我没有去弄温度采集,所以少一个servLet
这是控制界面的:
[mw_shl_code=java,true]package remoteControlByYuancong;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
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 com.mysql.jdbc.Connection;
import com.mysql.jdbc.Statement;
/**
* Servlet implementation class ContronlServlet
* 这个进行主要功能操作
*/
@WebServlet("/ControlServlet")
public class ControlServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public ControlServlet() {
super();
// TODO Auto-generated constructor stub
}
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
response.setContentType("text/html;charset=UTF-8");
String p=request.getParameter("p");
//request.getParameter()方法是用来获取URL中的参数的
String a2 = "0";
String a3 = "0";
String n = "0";
String a2s = "0";
String a3s = "0";
Connection conn = null;
Statement stmt = null;
ResultSet rs1 = null;
int s = 0;
try (PrintWriter out = response.getWriter())
{
if (p.equals("1"))
{
try
{
Class.forName("com.mysql.jdbc.Driver");
System.out.println("成功加载数据库");
conn = (Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/mydata","root" ,"521314");
stmt = (Statement) conn.createStatement();
System.out.println("正在");
System.out.println("查询最后一条记录...");
rs1 = stmt.executeQuery("SELECT * FROM t1 ORDER BY aid DESC LIMIT 1;");
System.out.println("完成");
while(rs1.next())
{
n = rs1.getString(2);//第二列
if (n.equals("1"))
{
a2s = "1";
}
else
{
a2s = "0";
}
n = rs1.getString(3);
if (n.equals("1"))
{
a3s = "1";
}
else
{
a3s = "0";
}
}
}
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());
} catch (ClassNotFoundException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
/* TODO output your page here. You may use following sample code. */
out.println("<!DOCTYPE html>");
out.println("<html>");
out.println("<head>");
out.println("<title>Remote Controlled LEDs.</title>");
out.println("<meta charset=\"UTF-8\">");
out.println("</head>");
out.println("<body onLoad=\"setForm();\">");
out.println("<br><br>");
out.println("巨丑遥控器");
out.println("<form id=\"f1\" method=\"POST\" action=\"http://15440a9z84.iask.in:41498/remoteControlByYuancong/ControlServlet?p=2\">");
out.println("<table align=\"center\" border=\"1\">");
out.println("<tr>");
out.println("<td>A2:</td>");
if (a2s.equals("0"))
{
out.println("<td><input type=\"radio\" name=\"a2\" value=\"0\" checked>Off</td>");
out.println("<td><input type=\"radio\" name=\"a2\" value=\"1\">On</td>");
}
else
{
out.println("<td><input type=\"radio\" name=\"a2\" value=\"0\">Off</td>");
out.println("<td><input type=\"radio\" name=\"a2\" value=\"1\" checked>On</td>");
}
out.println("</tr>");
out.println("<tr>");
out.println("<td>A3:</td>");
if (a3s.equals("0"))
{
out.println("<td><input type=\"radio\" name=\"a3\" value=\"0\" checked>Off</td>");
out.println("<td><input type=\"radio\" name=\"a3\" value=\"1\">On</td>");
}
else
{
out.println("<td><input type=\"radio\" name=\"a3\" value=\"0\">Off</td>");
out.println("<td><input type=\"radio\" name=\"a3\" value=\"1\" checked>On</td>");
}
out.println("</tr>");
out.println("<tr>");
out.println("<td colspan=\"3\" align=\"center\"><input type=\"submit\" value=\"确认\"></td>");
out.println("</tr>");
out.println("</table>");
out.println("</form>");
out.println("</body>");
out.println("</html>");
//System.out.println("aaa");
}
else
{
if (!request.getParameter("a2").equals("1"))
{
a2 = "0";
}
else
{
a2 = "1";
}
if (!request.getParameter("a3").equals("1"))
{
a3 = "0";
}
else
{
a3 = "1";
}
try
{
conn = (Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/mydata","root" ,"521314");
stmt = (Statement) conn.createStatement();
System.out.println("正在");
System.out.println("追加记录......");
s = stmt.executeUpdate("INSERT INTO t1 (a2,a3) VALUES ('"
+ a2 + "','"
+ a3 + "')");
System.out.println("完成");
//这个s只是起到了一个储存返回值的作用,但是对这个返回值的操作,原来的代码并没有写
//也因此IDE检测到这个s没有使用过所以给了警报
}
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());
}
/* TODO output your page here. You may use following sample code. */
//应该是生成了一个新的页面吧
out.println("<!DOCTYPE html>");
out.println("<html>");
out.println("<head>");
out.println("<title>Remote Controlled LEDs.</title>");
out.println("<meta charset=\"UTF-8\">");
out.println("</head>");
out.println("<body onLoad=\"setForm();\">");
out.println("<br><br>");
out.println("<form id=\"f1\" method=\"POST\" action=\"http://15440a9z84.iask.in:41498/remoteControlByYuancong/ControlServlet?p=2\">");
out.println("<table align=\"center\" border=\"1\">");
out.println("<tr>");
out.println("<td>A2:</td>");
if (a2.equals("0"))
{
out.println("<td><input type=\"radio\" name=\"a2\" value=\"0\" checked>Off</td>");
out.println("<td><input type=\"radio\" name=\"a2\" value=\"1\">On</td>");
}
else
{
out.println("<td><input type=\"radio\" name=\"a2\" value=\"0\">Off</td>");
out.println("<td><input type=\"radio\" name=\"a2\" value=\"1\" checked>On</td>");
}
out.println("</tr>");
out.println("<tr>");
out.println("<td>A3:</td>");
if (a3.equals("0"))
{
out.println("<td><input type=\"radio\" name=\"a3\" value=\"0\" checked>Off</td>");
out.println("<td><input type=\"radio\" name=\"a3\" value=\"1\">On</td>");
}
else
{
out.println("<td><input type=\"radio\" name=\"a3\" value=\"0\">Off</td>");
out.println("<td><input type=\"radio\" name=\"a3\" value=\"1\" checked>On</td>");
}
out.println("</tr>");
out.println("<tr>");
out.println("<td colspan=\"3\" align=\"center\"><input type=\"submit\" value=\"确认\"></td>");
out.println("</tr>");
out.println("</table>");
out.println("</form>");
out.println("</body>");
out.println("</html>");
}
}
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
processRequest(request, response);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
processRequest(request, response);
}
}
[/mw_shl_code]
最后是用于回复UNO的servlet
[mw_shl_code=java,true]package remoteControlByYuancong;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
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 com.mysql.jdbc.Connection;
import com.mysql.jdbc.Statement;
/**
* Servlet implementation class ResponseServlet
* 这里写回答Arduino板子的servlet
*/
@WebServlet("/ResponseServlet")
public class ResponseServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public ResponseServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
String a2 = "0";
String a3 = "0";
String n = "0";
response.setContentType("text/text");
try (PrintWriter out1 = response.getWriter())
{
Connection conn = null;
Statement stmt = null;
ResultSet rs1 = null;
int s = 0;
try
{
Class.forName("com.mysql.jdbc.Driver");
System.out.println("成功加载数据库");
conn = (Connection) DriverManager.getConnection("jdbc:mysql://localhost/mydata?" +
"user=root&password=521314");
stmt = (Statement) conn.createStatement();
System.out.println("==");
System.out.println("查询最后一条记录...");
rs1 = stmt.executeQuery("SELECT * FROM t1 ORDER BY aid DESC LIMIT 1;");
while(rs1.next())
{
n = rs1.getString(2);
if (n.equals("1"))
{
a2 = "1";
}
else
{
a2 = "0";
}
n = rs1.getString(3);
if (n.equals("1"))
{
a3 = "1";
}
else
{
a3 = "0";
}
}
}
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());
} catch (ClassNotFoundException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
/* TODO output your page here. You may use following sample code. */
//out1.println("<!DOCTYPE html>");
// out1.println("<html>");
// out1.println("<head>");
// out1.println("<title>Servlet myservlet2</title>");
// out1.println("<meta charset=\"UTF-8\">");
// out1.println("</head>");
// out1.println("<body>");
out1.println("A2=" + a2 + ",A3=" + a3 );
// out1.println("</body>");
// out1.println("</html>");
}
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
processRequest(request,response);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
processRequest(request,response);
}
public String getServletInfo() {
return "Short description";
}// </editor-fold>
}
[/mw_shl_code]
以上就是所有的代码
而且我现在的网页可以在外网打开
这是地址:http://15440a9z84.iask.in:41498/remoteControlByYuancong/
老师有兴趣可以看看,本来界面是特别丑的,但是因为与板子的通信问题一直搞不定,干等浪费时间,于是就在网页的颜值上下了一点功夫。。。。
|
|