简体   繁体   English

Java Servlet和Raspberry Pi IO

[英]Java Servlets and Raspberry Pi IO

I'm trying to learn how to write simple servlets to use with a Raspberry Pi. 我正在尝试学习如何编写与Raspberry Pi一起使用的简单servlet。 I want to control the board I/O via web.I'm using the Pi4J library which is a wrapper for the WiringPi C library.It works when I use it to blink a led locally so I assume that I'm doing something wrong coding my servlet. 我想通过Web控制板卡的I / O,我使用的是Pi4J库,它是WiringPi C库的包装器,当我使用它来本地闪烁LED时它可以工作,所以我认为我做错了编码我的servlet。

This is the code I wrote: 这是我写的代码:

package com.luca.servlet;

import com.pi4j.io.gpio.GpioController;
import com.pi4j.io.gpio.GpioFactory;
import com.pi4j.io.gpio.GpioPinDigitalOutput;
import com.pi4j.io.gpio.RaspiPin;
import com.pi4j.io.gpio.PinState;

public class MyServlet extends javax.servlet.http.HttpServlet {

  private GpioController gpio=GpioFactory.getInstance();
  private GpioPinDigitalOutput redLed=gpio.provisionDigitalOutputPin(RaspiPin.GPIO_23,PinState.LOW);
  private GpioPinDigitalOutput greenLed=gpio.provisionDigitalOutputPin(RaspiPin.GPIO_22,PinState.LOW);
  private GpioPinDigitalOutput blueLed=gpio.provisionDigitalOutputPin(RaspiPin.GPIO_21,PinState.LOW);
  private GpioPinDigitalOutput[] pins=new GpioPinDigitalOutput[]{redLed,greenLed,blueLed};

  @Override
  public void doGet(javax.servlet.http.HttpServletRequest request,javax.servlet.http.HttpServletResponse response) throws java.io.IOException {
    java.io.PrintWriter print=response.getWriter();
    print.write("<body>"+ 
                   "<p> Choose a color! </p>"+
                   "<form action=\"first\" method=\"POST\">"+
                   "<input type=\"submit\" name=\"button\" value=\"red\"/>"+
                   "</form>"+
                   "<form action=\"first\" method=\"POST\">"+
                   "<input type=\"submit\" name=\"button\" value=\"green\"/>"+
                   "</form>"+
                   "<form action=\"first\" method=\"POST\">"+
                   "<input type=\"submit\" name=\"button\" value=\"blue\"/>"+         
                   "</form>"+
                 "</body>"); 
  }

  public void doPost(javax.servlet.http.HttpServletRequest request,javax.servlet.http.HttpServletResponse response) throws java.io.IOException {
    java.io.PrintWriter pw=response.getWriter();
    String act=request.getParameter("button"); 
    switch(act) {
      case "red":
        togglePin();
        redLed.high();
        pw.write("<p>the led is red!</p>");
        break;
      case "green":
        togglePin();
        greenLed.high();
        pw.write("<p>the led is green</p>");
        break;
      case "blue":
        togglePin();
        blueLed.high();
        pw.write("<p>the led is blue!</p>");
        break;
    }
  }

  private void togglePin() {
    for (GpioPinDigitalOutput pin : pins) 
      if (pin.isHigh()) pin.toggle();
  }

it compiles fine and I manually deploy it inside tomcat,with the deployment descriptor and all. 它编译良好,我将其与部署描述符及所有内容手动部署在tomcat中。 But when I connect it says to me that the resource is unavailable. 但是当我连接时,它告诉我该资源不可用。 If I remove the GPIO related code it works fine. 如果我删除与GPIO相关的代码,则可以正常工作。

Can someone please help me out? 有人可以帮我吗? Google searching doesn't seem to help Google搜索似乎没有帮助

You need to set an Url Pattern in order to access to your servlet methods like POST and GET . 您需要设置一个Url Pattern才能访问POSTGET之类的servlet方法。

For example use: 例如使用:

@WebServlet("/raspberryServelt")
public class MyServlet extends javax.servlet.http.HttpServlet {
...

And then access to the servlet methods doing a POST or GET request... 然后访问执行POSTGET请求的servlet方法...

For example doing a get to: 例如,进行以下操作:

localhost:8080/yourWebAppName/raspberryServelt 本地主机:8080 / yourWebAppName / raspberryServelt

Will work... 将工作...

您必须将pi4j库放在项目WEB-INF / lib中

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

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