turtlestitch/libraries/mqtt.xml

2 wiersze
13 KiB
XML

<blocks app="Snap! 7, https://snap.berkeley.edu" version="2"><palette><category name="MQTT" color="143,0,100,1"/></palette><block-definition s="$cloudOutline MQTT connect to %&apos;broker&apos; $nl $options-1-192-192-192 %&apos;options&apos;" type="command" category="MQTT"><comment w="416.6666666666666" collapsed="false">Connect to a broker&#xD;&#xD;The options are not required but can be filled if needed&#xD;&#xD;1. username (some brokers need this)&#xD;2. password (some brokers need this)&#xD;3. keepalive (default time is 60 seconds but some brokers need a shorter time)&#xD;4. connection callback (ringed set of scripts to be run when connection is made or remade)&#xD;&#xD;Note: The connection callback doesn&apos;&apos;t have to be the 4th option - it just has to be the last (or only) option.&#xD;&#xD;ADVANCED: The brokers offered in the menu are accessed using default ports and basepaths (if needed). If accessing other brokers, the complete urls should be formed like this:&#xD;&#xD;siteURL:port/basepath &#xD;e.g mqtt.eclipseprojects.io:443/mqtt&#xD;&#xD;&#xD;</comment><header></header><code></code><translations></translations><inputs><input type="%s">broker.emqx.io<options>broker.emqx.io&#xD;mqtt.eclipseprojects.io&#xD;test.mosquitto.org&#xD;broker.xmqtt.net</options></input><input type="%mult%s"></input></inputs><script><block s="doApplyExtension"><l>src_load(url)</l><list><l>libraries/mqtt.js</l></list></block><block s="doApplyExtension"><l>src_load(url)</l><list><l>libraries/mqttExtension.js</l></list></block><block s="doIf"><block s="reportNot"><block s="reportJoinWords"><block var="options"/></block></block><script><block s="doSetVar"><l>options</l><block s="reportNewList"><list></list></block></block></script></block><block s="doDeclareVariables"><list><l>connection status</l><l>callback</l></list></block><block s="doSetVar"><l>connection status</l><l>connecting</l></block><block s="doSetVar"><l>callback</l><l></l></block><block s="doIf"><block s="reportIsA"><block s="reportListItem"><l><option>last</option></l><block var="options"/></block><l><option>command</option></l></block><script><block s="doSetVar"><l>callback</l><block s="reportListItem"><l><option>last</option></l><block var="options"/></block></block><block s="doDeleteFromList"><l><option>last</option></l><block var="options"/></block></script></block><block s="doSetVar"><l>options</l><block s="reportListAttribute"><l><option>json</option></l><block s="reportNewList"><list><block s="reportNewList"><list><l>username</l><block s="reportListItem"><l>1</l><block var="options"/></block></list></block><block s="reportNewList"><list><l>password</l><block s="reportListItem"><l>2</l><block var="options"/></block></list></block><block s="reportNewList"><list><l>keepalive</l><block s="reportListItem"><l>3</l><block var="options"/></block></list></block></list></block></block></block><block s="doApplyExtension"><l>mqt_connect(broker,callback,options)</l><list><block var="broker"/><block var="callback"/><block var="options"/></list></block><block s="doWaitUntil"><block s="reportNot"><block s="reportEquals"><block var="connection status"/><l>connecting</l></block></block></block><block s="doIf"><block s="reportNot"><block s="reportEquals"><block var="connection status"/><l>connected</l></block></block><script><block s="doApplyExtension"><l>err_error(msg)</l><list><block var="connection status"/></list></block></script></block></script></block-definition><block-definition s="$cloudOutline MQTT publish to %&apos;broker&apos; topic %&apos;topic&apos; message %&apos;message&apos; $nl $options-1-192-192-192 %&apos;options&apos;" type="command" category="MQTT"><comment w="200" collapsed="false">Publish a message to an MQTT broker&#xD;&#xD;The options are not required but can be filled if needed&#xD;&#xD;1. qos (quality of service 0,1 or 2)&#xD;2. retain flag (boolean)&#xD;</comment><header></header><code></code><translations></translations><inputs><input type="%s">broker.emqx.io<options>broker.emqx.io&#xD;mqtt.eclipseprojects.io&#xD;test.mosquitto.org&#xD;broker.xmqtt.net</options></input><input type="%s">name</input><input type="%s">hello</input><input type="%mult%s"></input></inputs><script><block s="doApplyExtension"><l>src_load(url)</l><list><l>libraries/mqtt.js</l></list></block><block s="doApplyExtension"><l>src_load(url)</l><list><l>libraries/mqttExtension.js</l></list></block><block s="doIf"><block s="reportNot"><block s="reportJoinWords"><block var="options"/></block></block><script><block s="doSetVar"><l>options</l><block s="reportNewList"><list></list></block></block></script></block><block s="doSetVar"><l>options</l><block s="reportListAttribute"><l><option>json</option></l><block s="reportNewList"><list><block s="reportNewList"><list><l>qos</l><block s="reportListItem"><l>1</l><block var="options"/></block></list></block><block s="reportNewList"><list><l>retain</l><block s="reportListItem"><l>2</l><block var="options"/></block></list></block></list></block></block></block><block s="doIf"><block s="reportIsA"><block var="message"/><l><option>list</option></l></block><script><block s="doSetVar"><l>message</l><block s="reportListAttribute"><l><option>json</option></l><block var="message"/></block></block></script></block><block s="doIf"><block s="reportIsA"><block var="message"/><l><option>number</option></l></block><script><block s="doSetVar"><l>message</l><block s="reportJoinWords"><list><block var="message"/></list></block></block></script></block><block s="doApplyExtension"><l>mqt_pub(broker,topic,message,options)</l><list><block var="broker"/><block var="topic"/><block var="message"/><block var="options"/></list></block></script></block-definition><block-definition s="$cloudOutline MQTT subscribe to %&apos;broker&apos; topic %&apos;topic&apos; $nl on each message run %&apos;callback&apos;" type="command" category="MQTT"><comment w="216.9999999999999" collapsed="false">Subscribe to a topic on a broker&#xD;&#xD;Make sure you have already run an MQTT connect block before using this one&#xD;&#xD;You should add a script to be run inside he grey ring.&#xD;&#xD;It will be run each time a message is received&#xD;&#xD;If you click on the arrow on the grey ring twice, you can use #1 and #2 inside yoiru script.&#xD;&#xD;#1 will contain the received message&#xD;&#xD;#2 will contain the received topic name</comment><header></header><code></code><translations></translations><inputs><input type="%s">broker.emqx.io<options>broker.emqx.io&#xD;mqtt.eclipseprojects.io&#xD;test.mosquitto.org&#xD;broker.xmqtt.net</options></input><input type="%s">name</input><input type="%cmdRing"></input></inputs><script><block s="doApplyExtension"><l>src_load(url)</l><list><l>libraries/mqtt.js</l></list></block><block s="doApplyExtension"><l>src_load(url)</l><list><l>libraries/mqttExtension.js</l></list></block><block s="doApplyExtension"><l>mqt_sub(broker,topic,callback,options)</l><list><block var="broker"/><block var="topic"/><block var="callback"/><l></l></list></block></script></block-definition><block-definition s="$cloudOutline MQTT unsubscribe from %&apos;broker&apos; topic %&apos;topic&apos;" type="command" category="MQTT"><header></header><code></code><translations></translations><inputs><input type="%s">broker.emqx.io<options>broker.emqx.io&#xD;mqtt.eclipseprojects.io&#xD;test.mosquitto.org&#xD;broker.xmqtt.net</options></input><input type="%s">name</input></inputs><script><block s="doApplyExtension"><l>src_load(url)</l><list><l>libraries/mqtt.js</l></list></block><block s="doApplyExtension"><l>src_load(url)</l><list><l>libraries/mqttExtension.js</l></list></block><block s="doApplyExtension"><l>mqt_unsub(broker,topic)</l><list><block var="broker"/><block var="topic"/></list></block></script></block-definition><block-definition s="$cloudOutline MQTT disconnect from %&apos;broker&apos;" type="command" category="MQTT"><header></header><code></code><translations></translations><inputs><input type="%s">all<options>all&#xD;broker.emqx.io&#xD;mqtt.eclipseprojects.io&#xD;test.mosquitto.org&#xD;broker.xmqtt.net</options></input></inputs><script><block s="doApplyExtension"><l>src_load(url)</l><list><l>libraries/mqtt.js</l></list></block><block s="doApplyExtension"><l>src_load(url)</l><list><l>libraries/mqttExtension.js</l></list></block><block s="doApplyExtension"><l>mqt_disconnect(broker)</l><list><block var="broker"/></list></block></script></block-definition><block-definition s="$cloudOutline MQTT response %&apos;broker&apos; topic %&apos;topic&apos; $nl reply %&apos;reply&apos;" type="command" category="MQTT"><header></header><code></code><translations></translations><inputs><input type="%s">broker.emqx.io<options>broker.emqx.io&#xD;mqtt.eclipseprojects.io&#xD;test.mosquitto.org&#xD;broker.xmqtt.net</options></input><input type="%s">name</input><input type="%repRing"></input></inputs><script><block s="doDeclareVariables"><list><l>resp</l><l>callbackTopic</l></list></block><custom-block s="$cloudOutline MQTT subscribe to %s topic %s %br on each message run %cmdRing"><block var="broker"/><block var="topic"/><block s="reifyScript"><script><block s="doSetVar"><l>resp</l><block s="evaluate"><block var="reply"/><list><block var="received topic"/><block var="received message"/></list></block></block><block s="doSetVar"><l>callbackTopic</l><block s="reportListItem"><l><option>last</option></l><block s="reportTextSplit"><block var="topic"/><l>/</l></block></block></block><block s="doIf"><block s="reportNot"><block s="reportEquals"><block var="callbackTopic"/><block var="topic"/></block></block><script><custom-block s="$cloudOutline MQTT publish to %s topic %s message %s %br $options-1-192-192-192 %mult%s"><block var="broker"/><block var="callbackTopic"/><block var="resp"/><list><l></l></list></custom-block></script></block></script><list><l>received message</l><l>received topic</l></list></block></custom-block></script></block-definition><block-definition s="$cloudOutline MQTT request %&apos;broker&apos; topic %&apos;topic&apos; idCall %&apos;idCall&apos; message %&apos;msg&apos; $nl on each received messsage run %&apos;callback&apos;" type="command" category="MQTT"><header></header><code></code><translations></translations><inputs><input type="%s">broker.emqx.io<options>broker.emqx.io&#xD;mqtt.eclipseprojects.io&#xD;test.mosquitto.org&#xD;broker.xmqtt.net</options></input><input type="%s">name</input><input type="%s">idCall</input><input type="%s">hello</input><input type="%cmdRing"></input></inputs><script><custom-block s="$cloudOutline MQTT subscribe to %s topic %s %br on each message run %cmdRing"><block var="broker"/><block s="reportJoinWords"><list><l>idCall-</l><block var="idCall"/></list></block><block s="reifyScript"><script><block s="doRun"><block var="callback"/><list><block var="received topic"/><block var="received message"/></list></block></script><list><l>received message</l><l>received topic</l></list></block></custom-block><custom-block s="$cloudOutline MQTT publish to %s topic %s message %s %br $options-1-192-192-192 %mult%s"><block var="broker"/><block s="reportJoinWords"><list><block var="topic"/><l>/</l><block s="reportJoinWords"><list><l>idCall-</l><block var="idCall"/></list></block></list></block><block var="msg"/><list><l></l></list></custom-block></script></block-definition><block-definition s="$cloudOutline MQTT example response reporter topic %&apos;topic&apos; message %&apos;msg&apos;" type="reporter" category="MQTT"><header></header><code></code><translations></translations><inputs><input type="%s">name</input><input type="%s">hello</input></inputs><script><block s="doReport"><block s="reportJoinWords"><list><l>Echo: </l><block var="msg"/></list></block></block></script></block-definition><block-definition s="$cloudOutline MQTT request %&apos;broker&apos; topic %&apos;topic&apos; idCall %&apos;idCall&apos; message %&apos;msg&apos;" type="reporter" category="MQTT"><header></header><code></code><translations></translations><inputs><input type="%s">broker.emqx.io<options>broker.emqx.io&#xD;mqtt.eclipseprojects.io&#xD;test.mosquitto.org&#xD;broker.xmqtt.net</options></input><input type="%s">name</input><input type="%s">idCall</input><input type="%s">hello</input></inputs><script><block s="doDeclareVariables"><list><l>resp</l><l>semaphore</l></list></block><block s="doSetVar"><l>semaphore</l><l>0</l></block><custom-block s="$cloudOutline MQTT subscribe to %s topic %s %br on each message run %cmdRing"><block var="broker"/><block s="reportJoinWords"><list><l>idCall-</l><block var="idCall"/></list></block><block s="reifyScript"><script><block s="doSetVar"><l>resp</l><block var="received message"/></block></script><list><l>received message</l><l>received topic</l></list></block></custom-block><custom-block s="$cloudOutline MQTT publish to %s topic %s message %s %br $options-1-192-192-192 %mult%s"><block var="broker"/><block s="reportJoinWords"><list><block var="topic"/><l>/</l><block s="reportJoinWords"><list><l>idCall-</l><block var="idCall"/></list></block></list></block><block var="msg"/><list><l></l></list></custom-block><block s="doWaitUntil"><block s="reportEquals"><block var="semaphore"/><l>1</l></block></block><block s="doReport"><block var="resp"/></block></script></block-definition></blocks>