Is it possible to bridge MQTT broker mosquitto to Cloud PubSub? - mqtt

I have a local mosquitto broker that I would like to connect to Google Cloud pubsub queue.
I looked into Google Cloud IOT core but with no luck, I cannot seem to connect to it
Here is my mosquitto configuration
#
# A full description of the configuration file is at
# /usr/share/doc/mosquitto/examples/mosquitto.conf.example
pid_file /var/run/mosquitto.pid
persistence true
persistence_location /var/lib/mosquitto/
# Goolge IoT Core Configuration
connection bridge-to-gcp
address mqtt.googleapis.com:8883
bridge_attempt_unsubscribe false
bridge_certfile /Downloads/rsa_cert.pem
bridge_keyfile /Downloads/rsa_private.pem
bridge_cafile /Downloads/roots.pem
bridge_protocol_version mqttv311
bridge_insecure false
tls_version tlsv1.2
try_private true
start_type automatic
cleansession true
notifications false
local_clientid local-to-remote-gcp-bridge
remote_clientid projects/toolsense-dev/locations/europe-west1/registries/test-registry/devices/test-device
topic # both
Is there a way to forward all the messages received from edge devices to Google Cloud Pubsub?

After deeper research, it is not possible without the existence of a gateway device either virtually or physically. It could even be client code running on same machine as the one running the broker.
This usecase is however possible in AWS Iot
https://wiki.seeedstudio.com/Arduino-AWS-IOT-Bridge/

Related

Mosquitto broker authentification even for same machine clients

I am working on a MQTT project using local mosquitto broker.
Currently for testing, I want my broker to require authentication even from same machine clients. But it looks like the broker is allowing them even if they don't have the username or the password.
my mosquitto.conf:
allow_anonymous true
password_file C:\Documents\...\password_file.txt
For Mosquitto v2.0.x and newer just remove the allow_anonymous true or set it to false (for previous versions)
When set to true mosquitto will allow clients to connect without a username or password.
Writing this for others who may be in the same situation as I was.
mosquitto.exe -v -c mosquitto.conf
solved the problem.

Looking for a working example of a non-secure bridge between a local Mosquitto instance and the Watson IoT platform

I'm trying to establish a non-secure bridge from Mosquitto running on Home Assistant (home-assistant.io) to the MQTT broker on the Watson IoT platform. Why non-secure? To take out additional complexity as I can't connect. Once this step is successful I'll move on to secure the connection.
Firstly I have setup the Watson IoT platform and created a gateway device type ('ha-iotp-gateway') and device ('ha'). No other changes to the IoT platform.
I then created a bridge config file to connect Mosquitto with the Watson IoT platform broker by adopting the conf shared in this post and now have the following:
connection bridge-to-watsoniot
address xxxxxx.messaging.internetofthings.ibmcloud.com:1883
cleansession true
try_private false
bridge_attempt_unsubscribe false
notifications false
notification_topic iot-2/type/ha-iotp-gateway/id/ha/evt/status/fmt/raw
remote_username use-token-auth
remote_password <token>
remote_clientid g:xxxxxx:ha-iotp-gateway:ha
topic iot-2/type/+/id/+/cmd/+/fmt/+ in iot-2/type/+/id/+/cmd/+/fmt/+
topic iot-2/type/+/id/+/evt/+/fmt/+ out iot-2/type/+/id/+/evt/+/fmt/+
connection_messages true
The Mosquitto log file shows a connection attempt
1606909705: Connecting bridge bridge-to-watsoniot (xxxxxx.messaging.internetofthings.ibmcloud.com:1883)
1606909706: Socket error on client local.g:xxxxxx:ha-iotp-gateway:ha, disconnecting.
1606909707: New connection from 172.30.32.1 on port 1883.
[INFO] found homeassistant on local database
1606909707: New client connected from 172.30.32.1 as auto-BD9CD1CE-BE73-6CBA-196D-C02E83268AEB (p2, c1, k60, u'homeassistant').
1606909712: Connecting bridge bridge-to-watsoniot (xxxxxx.messaging.internetofthings.ibmcloud.com:1883)
1606909723: Connection Refused: not authorised
1606909723: Socket error on client local.g:xxxxxx:ha-iotp-gateway:ha, disconnecting.
The gateway device on the Watson IoT platform (created earlier) shows connected and the connection log shows
Closed connection. The operation is not authorized.
Has anyone been successful in establishing a non-secure connection to Watson IoT? I'm not sure whether the IoT platform is correctly configured or my bridge config has issues - or both.

How paho client can know status of bridge connections?

