JMRI keeps repeating MQTT messages to my Pico Wifi via Hivemq. Is there a way to stop this or reduce the repeitition? - mqtt

I borrowed code from Toms Hardware on how to use MQTT and subscribe. JRMI is the publisher of the messages and it keeps repeating them over and over again. Is there anyway to have the message sent only once? I dont have this problem when I subscribe to MQTT via http://www.hivemq.com/demos/websocket-client/ The MQTT service I'm using is broker.hivemq.com
For those not familiar with JRMI, it is the JAVA program that model railroads use to control tracks,lighting, DCC etc. Ref: https://www.jmri.org/
The link to Tom's is here https://www.tomshardware.com/how-to/send-and-receive-data-raspberry-pi-pico-w-mqtt
The code adapted from Tom's is
import network
import time
from machine import Pin
from umqtt.simple import MQTTClient
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect("whatever","pwd")
time.sleep(5)
print(wlan.isconnected())
mqtt_server = 'broker.hivemq.com'
client_id = 'bigles'
topic_sub = b'/trains/track/turnout/#'
def sub_cb(topic, msg):
print("New message on topic {}".format(topic.decode('utf-8')))
msg = msg.decode('utf-8')
print(msg)
def mqtt_connect():
client = MQTTClient(client_id, mqtt_server, keepalive=60)
client.set_callback(sub_cb)
client.connect()
print('Connected to %s MQTT Broker'%(mqtt_server))
return client
def reconnect():
print('Failed to connect to MQTT Broker. Reconnecting...')
time.sleep(5)
machine.reset()
try:
client = mqtt_connect()
except OSError as e:
reconnect()
while True:
client.subscribe(topic_sub)
time.sleep(1)
The setup inside JRMI for MQTT (edit->preferences) is as follows:

JMRI, by default, publishes with "the retain option on". When you subscribe to a topic the broker will send you the most recent (if any) retained message. This occurs even if you already had an identical subscription as per the MQTT Spec:
If a Server receives a SUBSCRIBE Packet containing a Topic Filter that is identical to an existing Subscription’s Topic Filter then it MUST completely replace that existing Subscription with a new Subscription. The Topic Filter in the new Subscription will be identical to that in the previous Subscription, although its maximum QoS value could be different. Any existing retained messages matching the Topic Filter MUST be re-sent, but the flow of publications MUST NOT be interrupted [MQTT-3.8.4-3].
In your code you are calling Subscribe in a loop:
while True:
client.subscribe(topic_sub)
time.sleep(1)
To avoid the repeated messages move the subscribe out of the loop (you only need to subscribe once!). Something like the following (simplified!) code:
client = mqtt_connect()
client.subscribe(topic_sub)
while True:
client.wait_msg() // Use client.check_msg() if you have other stuff to do

Related

awsiot mqtt reinstantiating subscriptions

I have a use case where the IoT-enabled device will be in patchy areas of connectivity (LTE/Mobile).
I am using Greengrass. While core GG services will re-establish connectivity and subscriptions, I have a component in which I subscribe to a topic:
import awsiot.greengrasscoreipc
import awsiot.greengrasscoreipc.client as client
from awsiot.greengrasscoreipc.model import (
QOS,
IoTCoreMessage,
SubscribeToIoTCoreRequest,
)
...
...
handler = StreamHandler()
ipc_client = awsiot.greengrasscoreipc.connect()
operation = ipc_client.new_subscribe_to_iot_core(handler)
topic_name = f"$aws/things/{os.environ['AWS_IOT_THING_NAME']}/tunnels/notify"
request = SubscribeToIoTCoreRequest(topic_name=topic_name, qos=QOS.AT_LEAST_ONCE)
future = operation.activate(request)
future.result(15)
My question is, does awsiot sdk, out of the box re-establish broken connections and reinstate previous subscriptions, or do I have to configure this mechanism?
Does QOS.AT_LEAST_ONCE give me this feature, or are there additional configuration parameters I need to supply at the awsiot.greengrasscoreipc.connect() level?

