Uses 1103K of libraries. Click here for Pure Java version (4531L/22K).
!7 lib 1400324 // Eclipse Paho (MQTT) import org.eclipse.paho.client.mqttv3.*; cmodule MQTTSpike > DynPrintLog { switchable S topic = "iot_data"; S messageText; transient MqttClient client; visual centerAndSouthWithMargins(super, dm_textFieldAndSubmit messageText('sendMessage, buttonText := "Send message")); start-thread { client = new MqttClient("tcp://localhost:1883", MqttClient.generateClientId()); client.setCallback(new MyCallback); client.connect(); client.subscribe(topic); print("Connected & subscribed to " + topic); } void cleanMeUp_mqtt ctex { if (client != null) { client.disconnect(); client.close(); } } class MyCallback implements MqttCallback { public void connectionLost(Throwable throwable) enter { print("Connection to MQTT broker lost!"); } public void messageArrived(S s, MqttMessage mqttMessage) enter { print("Message received: "+ fromUtf8(mqttMessage.getPayload())); } public void deliveryComplete(IMqttDeliveryToken token) enter { print("Delivery complete: " + token); } } void sendMessage ctex { temp enter(); new MqttMessage message; message.setPayload(toUtf8(messageText)); client.publish(topic, message); print("Published message: " + messageText); } // API void sendMessage(S topic, S text) ctex { new MqttMessage message; message.setPayload(toUtf8(text)); client.publish(topic, message); print("Published message in " + topic + ": " + text); } }
Began life as a copy of #1026859
download show line numbers debug dex old transpilations
Travelled to 7 computer(s): bhatertpkbcr, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tvejysmllsmz, vouqrxazstgt, xrpafgyirdlv
No comments. add comment
Snippet ID: | #1026860 |
Snippet name: | MQTT Subscriber & Publisher Spike (also used in Smart Home Demo) |
Eternal ID of this version: | #1026860/8 |
Text MD5: | 2dfeff8fc1626a0e2799653bc804be4c |
Transpilation MD5: | 30103c98fe539d92cb7c25261709000f |
Author: | stefan |
Category: | javax / mqtt |
Type: | JavaX source code (Dynamic Module) |
Public (visible to everyone): | Yes |
Archived (hidden from active list): | No |
Created/modified: | 2020-01-29 23:41:55 |
Source code size: | 1618 bytes / 60 lines |
Pitched / IR pitched: | No / No |
Views / Downloads: | 246 / 1391 |
Version history: | 7 change(s) |
Referenced in: | #1026869 - dm_mqtt_send |