User avatar
wallarug
Posts: 460
Joined: Mon May 14, 2012 8:21 am
Location: Sydney, Australia

Webiopi Issues (PWM)

Sat Sep 06, 2014 12:21 pm

Hey Guys!

I have set up my gertboard so that I can use the LEDs to test some feature of webiopi.

I can't get the PWM features working... can someone PLEASE help me fix this?

What happens when I send PWM to POST:
1) Slide "Slider" in Chrome
2) The LED "pulses" on for a fraction of a second, then, turns off.

Code samples below:
index.html:

Code: Select all

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <meta name="viewport" content = "height = device-height, width = 420, user-scalable = no" /> 
        <title>Hot Box Control</title>
        <script type="text/javascript" src="/webiopi.js"></script>
	 <script type="text/javascript" src="/jquery-1.11.1.min.js"></script>
	 <script type="text/javascript" src="/jquery.mobile-1.4.3.min.js"></script>
	 <link rel="stylesheet" href="/jquery.mobile.icons.min.css" />
	 <link rel="stylesheet" href="/jquery.mobile.structure-1.4.3.min.css" />
	 <link rel="stylesheet" href="/jquery.mobile-1.4.3.min.css" />
        <script type="text/javascript">
       var current = "off"; 
	webiopi().ready(function() {
                var content, button, contentA, masters;
		  
                contentA = $("#contentA");
		  content = $("#content");
		  masters = $("#masters");
              
//webiopi().updateFunction(4,"PWM");   
//webiopi().enablePWM(4);
alert(webiopi().getFunction(4));    
                
		// Gertboard Buffers Section --> GPIOs 2,3,4,7,8,9,10,11 and 22,23,24,25
		// create a "SWITCH" labeled button for Buffer
                button = webiopi().createGPIOButton(2, "GPIO 2");
                contentA.append(button); // append button to content div
		// create a "SWITCH" labeled button for Buffer
                button = webiopi().createGPIOButton(3, "GPIO 3");
                contentA.append(button); // append button to content div
		// create a "SWITCH" labeled button for Buffer
                button = webiopi().createGPIOButton(4, "GPIO 4");
                contentA.append(button); // append button to content div
		// create a "SWITCH" labeled button for Buffer
                button = webiopi().createGPIOButton(7, "GPIO 7");
                contentA.append(button); // append button to content div
		// create a "SWITCH" labeled button for Buffer
                button = webiopi().createGPIOButton(8, "GPIO 8");
                contentA.append(button); // append button to content div
		// create a "SWITCH" labeled button for Buffer
                button = webiopi().createGPIOButton(9, "GPIO 9");
                contentA.append(button); // append button to content div
		// create a "SWITCH" labeled button for Buffer
                button = webiopi().createGPIOButton(10, "GPIO 10");
                contentA.append(button); // append button to content div
		// create a "SWITCH" labeled button for Buffer
                button = webiopi().createGPIOButton(11, "GPIO 11");
                contentA.append(button); // append button to content div
		// create a "SWITCH" labeled button for Buffer
                button = webiopi().createGPIOButton(22, "GPIO 22");
                contentA.append(button); // append button to content div
		// create a "SWITCH" labeled button for Buffer
                button = webiopi().createGPIOButton(23, "GPIO 23");
                contentA.append(button); // append button to content div
		// create a "SWITCH" labeled button for Buffer
                button = webiopi().createGPIOButton(24, "GPIO 24");
                contentA.append(button); // append button to content div
		// create a "SWITCH" labeled button for Buffer
                button = webiopi().createGPIOButton(25, "GPIO 25");
                contentA.append(button); // append button to content div
		// MASTER SWITCHES ----
		// Button HOLD MASTER  
		  button = webiopi().createButton("hold", "HOLD_ALL", mousedownMaster, mouseupMaster);
		  masters.append(button);
		// Button MASTER ON/OFF
		  button = webiopi().createButton("onoff","ON_OFF", onoffMaster);
		  masters.append(button);



		// HOT BOX CENTRAL CONTROL / LAYOUT


             
                // create a "LED" labelled button for GPIO 7
                button = webiopi().createGPIOButton(7, "LED");
                content.append(button); // append button to content div
                       
                // create a button that output a single pulse
                button = webiopi().createPulseButton("pulse", "Pulse", 7);
                content.append(button); // append button to content div
                
                // create a button which output a bit sequence on GPIO 7 with a 100ms period 
                button = webiopi().createSequenceButton("sos", "S.O.S 1", 7, 100, "01010100110011001100101010");
                content.append(button); // append button to content div
                       
                // the previous button will always output the same sequence
                // you can also create a simple button with your own function 
                button = webiopi().createButton("sos2", "S.O.S 2", outputSequence);
                content.append(button); // append button to content div
                
                // you can also create a button which calls a different function for mouse down and up events
                //button = webiopi().createButton("hold", "Hold", mousedown, mouseup);
                //content.append(button);
                
                // Only for Chrome and Safari, create a slider that pulse out a 0-100% duty cycle ratio on GPIO 8
                button = webiopi().createRatioSlider(4);
		  webiopi().pulseRatio(4,0.5);
                content.append(button);
                       
                // Only for Chrome and Safari, create a slider that pulse out a -45 to +45° angle on GPIO 9
                button = webiopi().createAngleSlider(9);
                content.append(button);
		
		

        });
                
 
	function mousedownMaster() {
              webiopi().digitalWrite(2, 1);
              webiopi().digitalWrite(3, 1);
              webiopi().digitalWrite(4, 1);
              webiopi().digitalWrite(7, 1);
              webiopi().digitalWrite(8, 1);
              webiopi().digitalWrite(9, 1);
              webiopi().digitalWrite(10, 1);
              webiopi().digitalWrite(11, 1);
              webiopi().digitalWrite(22, 1);
              webiopi().digitalWrite(23, 1);
              webiopi().digitalWrite(24, 1);
              webiopi().digitalWrite(25, 1);		
		
        }
        
        function mouseupMaster() {
              webiopi().digitalWrite(2, 0);
              webiopi().digitalWrite(3, 0);
              webiopi().digitalWrite(4, 0);
              webiopi().digitalWrite(7, 0);
              webiopi().digitalWrite(8, 0);
              webiopi().digitalWrite(9, 0);
              webiopi().digitalWrite(10, 0);
              webiopi().digitalWrite(11, 0);
              webiopi().digitalWrite(22, 0);
              webiopi().digitalWrite(23, 0);
              webiopi().digitalWrite(24, 0);
              webiopi().digitalWrite(25, 0);
        }
        
	function onoffMaster() {
              if(current=="off"){
		webiopi().digitalWrite(2, 1);
              webiopi().digitalWrite(3, 1);
              webiopi().digitalWrite(4, 1);
              webiopi().digitalWrite(7, 1);
              webiopi().digitalWrite(8, 1);
              webiopi().digitalWrite(9, 1);
              webiopi().digitalWrite(10, 1);
              webiopi().digitalWrite(11, 1);
              webiopi().digitalWrite(22, 1);
              webiopi().digitalWrite(23, 1);
              webiopi().digitalWrite(24, 1);
              webiopi().digitalWrite(25, 1);
		current = "on";		
		}else{
		webiopi().digitalWrite(2, 0);
              webiopi().digitalWrite(3, 0);
              webiopi().digitalWrite(4, 0);
              webiopi().digitalWrite(7, 0);
              webiopi().digitalWrite(8, 0);
              webiopi().digitalWrite(9, 0);
              webiopi().digitalWrite(10, 0);
              webiopi().digitalWrite(11, 0);
              webiopi().digitalWrite(22, 0);
              webiopi().digitalWrite(23, 0);
              webiopi().digitalWrite(24, 0);
              webiopi().digitalWrite(25, 0);
		current = "off";
		}
		
        }

        function outputSequence() {
                var sequence = "01010100110011001100101010" // S.O.S. morse code or whatever you want
                // output sequence on gpio 7 with a 100ms period
                webiopi().outputSequence(7, 100, sequence, sequenceCallback);
        }
        
        function sequenceCallback(gpio, data) {
                alert("sequence on " + gpio + " finished with " + data);
        }
        
        function callMacro() {
                var args = [4,2,3] // or whatever you want
                // call myMacroWithArgs(arg)
                webiopi().callMacro("myMacroWithArgs", args, macroCallback);
        }

	 function updatePWMcycle(args) {
                //var args =  // or whatever you want
                // call myMacroWithArgs(arg)
                webiopi().callMacro("SetPWMonLight", args, macroCallback);
		  console.log("args: " + args);
        }
        
        function macroCallback(macro, args, data) {
                alert(macro + " returned with " + data);
        }
        // For Slider Testing
		$( ".slider-1" ).on( 'slidestop', function( event, ui ) { 
			updatePWMcycle($("#slider-1").value);
			console.log("Change Occured!!");
		});
        </script>
        <style type="text/css">
                button {
                        display: block;
                        margin: 5px 5px 5px 5px;
                        width: 160px;
                        height: 45px;
                        font-size: 16pt;
                        font-weight: bold;
                        color: black;
                }
                custom_button {
                        display: block;
                        margin: 5px 5px 5px 5px;
                        width: 160px;
                        height: 45px;
                        font-size: 24pt;
                        font-weight: bold;
                        color: black;
                }
                
                input[type="range"] {
                        display: block;
                        width: 160px;
                        height: 45px;
                }
                
                #gpio2.LOW {
                        background-color: White;
                }
                
                #gpio2.HIGH {
                        background-color: Red;
                }
                #gpio3.LOW {
                        background-color: White;
                }
                
                #gpio3.HIGH {
                        background-color: Red;
                }
                #gpio4.LOW {
                        background-color: White;
                }
                
                #gpio4.HIGH {
                        background-color: Red;
                }
                #gpio7.LOW {
                        background-color: White;
                }
                
                #gpio7.HIGH {
                        background-color: Red;
                }
                #gpio8.LOW {
                        background-color: White;
                }
                
                #gpio8.HIGH {
                        background-color: Red;
                }
                #gpio9.LOW {
                        background-color: White;
                }
                
                #gpio9.HIGH {
                        background-color: Red;
                }
                #gpio10.LOW {
                        background-color: White;
                }
                
                #gpio10.HIGH {
                        background-color: Red;
                }
                #gpio11.LOW {
                        background-color: White;
                }
                
                #gpio11.HIGH {
                        background-color: Red;
                }
                #gpio22.LOW {
                        background-color: White;
                }
                
                #gpio22.HIGH {
                        background-color: Red;
                }
                #gpio23.LOW {
                        background-color: White;
                }
                
                #gpio23.HIGH {
                        background-color: Red;
                }
                #gpio24.LOW {
                        background-color: White;
                }
                
                #gpio24.HIGH {
                        background-color: Red;
                }
                #gpio25.LOW {
                        background-color: White;
                }
                
                #gpio25.HIGH {
                        background-color: Red;
                }






        </style>
