简体   繁体   English

如果复选框为true,则为paho-mqtt-client设置变量/主题

[英]if checkbox true, set variable/topic for paho-mqtt-client

Hi i want to set the mqtt-topic to a specific value when a specific checkbox is checked. 嗨,我想在选中特定复选框时将mqtt-topic设置为特定值。 See my code here: my code I think I need a submit-button to send a value to my HTML document. 在这里查看我的代码: 我的代码我认为我需要一个提交按钮才能将值发送到HTML文档。 However, I have no idea. 但是,我不知道。 I have to set the topic before the script is executed. 我必须在执行脚本之前设置主题。 The topic is stored in the variable options (js-part). 主题存储在变量选项(js-part)中。

form-part 形式部分

    <form>  
    <div id="measurement" style="float:left">
    <label for="measurement"><b>Measurement-Type:</b><br />
        <input type="checkbox" id="temperature" name="temperature">temperature<br />
        <input type="checkbox" id="moisture" name="moisture">moisture<br />
        <input type="checkbox" id="conductivity" name="conductivity">conductivity<br />
        <input type="checkbox" id="light_intensity" name="light_intensity">light_intensity<br />
        <input type="checkbox" id="pressure" name="pressure">pressure<br />
        <input type="checkbox" id="ppm" name="ppm">ppm<br />
        <input type="checkbox" id="intensity" name="intensity">intensity<br />
        <input type="checkbox" id="um" name="um">um<br />
    </label>
    </div>
    <div id="set" style="float:left">
        <input type="submit" onclick="topic();" />
    </div>
</form> 

script-part 脚本部分

var topic = '';

function topic() {
    //if id="temperature" set var topic to "//////temperature"
    if (document.getElementById("temperature").checked == true) {
      document.getElementById("temperature").checked;
      topic = "//////temperature"
    }

var options = {
    timeout: 3,
    userName: "user",
    password: "pw",
    onSuccess: function () {
        console.log("mqtt broker connected");

        //here is the topic variable
        client.subscribe(topic, {qos: 0});
        },
        onFailure: function (message) {
            console.log("Connection failed: " + message.errorMessage);
    }
};

i dont know, i think this code here is not useful, but I have to put one here 我不知道,我认为这里的代码没有用,但是我必须在这里放一个

1) First, you're missing a curly brace to end the function topic(). 1)首先,您缺少大括号来结束函数topic()。 I'm assuming you don't want the options variable inside the topic function. 我假设您不想在topic函数中使用options变量。

function topic() {
    //if id="temperature" set var topic to "//////temperature"
    if (document.getElementById("temperature").checked == true) {
      document.getElementById("temperature").checked;
      topic = "//////temperature"
    }
}  // <<- ADD THIS HERE

2) What you don't show are the statements where you connect to the MQTT server and where the client object gets created. 2)您未显示的是连接到MQTT服务器和创建客户端对象的语句。 The functions you embedded in the options variable are callbacks for those statements. 您嵌入在options变量中的函数是这些语句的回调。 It's important because of the sequence of events. 由于事件的顺序很重要。 Depending on when you connect, topic might or might not be defined. 根据您连接的时间,可能会或可能不会定义主题。 It's not clear from your code. 从您的代码尚不清楚。 For example, where are: 例如,在哪里:

client = new Paho.MQTT.Client(MQTT_ADDRESS, MQTT_PORT, MQTT_CLIENT_ID);
client.connect(options);

3) You might also want to check the value of topic before you subscribe in case it's empty: 3)您可能还想在订阅之前检查topic的值,以防其为空:

if (topic !== '') client.subscribe(topic);

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM