Omega Onion connecting to Cloud Cloud IoT



  • Folks --

    Update 2018-05-30: I've revised this working solution to make it easier to follow.

    Here's a post summarizing some Golang code on an Omega2 connecting to Google Cloud IoT Core.

    I'm a Googler and an Onion (!?) and have 3x Omega2s.

    On Sunday I noticed this post on the Onion Developer docs showing integration with cloud platforms and was encouraged to add Google Cloud Platform to this list. In the post, I describe working Golang code that connects Omegas to Google Cloud IoT Core.

    Working Configuration

    I had some help from a colleague who helped me understand that my attempt to use an MQTT broker was my problem and that Google Cloud IoT Core does not work with sub-brokers. Instead, I was advised to focus on the mosquitto_pub and mosquitto_sub approach. This works and is the solution:

    mosquitto_pub \
    --host mqtt.googleapis.com \
    --port 8883 \
    --id ${LONG_REGISTRY}/devices/${DEVICE} \
    --username unused \
    --pw ${PASSWORD} \
    --cafile /roots.pem \
    --tls-version tlsv1.2 \
    --protocol-version mqttv311 \
    --debug \
    --qos 1\
    --topic /devices/${DEVICE}/events \
    --message "$(date +"%y%m%d%H%M%S") Hello Henry!"
    

    and this:

    mosquitto_sub \
    --host mqtt.googleapis.com \
    --port 8883 \
    --id ${LONG_REGISTRY}/devices/${DEVICE} \
    --username unused \
    --pw ${PASSWORD} \
    --cafile /roots.pem \
    --tls-version tlsv1.2 \
    --protocol-version mqttv311 \
    --debug \
    --qos 1 \
    --topic /devices/${DEVICE}/config
    
    

    Earlier Work

    Record of trial-and-error. For continuity only

    I'm struggling to get connectivity with Mosquitto as outlined in the Onion document but I know how to get connectivity working (because the Golang code works). When I try mosquitto_pub commands below, I don't promptly receive the messages on GCP (as I do with the Golang code).

    I'd value your help. I've redacted values and replaced with $VARIABLES but the values are correctly reflected in the config. Cloud IoT MQTT uses a JWT for the password and there's no mutual auth. I pulled the JWT code from the Golang and use this to generate the value for $JWT used in the config below so I'm confident that's correct too.

    I suspect one error is in the mapping of topics:

    # For debugging
    log_type all
    log_dest file /var/log/mosquitto/mosquitto.log
    
    connection_messages true
    
    tls_version tlsv1.2
    
    # Bridge to GCP Cloud IoT Core
    connection bridge-to-gcp
    address mqtt.googleapis.com:8883
    cleansession true
    bridge_attempt_unsubscribe false
    bridge_cafile /roots.pem
    remote_username unused
    remote_password $JWT
    remote_clientid projects/$PROJECT/locations/$REGION/registries/$REGISTRY/devices/$DEVICE
    
    topic # out 1
    topic # in 1
    

    For the clientid shown above, at least 2 MQTT topics are available, telemetry can go out on /devices/$DEVICE/event and config arrives on /devices/$DEVICE/config

    I tried:

    mosquitto_pub \
    --topic /events \
    --message '{"test": "Hello Henry!"}' \
    --qos 1
    
    mosquitto_pub \
    --topic /devices/$DEVICE/events \
    --message '{"test": "Hello Henry!"}' \
    --qos 1
    
    mosquitto_pub \
    --topic projects/$PROJECT/locations/$REGION/registries/$REGISTRY/devices/$DEVICE/events \
    --message '{"test": "Hello Henry!"}' \
    --qos 1
    

    Here's part of the logs when trying to mosquitto_pub:

    New connection from 127.0.0.1 on port 1883.
    New client connected from 127.0.0.1 as mosqpub/3501-20237b7dc0 (c1, k60).
    Sending CONNACK to mosqpub/3501-20237b7dc0 (0, 0)
    Received PUBLISH from mosqpub/3501-20237b7dc0 (d0, q1, r0, m1, '/events', ... (24 bytes))
    Sending PUBACK to mosqpub/3501-20237b7dc0 (Mid: 1)
    Received DISCONNECT from mosqpub/3501-20237b7dc0
    Client mosqpub/3501-20237b7dc0 disconnected.
    New connection from 127.0.0.1 on port 1883.
    New client connected from 127.0.0.1 as mosqpub/3502-20237b7dc0 (c1, k60).
    Sending CONNACK to mosqpub/3502-20237b7dc0 (0, 0)
    Received PUBLISH from mosqpub/3502-20237b7dc0 (d0, q1, r0, m1, '/devices/$DEVICE/events', ... (24 bytes))
    Sending PUBACK to mosqpub/3502-20237b7dc0 (Mid: 1)
    Received DISCONNECT from mosqpub/3502-20237b7dc0
    Client mosqpub/3502-20237b7dc0 disconnected.
    New connection from 127.0.0.1 on port 1883.
    New client connected from 127.0.0.1 as mosqpub/3503-20237b7dc0 (c1, k60).
    Sending CONNACK to mosqpub/3503-20237b7dc0 (0, 0)
    Received PUBLISH from mosqpub/3503-20237b7dc0 (d0, q1, r0, m1, 'projects/$PROJECT/locations/$REGION/registries/$REGISTRY/devices/$DEVICE/events', ... (24 bytes))
    Sending PUBACK to mosqpub/3503-20237b7dc0 (Mid: 1)
    Received DISCONNECT from mosqpub/3503-20237b7dc0
    Client mosqpub/3503-20237b7dc0 disconnected.
    Bridge local.projects/$PROJECT/locations/$REGION/registries/$REGISTRY/devices/$DEVICE doing local SUBSCRIBE on topic #
    

    I found some guidance on the always-excellent Digital Ocean site and am able to send messages to Cloud IoT using mosquitto_pub:

    mosquitto_pub \
    --host mqtt.googleapis.com \
    --port 8883 \
    --id ${LONG_REGISTRY}/devices/${DEVICE} \
    --username unused \
    --pw ${PASSWORD} \
    --cafile /roots.pem \
    --tls-version tlsv1.2 \
    --protocol-version mqttv311 \
    --debug \
    --topic /devices/${DEVICE}/events \
    --message "$(date +"%y%m%d%H%M%S") Hello Henry!"
    

    So the problem (I think) is in my configuration file.



  • That's awesome !
    Welcome GCP IoT to the Onion family ! šŸ˜œ


  • administrators

    @Daz-Wilkin That's awesome!!! We'll make sure to update our Cloud Platforms page to include GCP!
    To avoid confusion, can you please move the working commands to the top of the post?

    Oh and how's Onioneer for the Onion version of Googler? šŸ˜„



  • @Lazar-Demin --

    Thanks for the shout-out!

    I didn't receive an email when you posted to this topic šŸ˜ž

    Yes, I'll revise the post to move the working content to the top.

    So, may we have Google Cloud Platform added to your Connecting to Cloud Platforms?

    Onioneer, yes! šŸ™‚


Log in to reply
 

Looks like your connection to Community was lost, please wait while we try to reconnect.