</head>
<body>
  <div data-role="page" id='home' data-theme='a' data-content='a'>
    <div data-role="header" data-theme="a">
      <h1>Hot Box Central Control</h1>
   </div>
    <div data-role="content">
        <div class='ui-grid-b'>
           <div class='ui-block-a' style='text-align:center;'>
		<div data-role='header' data-theme='b'><h2> Gertboard Buffers </h2></div>
		<br />
		<table>
		<tr><th>Overrides</th><th>MASTERS</th></tr>
		<tr>
		<td><div id="contentA" align="center"></div></td>
		<td><div id="masters" align="center"></div></td>
		</tr>
		</table>
		
	    </div>
	    <div class='ui-block-b'>
		
		<div id="HotBox"></div>
	    </div>
	    <div class='ui-block-c'>
		<div id="content" align="center"></div>
		<input type="range" name="slider-1" id="slider-1" value="60" min="0" max="100" />
		<button onclick="$.ajax({type:'POST',url: '/GPIO/2/function/pwm',data: 'pwm'});">POST AJAX PWM</button>
		<button onclick="$.ajax({type:'POST',url: '/GPIO/2/pulseRatio/0.5',data: '0.5'});">POST AJAX</button>
		<form action="" method="POST">
		<button href"#" name="buttonpost" type="submit" value="/GPIO/2/pulseRatio/0.5">POST 0.5 DUTY</button>
		</form>
	    </div>

    </div>
    <!-- /content -->

        
