{{announcement.body}}
{{announcement.title}}

How to Integrate Arduino and Google Cloud Platform

DZone 's Guide to

How to Integrate Arduino and Google Cloud Platform

Learn more about integrating Arduino and GCP in this beginner tutorial!

· IoT Zone ·
Free Resource

Learn more about integrating Arduino and GCP in this beginner tutorial!

This Arduino practical tutorial demonstrates how to integrate Arduino and Google Cloud Platform. In more detail, this Arduino tutorial describes how to implement an Arduino sketch that sends data to Google Sheets.

This is a common scenario where you have to send data acquired by Arduino to a remote IoT cloud platform. Let us suppose we have to monitor the room temperature and store these values somewhere in the cloud so that they can be elaborated later. This is a typical scenario where it is necessary to know how to send data from Arduino to the cloud.

You may also like:  Setting Up Your Own IoT Cloud Server

When it is necessary to send data from Arduino to the cloud, there must be an IoT cloud platform that accepts these values and stores them. Usually, an IoT cloud platform exposes a set of APIs to simplify the data exchange process. The cloud IoT platform protects these APIs using an authentication mechanism. Therefore, it is necessary to implement all the code required on the Arduino side to accomplish the task of sending data to the cloud. There is another way of achieving the same result.

Using this guide about IoT with Arduino, you can experiment, by yourself, with the power of Arduino and how to connect it to Google Cloud to build your first IoT project.

Quicky, in a few minutes, and without knowledge of programming, it is possible to dive into IoT and build an Arduino cloud data logger that uses Google Sheets to store data.

You will learn:

  • How to connect Arduino to sensors
  • How to send data from Arduino to Google Cloud (Google sheets)
  • How to use Temboo with Arduino

Introduction

IoT is one of the most important technological trends nowadays. In this post, we want to explore how to build an IoT project that uses Arduino and Google. The interesting aspect of the Internet of Things is that we can experiment with IoT using simple development devices and existing IoT cloud platforms that provide services. It is not necessary to spend a lot of money to jump into the IoT ecosystem. The purpose of this project is building a cloud data logger that records the temperature and pressure and stores these values into the Google Cloud Platform using Google Sheet.

There are several scenarios where this Arduino project could be useful and it is necessary to connect Arduino to Google Cloud. As stated before, we might want to track the temperature and the humidity of our room and store this information in a worksheet in order to elaborate on the values somehow.

Moreover, we could calculate the mean value of the temperature and the humidity during the days. We might want to plot these values using some charts. Therefore, this Arduino project has several applications, and it is very useful. Nevertheless, it is a good starting point when you approach for the first time Arduino and how to use Arduino in IoT.

To integrate Arduino and Google Cloud, and to help Arduino to send data to Google Sheet, this IoT project uses Temboo. This is an IoT cloud platform that provides several integration services that simplify the process of integrating different systems.

This Arduino programming tutorial has three simple steps to get the goal:

  1. Authorize our device using OAuth 2 and get the token to exchange data with Google
  2. Connect the sensor to the Arduino
  3. Send the data to the Google Cloud (Google sheet) using Temboo

Integrating Arduino and Google Cloud Platform Using OAuth

In this first step, we have to authorize our Arduino board to send data to Google Cloud Platform using the OAuth2 mechanism. To simplify this authorization process, we will use Temboo to provide a set of services to get the OAuth token. We have covered previously how to integrate Arduino MKR1000 with Twitter using Temboo.

During this step, we assume you have already a Google Cloud account so that you can access the Google Developer console.

Before digging into the details of using Arduino and Goole cloud for this IoT project, it is necessary you have created a Google API project. Moreover, it is important to enable Google Sheet API in order to use this API with Arduino.

Once you have your project configured correctly using the Google Cloud console, you should have a Client ID and Secret key. These two keys are very important, and we will use them later during the project. The final result is shown in the picture below:

How to Use Arduino and Temboo

Now, it is time to focus our attention on the Temboo. If you do not have a Temboo account, go to the Temboo homepage and create it for free. Once you have an account, log in and select the OAuth 2 service on the left side of the menu.

Select on the left side of the menu, the InitializeOAuth item under Utilities>Authentication>OAuth2 and fill the form as shown in the picture below:

The Client Id is the one you get from the Google Cloud Platform as shown in the step above. Now, click on the button and wait for the response. If everything goes well, you will get the result you are looking for:

This result is very important because we will use these values later. Now, you have to enable the access to Google Sheet. For this purpose, in your browser copy and paste the first value, the URL shown above. You should see an authentication request sent by Google and then a blank page. It is almost done. The last step is using FinalizeOAuth as shown below:

This will occur where the CallbackID value was retrieved in the step above. That's all. Now if you send the request, you get the token we will use during the project:

Now, we have our token to use in the next API calls. By now, you did not write a line of code. Just configuration as promised!

Connecting the Sensor to Arduino

This is the easiest step, we have simply to connect the sensor to the Arduino. In this example, we will use an Arduino MKR1000 board, but you can use an Arduino Uno with the Wi-Fi shield. It is important to notice that the board must be compatible with Temboo.

For this Arduino project, we are using a BMP280 sensor that measures temperature and pressure. However, it is possible to use other sensors too. You can connect all the sensors you like. This is just an example.

The wiring details are shown below:

The Arduino software code is very simple. This is only a little piece of code to use. Create a new Arduino sketch using your Arduino IDE (or any IDE you like) and add these lines at the beginning to read the sensor data:

#include <Adafruit_Sensor.h>
#include <Adafruit_BMP280.h>


Then, to read the values you have to add:

float temp = bme.readTemperature();
float pressure = bme.readPressure();


That's all.

If you want to optimize the way Arduino uses the battery without draining it fast, you can read this article describing how to manage device power in IoT.

Sending Data to Google Cloud Platform Via Google Sheet

The last step is sending the data acquired by the sensor to Google Cloud Platform. Therefore, we will use another Temboo choreo (AppendValue) under Google>Sheets.

After clicking on this choreo, Temboo shows a form where you have to add all the information required as ClientId, Token, and so on as shown in the picture below:

If you are wondering where you get the SpreadsheetID, you can get it from the URL when accessing the sheet from your browser:

Once you have added all the information required, Temboo will generate all the code you need to integrate Arduino and Google Cloud Platform. Just writing a few lines of code you have implemented an IoT system that uses Google Cloud API. It is necessary to modify the Temboo code adding the values read from the sensor, using the piece code previously described.

Finally, running the sketch, you will see that Arduino starts sending data to the Google Cloud:

At the end of this post, you have built an IoT system and explored how to integrate Arduino and Google cloud. Writing a few lines of code, you have built an IoT system that sends data to the Google Sheet. The interesting part of this project is the way you have implemented it. Configuring Google Cloud Platform, without much knowledge about IoT, you have developed your first IoT project! Congratulations!

Further Reading

[DZone Guide] The Internet of Things: Connecting Devices and Data

Setting Up Your Own IoT Cloud Server

A Routing IoT Gateway to the Cloud

Topics:
iot ,arduino ,good cloud platform ,tutorial ,integration

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}