[vc_row][vc_column][vc_column_text]
In a previous post, we showed how to install jsp web server on pcDuino. In this project, we will use GPIO to control lamp through a web browser.
[/vc_column_text][vc_tour][vc_tab title=”Parts List” tab_id=”1394864346-1-100″][vc_column_text]1. 1 x pcDuino v2
Install jsp web server on the pcDuino.
We also need couple of jumper wires and a LED.[/vc_column_text][/vc_tab][vc_tab title=”Test result” tab_id=”1394864346-2-83″][vc_column_text]When we set the GPIO pin connected to the LED as high level, the LED will turn on.
When we set the GPIO pin connected to the LED as low level, the LED will turn off.
[/vc_column_text][/vc_tab][vc_tab title=”Test code” tab_id=”1394864950198-2-7″][vc_column_text]
<%@ page session=
"false"
import
=
"java.io.*"
%>
<%@ page language=
"java"
import
=
"java.util.*"
pageEncoding=
"utf-8"
%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+
"://"
+request.getServerName()+
":"
+request.getServerPort()+path+
"/"
;
//response.sendRedirect(path+"ocloudmanager");
class
GPIO_Pin {
private
String modeURI =
"/sys/devices/virtual/misc/gpio/mode/"
;
private
String statusURI =
"/sys/devices/virtual/misc/gpio/pin/"
;
private
int
pin =
0
;
public
static
final
String HIGH =
"1"
, LOW =
"0"
, INPUT =
"0"
, OUTPUT =
"1"
, INPUT_PU =
"8"
;
//定义常量
public
GPIO_Pin(
int
pin) {
//实例化GPIO对象
modeURI +=
"gpio"
+ pin;
statusURI +=
"gpio"
+ pin;
this
.pin = pin;
}
public
GPIO_Pin(String pin) {
//实例化GPIO对象
modeURI +=
"gpio"
+ pin;
statusURI +=
"gpio"
+ pin;
this
.pin =Integer.parseInt(pin);
}
public
int
getPin() {
//获得当前GPIO对象的pin脚
return
pin;
}
public
void
overrideURI(String uri) {
modeURI = uri +
"mode/gpio"
+ pin;
statusURI = uri +
"pin/gpio"
+ pin;
}
public
void
setMode(String mode) {
//设置pin控制器脚状态
writeToFile(getModeURI(), mode);
}
public
void
set(String state) {
//设置pin脚状态
writeToFile(getStatusURI(), state);
}
public
void
setHIGH() {
//set pin as high level
writeToFile(getStatusURI(), HIGH);
}
public
void
setLOW() {
//set pin as low level
writeToFile(getStatusURI(), LOW);
}
public
void
setModeINPUT() {
writeToFile(getModeURI(), INPUT);
}
public
void
setModeOUTPUT() {
writeToFile(getModeURI(), OUTPUT);
}
public
void
setModeINPUT_PU() {
writeToFile(getModeURI(), INPUT_PU);
}
public
String getModeURI() {
return
modeURI;
}
public
String getStatusURI() {
return
statusURI;
}
public
String getPinMode() {
try
{
BufferedReader reader =
new
BufferedReader(
new
FileReader(getModeURI()));
String data = reader.readLine();
reader.close();
return
data;
}
catch
(IOException e) {
}
return
""
;
}
public
String getPinStatus() {
try
{
BufferedReader reader =
new
BufferedReader(
new
FileReader(getStatusURI()));
String data = reader.readLine();
reader.close();
return
data;
}
catch
(IOException e) {
}
return
""
;
}
private
void
writeToFile(String URI, String data) {
try
{
File file =
new
File(URI);
file.delete();
File newFile =
new
File(URI);
newFile.createNewFile();
FileWriter writer =
new
FileWriter(URI);
writer.write(data);
writer.close();
}
catch
(IOException e) {
e.printStackTrace();
}
}
}
String pin=request.getParameter(
"pin"
);
String mode=request.getParameter(
"mode"
);
String state=request.getParameter(
"state"
);
GPIO_Pin gpio=
null
;
if
(pin!=
null
){
gpio=
new
GPIO_Pin(pin);
if
(
"1"
.equals(mode)){
gpio.setModeOUTPUT();
if
(
"1"
.equals(state))
gpio.setHIGH();
else
if
(
"0"
.equals(state)){
gpio.setLOW();
}
}
else
if
(
"0"
.equals(mode)){
gpio.setModeINPUT();
state=gpio.getPinStatus();
out.println(
" <script type="text/javascript"> alert(""
+(
"1"
.equals(state)?
"set high level"
:
"set low level"
)+
""); </script>"
);
}
}
%>
<!DOCTYPE HTML PUBLIC
"-//W3C//DTD HTML 4.01 Transitional//EN"
>
<html>
<head>
<base href=
"<%=basePath%>"
>
<title>first page </title>
<meta http-equiv=
"pragma"
content=
"no-cache"
>
<meta http-equiv=
"cache-control"
content=
"no-cache"
>
<meta http-equiv=
"expires"
content=
"0"
>
<meta http-equiv=
"keywords"
content=
"keyword1,keyword2,keyword3"
>
<meta http-equiv=
"description"
content=
"This is my page"
>
<!--
<link rel=
"stylesheet"
type=
"text/css"
href=
"styles.css"
>
-->
</head>
<body>
<table>
<tr>
<td>pin</td>
<td>
1
</td>
<td>
2
</td>
<td>
3
</td>
<td>
4
</td>
<td>
5
</td>
<td>
6
</td>
<td>
7
</td>
<td>
8
</td>
<td>
9
</td>
<td>
10
</td>
<td>
11
</td>
<td>
12
</td>
<td>
13
</td>
</tr>
<tr>
<td>set high level</td>
<td><a href=
"?mode=1&pin=1&state=1"
>set high level</a></td>
<td><a href=
"?mode=1&pin=2&state=1"
>set high level</a></td>
<td><a href=
"?mode=1&pin=3&state=1"
>set high level</a></td>
<td><a href=
"?mode=1&pin=4&state=1"
>set high level</a></td>
<td><a href=
"?mode=1&pin=5&state=1"
>set high level</a></td>
<td><a href=
"?mode=1&pin=6&state=1"
>set high level</a></td>
<td><a href=
"?mode=1&pin=7&state=1"
>set high level</a></td>
<td><a href=
"?mode=1&pin=8&state=1"
>set high level</a></td>
<td><a href=
"?mode=1&pin=9&state=1"
>set high level</a></td>
<td><a href=
"?mode=1&pin=10&state=1"
>set high level</a></td>
<td><a href=
"?mode=1&pin=11&state=1"
>set high level</a></td>
<td><a href=
"?mode=1&pin=12&state=1"
>set high level</a></td>
<td><a href=
"?mode=1&pin=13&state=1"
>set high level</a></td>
</tr>
<tr>
<td>set low level</td>
<td><a href=
"?mode=1&pin=1&state=0"
>set low level</a></td>
<td><a href=
"?mode=1&pin=2&state=0"
>set low level</a></td>
<td><a href=
"?mode=1&pin=3&state=0"
>set low level</a></td>
<td><a href=
"?mode=1&pin=4&state=0"
>set low level</a></td>
<td><a href=
"?mode=1&pin=5&state=0"
>set low level</a></td>
<td><a href=
"?mode=1&pin=6&state=0"
>set low level</a></td>
<td><a href=
"?mode=1&pin=7&state=0"
>set low level</a></td>
<td><a href=
"?mode=1&pin=8&state=0"
>set low level</a></td>
<td><a href=
"?mode=1&pin=9&state=0"
>set low level</a></td>
<td><a href=
"?mode=1&pin=10&state=0"
>set low level</a></td>
<td><a href=
"?mode=1&pin=11&state=0"
>set low level</a></td>
<td><a href=
"?mode=1&pin=12&state=0"
>set low level</a></td>
<td><a href=
"?mode=1&pin=13&state=0"
>set low level</a></td>
</tr>
<tr>
<td>read pin state</td>
<td><a href=
"?mode=0&pin=1&state=0"
>read pin state</a></td>
<td><a href=
"?mode=0&pin=2&state=0"
>read pin state</a></td>
<td><a href=
"?mode=0&pin=3&state=0"
>read pin state</a></td>
<td><a href=
"?mode=0&pin=4&state=0"
>read pin state</a></td>
<td><a href=
"?mode=0&pin=5&state=0"
>read pin state</a></td>
<td><a href=
"?mode=0&pin=6&state=0"
>read pin state</a></td>
<td><a href=
"?mode=0&pin=7&state=0"
>read pin state</a></td>
<td><a href=
"?mode=0&pin=8&state=0"
>read pin state</a></td>
<td><a href=
"?mode=0&pin=9&state=0"
>read pin state</a></td>
<td><a href=
"?mode=0&pin=10&state=0"
>read pin state</a></td>
<td><a href=
"?mode=0&pin=11&state=0"
>read pin state</a></td>
<td><a href=
"?mode=0&pin=12&state=0"
>read pin state</a></td>
<td><a href=
"?mode=0&pin=13&state=0"
>read pin state</a></td>
</tr>
</table>
</body>
</html>
[/vc_column_text][/vc_tab][/vc_tour][/vc_column][/vc_row]
Leave a Reply
You must be logged in to post a comment.