1

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.

  • 2
    Is it a permissions problem? wiringPi requires root privileges to access the GPIO. Does your applet have root privileges? – joan Dec 8 '15 at 15:35
  • It is a permission problem as @joan indicated. The applet runs in the context of the webserver and has (thus) no root privileges. – Diederik de Haas Dec 8 '15 at 16:26
  • @DiederikdeHaas I start tomcat as sudo ./startup.sh – Luca Dec 8 '15 at 16:44
  • 1
    @Luca I assumed that you were using raspbian tomcat packages. Note that it is incredibly dangerous to run a webserver as root. One security problem in either tomcat or your code and the intruder has root privileges on your system. – Diederik de Haas Dec 8 '15 at 18:03
  • I have a software package which I think does what you are trying to achieve - as a former engineer, it might suit your needs - see grapl.16mb.com – user38056 Dec 10 '15 at 13:02
2

I finally found out what the problem was! I forgot to add the Pi4J libraries (the jars) in the lib subfolder of my web application. That was stupid :D . Thanks anyway!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.