上个月入手一枚Arduino UNO WiFi板子,据官网介绍,该板子是UNO集成了ESP8266,且可以不使用AT命令。入手后,一直有点别的事情要忙,昨天才拿出来试验一下。不过遇到个有趣的问题,希望大神们能给予指点。
我是打算用这板子,向服务器每秒发送一个字符串(门的开关状况、开关时间)。拓扑结构如下图:
板子的设置没问题,按照官网的做即可,非常简单。
然后就是灌了一段试验的代码,测试向服务器发送字符串"50":
- #include <Wire.h>
- #include <Ciao.h>
- #define CONNECTOR "rest"
- #define SERVER_ADDR "192.168.88.87" // Server IP
- String command = "/servlet/testWifi?p=50";
- void setup()
- {
- Ciao.begin();
- Ciao.write(CONNECTOR, SERVER_ADDR, command);
- delay(50);
- }
- void loop()
- {
- Ciao.write(CONNECTOR, SERVER_ADDR, command);
- }
复制代码
服务器端,有个Servlet接收这个字符串,然后在Terminal里显示。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;
-
- @WebServlet(urlPatterns = {"/testWifi"})
- public class testWifi extends HttpServlet
- {
- protected void processRequest(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException
- {
- response.setContentType("text/html;charset=UTF-8");
- try (PrintWriter out = response.getWriter())
- {
- request.setCharacterEncoding("UTF-8");
- response.setContentType("text/html;charset=UTF-8");
- String para01 = request.getParameter("p");
- System.out.println("Parameter = " + para01);
- }
- }
-
- @Override
- protected void doGet(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException
- {
- processRequest(request, response);
- }
-
- @Override
- protected void doPost(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException
- {
- processRequest(request, response);
- }
-
- @Override
- public String getServletInfo()
- {
- return "Short description";
- }// </editor-fold>
- }
复制代码
服务器是抓到这个字符串了,貌似很成功。
但是,这个字符串在Terminal里是以7秒间隔显示的!无论我怎么调整LOOP内的delay或者干脆去掉了delay,这个间隔依然是7秒!
请各位大神施以援手。老夫这厢作揖了。
|