Google Cloud IoT Core ESP32: Send data to Google IoT Platform using MQTT

This tutorial covers how to integrate Google Cloud IoT and ESP32. In more detail, it will describe how to send data to Google IoT Platform ESP32 through MQTT. This ESP32 tutorial will cover the following topics:

  • How to configure Google Cloud Platform IoT console
  • Sending data from ESP32 to Google IoT using MQTT

To better understand the steps to follow to integrate ESP32 and Google Cloud IoT, in this tutorial we will send the temperature and humidity through MQTT. Therefore, we will connect ESP32 to DHT11 to acquire this information.

ESP32 MQTT client: Publish and Subscribe

Configuring Google Cloud Platform IoT core to use with ESP32

Firstly, it is necessary to create an account on the Google Platform. You can create this account for free and you have $300 to spend on your tests. Then, you have to enable the IoT API using the console:

Google Core IoT API setup with ESP32

Creating Core IoT Registry

Once you have enabled the API, then the first thing to do is creating a Google Core IoT Registry. We assume you have already created a project. If this is not the case, you can simply create it giving a name to the project. Before connecting and sending data from ESP32 to Google Cloud IoT, it is necessary to create and configure the device using the console or the command line.

A device registry represents a container of one or more devices. Using a registry we can configure some common properties. So, add a new registry:

ESP32 with Google cloud IoT

You have to provide two information:

  • the Registry ID that we will use later when developing the ESP32 client that connects to Google Core IoT
  • and the region.

Moreover, you have to create a topic. This is the channel where we will publish the sensor readings coming from ESP32 and DHT11:

Google Cloud Topic

In this example, we have created a topic named data-topic. That’s all. Now you can save and create your registry.

By default, HTTP and MQTT protocols are enabled. Therefore, we can use both protocols to connect to Google Cloud IoT. Anyway, we will only the MQTT protocol. With this intention, the ESP32 will connect to the IoT Core only using MQTT. Once you have saved your information, the registry overview should look like:

Configuring Cloud IoT Device

Once, the registry is configured, we can create and configure the device. The device represents our physical device and all the information we have to provide in order to connect to Google IoT. Google Cloud IoT Core requires some certificates so that the ESP32 can connect to it. Therefore, let us create a new certificate

Creating certificates

In this step, we will create an Elliptic Certificate private and public key pair:

openssl ecparam -genkey -name prime256v1 -noout -out esp32-device.pem
openssl ec -in esp32-device.pem -pubout -out esp32-device-public.pemCode language: CSS (css)

Now to get the public key to use in the device configuration, write this command:

cat esp32-device-public.pem Code language: CSS (css)

The code is the public key you have to copy in the device definition.

Adding a new device

Let us add a new device:

As you can notice in the image above, it is necessary to provide:

  • device-id that must be unique
  • a certificate to authenticate the ESP32

Save everything.

In the end, the device is ready and the information you provided should look like the image below:

Connecting ESP32 to Google Cloud IoT

Once, we completed all the configuration, we can focus our attention on how to connect the ESP32 to Google Cloud IoT. Even if the connection uses the MQTT protocol, it is not so easy to connect directly the ESP32 to Google IoT without using a library.

In more detail, the ESP32 must be authenticated. As the authentication mechanism, this IoT platform uses JWT signed using your private key built. Consequently, to connect and send data using MQTT we have two different options:

In this ESP32 tutorial, we will use the second option because it is easier. Therefore, it is necessary to import the library before going on.

Clone the repository or copy somehow these files into your ESP32 project:

  • /examples/Esp32-lwmqtt/esp32_mqtt.h
  • /examples/Esp32-lwmqtt/main.cpp
  • /examples/Esp32-lwmqtt/ciotc_config.h

To clone the repository use the following command:

git clone language: PHP (php)

To customize the library according to our needs, it is necessary to modify the file named ciotc_config.h:

