WebIOPi:我无法将参数从 JS 传递到 Python
WebIOPi: I can't pass arguments from JS to Python
您好,
我在 Raspberry PI 2 B+ 上有一个小项目 运行ning。主要目标是通过 I2C 和 WebIOPi 网络接口(0.7.1,包括 Pi2 的补丁)控制机电开关(独立地址,4 个位置)。
我已经用我写的一个小的 c++ 程序成功地测试了 I2C 总线,我还能够通过 Python 宏控制 WebIOPi 接口上的切换。但基于我想在总线上使用十多个开关的事实,我需要将参数传递给我的 python 宏(地址和位置)。问题来了。每次我尝试传递参数时,就像在教程中解释的那样,它无法 运行 切换。
摘录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,[]);
});
摘录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])
在这种情况下,posButton1 工作正常并驱动开关。但是第二个选项在点击开关时什么都不做。
我尝试了不同的语法,比如不将 callMacro() 的回调与“[]”隔开,或者将参数作为字符串或不使用“[]”,甚至像 Position 这样的单个参数。
此外,我尝试使用 createMacroButton(),但我得到了同样的负面结果。
我希望这些信息足以正确描述问题。
此致
HP
P.S.: 我在 JS 和 Python 方面的经验非常有限,因为这是我第一个使用这些语言的项目。
已找到解决方案!
我通过在 Python 脚本中使用类型转换 (int()) 找到了解决方案:
@webiopi.macro
def sendI2C2(SWITCH, POS):
i2c.write_byte(address_list1[int(SWITCH)], POSITION[int(POS)])
您好,
我在 Raspberry PI 2 B+ 上有一个小项目 运行ning。主要目标是通过 I2C 和 WebIOPi 网络接口(0.7.1,包括 Pi2 的补丁)控制机电开关(独立地址,4 个位置)。 我已经用我写的一个小的 c++ 程序成功地测试了 I2C 总线,我还能够通过 Python 宏控制 WebIOPi 接口上的切换。但基于我想在总线上使用十多个开关的事实,我需要将参数传递给我的 python 宏(地址和位置)。问题来了。每次我尝试传递参数时,就像在教程中解释的那样,它无法 运行 切换。
摘录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,[]);
});
摘录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])
在这种情况下,posButton1 工作正常并驱动开关。但是第二个选项在点击开关时什么都不做。 我尝试了不同的语法,比如不将 callMacro() 的回调与“[]”隔开,或者将参数作为字符串或不使用“[]”,甚至像 Position 这样的单个参数。 此外,我尝试使用 createMacroButton(),但我得到了同样的负面结果。
我希望这些信息足以正确描述问题。
此致 HP
P.S.: 我在 JS 和 Python 方面的经验非常有限,因为这是我第一个使用这些语言的项目。
已找到解决方案!
我通过在 Python 脚本中使用类型转换 (int()) 找到了解决方案:
@webiopi.macro
def sendI2C2(SWITCH, POS):
i2c.write_byte(address_list1[int(SWITCH)], POSITION[int(POS)])