Google Cloud Platform (read more), offered by Google, is a suite of cloud computing services that run on the same infrastructure that Google uses internally for its end-user products, such as Google Search and YouTube. Alongside a set of management tools, it provides a series of modular cloud services including computing, data storage, data analytics and machine learning.
One of the services that Google also provides on its Google Cloud Platform and that is used in this tutorial is called Cloud IoT Core (read more). Cloud IoT Core is a fully managed service that allows you to easily and securely connect, manage, and ingest data from millions of globally dispersed devices. Cloud IoT Core, in combination with other services on Google Cloud IoT platform, provides a complete solution for collecting, processing, analyzing, and visualizing IoT data in real time to support improved operational efficiency. Cloud IoT Core service is fully supported on ESPRESSObin board.
Cloud IoT Core
In order for ESPRESSObin to connect and communicate with the Cloud IoT Core service, the following steps need to be completed:
- Setting up a working Ubuntu filesystem flashed on ESPRESSObin (as shown in Boot from removable storage - Ubuntu).
- Obtaining permission access to Cloud IoT Core service. At the time of writing this tutorial, Cloud IoT Core service is in a public beta state and an application form needs to be filed out to apply for access to the public beta. Getting and setting up the Cloud IoT Core is out of the scope of this tutorial. To obtain the access, click here from where you will be able to access the application form. When access to Cloud IoT Core is granted, please follow the official Cloud IoT Core documentation available here to set up your project and devices. This is needed in order to be able to connect to the Cloud IoT Core using ESPRESSObin.
Now that we have ESPRESSObin with Ubuntu file system and access to Cloud IoT Core with a configured project, the final component is setting up the application that will be run on the ESPRESSObin. This application is called google-iot-mqtt-publisher, a MQTT client which is used to publish messages to a specific topic on Google Cloud IoT.
In order to use the google-iot-mqtt-publisher, we need to build it out from source code. The google-iot-mqtt-publisher is written in Go programming language, to build it, you need to have a working Go installation. Since the board is running Ubuntu, the recommended way is to install Go onto the board itself using a package manager. Alternative is to follow the official Go instalation instruction. It is also possible to build google-iot-mqtt-publisher on a different Linux machine. Doing this, google-iot-mqtt-publisher needs to be cross-compiled for MACCHIATObin. This is out of scope of this tutorial. Instructions for installing Go onto the board itself using a package manager can be found below.
Run the commands listed below to install Go onto the board itself using a package manager:
First, update the package list:
# apt-get update
Then, install Git and Go:
# apt-get install git golang-go
Next thing we need to do is set correct Go paths. As root open .profile config file:
# sudo vim ~/.profile
and in there set Go's root value ($HOME/go will be our Go workspace directory):
export GOPATH=$HOME/go export PATH=$PATH:$GOPATH/bin
and refresh your profile with:
# source ~/.profile
Make the directory for your Go workspace as you have set it in your .profile:
# mkdir $HOME/go
This is where Go will build its files.
After Go is installed on your local machine, we can move to building the google-iot-mqtt-publisher.
Create a directory which will hold the contents of the google-iot-mqtt-publisher:
mkdir -p $GOPATH/src/github.com/MarvellEmbeddedProcessors/google-iot-mqtt-publisher-marvell
Get the google-iot-mqtt-publisher source code via Git or by downloading the ZIP archive containing the source code from here and put the source into the $GOPATH/src/github.com/MarvellEmbeddedProcessors/google-iot-mqtt-publisher-marvell folder.
Finally, position yourself in the $GOPATH/src/github.com/MarvellEmbeddedProcessors/google-iot-mqtt-publisher-marvell directory and fetch third party libraries:
$ go get
after which we can cross-compile for arm64:
$ go build -o google-iot-mqtt-publisher
This will generate an executable with the file name of google-iot-mqtt-publisher in the same directory. In order to run the executable on the ESPRESSObin, the certs/ directory must also be installed. The certs directory contains three files:
- roots.pem - A root certificate from Google that must be installed alongside the google-iot-mqtt-publisher executable.
- rsa_cert.pem - Self-signed public key that is used by Cloud IoT Core.
- rsa_private.pem - Self-signed private key used by google-iot-mqtt-publisher to authenticate your device to Cloud IoT Core.
The keys mentioned here are merely an example. Make sure to use your own self-signed keys for your device. Device authentication must be configured in Cloud IoT Core prior to using google-iot-mqtt-publisher.
That's it. You have successfully built the google-iot-mqtt-publisher which is ready for usage.
Obtain google-iot-mqtt-publisher help page with:
$ ./google-iot-mqtt-publisher --help google-iot-mqtt-publisher is a MQTT client used to publish messages to a specific topic on Google Cloud IoT. Usage: google-iot-mqtt-publisher [flags] Flags: -d, --device-id string Device ID -m, --message string A string which will be sent to a topic. -p, --project-id string Project ID -r, --registry-id string Registry ID -t, --topic string A topic to which the message will be sent.
To publish a message, run the google-iot-mqtt-publisher tool with the correct values for flags, for example:
$ ./google-iot-mqtt-publisher --project-id "example-project" --registry-id "example-registry" --device-id "example-device" --topic "events" --message "example-message-containing-telemetry-data"
Make sure to replace the above values with your own.
To fetch the published message, you can use the gcloud available from Google Cloud SDK. Follow the Google IoT Cloud Core How-to Guide to install gcloud locally on your Linux machine. After you have installed gcloud, run the following command to fetch the published message (again, replace example values with your own):
$ gcloud beta pubsub subscriptions pull --auto-ack projects/example-project/subscriptions/example-subscription
If everything went well, you have successfully sent a MQTT message from your ESPRESSObin to Cloud IoT Core service and were able to fetch this message locally on your Linux machine using the gcloud. Above instructions can also be seen in an asciinema video here.