Google IoT Core with an ESP32 IoT client, Part 1 Firmware

ESP32 rocks as an IoT client!

If you need some help to setup your ESP32 and Mongoose environment try this.


In this project, we are going to connect the Sparkfun ESP32 Thing board to the Google IoT core. We use the MQTT-bridge and we explore the following Google concepts:

  • events, by sending ESP32 core temperature data, free ram and uptime
  • state, sending hardware, revision, firmware and total ram
  • config, set the temperature offset for the built-in sensor
  • commands, turn on/off onboard LED

The temperature is accessed using the JavaScript library api_esp32.js Which reads the built-in temperature sensor.

My colleague Edit Karasz has an excellent instruction for provisioning your Google IoT Core platform using Teraform, you can find the project here. The Sparkfun board looks like this:

This instruction is based on the following instruction made my Cesanta.

Setup Google IoT Core

Start by doing the following:

gcloud components install beta
  • Authenticate with Google Cloud:
gcloud auth login

If you want to log out from all the accounts run the following command

gcloud auth revoke --all

If you want to log out from a specific account then run the following command

gcloud auth revoke <your_account>
  • Create a cloud project — choose your unique project name:
gcloud projects create YOUR_PROJECT_NAME

We are calling our project “hm-iot-project-esp32-demo”

gcloud projects create hm-iot-project-esp32-demo

If you log in to your account you should see the project in your drop-down list. As seen below:

While you are logged in you also need to activate billing:

  • Add permissions for IoT Core:
gcloud projects add-iam-policy-binding YOUR_PROJECT_NAME --role=roles/pubsub.publisher
  • In our case it means:
gcloud projects add-iam-policy-binding hm-iot-project-esp32-demo --role=roles/pubsub.publisher

If billing is not activated you will get an error and you will not be able to proceed.

  • Set default values for gcloud:
gcloud config set project YOUR_PROJECT_NAME
gcloud config set project hm-iot-project-esp32-demo
  • Create PubSub topic for device data:
gcloud beta pubsub topics create iot-topic
  • Create PubSub subscription for device data:
gcloud beta pubsub subscriptions create --topic iot-topic iot-subscription
  • Create a device registry, we use the US to get full functionality:
gcloud beta iot registries create iot-registry --region us-central1 --event-pubsub-topic=iot-topic

Setup Mongoose OS

mos gcp-iot-setup --gcp-project YOUR_PROJECT_ID --gcp-region us-central1 --gcp-registry iot-registry
mos gcp-iot-setup --gcp-project hm-iot-project-esp32-demo --gcp-region us-central1 --gcp-registry iot-registry

Google MQTT Bridge

If you are used to working with MQTT there are some surprises…

The topics you can subscribe/publish to are pre-defined by Google. Have a look in the mos.yml, you will see several GCP settings such as:

  • [“gcp.enable”, true]
  • [“gcp.project”, “hm-iot-project-esp32-demo”]
  • [“gcp.region”, “us-central1”]
  • [“gcp.registry”, “iot-registry”]
  • [“gcp.device”, “esp32_04FFB0”]
  • [“gcp.key”, “gcp-esp32_04FFB0.key.pem”]

You will have to replace gcp.device and gcp.key with your settings.

You will be working with the following “topics” with the following prefix /devices/{device-id}/:


Device events are used to publish information from the device, such as temperature, humidity etc.


Device state information captures the current status of the device, not the environment. Devices can describe their state with an arbitrary user-defined blob of data sent from the device to the cloud.


A device configuration is an arbitrary user-defined blob of data sent from Cloud IoT Core to a device.


Commands are transitory, one-time directives sent to devices that are connected to Cloud IoT Core and subscribed to the commands topic. Commands can be useful when you want to:

  • Send messages quickly to many devices at a specific time
  • Send large-volume messages to many devices at a particular time
  • Send time-bound directives that should expire
  • Send incremental device settings

Google’s documentation on the subject can be found here.

Running the code

In mos.yml change the values:

  • [“wifi.sta.ssid”, “YOUR SSID”]
  • [“wifi.sta.pass”, “YOUR KEY”]
  • [“”, “YOUR ID”]
mos gcp-iot-setup --gcp-project YOUR_PROJECT_ID --gcp-region us-central1 --gcp-registry YOUR_REGISTRY

This will generate new keys and enable GCP.

  • [“gcp.enable”, true]

Credits and highly recommended links

Source Code

The source code for this article can be found here.