之前有提過RaspberryPi -串聯二個74HC595範例,範例執行時需要執行Java Application,這可以拿來當做shell指令,不過如果想遠端控制呢?
想法是利用Raspberry pi架設wifi ap,或是把pi接入區域網路,再連入tomcat server執行指令。
PI在安裝完tomcat後,因為要使用GOIP,所以得把tomcat執行的權限及群組設成root。
可以參考Controlling the Raspberry Pi’s GPIO pins from a web browser
nano /etc/default/tomcat7 # TOMCAT7_USER=root # TOMCAT7_GROUP=root systemctl restart tomcat7
程式碼如下:
在控制IC時,記得每次控制完,要unprovisionPin GpioPinDigitalOutpu
package com.yslifes.ic; import com.pi4j.io.gpio.GpioController; import com.pi4j.io.gpio.GpioFactory; import com.pi4j.io.gpio.GpioPinDigitalOutput; import com.pi4j.io.gpio.Pin; import com.pi4j.io.gpio.PinState; import com.pi4j.wiringpi.Shift; import java.lang.Thread; /** * * @author Eric */ public class IC74CH595 { private GpioPinDigitalOutput mr ; private GpioPinDigitalOutput shcp ; private GpioPinDigitalOutput stcp; private GpioPinDigitalOutput ds; private final GpioController gpio = GpioFactory.getInstance();; private long ms ; private int data; public IC74CH595(Pin dspin,Pin mrpin,Pin stcppin,Pin shcppin) { mr = gpio.provisionDigitalOutputPin(mrpin, PinState.HIGH); ds = gpio.provisionDigitalOutputPin(dspin, PinState.LOW); shcp = gpio.provisionDigitalOutputPin(shcppin, PinState.LOW); stcp = gpio.provisionDigitalOutputPin(stcppin, PinState.LOW); ms = 300; } public void setLED(int i) { data = i; stcp.setState(PinState.LOW); Shift.shiftOut((byte)ds.getPin().getAddress(), (byte)shcp.getPin().getAddress(), (byte)Shift.MSBFIRST, (byte)(data>> 8)); Shift.shiftOut((byte) ds.getPin().getAddress(), (byte) shcp.getPin().getAddress(), (byte) Shift.MSBFIRST, (byte) data); stcp.setState(PinState.HIGH); delay(); } public void shutdown() { gpio.shutdown(); gpio.unprovisionPin(mr); gpio.unprovisionPin(ds); gpio.unprovisionPin(shcp); gpio.unprovisionPin(stcp); } public void clear() { mr.setState(PinState.LOW); mr.setState(PinState.HIGH); } private void delay() { try { Thread.sleep(ms); } catch (InterruptedException e) { System.out.println(e.toString()); } } }
package com.yslifes.ic; import com.pi4j.io.gpio.RaspiPin; import javax.servlet.http.HttpServletRequest; import org.apache.log4j.Logger; /** * * @author Eric */ public class ICUtils { private static Logger logger ; static { logger = Logger.getLogger(ICUtils.class); } public static void setLED(HttpServletRequest req) { String[] params = {req.getParameter("p1"),req.getParameter("p2"),req.getParameter("p3"),req.getParameter("p4"), req.getParameter("p5"),req.getParameter("p6"),req.getParameter("p7"),req.getParameter("p8"), req.getParameter("p9"),req.getParameter("p10"),req.getParameter("p11"),req.getParameter("p12"), req.getParameter("p13"),req.getParameter("p14"),req.getParameter("p15"),req.getParameter("p16")}; int data = 0; for(int i=15;i>=0;i--) { logger.debug("i:"+i+"="+params[i] +";"+data); if(params[i]!=null && params[i].equals("1")) { data += 1; } if(i!=0) data = data<<1; } logger.debug("mydata:"+data); IC74CH595 ic = new IC74CH595(RaspiPin.GPIO_23,RaspiPin.GPIO_27,RaspiPin.GPIO_24,RaspiPin.GPIO_25); ic.setLED(data); ic.shutdown(); } }
web部份
LightLED.jsp
<%@page contentType="text/html" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>JSP Page</title> </head> <body> <% com.yslifes.ic.ICUtils.setLED(request); %> </body> </html>
index.html
<!DOCTYPE html> <html> <head> <title>TODO supply a title</title> <meta charset="UTF-8"> <!--<meta name="viewport" content="width=device-width, initial-scale=1.0">--> <style type="text/css"> .btimg { background-image: url('Animals.jpg'); background-repeat: no-repeat; width:200px; height:200px; } input[type=checkbox] { width:50px; height:50px; } </style> </head> <body> <form action="LightLED.jsp" target="_iframe"> <table> <tr> <td><div class="btimg" style="background-position: -22px -22px; "> <input type="checkbox" name="p1" value="1"></div></td> <td><div class="btimg" style="background-position: -202px -22px; "> <input type="checkbox" name="p2" value="1"></div></td> <td><div class="btimg" style="background-position: -392px -22px; "> <input type="checkbox" name="p3" value="1"></div></td> <td><div class="btimg" style="background-position: -596px -22px; "> <input type="checkbox" name="p4" value="1"></div></td> <td><div class="btimg" style="background-position: -17px -202px; "> <input type="checkbox" name="p5" value="1"></div></td> <td><div class="btimg" style="background-position: -192px -202px; "> <input type="checkbox" name="p6" value="1"></div></td> <td><div class="btimg" style="background-position: -392px -202px; "> <input type="checkbox" name="p7" value="1"></div></td> <td><div class="btimg" style="background-position: -596px -202px; "> <input type="checkbox" name="p8" value="1"></div></td> </tr> <tr> <td><div class="btimg" style="background-position: -10px -382px; "> <input type="checkbox" name="p9" value="1"></div></td> <td><div class="btimg" style="background-position: -202px -382px; "> <input type="checkbox" name="p10" value="1"></div></td> <td><div class="btimg" style="background-position: -392px -382px; "> <input type="checkbox" name="p11" value="1"></div></td> <td><div class="btimg" style="background-position: -596px -382px; "> <input type="checkbox" name="p12" value="1"></div></td> <td><div class="btimg" style="background-position: -10px -577px; "> <input type="checkbox" name="p13" value="1"></div></td> <td><div class="btimg" style="background-position: -202px -577px; "> <input type="checkbox" name="p14" value="1"></div></td> <td><div class="btimg" style="background-position: -392px -577px; "> <input type="checkbox" name="p15" value="1"></div></td> <td><div class="btimg" style="background-position: -596px -577px; "> <input type="checkbox" name="p16" value="1"></div></td> </tr> </table> <div><button type="submit" style="width:100px;height:100px">亮燈</button></div> </form> <hr> <form target="_iframe" action="LightLED.jsp"> <button type="submit" style="width:100px;height:100px">清除</button> </form> <iframe name="_iframe" id="_iframe" frameborder="0" src="LightLED.jsp"></iframe> </body> </html>
選擇要亮的LED確認後Raspberry pi接收到指令就會亮燈了。