const char *ssid = "your_ssid";
const char *password = "wifi_password";
// Cloud iot details.
const char *project_id = "esp32-project-266916";
const char *location = "europe-west1";
const char *registry_id = "esp32-swa-registry";
const char *device_id = "esp32-device";
// Configuration for NTP
const char* ntp_primary = "";
const char* ntp_secondary = "";
#define LED_BUILTIN 13
const char *private_key_str =
// Time (seconds) to expire token += 20 minutes for drift
const int jwt_exp_secs = 3600; // Maximum 24H (3600*24)
const char *root_cert =
    "-----BEGIN CERTIFICATE-----\n"
    "-----END CERTIFICATE-----\n";Code language: PHP (php)

In this file, you have to replace the following information:

  • Wifi ssid
  • Wifi password
  • project_id
  • registry_id
  • location
  • device_id

You can get this information from the configuration steps shown in the previous paragraphs. Moreover, you need the private key. You can create it using this command:

openssl ec -in esp32-device.pem -noout -textCode language: CSS (css)

Copy the result into the private_key_str. To get the root certificate in root_cert open your browser and type: language: JavaScript (javascript)

Save the file and copy the content into the root_cert.

Before going on, it is necessary to import the MQTT library (using this repository) so that the project will compile.

Recommended: How to connect ESP32 to AWS IoT using AWS MQTT
Getting started with ESP32: Build a weather station (BMP280 + SSD1306)
Send Email using ESP32 with SMTP Server: Plain and HTML email
ESP32 MQTT client: Publish and Subscribe. HiveMQ and BME280 example

Sending data to Google Cloud IoT from ESP32 using MQTT

Finally, it is time to send data to Goole IoT using MQTT from the ESP32. As said before, to explain how to do it, we will acquire temperature and humidity using DHT11. Therefore, we will modify the file main.cpp in this way:

#include <Arduino.h>
#include <WiFiClientSecure.h>
#include "esp32-mqtt.h"
#include <Adafruit_Sensor.h>
#include "DHT.h"
#include <ArduinoJson.h>
#define DHTTYPE DHT11
#define DHT_PIN 27
char buffer[100];
void setup() {
  // put your setup code here, to run once:
unsigned long lastMillis = 0;
void loop() {
  delay(10);  // <- fixes some issues with WiFi stability
  if (!mqttClient->connected()) {
  if (millis() - lastMillis > 60000) {
    Serial.println("Publishing value");
    lastMillis = millis();
    float temp = dht.readTemperature();
    float hum = dht.readHumidity();
    StaticJsonDocument<100> doc;
    doc["temp"] = temp;
    doc["humidity"] = hum;
    serializeJson(doc, buffer);
    //publishTelemetry(mqttClient, "/sensors", getDefaultSensor());
    publishTelemetry( buffer);
}Code language: PHP (php)

The code is very simple. The DHT11 connected to the ESP32 using PIN 27 reads the temperature and the humidity. The code generates a JSON payload and sends it to Cloud Core IoT using MQTT.

Testing the results

To visualize the data sent through the MQTT protocol, we can create a subscriber that reads data on the data-topic channel. Let us create a subscriber as shown in the picture below:

Runs the code above and check the dashboard on Cloud Core IoT:

Then you can visualize the MQTT messages sent by ESP32:

Google Cloud IoT Core with ESP32

That’s all!!!

If you have Raspberry Pi read how to use NodeJS with Raspberry Pi and GCP.

Wrapping up

To summarize, in this ESP32 tutorial we have covered how to connect ESP32 to Google Cloud IoT. We have learned how to configure Google Cloud IoT in order to accept data through the MQTT protocol.

    1. chanchhaychhada noun May 20, 2020
      • Francesco Azzola May 20, 2020
    2. John Kicklighter July 5, 2020
    3. nhat hoang September 28, 2020
    4. Gulzaib March 21, 2021
      • Francesco Azzola March 22, 2021
    5. Gulzaib Khan March 26, 2021
    6. Anonymous March 27, 2021
    7. Gulzaib Khan May 20, 2021
    8. John May 29, 2021
    9. Anon June 4, 2021
    10. Anonymous June 12, 2021
    11. Jimson Kurian June 22, 2021
    12. Felix July 4, 2021
    13. GS July 7, 2021
      • Francesco Azzola July 9, 2021
    14. GS July 8, 2021
    15. Muhammad Usman November 15, 2021
    16. Keith Chico November 22, 2021

    Add Your Comment