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 MACCHIATObin board.
Cloud IoT Core
In order for MACCHIATObin to connect and communicate with the Cloud IoT Core service, the following steps need to be completed:
- Setting up a working Ubuntu filesystem with the google-iot-mqtt-publisher MQTT client binary. How to do this will be shown a little later.
- 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 MACCHIATObin.
Now that we have access to Cloud IoT Core with a configured project, the final component is setting up the application that will be run on the MACCHIATObin. 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. The application can be obtained by either:
- Downloading a prebuilt Ubuntu 16.04.3 LTS image containing the binary
- Building it from source on a MACCHIATObin board running Ubuntu
On our Software page under Ubuntu SD card + Google IoT package you can obtain a prebuilt Ubuntu 16.04.3 LTS image (ubuntu-xenial-mcbin-google-mqtt_u-boot_sd.img.gz) with the google-iot-mqtt-publisher binary stored in /usr/bin/. The image is configured to boot directly from the SD card.
The image requires a micro SD card or USB flash drive with 4 GB capacity or more.
After downloading it, flash it on a Linux machine with the following command (replace sdx with your device label):
$ gunzip -c ubuntu-xenial-mcbin-google-mqtt_u-boot_sd.img.gz | sudo dd of=/dev/sdx
Alternatively, extract the image first using gzip and flash with dd or some other tool which you prefer.
Bigger medium sizeIn case your removable storage medium is larger than 4 GB, resize the partition either with GParted or manually to use all available space. To do it manually:
1. If mounted, first unmount the partition (replace sdxX with your partition label):
$ sudo umount /dev/sdxX
2. Check for free space:
$ sudo parted /dev/sdx print free Model: Generic STORAGE DEVICE (scsi) Disk /dev/sdx: 15.9GB Sector size (logical/physical): 512B/512B Partition Table: msdos Disk Flags: Number Start End Size Type File system Flags 32.3kB 10.5MB 10.5MB Free Space 1 10.5MB 45.1MB 34.6MB primary fat16 2 45.1MB 3880MB 3835MB primary ext3 3880MB 15.9GB 12.1GB Free Space
3. Increase the desired partition to the desired size, e.g. to increase the second partition to use all the available space (15.9GB in our case):
$ sudo parted /dev/sdx resizepart 2 15.9GB
4. Check the filesystem with:
$ sudo e2fsck -f -y -v -C 0 /dev/sdxX
5. Lastly, resize the filesystem
$ sudo resize2fs -p /dev/sdxX
Unplug the micro SD card from your Linux machine and plug it into the MACCHIATObin board. Relocate the jumpers (or DIP switches on revisions 1.2 and older) on the MACCHIATObin board to boot from the SD card as instructed in MACCHIATObin Interface list, section Boot Selection. Lastly, plug in the board and you should see Ubuntu booting directly from the SD card.
Building From Source
The google-iot-mqtt-publisher can be also built from source on the MACCHIATObin board running Ubuntu. How to build and flash a clean Ubuntu filesystem is shown in Creating Ubuntu filesystem.
The google-iot-mqtt-publisher is written in Go programming language, and building it requires 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 and configured 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 compile with:
# go build -o google-iot-mqtt-publisher
This will generate an executable with the file name of google-iot-mqtt-publisher-marvell in the same directory. In order to run the executable on the MACCHIATObin, 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.
NoteYou must replace the private key with your own key used during device authentication configuration in Cloud IoT Core. Otherwise, the google-iot-mqtt-publisher will not be able to authenticate your device during initial connection to the Cloud IoT Core service.
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 MACCHIATObin 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.