From 0c8c9ad976879f7c90f9915a60845ccb0cdb337d Mon Sep 17 00:00:00 2001 From: manuel Date: Wed, 25 Dec 2013 13:25:16 +0100 Subject: initial commit --- examples/clients/coap-client.py | 15 ++++ examples/clients/webiopi-client.py | 46 ++++++++++++ examples/magpi-9-cambot/cambot.py | 139 ++++++++++++++++++++++++++++++++++++ examples/magpi-9-cambot/index.html | 70 ++++++++++++++++++ examples/magpi-9-cambot/stream.sh | 11 +++ examples/magpi-9-cambot2/cambot.py | 117 ++++++++++++++++++++++++++++++ examples/magpi-9-cambot2/index.html | 70 ++++++++++++++++++ examples/magpi-9-cambot2/stream.sh | 11 +++ examples/scripts/basic/script.py | 34 +++++++++ examples/scripts/blink/script.py | 41 +++++++++++ examples/scripts/macros/index.html | 120 +++++++++++++++++++++++++++++++ examples/scripts/macros/script.py | 60 ++++++++++++++++ examples/scripts/simple/index.html | 68 ++++++++++++++++++ 13 files changed, 802 insertions(+) create mode 100644 examples/clients/coap-client.py create mode 100644 examples/clients/webiopi-client.py create mode 100644 examples/magpi-9-cambot/cambot.py create mode 100644 examples/magpi-9-cambot/index.html create mode 100755 examples/magpi-9-cambot/stream.sh create mode 100644 examples/magpi-9-cambot2/cambot.py create mode 100644 examples/magpi-9-cambot2/index.html create mode 100755 examples/magpi-9-cambot2/stream.sh create mode 100644 examples/scripts/basic/script.py create mode 100644 examples/scripts/blink/script.py create mode 100644 examples/scripts/macros/index.html create mode 100644 examples/scripts/macros/script.py create mode 100644 examples/scripts/simple/index.html (limited to 'examples') 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 @@ +from webiopi.protocols.coap import * +from time import sleep + +client = COAPClient() +client.sendRequest(COAPPost("coap://224.0.1.123/GPIO/25/function/out")) +state = True + +while True: + response = client.sendRequest(COAPPost("coap://224.0.1.123/GPIO/25/value/%d" % state)) + if response: + print("Received response:\n%s" % response) + state = not state + else: + print("No response received") + 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 @@ +from webiopi.clients import * +from time import sleep + +# Create a WebIOPi client +client = PiHttpClient("192.168.1.234") +#client = PiMixedClient("192.168.1.234") +#client = PiCoapClient("192.168.1.234") +#client = PiMulticastClient() + +client.setCredentials("webiopi", "raspberry") + +# RPi native GPIO +gpio = NativeGPIO(client) +gpio.setFunction(25, "out") +state = True + +# DAC named "dac1" +dac = DAC(client, "dac1") + +# ADC named "adc1" +adc = ADC(client, "adc1") +value = 0.0 + +# Temperature sensor named "temp0" +temp = Temperature(client, "temp0") + +while True: + # toggle digital state + state = not state + gpio.digitalWrite(25, state) + + # increase analog value + value += 0.01 + if value > 1.0: + value = 0.0 + dac.writeFloat(0, value) + + # DAC output 0 is wired to ADC input 1 + val = adc.readFloat(1) + print("Analog = %.2f" % val) + + # Retrieve temperature + t = temp.getCelsius() + print("Temperature = %.2f Celsius" % t) + + 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 @@ +# Imports +import webiopi + +# Retrieve GPIO lib +GPIO = webiopi.GPIO + +# -------------------------------------------------- # +# Constants definition # +# -------------------------------------------------- # + +# Left motor GPIOs +L1=9 # H-Bridge 1 +L2=10 # H-Bridge 2 +LS=11 # H-Bridge 1,2EN + +# Right motor GPIOs +R1=23 # H-Bridge 3 +R2=24 # H-Bridge 4 +RS=25 # H-Bridge 3,4EN + +# -------------------------------------------------- # +# Convenient PWM Function # +# -------------------------------------------------- # + +# Set the speed of two motors +def set_speed(speed): + GPIO.pulseRatio(LS, speed) + GPIO.pulseRatio(RS, speed) + +# -------------------------------------------------- # +# Left Motor Functions # +# -------------------------------------------------- # + +def left_stop(): + GPIO.output(L1, GPIO.LOW) + GPIO.output(L2, GPIO.LOW) + +def left_forward(): + GPIO.output(L1, GPIO.HIGH) + GPIO.output(L2, GPIO.LOW) + +def left_backward(): + GPIO.output(L1, GPIO.LOW) + GPIO.output(L2, GPIO.HIGH) + +# -------------------------------------------------- # +# Right Motor Functions # +# -------------------------------------------------- # +def right_stop(): + GPIO.output(R1, GPIO.LOW) + GPIO.output(R2, GPIO.LOW) + +def right_forward(): + GPIO.output(R1, GPIO.HIGH) + GPIO.output(R2, GPIO.LOW) + +def right_backward(): + GPIO.output(R1, GPIO.LOW) + GPIO.output(R2, GPIO.HIGH) + +# -------------------------------------------------- # +# Macro definition part # +# -------------------------------------------------- # + +def go_forward(): + left_forward() + right_forward() + +def go_backward(): + left_backward() + right_backward() + +def turn_left(): + left_backward() + right_forward() + +def turn_right(): + left_forward() + right_backward() + +def stop(): + left_stop() + right_stop() + +# -------------------------------------------------- # +# Initialization part # +# -------------------------------------------------- # + +# Setup GPIOs +GPIO.setFunction(LS, GPIO.PWM) +GPIO.setFunction(L1, GPIO.OUT) +GPIO.setFunction(L2, GPIO.OUT) + +GPIO.setFunction(RS, GPIO.PWM) +GPIO.setFunction(R1, GPIO.OUT) +GPIO.setFunction(R2, GPIO.OUT) + +set_speed(0.5) +stop() + +# -------------------------------------------------- # +# Main server part # +# -------------------------------------------------- # + + +# Instantiate the server on the port 8000, it starts immediately in its own thread +server = webiopi.Server(port=8000, login="cambot", password="cambot") + +# Register the macros so you can call it with Javascript and/or REST API + +server.addMacro(go_forward) +server.addMacro(go_backward) +server.addMacro(turn_left) +server.addMacro(turn_right) +server.addMacro(stop) + +# -------------------------------------------------- # +# Loop execution part # +# -------------------------------------------------- # + +# Run our loop until CTRL-C is pressed or SIGTERM received +webiopi.runLoop() + +# -------------------------------------------------- # +# Termination part # +# -------------------------------------------------- # + +# Stop the server +server.stop() + +# Reset GPIO functions +GPIO.setFunction(LS, GPIO.IN) +GPIO.setFunction(L1, GPIO.IN) +GPIO.setFunction(L2, GPIO.IN) + +GPIO.setFunction(RS, GPIO.IN) +GPIO.setFunction(R1, GPIO.IN) +GPIO.setFunction(R2, GPIO.IN) + 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 @@ + + + + + + CamBot + + + + + +
+
+
+
+
+
+ + 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 @@ +#!/bin/sh + +STREAMER=mjpg_streamer +DEVICE=/dev/video0 +RESOLUTION=320x240 +FRAMERATE=25 +HTTP_PORT=8001 + +PLUGINPATH=/usr/local/lib + +$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 @@ +# This version uses new-style automatic setup/destroy/mapping +# Need to change /etc/webiopi + +# Imports +import webiopi + +# Retrieve GPIO lib +GPIO = webiopi.GPIO + +# -------------------------------------------------- # +# Constants definition # +# -------------------------------------------------- # + +# Left motor GPIOs +L1=17 # H-Bridge 1 +L2=18 # H-Bridge 2 +LS=21 # H-Bridge 1,2EN + +# Right motor GPIOs +R1=23 # H-Bridge 3 +R2=24 # H-Bridge 4 +RS=25 # H-Bridge 3,4EN + +# -------------------------------------------------- # +# Convenient PWM Function # +# -------------------------------------------------- # + +# Set the speed of two motors +def set_speed(speed): + GPIO.pulseRatio(LS, speed) + GPIO.pulseRatio(RS, speed) + +# -------------------------------------------------- # +# Left Motor Functions # +# -------------------------------------------------- # + +def left_stop(): + GPIO.output(L1, GPIO.LOW) + GPIO.output(L2, GPIO.LOW) + +def left_forward(): + GPIO.output(L1, GPIO.HIGH) + GPIO.output(L2, GPIO.LOW) + +def left_backward(): + GPIO.output(L1, GPIO.LOW) + GPIO.output(L2, GPIO.HIGH) + +# -------------------------------------------------- # +# Right Motor Functions # +# -------------------------------------------------- # +def right_stop(): + GPIO.output(R1, GPIO.LOW) + GPIO.output(R2, GPIO.LOW) + +def right_forward(): + GPIO.output(R1, GPIO.HIGH) + GPIO.output(R2, GPIO.LOW) + +def right_backward(): + GPIO.output(R1, GPIO.LOW) + GPIO.output(R2, GPIO.HIGH) + +# -------------------------------------------------- # +# Macro definition part # +# -------------------------------------------------- # +@webiopi.macro +def go_forward(): + left_forward() + right_forward() + +@webiopi.macro +def go_backward(): + left_backward() + right_backward() + +@webiopi.macro +def turn_left(): + left_backward() + right_forward() + +@webiopi.macro +def turn_right(): + left_forward() + right_backward() + +@webiopi.macro +def stop(): + left_stop() + right_stop() + +# Called by WebIOPi at script loading +def setup(): + # Setup GPIOs + GPIO.setFunction(LS, GPIO.PWM) + GPIO.setFunction(L1, GPIO.OUT) + GPIO.setFunction(L2, GPIO.OUT) + + GPIO.setFunction(RS, GPIO.PWM) + GPIO.setFunction(R1, GPIO.OUT) + GPIO.setFunction(R2, GPIO.OUT) + + set_speed(0.5) + stop() + + +# Called by WebIOPi at server shutdown +def destroy(): + # Reset GPIO functions + GPIO.setFunction(LS, GPIO.IN) + GPIO.setFunction(L1, GPIO.IN) + GPIO.setFunction(L2, GPIO.IN) + + GPIO.setFunction(RS, GPIO.IN) + GPIO.setFunction(R1, GPIO.IN) + GPIO.setFunction(R2, GPIO.IN) + 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 @@ + + + + + + CamBot + + + + + +
+
+
+
+
+
+ + 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 @@ +#!/bin/sh + +STREAMER=mjpg_streamer +DEVICE=/dev/video0 +RESOLUTION=320x240 +FRAMERATE=25 +HTTP_PORT=8001 + +PLUGINPATH=/usr/local/lib + +$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 @@ +# Imports +import webiopi + +# Enable debug output +webiopi.setDebug() + +# Retrieve GPIO lib +GPIO = webiopi.GPIO +SWITCH = 21 +SERVO = 23 +LED0 = 24 +LED1 = 25 + +# Called by WebIOPi at script loading +def setup(): + webiopi.debug("Basic script - Setup") + # Setup GPIOs + GPIO.setFunction(SWITCH, GPIO.IN) + GPIO.setFunction(SERVO, GPIO.PWM) + GPIO.setFunction(LED0, GPIO.PWM) + GPIO.setFunction(LED1, GPIO.OUT) + + GPIO.pwmWrite(LED0, 0.5) # set to 50% ratio + GPIO.pwmWriteAngle(SERVO, 0) # set to 0 (neutral) + GPIO.digitalWrite(LED1, GPIO.HIGH) + +# Called by WebIOPi at server shutdown +def destroy(): + webiopi.debug("Basic script - Destroy") + # Reset GPIO functions + GPIO.setFunction(SWITCH, GPIO.IN) + GPIO.setFunction(SERVO, GPIO.IN) + GPIO.setFunction(LED0, GPIO.IN) + 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 @@ +# Imports +import webiopi + +# Enable debug output +webiopi.setDebug() + +# Retrieve GPIO lib +GPIO = webiopi.GPIO +SWITCH = 21 +SERVO = 23 +LED0 = 24 +LED1 = 25 + +# Called by WebIOPi at script loading +def setup(): + webiopi.debug("Blink script - Setup") + # Setup GPIOs + GPIO.setFunction(SWITCH, GPIO.IN) + GPIO.setFunction(SERVO, GPIO.PWM) + GPIO.setFunction(LED0, GPIO.PWM) + GPIO.setFunction(LED1, GPIO.OUT) + + GPIO.pwmWrite(LED0, 0.5) # set to 50% ratio + GPIO.pwmWriteAngle(SERVO, 0) # set to 0 (neutral) + GPIO.digitalWrite(LED1, GPIO.HIGH) + +# Looped by WebIOPi +def loop(): + # Toggle LED each 5 seconds + value = not GPIO.digitalRead(LED1) + GPIO.digitalWrite(LED1, value) + webiopi.sleep(5) + +# Called by WebIOPi at server shutdown +def destroy(): + webiopi.debug("Blink script - Destroy") + # Reset GPIO functions + GPIO.setFunction(SWITCH, GPIO.IN) + GPIO.setFunction(SERVO, GPIO.IN) + GPIO.setFunction(LED0, GPIO.IN) + 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 @@ + + + + + + WebIOPi | Demo + + + + + +
+ + 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 @@ +# Imports +import webiopi +import time + +# Enable debug output +webiopi.setDebug() + +# Retrieve GPIO lib +GPIO = webiopi.GPIO + +SWITCH = 21 +SERVO = 23 +LED0 = 24 +LED1 = 25 + +# Called by WebIOPi at script loading +def setup(): + webiopi.debug("Script with macros - Setup") + # Setup GPIOs + GPIO.setFunction(SWITCH, GPIO.IN) + GPIO.setFunction(SERVO, GPIO.PWM) + GPIO.setFunction(LED0, GPIO.PWM) + GPIO.setFunction(LED1, GPIO.OUT) + + GPIO.pwmWrite(LED0, 0.5) # set to 50% ratio + GPIO.pwmWriteAngle(SERVO, 0) # set to 0 (neutral) + GPIO.digitalWrite(LED1, GPIO.HIGH) + + gpio0 = webiopi.deviceInstance("gpio0") + gpio0.digitalWrite(0, 0) + +# Looped by WebIOPi +def loop(): + # Toggle LED each 5 seconds + value = not GPIO.digitalRead(LED1) + GPIO.digitalWrite(LED1, value) + webiopi.sleep(5) + +# Called by WebIOPi at server shutdown +def destroy(): + webiopi.debug("Script with macros - Destroy") + # Reset GPIO functions + GPIO.setFunction(SWITCH, GPIO.IN) + GPIO.setFunction(SERVO, GPIO.IN) + GPIO.setFunction(LED0, GPIO.IN) + GPIO.setFunction(LED1, GPIO.IN) + gpio0 = webiopi.deviceInstance("gpio0") + gpio0.digitalWrite(0, 1) + +# A macro which says hello +@webiopi.macro +def HelloWorld(first, last): + webiopi.debug("HelloWorld(%s, %s)" % (first, last)) + return "Hello %s %s !" % (first, last) + +# A macro without args which return nothing +@webiopi.macro +def PrintTime(): + webiopi.debug("PrintTime: " + time.asctime()) + 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 @@ + + + + + + WebIOPi | Demo + + + + + +
+ + -- cgit v1.2.3