Mqtt Brigde: Displaying MQTT messages in a browser - mqtt

I am new to mqtt, so any answer is appreciated.
My objective is to display real time Mqtt messages on a html page. It seems easy but not for me.
I am not sure on how to create a bridge between my mosquitto broker(it is running on my Rasberry Pi) and HiveMQ(at the moment running on a laptop).
And how do I link my html page to display the mqtt messages ?
I don't what to use java or other complicated plugins.
thanks.

You are using HiveMQ, which has builtin support for Websockets. What you'll then want, is to use the Paho MQTT JavaScript in your Web page; that connects to the Websocket server (HiveMQ) on the TCP port you've configured there, and handles the communication between your Web app and the MQTT broker (HiveMQ).
There's a full-featured Websocket client which does that which you can use as a starting point for your own code.

I see jpmens has covered the Broker to HTML (Websockets) bit.
To bridge you should be able to do it from either the mosquitto side or the HiveMQ side.
For mosquitto you need to add a short section to your mosquitto.conf file
Assuming your HiveMQ broker is running on IP address 10.0.0.5 then something like the following is needed:
connection hiveMQ
address 10.0.0.5
topic # out
The first line just names the connection, the second gives the location of the remote broker. The third is a bit more complex, it controls what gets sent across the bride. This example sends everything (# wildcard topic match) from mosquitto to hivemq (out), but nothing will come back the other way.
Getting the topic line right can be complicated if you want to things to go in both directions. Full details can be found in the mosquitto.conf man page

Related

Why won't TwinCat 3 Analytics Data Logger connect to my MQTT server?

I am trying to use TwinCat 3 with TcAnalytics to create a Data Logger which logs values to MQTT. I have created this Data Logger, and set the settings to 127.0.0.1 and also created the same connection in my Target Browser under TcAnalytics.
The problem is that the PlcStream1 created for the Data Logger is showing 'Disconnected' in the Online tab at the same time as the Target Browser connection shows everything is fine and connected properly.
I have seen a video of someone setting this up and the data stream from the Data Logger appears nested under the connection in the Target Browser once it's setup. For me this does not show up either.
I have tested my local MQTT server with a third party tool that can publish and subscribe to it just fine.
Why won't the Data Logger connect to MQTT and publish data to it?
My setting in the Data Logger 1 Parameter (Init) tab look accurate to me. They are shown below.
Does anyone know how to get TcAnalytics to log data to MQTT? It seems like a really straight forward setup but it's just not connecting.
The problem here was that my MQTT server was not serving on all interfaces. Contrary to some data I found, Mosquitto doesn't serve to all interfaces by default. I needed to add this to my mosquitto.conf file.
listener 1883
allow_anonymous true

How to get broker details on connectionLost()

I am using eclipse paho java client to connect to mqtt broker.
Have written a subscriber client implementing MqttCallbackExtended.
I am getting connectionLost() callback.
But how do I get to know that which broker lost the connection.
I have specified multiple uri's via setServerURIs() api of MqttConnectOptions.
If you have specified multiple brokers, they all should be part of the same cluster offering the same topic space.
This means you shouldn't need to care from the client side which broker you were connected to as the client will just move to the next in the list when it tries to reconnect.
But if you really need to know, then you can always log the URI when the connection is created using the information from the connectionComplete() callback on the MqttCallbackExtended class

In the MQTT protocol, how does a client identify a server?

I have read that the overhead is low. What does it really mean when compared to HTTP? Does it not use the ip address of the server to which a client tries to connect to? If not, how does a client connect to a server?
Low overhead means that for a given size of messages there is very little extra information sent. It has nothing to do with broker discovery.
E.g. for a HTTP message there us a relatively large amount of HTTP Headers sent before any of the message is transmitted.
The client will connect to the broker via it's IP address. This can either be known in advance, looked up from a host name via DNS or looked up via a TXT record in the DNS for a given domain. You can see examples of broker discovery on the mqtt.org site here

Is it possible to keep a connection with two different MQTT Brokers when using the Node-RED?

I have a server on the cloud that receives MQTT messages and I'm running the Node-RED on a Raspberry pi on my localhost...
With the Raspberry, I'm receiving data from different devices using the MQTT protocol. And until here, it works great! The problem is when I try to receive messages from the MQTT broker that's running locally (on the Raspberry) and then send these messages to other MQTT broker that's running on another server. Something like this image:
When I try to use two different brokers with the Node-RED, it automatically updates all other nodes to the last broker that I had configured. So, is it possible to open a connection with two different MQTT Brokers when I'm using the Node-RED?
In the MQTT node edit dialog, the 'Server' field is a select box. It lists all of the broker configurations you have in your flow.
To add a connection to a different broker, select the 'Add new mqtt-broker...' option then click the button next to the select box.
That adds a new connection configuration rather than edit the one already being used by your existing nodes.

How can I get nodemcu to popup a browser window upon connection to an ESP8266 AP?

I know in airports, for example, I've connected to their AP, and it pops up a browser window to log in on my device. Is it possible to do so with NodeMCU in lua, or even with c firmware?
This can accomplished by setting the DNS server for a connecting client [via DHCP] to a sort of DNS proxy. It doesn't need to be a fully featured DNS server, it only needs to be able to either return a static DNS answer for any host name query or forward the request to a real DNS server, to resolve host names as usual.
The static answer effectively hijacks web requests at the DNS level, by forging the DNS answer, causing all host names to resolve to the IP address of a local web server. That local web server ignores any Uri details and serves a login prompt for every request. It must also maintain a list of client MAC addresses that have authenticated.
NodeMCU does have a built-in DHCP server, as part of it's built-in WiFi AP, but running both a web and a DNS proxy in ESP8266's limited memory would be a hell of a trick. I think that two of them working cooperatively, interfaced using the SPI bus might be workable... maybe even three of them, one dedicated to maintaining the list of authenticated MACs, expiring them, etc.
Note that the only part of this I have done on an ESP 8266 is some very simple web server functionality, so it's mostly theory. If you try it I'd be very interested in hearing about it. :-)
You might want to try out CaptiveIntraweb project (https://github.com/reischle/CaptiveIntraweb) which is based on NodeMCU.
There is also thread (http://www.esp8266.com/viewtopic.php?f=32&t=3618) on ESP8266 community forum that discusses the solution details.

Resources