I have one remote broker (cloudmqtt) and one local broker on my board. Both are connected as bridge. I have one paho client connected to local broker. I want to know the status of bridge in order to publish message. I know I can publish message to local broker without knowing status and broker will take care. But I want to design my application like I will Publish message only if bridge is Up.
I am using paho client library in C and mosquitto broker v1.6.
Below is my conifg file :
pid_file /var/run/mosquitto.pid
persistence true
persistence_location /var/lib/mosquitto/
log_dest file /var/log/mosquitto/mosquitto.log
log_timestamp true
log_timestamp_format %Y-%m-%dT%H:%M:%S
log_type all
user root
connection cloudmqtt
address xxxx.cloudmqtt.com:13287
remote_username xxxxxxx
start_type automatic
try_private true
remote_password xxxxxx
notifications true
notification_topic /broker/connection/state
restart_timeout 20
max_queued_messages 0
topic # both 2
The short answer is you can't at a pure MQTT protocol level or specific to the Paho client (and you shouldn't care for the reasons you mentioned).
Now having said all that you can actually get messages about the bridge status from the $SYS/broker/connection/<remote-clientid>/# topic tree on mosquitto. To enable this you need to set the notification true flag in the bridge config. The doc for configuring bridges is here

Connect local Mosquitto MQTT broker to Google Cloud IoT

In my current setup i read data using ebusd from my heating system which works perfectly fine. In the next step i'd like to send this data to a Google IoT Core Device. Unfortunately ebusd does not support setting the clientid externally.
To circumvent this i installed a local Mosquitto Broker which accepts unsecured messages in my Raspberry Pi and shall bridge them to the Google IoT Core Broker. Here is how i configured my local Mosquitto Broker:
# Place your local configuration in /etc/mosquitto/conf.d/
#
# A full description of the configuration file is at
# /usr/share/doc/mosquitto/examples/mosquitto.conf.example
pid_file /var/run/mosquitto.pid
persistence true
persistence_location /var/lib/mosquitto/
log_type all
log_dest file /var/log/mosquitto/mosquitto.log
# Goolge IoT Core Configuration
connection bridge-to-gcp
address mqtt.googleapis.com:8883
bridge_attempt_unsubscribe false
bridge_certfile /home/pi/certificates/rsa_cert.pem
bridge_keyfile /home/pi/certificates/rsa_private.pem
bridge_cafile /home/pi/certificates/roots.pem
bridge_protocol_version mqttv311
bridge_insecure false
tls_version tlsv1.2
try_private true
start_type automatic
cleansession true
notifications false
local_clientid local-to-remote-gcp-bridge
remote_username unused
remote_password <JWT Token generated using jwt.io>
remote_clientid projects/project-251313/locations/europe-west1/registries/prot/devices/ebusd12345
topic # both
include_dir /etc/mosquitto/conf.d
Although this setup works with AWS IoT with Google Cloud IoT i get
1542622526: Connecting bridge bridge-to-gcp (mqtt.googleapis.com:8883)
1542622526: Bridge projects/project-251313/locations/europe-west1/registries/prot/devices/ebusd12345 sending CONNECT
1542622526: Socket error on client local.projects/project-251313/locations/europe-west1/registries/prot/devices/ebusd12345, disconnecting.
After googling some time i found this blog (see http://community.onion.io/topic/2858/omega-onion-connecting-to-cloud-cloud-iot/2) which states that Google Cloud IoT Core does not support connections by "sub-brokers" but i didn't find a second statement claiming the same.
Can anybody either confirm that i am trying to do something impossible or please help with with configuring the broker properly?
I reached out to the author of that blog and other people who work on Cloud IoT Core and unfortunately it seems like MQTT bridging indeed is currently not supported. In the meantime, it seems like you're in contact with the ebusd owner on Github to add a configurable clientid feature, which would be a good temporary fix.
With that said, we have a feature that will be going into beta soon that will help you address this kind of issue specifically, so stay tuned!

Why when cleansession is enabled and set to true in Mosquitto.Conf Mosquitto Broker refuses to start?

I recently upgraded and updated my Mosquitto Broker to 1.4.15 on Raspberry-pi3. It works as expected. However, every time I enable and set cleansession to true in the Mosquitto.Conf file, Mosquitto broker refuses to start. As soon as I comment out or disable cleansession, Mosquitto broker starts up immediately by its service. The reason I need to set this to true is because I want the Mosquitto Broker to clean up any disconnected clients' session. So that the same client can reconnect again to the broker. Am I doing this right? or Is there another Mosquitto Broker's feature that I can use in place of cleansession?
The cleansession flag in the mosquitto.conf is to control what options mosquitto uses when it connects to remote brokers as a client when setting up a bridge.
If you want your clients to connect with a clean session then you need to set that option in your client library when setting up the connection not on the broker.

Resources