Python paho MQTT, client.on_publish not called even though message is sent to broker

Ok so this is one of those "what-am-I-missing-here?" kind of questions.
My callback registered for the client.on_publish event is never called even though messages are successfully published to the broker (no log messages are printed from the callback and it doesn't stop on a break point in VS Code). The message in particular that I'm interested in is published from the on_msg callback, I have tried to publish it outside of any callbacks but it makes no difference, the message is received by the broker but the on_publish callback never triggers.
When debugging I can see that at the time of publishing there is a function registered for the on_publish callback (the client._on_publish property is set)
Python version 3.7.4
OS Windows 10
Code that initializes the client:
import paho.mqtt.client as mqtt
#configure mqtt client
client = mqtt.Client(config['MQTT']['client_id'])
client.on_connect = on_connect
client.on_message = on_msg
client.on_disconnect = on_disconnect
client.on_publish = on_publish
try:
client.connect(config["MQTT"]["host"], int(config["MQTT"]["port"]))
except Exception as e:
log.error("Failed to connect to MQTT broker", exc_info=True)
client.loop_start()
My callbacks for on_msg and on_publish:
def on_msg(client, userdata, msg):
global status_msg_id
log.debug("on_msg")
payload = json.loads(msg.payload)
if payload['id'] == config["MISC"]["session_id"]:
if payload['result'] == 0:
log.debug("payload content=ok")
status_msg_id = client.publish(
topic = config["MQTT"]["pub_status"],
payload = json.dumps({'status':'ok'}),
qos = 2)
else:
log.debug("payload content=error")
client.publish(
topic = config["MQTT"]["pub_status"],
payload = json.dumps({'status':'error'}),
qos = 2)
log.debug("msg id (mid) of status msg: {}".format(status_msg_id))
def on_publish(client, userdata, mid, granted_qos):
log.debug("on_publish, mid {}".format(mid))
# we never get here :(
So, why isn't the on_publish function called, what am I missing?
The signature for the on_publish callback is wrong.
From the docs:
on_publish()
on_publish(client, userdata, mid)
It should be:
def on_publish(client, userdata, mid):
log.debug("on_publish, mid {}".format(mid))

MQTT Client Publisher Acknowledgement M2MTT Liberary Code

I am using c# M2MQTT Client code to publish and subscribe the data. I have set the QOS Level 1 or 2. Do not know that how the publisher will get the notification when delivery completes. I have searched a lot on inter net but no code available. Please let me know if any one how to handle the acknowledgement at publisher end in c#.
MqttClient client = new MqttClient(IPAddress.Parse(mqttserverurl));
clientId = Guid.NewGuid().ToString();
client.Connect(clientId, uname, pwd);`enter code here`
client.Publish("testtopic", Encoding.UTF8.GetBytes("Hi"), MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE, false);
You don't.
It's all handled internally by the MQTT client library and M2MQTT doesn't seem to have a on_publish callback.

How to get solace queue statistics from Solclient API? c#

I am looking to retrieve some Solace queue stats e.g. the current messages spooled count out of the maximum limit for us to set a threshold to stop publishing more messages to the queue.
Also, to subscribe to vpn events to track message discard rates.
By the time we receive errors e.g. MaxMsgUsageExceeded/SpoolOverQuota, it will be too late.
I can't seem to find any of these on SolaceSystems.Solclient.Messaging API
https://docs.solace.com/API-Developer-Online-Ref-Documentation/net/html/7f10bcf6-19f4-beff-0768-ced843e35168.htm
Would be great if someone could help
(using C# for this)
To poll for Solace queue stats from your C# application, you could use legacy SEMP over the message bus to make a SEMP request for the details that you want. Semp (Solace Element Management Protocol) is a request/reply protocol that uses an XML schema to identify all managed objects available in a message broker. Applications can use SEMP to manage and monitor a message broker.
To allow for legacy SEMP to be used over the message bus, as opposed to the management interface, it first needs to be enabled on the Solace PubSub+ message broker at the VPN level.
To publish a SEMP request with the Solace .Net Messaging API, perform the following steps:
Create a Session.
Create the message topic. “#SEMP//SHOW”
ITopic topic = ContextFactory.Instance.CreateTopic( “#SEMP/<router name>/SHOW”);
Create a request message and set its Destination to the topic in Step 2:
IMessage requestMsg = ContextFactory.Instance.CreateMessage();
requestMsg.Destination = topic;
Set the SEMP request string as the binary attachment.
string SOLTR_VERSION = "8_4_0" //change to the message-broker's version
string SEMP_SHOW_QUEUE = "<rpc semp-version=\"soltr/" + SOLTR_VERSION +
"<show><queue><name>queueName</name><detail></detail></queue></show></rpc>";
requestMsg.BinaryAttachment = Encoding.UTF8.GetBytes(SEMP_SHOW_QUEUE);
Call the SendRequest(…) method on Session.
IMessage replyMsg;
ReturnCode rc = session.SendRequest(requestMsg, out replyMsg, timeout);
The SEMP response is returned in replyMsg.
Obtain the binary attachment data from the reply message:
replyMsg.BinaryAttachment
The binary attachment contains the SEMP reply for the command topic in the publish request.
The Solace PubSub+ message broker does raise an event when an egress message is discarded. However, it is only sent out approximately once every 60 seconds for the specified client so it is not possible to get these exact rates.
It is possible for your .NET application to subscribe to VPN-level events over the message-bus. To do this, you must first enable the Solace PubSub+ message broker to publish the events. You can then subscribe to the special topic and receive the events as messages.
The topic to subscribe to is:
#LOG/<level>/VPN/<routerName>/<eventName>/<vpnName>
The different levels can use the * wildcard. For example, if you wish to subscribe to all VPN events of all levels for the VPN apple on router QA-NY1, the topic string would be:
#LOG/*/VPN/QA-NY1/*/apple
SEMP (starting in v2) is a RESTful API for configuring, monitoring, and administering a Solace PubSub+ broker.
1-The swapper page link is SEMP V2 API
2-The Swagger metadata definitions URL is located # http://{solace-sever-url}/SEMP/v2/config/spec
3- From Visual studio, add REST API Client
4-In the configuration dialog pass swagger metadata URL (defined at step 2), for code purpose I choose SolaceSemp as input value parameter for client namespace input.
4 Once you click ok, VS will create the client along with the models under SolaceSemp namespace
5 Start using the client as per following
using SolaceSemp;
using Microsoft.Rest;
var credentials = new BasicAuthenticationCredentials();
credentials.UserName = "place user name";
credentials.Password = "place password";
using (var client = new SolaceSempClient(credentials))
{
var model = client.GetAboutApi();
}

MQTT subscriber to know who is the publisher

From what I read in MQTT protocol message payload, it doesn't seem to support telling who is the publisher on a published message. But is it possible that a MQTT subscriber to know which publisher the message are from?
A 'msg.publisher' workaround maybe?
#!/usr/bin/env python
import mosquitto
def on_message(mosq, obj, msg):
print "Publisher: %s, Topic: %s, "Msg: %s" % (msg.publisher, msg.topic, msg.payload)
cli = mosquitto.Mosquitto()
cli.on_message = on_message
cli.connect("127.0.0.1", 1883, 60)
cli.subscribe("dns/all", 0)
cli.subscribe("nagios/#", 0)
while cli.loop() == 0:
pass
You're right, the MQTT specification has no field in the PUBLISH packet to specify which publisher a certain message comes from.
I can think of two possible "work-around" implementations:
1) Add the publisher information in the payload of the message. Application level parsing would allow you to retrieve the publisher ID from the message payload.
2) Add the publisher information in the topic. You could concoct a clever topic hierarchy with a level dedicated to the publisher.
For example: data/
Your subscriber could then subscribe to data/+ and parse the last level to retrieve the publisher ID.

Resources