简体   繁体   中英

Java Servlets and Raspberry Pi IO

I'm trying to learn how to write simple servlets to use with a Raspberry Pi. 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.

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. But when I connect it says to me that the resource is unavailable. If I remove the GPIO related code it works fine.

Can someone please help me out? Google searching doesn't seem to help

You need to set an Url Pattern in order to access to your servlet methods like POST and GET .

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...

For example doing a get to:

localhost:8080/yourWebAppName/raspberryServelt

Will work...

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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