Accessing Cloud IoT Core from Arduino

I’m excited to write about something that I have been working on with a SWE who has been generously donating his 20% time to our team, an Arduino Library for Google Cloud IoT Core.

Be warned, the library is not production-ready and is an experiment. I also anticipate some rather aggressive changes to the library soon while we explore additional hardware beyond the ESP8266. Oh, and we’re accepting code contributions if you have ideas and sign the thing.

If you have git installed, you can just clone the project into its own folder:

If you don’t have git, just download a zip of the project’s initial release.

Next, you will need to import the library into Arduino because we’re not yet released to the library manager (in the near future, we hope you will just use the Manage Libraries link).

Open Arduino and select the Sketch > Include Library > Add .ZIP library menu item — Note Arduino also can handle file folders as zip files, select the folder and click “open” while it is higlighted.

Select either the folder you cloned or the zip file you downloaded in the IDE.

Now, the library will show up under “Contributed libraries” as Google Cloud IoT Core JWT / ESP8266 (which astute readers probably noticed in the screenshot).

With the library installed, open the demo project by selecting:

File > Examples > Google Cloud IoT Core JWT / ESP8266 > Basic.ino

You will now need to configure your demo project. First, replace the ssid and password with your ssid and password.

Next, you will need to generate Elliptic Curve (EC) device credentials:

Next, you will need to create a Cloud IoT Core project. My favorite way to do this is with the gcloud command-line utility but you can do this with the Google Cloud Console as well. To do this using gcloud, perform the following commands:

Now that you have created a registry and registered your device using the device public certificate, you need to get the key fingerprint from the device key (the private EC key):

Take the “priv” part of the key and put it into the sample app, replacing the private_key_str constant.

Next, update your project_id to the project that you can look up using `gcloud config list` and then set the location, registry_id, and device_id values to the values you entered when creating your device.

After you upload the sketch to the ESP8266 board, it will run and if everything worked correctly, you will see the configuration change message come down on the device if you monitor the logs in the Arduino Serial monitor:

And now, you are ready to party!


If you’re seeing an “Error 400 Bad Request” there might be a space in one of your parameters.

If you’re seeing an “Error 401 Not Authorized” but the JWT that is generated looks correct (you can verify using — then you may need to upgrade your ESP8266 library to the latest version using the Arduino Boards Manager (I have tested with 2.4.1). It might seem crazy but there is significant development that happens on the Arduino library and recent enhancements are necessary for everything to work.

If you’re seeing “Error 403 Not authorized” Double check all of the values that you entered in the `//Cloud iot details` section of the app. If any of these values is incorrect, authorization will fail. It can be helpful to debug by decoding the JWT and seeing if you’re setting the expiry date wrong or something. If everything looks right, it may just be worth creating a new device as a sanity check.

If you’re having trouble joining your wifi network, remember that you need to be on a 2.4 ghz network, not 5ghz.