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.pem

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

cat esp32-device-public.pem

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

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 = ""; #ifndef LED_BUILTIN #define LED_BUILTIN 13 #endif const char *private_key_str = "xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:" "de:17:9c:8f:fe:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:" "e8:fa"; // 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" "MIIEMjCCAxqgAwIBAgIBATANBgkqhkiG9w0BAQUFADB7MQswCQYDVQQGEwJHQjEb\n" "MBkGA1UECAwSR3JlYXRlciBNYW5jaGVzdGVyMRAwDgYDVQQHDAdTYWxmb3JkMRow\n" "GAYDVQQKDBFDb21vZG8gQ0EgTGltaXRlZDEhMB8GA1UEAwwYQUFBIENl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Bv/38ErjHT1r0iWAFf2C3BUrz9vHCv8S5dIa2LX1rzNLz\n" "Rt0vxuBqw8M0Ayx9lt1awg6nCpnBBYurDC/zXDrPbDdVCYfeU0BsWO/8tqtlbgT2\n" "G9w84FoVxp7Z8VlIMCFlA2zs6SFz7JsDoeA3raAVGI/6ugLOpyypEBMs1OUIJqsi\n" "l2D4kF501KKaU73yqWjgom7C12yxow+ev+to51byrvLjKzg6CYG1a4XXvi3tPxq3\n" "smPi9WIsgtRqAEFQ8TmDn5XpNpaYbg==\n" "-----END CERTIFICATE-----\n";

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 -text

Copy the result into the private_key_str. To get the root certificate in root_cert open your browser and type:

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 DHT dht(DHT_PIN,DHTTYPE); char buffer[100]; void setup() { // put your setup code here, to run once: Serial.begin(9600); Serial.println("Setup....."); dht.begin(); pinMode(LED_BUILTIN, OUTPUT); setupCloudIoT(); } unsigned long lastMillis = 0; void loop() { mqtt->loop(); delay(10); // <- fixes some issues with WiFi stability if (!mqttClient->connected()) { connect(); } 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); } }

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

    Add Your Comment