kevinzhang19701
发表于 2016-8-18 14:14
麻烦,将:
1. web页面
2. servlet1
3. servlet2
4. servlet3
5. uno板子上的程序
都贴出来。
荒草中的铁皮屋
发表于 2016-8-18 18:13
好的好的
荒草中的铁皮屋
发表于 2016-8-18 18:21
这是UNO代码: #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");
}
}
这是web页面的:
<!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>
因为我没有去弄温度采集,所以少一个servLet
这是控制界面的:
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);
}
}
最后是用于回复UNO的servlet
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>
}
以上就是所有的代码
而且我现在的网页可以在外网打开
这是地址:http://15440a9z84.iask.in:41498/remoteControlByYuancong/
老师有兴趣可以看看,本来界面是特别丑的,但是因为与板子的通信问题一直搞不定,干等浪费时间,于是就在网页的颜值上下了一点功夫。。。。
荒草中的铁皮屋
发表于 2016-8-19 14:58
kevinzhang19701 发表于 2014-12-31 15:33
初中生,对写代码有兴趣,喜欢动手动脑,不喜做作业滴主啊。
天呐~我要是初中搞这个我会幸福死~初中确实对计算机感兴趣,但是也就是瞎摸索,身边根本没有人用计算机做看片聊天听音乐打游戏之外的事情。上大学之前只知道折腾主机箱里的那一堆零件,系统有问题了会自己重装系统,在我亲戚眼里我就是修电脑滴~而且对他们来说这就是会玩儿电脑了。上了大学才知道我是多么无知啊,跟那些初中就参加算法竞赛的,还有自己做游戏修改器的同学比起来真是差远了:'(。现在我的大一刚结束,这个暑假放完我就成大二学僧了。我想在大二之前做点不一样的东西出来。。。因为我念的是软件工程专业,都是搞软件的,我就想结合一下硬件做点小玩意儿玩一玩,有了楼主的这篇文章对我来说是雪中送炭呢。:victory::victory::victory:
kevinzhang19701
发表于 2016-8-19 21:59
本帖最后由 kevinzhang19701 于 2016-8-19 22:10 编辑
荒草中的铁皮屋 发表于 2016-8-19 14:58
天呐~我要是初中搞这个我会幸福死~初中确实对计算机感兴趣,但是也就是瞎摸索,身边根本没有人用计算机做 ...
瞧着三个代码,没啥问题呀。
你在数据库里,模拟两个数据,然后从板子上获取这两个数据,在串口显示得出来吗?
这里搞一搞看看。
荒草中的铁皮屋
发表于 2016-8-19 22:25
哈哈哈哈哈 灵了灵了,收到了收到了。。。。果然那个delay(1000)有用的....可是为什么
荒草中的铁皮屋
发表于 2016-8-19 22:39
一开始我看您的代码的时候还觉得奇怪为什么要substring(147,170)也就是从 147 开始截取字符串呢。因为我在网页上看的效果是仅仅就只有A2=*,A3=%;我以为从(0,8)就可以了。但是串口监视器输出了还输出了如图的一大串字符,这是固定的格式吧?老师是一开始就知道,还是 显示出来出来以后一个一个查了?
kevinzhang19701
发表于 2016-8-20 16:46
荒草中的铁皮屋 发表于 2016-8-19 22:25
哈哈哈哈哈 灵了灵了,收到了收到了。。。。果然那个delay(1000)有用的....可是为什么 ...
1. 首先要恭喜,前面的辛苦没有白费。:P
2. delay(1000)是给网络板子一个稳定接收数据的时间。
3. web页面在网络上传输时,http头都有一段固定格式的字符串。
wuyaqi45
发表于 2017-4-4 08:45
这个版本在那里更新呢。翻墙容易。但是那里能找到更改过的文件。
kevinzhang19701
发表于 2017-4-5 14:49
wuyaqi45 发表于 2017-4-4 08:45
这个版本在那里更新呢。翻墙容易。但是那里能找到更改过的文件。
:)请问您指的是哪个文件?