Search code examples
javascriptpythonraspberry-piparameter-passingwebiopi

WebIOPi: I can't pass arguments from JS to Python


Hello,

I have a small Project running on a Raspberry PI 2 B+. The main goal is to control an electromechanical Switch (individual address, 4 Positions) over I2C and the WebIOPi webinterface (0.7.1 incl. Patch for the Pi2). I allready tested the I2C Bus successfully with a small c++ Programm i wrote and I'm also able to controll the switch over the WebIOPi Interface via Python Macro. But based on the fact that i want to use over ten switches on the Bus i need to pass arguments to my python macro (address and Position). Here comes the Problem. Each time I try to pass arguments, like it is explained on the Tutorials, it fails to run the switch.

Excerpt of Javascript:

var posButton1 = webiopi().createButton("posB11", "Pos 1", function(){
    webiopi().callMacro("sendI2C");
    });
$("#posB11").append(posButton1);
                
var posButton2 = webiopi().createButton("posB21", "Pos 2", function(){
    var args = [0,1];
    webiopi().callMacro("sendI2C2",args,[]);
    });

Excerpt of Python:

    import webiopi
    import smbus
    ...  
    @webiopi.macro
    def sendI2C():
        i2c.write_byte(address_list1[0], POSITION[0])
                
    @webiopi.macro
    def sendI2C2(SWITCH, POS):
        i2c.write_byte(address_list1[SWITCH], POSITION[POS])

In this case the posButton1 is working correctly and drives the switch. But the second option don't do nothing on a click on the switch. I tried different syntaxes, like not spacing the callback of callMacro() with "[]" or give the arguments as strings or without "[]" or even single arguments like the Position. In addition i tried to use the createMacroButton(), but i got the same negative results.

I hope this is enough Information to descripe the Problem right.

Best regards HP

P.S.: My experience in JS and Python is really limited, based on the fact, that this is my first Project with these Languages.


Solution

  • Solution found!

    I found a solution by using a typecast (int()) in the Python Script:

     @webiopi.macro
     def sendI2C2(SWITCH, POS):
         i2c.write_byte(address_list1[int(SWITCH)], POSITION[int(POS)])