summaryrefslogtreecommitdiffstats
path: root/examples
diff options
context:
space:
mode:
Diffstat (limited to 'examples')
-rw-r--r--examples/clients/coap-client.py15
-rw-r--r--examples/clients/webiopi-client.py46
-rw-r--r--examples/magpi-9-cambot/cambot.py139
-rw-r--r--examples/magpi-9-cambot/index.html70
-rwxr-xr-xexamples/magpi-9-cambot/stream.sh11
-rw-r--r--examples/magpi-9-cambot2/cambot.py117
-rw-r--r--examples/magpi-9-cambot2/index.html70
-rwxr-xr-xexamples/magpi-9-cambot2/stream.sh11
-rw-r--r--examples/scripts/basic/script.py34
-rw-r--r--examples/scripts/blink/script.py41
-rw-r--r--examples/scripts/macros/index.html120
-rw-r--r--examples/scripts/macros/script.py60
-rw-r--r--examples/scripts/simple/index.html68
13 files changed, 802 insertions, 0 deletions
diff --git a/examples/clients/coap-client.py b/examples/clients/coap-client.py
new file mode 100644
index 0000000..eba80e9
--- /dev/null
+++ b/examples/clients/coap-client.py
@@ -0,0 +1,15 @@
1from webiopi.protocols.coap import *
2from time import sleep
3
4client = COAPClient()
5client.sendRequest(COAPPost("coap://224.0.1.123/GPIO/25/function/out"))
6state = True
7
8while True:
9 response = client.sendRequest(COAPPost("coap://224.0.1.123/GPIO/25/value/%d" % state))
10 if response:
11 print("Received response:\n%s" % response)
12 state = not state
13 else:
14 print("No response received")
15 sleep(0.5)
diff --git a/examples/clients/webiopi-client.py b/examples/clients/webiopi-client.py
new file mode 100644
index 0000000..c277682
--- /dev/null
+++ b/examples/clients/webiopi-client.py
@@ -0,0 +1,46 @@
1from webiopi.clients import *
2from time import sleep
3
4# Create a WebIOPi client
5client = PiHttpClient("192.168.1.234")
6#client = PiMixedClient("192.168.1.234")
7#client = PiCoapClient("192.168.1.234")
8#client = PiMulticastClient()
9
10client.setCredentials("webiopi", "raspberry")
11
12# RPi native GPIO
13gpio = NativeGPIO(client)
14gpio.setFunction(25, "out")
15state = True
16
17# DAC named "dac1"
18dac = DAC(client, "dac1")
19
20# ADC named "adc1"
21adc = ADC(client, "adc1")
22value = 0.0
23
24# Temperature sensor named "temp0"
25temp = Temperature(client, "temp0")
26
27while True:
28 # toggle digital state
29 state = not state
30 gpio.digitalWrite(25, state)
31
32 # increase analog value
33 value += 0.01
34 if value > 1.0:
35 value = 0.0
36 dac.writeFloat(0, value)
37
38 # DAC output 0 is wired to ADC input 1
39 val = adc.readFloat(1)
40 print("Analog = %.2f" % val)
41
42 # Retrieve temperature
43 t = temp.getCelsius()
44 print("Temperature = %.2f Celsius" % t)
45
46 sleep(1)
diff --git a/examples/magpi-9-cambot/cambot.py b/examples/magpi-9-cambot/cambot.py
new file mode 100644
index 0000000..ed2e499
--- /dev/null
+++ b/examples/magpi-9-cambot/cambot.py
@@ -0,0 +1,139 @@
1# Imports
2import webiopi
3
4# Retrieve GPIO lib
5GPIO = webiopi.GPIO
6
7# -------------------------------------------------- #
8# Constants definition #
9# -------------------------------------------------- #
10
11# Left motor GPIOs
12L1=9 # H-Bridge 1
13L2=10 # H-Bridge 2
14LS=11 # H-Bridge 1,2EN
15
16# Right motor GPIOs
17R1=23 # H-Bridge 3
18R2=24 # H-Bridge 4
19RS=25 # H-Bridge 3,4EN
20
21# -------------------------------------------------- #
22# Convenient PWM Function #
23# -------------------------------------------------- #
24
25# Set the speed of two motors
26def set_speed(speed):
27 GPIO.pulseRatio(LS, speed)
28 GPIO.pulseRatio(RS, speed)
29
30# -------------------------------------------------- #
31# Left Motor Functions #
32# -------------------------------------------------- #
33
34def left_stop():
35 GPIO.output(L1, GPIO.LOW)
36 GPIO.output(L2, GPIO.LOW)
37
38def left_forward():
39 GPIO.output(L1, GPIO.HIGH)
40 GPIO.output(L2, GPIO.LOW)
41
42def left_backward():
43 GPIO.output(L1, GPIO.LOW)
44 GPIO.output(L2, GPIO.HIGH)
45
46# -------------------------------------------------- #
47# Right Motor Functions #
48# -------------------------------------------------- #
49def right_stop():
50 GPIO.output(R1, GPIO.LOW)
51 GPIO.output(R2, GPIO.LOW)
52
53def right_forward():
54 GPIO.output(R1, GPIO.HIGH)
55 GPIO.output(R2, GPIO.LOW)
56
57def right_backward():
58 GPIO.output(R1, GPIO.LOW)
59 GPIO.output(R2, GPIO.HIGH)
60
61# -------------------------------------------------- #
62# Macro definition part #
63# -------------------------------------------------- #
64
65def go_forward():
66 left_forward()
67 right_forward()
68
69def go_backward():
70 left_backward()
71 right_backward()
72
73def turn_left():
74 left_backward()
75 right_forward()
76
77def turn_right():
78 left_forward()
79 right_backward()
80
81def stop():
82 left_stop()
83 right_stop()
84
85# -------------------------------------------------- #
86# Initialization part #
87# -------------------------------------------------- #
88
89# Setup GPIOs
90GPIO.setFunction(LS, GPIO.PWM)
91GPIO.setFunction(L1, GPIO.OUT)
92GPIO.setFunction(L2, GPIO.OUT)
93
94GPIO.setFunction(RS, GPIO.PWM)
95GPIO.setFunction(R1, GPIO.OUT)
96GPIO.setFunction(R2, GPIO.OUT)
97
98set_speed(0.5)
99stop()
100
101# -------------------------------------------------- #
102# Main server part #
103# -------------------------------------------------- #
104
105
106# Instantiate the server on the port 8000, it starts immediately in its own thread
107server = webiopi.Server(port=8000, login="cambot", password="cambot")
108
109# Register the macros so you can call it with Javascript and/or REST API
110
111server.addMacro(go_forward)
112server.addMacro(go_backward)
113server.addMacro(turn_left)
114server.addMacro(turn_right)
115server.addMacro(stop)
116
117# -------------------------------------------------- #
118# Loop execution part #
119# -------------------------------------------------- #
120
121# Run our loop until CTRL-C is pressed or SIGTERM received
122webiopi.runLoop()
123
124# -------------------------------------------------- #
125# Termination part #
126# -------------------------------------------------- #
127
128# Stop the server
129server.stop()
130
131# Reset GPIO functions
132GPIO.setFunction(LS, GPIO.IN)
133GPIO.setFunction(L1, GPIO.IN)
134GPIO.setFunction(L2, GPIO.IN)
135
136GPIO.setFunction(RS, GPIO.IN)
137GPIO.setFunction(R1, GPIO.IN)
138GPIO.setFunction(R2, GPIO.IN)
139
diff --git a/examples/magpi-9-cambot/index.html b/examples/magpi-9-cambot/index.html
new file mode 100644
index 0000000..db69b41
--- /dev/null
+++ b/examples/magpi-9-cambot/index.html
@@ -0,0 +1,70 @@
1<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
2<html>
3<head>
4 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
5 <meta name="viewport" content = "height = device-height, width = 420, user-scalable = no" />
6 <title>CamBot</title>
7 <script type="text/javascript" src="/webiopi.js"></script>
8 <script type="text/javascript">
9 function init() {
10 var button;
11
12 button = webiopi().createButton("bt_up", "/\\", go_forward, stop);
13 $("#up").append(button);
14
15 button = webiopi().createButton("bt_left", "<", turn_left, stop);
16 $("#middle").append(button);
17
18 button = webiopi().createButton("bt_stop", "X", stop);
19 $("#middle").append(button);
20
21 button = webiopi().createButton("bt_right", ">", turn_right, stop);
22 $("#middle").append(button);
23
24 button = webiopi().createButton("bt_down", "\\/", go_backward, stop);
25 $("#down").append(button);
26 }
27
28 function go_forward() {
29 webiopi().callMacro("go_forward");
30 }
31
32 function go_backward() {
33 webiopi().callMacro("go_backward");
34 }
35
36 function turn_right() {
37 webiopi().callMacro("turn_right");
38 }
39
40 function turn_left() {
41 webiopi().callMacro("turn_left");
42 }
43
44 function stop() {
45 webiopi().callMacro("stop");
46 }
47
48 webiopi().ready(init);
49
50 </script>
51 <style type="text/css">
52 button {
53 margin: 5px 5px 5px 5px;
54 width: 50px;
55 height: 50px;
56 font-size: 24pt;
57 font-weight: bold;
58 color: black;
59 }
60 </style>
61</head>
62<body>
63 <div id="content" align="center">
64 <img width="320" height="240" src="http://raspberrypi:8001/?action=stream"><br/>
65 <div id="up"></div>
66 <div id="middle"></div>
67 <div id="down"></div>
68 </div>
69</body>
70</html>
diff --git a/examples/magpi-9-cambot/stream.sh b/examples/magpi-9-cambot/stream.sh
new file mode 100755
index 0000000..12d6579
--- /dev/null
+++ b/examples/magpi-9-cambot/stream.sh
@@ -0,0 +1,11 @@
1#!/bin/sh
2
3STREAMER=mjpg_streamer
4DEVICE=/dev/video0
5RESOLUTION=320x240
6FRAMERATE=25
7HTTP_PORT=8001
8
9PLUGINPATH=/usr/local/lib
10
11$STREAMER -i "$PLUGINPATH/input_uvc.so -n -d $DEVICE -r $RESOLUTION -f $FRAMERATE" -o "$PLUGINPATH/output_http.so -n -p $HTTP_PORT"
diff --git a/examples/magpi-9-cambot2/cambot.py b/examples/magpi-9-cambot2/cambot.py
new file mode 100644
index 0000000..f1013ab
--- /dev/null
+++ b/examples/magpi-9-cambot2/cambot.py
@@ -0,0 +1,117 @@
1# This version uses new-style automatic setup/destroy/mapping
2# Need to change /etc/webiopi
3
4# Imports
5import webiopi
6
7# Retrieve GPIO lib
8GPIO = webiopi.GPIO
9
10# -------------------------------------------------- #
11# Constants definition #
12# -------------------------------------------------- #
13
14# Left motor GPIOs
15L1=17 # H-Bridge 1
16L2=18 # H-Bridge 2
17LS=21 # H-Bridge 1,2EN
18
19# Right motor GPIOs
20R1=23 # H-Bridge 3
21R2=24 # H-Bridge 4
22RS=25 # H-Bridge 3,4EN
23
24# -------------------------------------------------- #
25# Convenient PWM Function #
26# -------------------------------------------------- #
27
28# Set the speed of two motors
29def set_speed(speed):
30 GPIO.pulseRatio(LS, speed)
31 GPIO.pulseRatio(RS, speed)
32
33# -------------------------------------------------- #
34# Left Motor Functions #
35# -------------------------------------------------- #
36
37def left_stop():
38 GPIO.output(L1, GPIO.LOW)
39 GPIO.output(L2, GPIO.LOW)
40
41def left_forward():
42 GPIO.output(L1, GPIO.HIGH)
43 GPIO.output(L2, GPIO.LOW)
44
45def left_backward():
46 GPIO.output(L1, GPIO.LOW)
47 GPIO.output(L2, GPIO.HIGH)
48
49# -------------------------------------------------- #
50# Right Motor Functions #
51# -------------------------------------------------- #
52def right_stop():
53 GPIO.output(R1, GPIO.LOW)
54 GPIO.output(R2, GPIO.LOW)
55
56def right_forward():
57 GPIO.output(R1, GPIO.HIGH)
58 GPIO.output(R2, GPIO.LOW)
59
60def right_backward():
61 GPIO.output(R1, GPIO.LOW)
62 GPIO.output(R2, GPIO.HIGH)
63
64# -------------------------------------------------- #
65# Macro definition part #
66# -------------------------------------------------- #
67@webiopi.macro
68def go_forward():
69 left_forward()
70 right_forward()
71
72@webiopi.macro
73def go_backward():
74 left_backward()
75 right_backward()
76
77@webiopi.macro
78def turn_left():
79 left_backward()
80 right_forward()
81
82@webiopi.macro
83def turn_right():
84 left_forward()
85 right_backward()
86
87@webiopi.macro
88def stop():
89 left_stop()
90 right_stop()
91
92# Called by WebIOPi at script loading
93def setup():
94 # Setup GPIOs
95 GPIO.setFunction(LS, GPIO.PWM)
96 GPIO.setFunction(L1, GPIO.OUT)
97 GPIO.setFunction(L2, GPIO.OUT)
98
99 GPIO.setFunction(RS, GPIO.PWM)
100 GPIO.setFunction(R1, GPIO.OUT)
101 GPIO.setFunction(R2, GPIO.OUT)
102
103 set_speed(0.5)
104 stop()
105
106
107# Called by WebIOPi at server shutdown
108def destroy():
109 # Reset GPIO functions
110 GPIO.setFunction(LS, GPIO.IN)
111 GPIO.setFunction(L1, GPIO.IN)
112 GPIO.setFunction(L2, GPIO.IN)
113
114 GPIO.setFunction(RS, GPIO.IN)
115 GPIO.setFunction(R1, GPIO.IN)
116 GPIO.setFunction(R2, GPIO.IN)
117
diff --git a/examples/magpi-9-cambot2/index.html b/examples/magpi-9-cambot2/index.html
new file mode 100644
index 0000000..db69b41
--- /dev/null
+++ b/examples/magpi-9-cambot2/index.html
@@ -0,0 +1,70 @@
1<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
2<html>
3<head>
4 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
5 <meta name="viewport" content = "height = device-height, width = 420, user-scalable = no" />
6 <title>CamBot</title>
7 <script type="text/javascript" src="/webiopi.js"></script>
8 <script type="text/javascript">
9 function init() {
10 var button;
11
12 button = webiopi().createButton("bt_up", "/\\", go_forward, stop);
13 $("#up").append(button);
14
15 button = webiopi().createButton("bt_left", "<", turn_left, stop);
16 $("#middle").append(button);
17
18 button = webiopi().createButton("bt_stop", "X", stop);
19 $("#middle").append(button);
20
21 button = webiopi().createButton("bt_right", ">", turn_right, stop);
22 $("#middle").append(button);
23
24 button = webiopi().createButton("bt_down", "\\/", go_backward, stop);
25 $("#down").append(button);
26 }
27
28 function go_forward() {
29 webiopi().callMacro("go_forward");
30 }
31
32 function go_backward() {
33 webiopi().callMacro("go_backward");
34 }
35
36 function turn_right() {
37 webiopi().callMacro("turn_right");
38 }
39
40 function turn_left() {
41 webiopi().callMacro("turn_left");
42 }
43
44 function stop() {
45 webiopi().callMacro("stop");
46 }
47
48 webiopi().ready(init);
49
50 </script>
51 <style type="text/css">
52 button {
53 margin: 5px 5px 5px 5px;
54 width: 50px;
55 height: 50px;
56 font-size: 24pt;
57 font-weight: bold;
58 color: black;
59 }
60 </style>
61</head>
62<body>
63 <div id="content" align="center">
64 <img width="320" height="240" src="http://raspberrypi:8001/?action=stream"><br/>
65 <div id="up"></div>
66 <div id="middle"></div>
67 <div id="down"></div>
68 </div>
69</body>
70</html>
diff --git a/examples/magpi-9-cambot2/stream.sh b/examples/magpi-9-cambot2/stream.sh
new file mode 100755
index 0000000..12d6579
--- /dev/null
+++ b/examples/magpi-9-cambot2/stream.sh
@@ -0,0 +1,11 @@
1#!/bin/sh
2
3STREAMER=mjpg_streamer
4DEVICE=/dev/video0
5RESOLUTION=320x240
6FRAMERATE=25
7HTTP_PORT=8001
8
9PLUGINPATH=/usr/local/lib
10
11$STREAMER -i "$PLUGINPATH/input_uvc.so -n -d $DEVICE -r $RESOLUTION -f $FRAMERATE" -o "$PLUGINPATH/output_http.so -n -p $HTTP_PORT"
diff --git a/examples/scripts/basic/script.py b/examples/scripts/basic/script.py
new file mode 100644
index 0000000..5750a69
--- /dev/null
+++ b/examples/scripts/basic/script.py
@@ -0,0 +1,34 @@
1# Imports
2import webiopi
3
4# Enable debug output
5webiopi.setDebug()
6
7# Retrieve GPIO lib
8GPIO = webiopi.GPIO
9SWITCH = 21
10SERVO = 23
11LED0 = 24
12LED1 = 25
13
14# Called by WebIOPi at script loading
15def setup():
16 webiopi.debug("Basic script - Setup")
17 # Setup GPIOs
18 GPIO.setFunction(SWITCH, GPIO.IN)
19 GPIO.setFunction(SERVO, GPIO.PWM)
20 GPIO.setFunction(LED0, GPIO.PWM)
21 GPIO.setFunction(LED1, GPIO.OUT)
22
23 GPIO.pwmWrite(LED0, 0.5) # set to 50% ratio
24 GPIO.pwmWriteAngle(SERVO, 0) # set to 0 (neutral)
25 GPIO.digitalWrite(LED1, GPIO.HIGH)
26
27# Called by WebIOPi at server shutdown
28def destroy():
29 webiopi.debug("Basic script - Destroy")
30 # Reset GPIO functions
31 GPIO.setFunction(SWITCH, GPIO.IN)
32 GPIO.setFunction(SERVO, GPIO.IN)
33 GPIO.setFunction(LED0, GPIO.IN)
34 GPIO.setFunction(LED1, GPIO.IN)
diff --git a/examples/scripts/blink/script.py b/examples/scripts/blink/script.py
new file mode 100644
index 0000000..90c6c8c
--- /dev/null
+++ b/examples/scripts/blink/script.py
@@ -0,0 +1,41 @@
1# Imports
2import webiopi
3
4# Enable debug output
5webiopi.setDebug()
6
7# Retrieve GPIO lib
8GPIO = webiopi.GPIO
9SWITCH = 21
10SERVO = 23
11LED0 = 24
12LED1 = 25
13
14# Called by WebIOPi at script loading
15def setup():
16 webiopi.debug("Blink script - Setup")
17 # Setup GPIOs
18 GPIO.setFunction(SWITCH, GPIO.IN)
19 GPIO.setFunction(SERVO, GPIO.PWM)
20 GPIO.setFunction(LED0, GPIO.PWM)
21 GPIO.setFunction(LED1, GPIO.OUT)
22
23 GPIO.pwmWrite(LED0, 0.5) # set to 50% ratio
24 GPIO.pwmWriteAngle(SERVO, 0) # set to 0 (neutral)
25 GPIO.digitalWrite(LED1, GPIO.HIGH)
26
27# Looped by WebIOPi
28def loop():
29 # Toggle LED each 5 seconds
30 value = not GPIO.digitalRead(LED1)
31 GPIO.digitalWrite(LED1, value)
32 webiopi.sleep(5)
33
34# Called by WebIOPi at server shutdown
35def destroy():
36 webiopi.debug("Blink script - Destroy")
37 # Reset GPIO functions
38 GPIO.setFunction(SWITCH, GPIO.IN)
39 GPIO.setFunction(SERVO, GPIO.IN)
40 GPIO.setFunction(LED0, GPIO.IN)
41 GPIO.setFunction(LED1, GPIO.IN)
diff --git a/examples/scripts/macros/index.html b/examples/scripts/macros/index.html
new file mode 100644
index 0000000..2dc118e
--- /dev/null
+++ b/examples/scripts/macros/index.html
@@ -0,0 +1,120 @@
1<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
2<html>
3<head>
4 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
5 <meta name="viewport" content = "height = device-height, width = 420, user-scalable = no" />
6 <title>WebIOPi | Demo</title>
7 <script type="text/javascript" src="/webiopi.js"></script>
8 <script type="text/javascript">
9 webiopi().ready(function() {
10 var content, button;
11 content = $("#content");
12
13 // create a "SWITCH" labeled button for GPIO 21
14 button = webiopi().createGPIOButton(21, "SWITCH");
15 content.append(button); // append button to content div
16
17 // create a "LED" labeled button for GPIO 25
18 button = webiopi().createGPIOButton(25, "LED1");
19 content.append(button); // append button to content div
20
21 // create a button that output a single pulse
22 button = webiopi().createPulseButton("pulse", "Pulse", 25);
23 content.append(button); // append button to content div
24
25 // create a button which output a bit sequence on GPIO 25 with a 100ms period
26 button = webiopi().createSequenceButton("sos", "S.O.S 1", 25, 100, "01010100110011001100101010");
27 content.append(button); // append button to content div
28
29 // the previous button will always output the same sequence
30 // you can also create a simple button with your own function
31 button = webiopi().createButton("sos2", "S.O.S 2", outputSequence);
32 content.append(button); // append button to content div
33
34 // create a button which call PrintTime
35 button = webiopi().createMacroButton("macro", "Print Time", "PrintTime");
36 content.append(button); // append button to content div
37
38 // create a button which call HelloWorld with "User,Name" argument
39 button = webiopi().createMacroButton("macro", "Hello ?", "HelloWorld", ["User", "Name"]);
40 content.append(button); // append button to content div
41
42 // the previous button will always call HelloWorld with "User,Name" argument
43 // you can also create a simple button with your own function
44 button = webiopi().createButton("macro2", "Hello !", callMacro);
45 content.append(button); // append button to content div
46
47 // you can also create a button which calls a different function for mouse down and up events
48 button = webiopi().createButton("hold", "Hold", mousedown, mouseup);
49 content.append(button);
50
51 // Only for Chrome and Safari, create a slider that pulse out a -45 to +45° angle on GPIO 23
52 button = webiopi().createAngleSlider(23);
53 content.append(button);
54
55 // Only for Chrome and Safari, create a slider that pulse out a 0-100% duty cycle ratio on GPIO 24
56 button = webiopi().createRatioSlider(24);
57 content.append(button);
58
59 webiopi().refreshGPIO(true);
60 });
61
62 function mousedown() {
63 webiopi().digitalWrite(25, 1);
64 }
65
66 function mouseup() {
67 webiopi().digitalWrite(25, 0);
68 }
69
70 function outputSequence() {
71 var sequence = "01010100110011001100101010" // S.O.S. morse code or whatever you want
72 // output sequence on gpio 25 with a 100ms period
73 webiopi().outputSequence(25, 100, sequence, sequenceCallback);
74 }
75
76 function sequenceCallback(gpio, data) {
77 alert("sequence on " + gpio + " finished with " + data);
78 }
79
80 function callMacro() {
81 var args = ["User","Name"] // or whatever you want
82 // call HelloWorld(args)
83 webiopi().callMacro("HelloWorld", args, macroCallback);
84 }
85
86 function macroCallback(macro, args, data) {
87 alert(data);
88 }
89
90 </script>
91 <style type="text/css">
92 button {
93 display: block;
94 margin: 5px 5px 5px 5px;
95 width: 160px;
96 height: 45px;
97 font-size: 24pt;
98 font-weight: bold;
99 color: black;
100 }
101
102 input[type="range"] {
103 display: block;
104 width: 160px;
105 height: 45px;
106 }
107
108 .LOW {
109 background-color: White;
110 }
111
112 .HIGH {
113 background-color: Red;
114 }
115 </style>
116</head>
117<body>
118 <div id="content" align="center"></div>
119</body>
120</html>
diff --git a/examples/scripts/macros/script.py b/examples/scripts/macros/script.py
new file mode 100644
index 0000000..a469b1a
--- /dev/null
+++ b/examples/scripts/macros/script.py
@@ -0,0 +1,60 @@
1# Imports
2import webiopi
3import time
4
5# Enable debug output
6webiopi.setDebug()
7
8# Retrieve GPIO lib
9GPIO = webiopi.GPIO
10
11SWITCH = 21
12SERVO = 23
13LED0 = 24
14LED1 = 25
15
16# Called by WebIOPi at script loading
17def setup():
18 webiopi.debug("Script with macros - Setup")
19 # Setup GPIOs
20 GPIO.setFunction(SWITCH, GPIO.IN)
21 GPIO.setFunction(SERVO, GPIO.PWM)
22 GPIO.setFunction(LED0, GPIO.PWM)
23 GPIO.setFunction(LED1, GPIO.OUT)
24
25 GPIO.pwmWrite(LED0, 0.5) # set to 50% ratio
26 GPIO.pwmWriteAngle(SERVO, 0) # set to 0 (neutral)
27 GPIO.digitalWrite(LED1, GPIO.HIGH)
28
29 gpio0 = webiopi.deviceInstance("gpio0")
30 gpio0.digitalWrite(0, 0)
31
32# Looped by WebIOPi
33def loop():
34 # Toggle LED each 5 seconds
35 value = not GPIO.digitalRead(LED1)
36 GPIO.digitalWrite(LED1, value)
37 webiopi.sleep(5)
38
39# Called by WebIOPi at server shutdown
40def destroy():
41 webiopi.debug("Script with macros - Destroy")
42 # Reset GPIO functions
43 GPIO.setFunction(SWITCH, GPIO.IN)
44 GPIO.setFunction(SERVO, GPIO.IN)
45 GPIO.setFunction(LED0, GPIO.IN)
46 GPIO.setFunction(LED1, GPIO.IN)
47 gpio0 = webiopi.deviceInstance("gpio0")
48 gpio0.digitalWrite(0, 1)
49
50# A macro which says hello
51@webiopi.macro
52def HelloWorld(first, last):
53 webiopi.debug("HelloWorld(%s, %s)" % (first, last))
54 return "Hello %s %s !" % (first, last)
55
56# A macro without args which return nothing
57@webiopi.macro
58def PrintTime():
59 webiopi.debug("PrintTime: " + time.asctime())
60
diff --git a/examples/scripts/simple/index.html b/examples/scripts/simple/index.html
new file mode 100644
index 0000000..516ecbe
--- /dev/null
+++ b/examples/scripts/simple/index.html
@@ -0,0 +1,68 @@
1<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
2<html>
3<head>
4 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
5 <meta name="viewport" content = "height = device-height, width = 420, user-scalable = no" />
6 <title>WebIOPi | Demo</title>
7 <script type="text/javascript" src="/webiopi.js"></script>
8 <script type="text/javascript">
9 webiopi().ready(function() {
10 webiopi().setFunction(25, "out");
11
12 var content, button;
13 content = $("#content");
14
15 // create a "LED" labeled button for GPIO 25
16 button = webiopi().createGPIOButton(25, "LED1");
17 content.append(button); // append button to content div
18
19 // create a button that output a single pulse
20 button = webiopi().createPulseButton("pulse", "Pulse", 25);
21 content.append(button); // append button to content div
22
23 // you can also create a button which calls a different function for mouse down and up events
24 button = webiopi().createButton("hold", "Hold", mousedown, mouseup);
25 content.append(button);
26
27 webiopi().refreshGPIO(true);
28 });
29
30 function mousedown() {
31 webiopi().digitalWrite(25, 1);
32 }
33
34 function mouseup() {
35 webiopi().digitalWrite(25, 0);
36 }
37
38 </script>
39 <style type="text/css">
40 button {
41 display: block;
42 margin: 5px 5px 5px 5px;
43 width: 160px;
44 height: 45px;
45 font-size: 24pt;
46 font-weight: bold;
47 color: black;
48 }
49
50 input[type="range"] {
51 display: block;
52 width: 160px;
53 height: 45px;
54 }
55
56 .LOW {
57 background-color: White;
58 }
59
60 .HIGH {
61 background-color: Red;
62 }
63 </style>
64</head>
65<body>
66 <div id="content" align="center"></div>
67</body>
68</html>