Building Google Cloud-connected sensors

Gus Class
4 min readApr 12, 2018

--

In this article, I’ll briefly cover the steps it takes to add a sensor to our provided examples and log its data using a Genuino MKR1000 (MKR).

Note that the provided code is not supported, expect things to break. I can only recommend it currently for learning and hobbyist applications. For production quality, there are plenty of options. Consider this approach a jumping off point for now.

Disclaimer aside, we recently added support for some genuine Arduino (Genuino) boards to the Arduino library for Google Cloud IoT Core. In general I’ve found the Genuino boards to have really good ADC implementations so I thought it would be a fun time to play with the dust sensors I ordered a few months ago. It was as easy as I could have hoped for!

Dust sensors tend to have a digital pin, an analog pin, and power. Connect the digital pin from the dust sensor to pin 12 on the MKR, connect the analog pin from the dust sensor to pin A3, and connect the power.

Image showing Dust sensor connected to Arduino

I say it a lot but — don’t do this at home — I was either impatient or lazy and just connected the pins directly using jumper cables, this does not protect the MKR hardware or the sensor properly and will not correctly pulse the LED. Instead, add a 150ohm resistor and 220 uf capacitor to the circuit as described in the Sharp datasheet.

At this point, when working with a sensor for the first time, I would typically find a Sketch for the sensor online and check the sensor against a calibrated sensor. At home, I compared the output of the sensor with my Awair:

As you can see, the dust I’m reading (low dust levels) matches what I am seeing on my accurate air quality hub, but because I’m missing the pulse circuit, the value may not be correct. Because I don’t have the capacitor / resistor that I need, I’ve switched to use an example with the MQ7 sensor — connect the analog pin to A3, digital pin to 8.

Starting from the Basic-MKR1000.ino project that ships in the Google Cloud IoT Arduino project, add the following code, which adds a very minimal implementation of a driver for reading voltages from an MQ sensor:

Next, copy the sendTelemetry function from the Basic ESP32 sample and change the Arduino loop function to call that function instead of getConfig.

You will need to fill in the values in the sample based on your Google Cloud configuration as discussed in my first post on the Arduino library.

And that is it for code! When the Arduino app runs, you will see your measured sensor data getting published to Google Cloud:

This image shows the program output with the dust values varying in negative ranges

What’s exciting is that this example could be adapted to any sensor that is supported by Arduino! For the MQ-** class of air quality sensors, you would generally just apply the same approach and calculate the registered value based on an existing driver or the data sheet for your sensor.

After the sensor data is successfully publishing, it’s time to read sensor data. Open the Google Cloud Shell and run the command:

You’ll notice in the table that is drawn, the measured dust values appear:

The left column shows the measured value, the right shows an ID for the message

As you can see, there’s no dust! At this point, you could write a Google Cloud Function to migrate the data from Google Cloud Pub/Sub or could use Dataflow to migrate the data from Pub/Sub to BigQuery.

Thanks for reading! Also, I want to make a shout out to Alvaro Viebrantz for helping with a refactor of the library that is a huge improvement over our starting examples!

Troubleshooting

If you’re having trouble getting the MKR1000 board to flash, it’s helpful to know that you can double-tap the BOOT button on the board to bring it explicitly into boot mode.

If you’re having trouble configuring the sample for the first time, you may want to took a peek at my last blog post on the topic.

--

--