</body>
</html>
script.py <-- I don't think this is working either.

Code: Select all

# Imports
import webiopi
import time

# Enable debug output
webiopi.setDebug()

# Retrieve GPIO lib
GPIO = webiopi.GPIO

SWITCH = 21
#SERVO  = 23
LED0   = 2
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())
config file:

Code: Select all

[GPIO]
# Initialize following GPIOs with given function and optional value
# This is used during WebIOPi start process
#21 = IN
#23 = OUT 0
#24 = OUT 0
#25 = OUT 1

#For Hot Box or LED lights
2 = OUT 0
3 = OUT 0
4 = OUT 0
7 = OUT 0
22 = OUT 0
23 = OUT 0
24 = OUT 0
25 = OUT 0
8 = OUT 0
9 = OUT 0
10 = OUT 0
11 = OUT 0

#------------------------------------------------------------------------#

[~GPIO]
# Reset following GPIOs with given function and optional value
# This is used at the end of WebIOPi stop process
#21 = IN
#23 = IN
#24 = IN
#25 = OUT 0
#For Hot Box or LED lights
2 = OUT 0
3 = OUT 0
4 = OUT 0
7 = OUT 0
22 = OUT 0
23 = OUT 0
24 = OUT 0
25 = OUT 0
8 = OUT 0
9 = OUT 0
10 = OUT 0
11 = OUT 0

#------------------------------------------------------------------------#

[SCRIPTS]
# Load custom scripts syntax :
# name = sourcefile
#   each sourcefile may have setup, loop and destroy functions and macros
myscript = /usr/share/webiopi/htdocs/app/hot-box/script.py

#------------------------------------------------------------------------#

[HTTP]
# HTTP Server configuration
enabled = true
port = 8000

# File containing sha256(base64("user:password"))
# Use webiopi-passwd command to generate it
passwd-file = /etc/webiopi/passwd

# Change login prompt message
prompt = "WebIOPi"

# Use doc-root to change default HTML and resource files location
#doc-root = /home/pi/webiopi/examples/scripts/macros

# Use welcome-file to change the default "Welcome" file
#welcome-file = index.html

#------------------------------------------------------------------------#

[COAP]
# CoAP Server configuration
enabled = true
port = 5683
# Enable CoAP multicast
multicast = true

#------------------------------------------------------------------------#

[DEVICES]
# Device configuration syntax:
# name = device [args...]
#   name   : used in the URL mapping
#   device : device name
#   args   : (optional) see device driver doc
# If enabled, devices configured here are mapped on REST API /device/name
# Devices are also accessible in custom scripts using deviceInstance(name)
# See device driver doc for methods and URI scheme available

# Raspberry native UART on GPIO, uncomment to enable
# Don't forget to remove console on ttyAMA0 in /boot/cmdline.txt
# And also disable getty on ttyAMA0 in /etc/inittab
#serial0 = Serial device:ttyAMA0 baudrate:9600

# USB serial adapters
#usb0 = Serial device:ttyUSB0 baudrate:9600
#usb1 = Serial device:ttyACM0 baudrate:9600

#temp0 = TMP102
#temp1 = TMP102 slave:0x49
#temp2 = DS18B20
#temp3 = DS18B20 slave:28-0000049bc218

#bmp = BMP085

#gpio0 = PCF8574
#gpio1 = PCF8574 slave:0x21

#light0 = TSL2561T
#light1 = TSL2561T slave:0b0101001

#gpio0 = MCP23017
#gpio1 = MCP23017 slave:0x21
#gpio2 = MCP23017 slave:0x22

#pwm0 = PCA9685
#pwm1 = PCA9685 slave:0x41

#adc0 = MCP3008
#adc1 = MCP3008 chip:1 vref:5
#dac1 = MCP4922 chip:1

#------------------------------------------------------------------------#

[REST]
# By default, REST API allows to GET/POST on all GPIOs
# Use gpio-export to limit GPIO available through REST API  
#gpio-export = 21, 23, 24, 25

# Uncomment to forbid changing GPIO values 
#gpio-post-value = false

# Uncomment to forbid changing GPIO functions 
gpio-post-function = false 

# Uncomment to disable automatic device mapping
#device-mapping = false

#------------------------------------------------------------------------#

[ROUTES]
# Custom REST API route syntax :
# source = destination
#   source      : URL to route
#   destination : Resulting URL
# Adding routes allows to simplify access with Human comprehensive URLs

# In the next example with have the bedroom light connected to GPIO 25
# and a temperature sensor named temp2, defined in [DEVICES] section
#  - GET  /bedroom/light       => GET  /GPIO/25/value, returns the light state
#  - POST /bedroom/light/0     => POST /GPIO/25/value/0, turn off the light
#  - POST /bedroom/light/1	   => POST /GPIO/25/value/1, turn on the light
#  - GET  /bedroom/temperature => GET  /devices/temp2/temperature/c, returns the temperature in celsius
#/bedroom/light = /GPIO/25/value
#/bedroom/temperature = /devices/temp2/temperature/c

#/livingroom/light = /devices/expander0/0
#/livingroom/brightness = /devices/adc/0/float
#/livingroom/temperature = /devices/temp0/temperature/c

#/weather/temperature = /devices/bmp/temperature/c
#/weather/pressure = /devices/bmp/pressure/hpa

Any help will be appreciated.

ANUJ VERMA
Posts: 1
Joined: Thu Feb 25, 2016 1:01 pm

Re: Webiopi Issues (PWM)

Thu Feb 25, 2016 1:04 pm

i am facing the same issue so if you find some solution then plz share it.

Return to “General programming discussion”