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.
Recommended:
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:

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:

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:

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
Code 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 https://github.com/GoogleCloudPlatform/google-cloud-iot-arduino.git
Code 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 = "pool.ntp.org";
const char* ntp_secondary = "time.nist.gov";
#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"
"GAYDVQQKDBFDb21vZG8gQ0EgTGltaXRlZDEhMB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Ag9g1o1Q\n"
"GE8mTgHj5rCl7r+8dFRBv/38ErjHT1r0iWAFf2C3BUrz9vHCv8S5dIa2LX1rzNLz\n"
"Rt0vxuBqw8M0Ayx9lt1awg6nCpnBBYurDC/zXDrPbDdVCYfeU0BsWO/8tqtlbgT2\n"
"G9w84FoVxp7Z8VlIMCFlA2zs6SFz7JsDoeA3raAVGI/6ugLOpyypEBMs1OUIJqsi\n"
"l2D4kF501KKaU73yqWjgom7C12yxow+ev+to51byrvLjKzg6CYG1a4XXvi3tPxq3\n"
"smPi9WIsgtRqAEFQ8TmDn5XpNpaYbg==\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 -text
Code language: CSS (css)
Copy the result into the private_key_str
. To get the root certificate in root_cert
open your browser and type:
https://pki.goog/roots.pem
Code 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 MQTTGetting 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);
}
}
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:

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.
i am totally lost you here
>>
Clone the repository or copy somehow these files into your ESP32 project:
esp32_mqtt.h
main.cpp
ciotc_config.h
<<
can you explain?
Hi I added a description in my post. Hope it will help you.
You have to clone the repository and then under /examples/Esp32-lwmqtt you will find the code you need.
Let me know if you need more info
This worked great. Thanks for writing this up. I was able to see my published RSSI output messages on GCP on the first try.
Hi, I uploaded the data on Google Cloud, Thank you so much for your instruction.
However, my data sent from ESP32 to Cloud took about 2-3 minutes to view in the subscribers. Is it hardwired? If not, How can I fix this?
Plus, I want to access the data I uploaded on Cloud from another device as an end-user, Could you show me how to do that?
Thanks in advance.
Hey there,
I really appreciate your detailed instructions and helping code to connect ESP32 to GCP. However, after successfully creating a registry I don’t know how to exactly write the code for generating the elliptic public and private key pairs. Can you kindly guide as to what we could do?
Do you mean this one?
openssl ec -in esp32-device.pem -noout -text
You don’t have to write code it is a shell command
Oh yes, thank you I was able to figure it out. However now before sending data to cloud GCP from ESP32. My Arduino code, which is updated according to the commands mentioned above, cannot compile. The error which it mentions is (in esp32-mqtt.h), is that no such directory of mqtt.h exists. Can you help me here? I also imported the repository in zip format to Arduino as you mentioned.
You should import a MQTT library i guess
I’ve been trying ;( without success though
settings incorrect or missing cypher for SSL,
I’m getting this error message kindly help;(
Hello Francesco,
I am getting this error message “Settings incorrect or missing a cyper for SSL Connect with mqtt.2030.ltsapis.goog:8883”
How do i fix it ?
Are you sure the MQTT broker you are using is correct?
I am facing the exact same issue. Were you able to resolve it?
openssl ec -in esp32-device.pem -noout -text
is it correct if we type this command in googlr cloud shell ?
Setup…..
Starting wifi
Connecting to WiFi
Waiting on time sync…
checking wifi…Connecting…
Refreshing JWT
not connected
Settings incorrect or missing a cyper for SSL
Connect with mqtt.2030.ltsapis.goog:8883
ClientId: projects/esp32-317614/locations/us-central1/registries/ESP32/devices/esp32-device
Waiting 60 seconds, retry will likely fail
Im getting this error
I also get “Settings incorrect or missing a cyper for SSL” 🙁 on ESP32 and ESP8266
I am on latetes libraries but still no luch 🙁
Thanks
PS. mongoose-os.com works just fine on both boards
hi everyone and @Francesco Azzola,
i have the same problem ,not connnected. Is there anyone who solves this error or any advances until now??
Regards,
GS
Hi, I guess something is changed from the time I wrote this post. I should investigate it.
Hi everyone I also have same problem ,
@Felix how did you run arduino code with MQTT in mongoose os ?
Regards,
GS.
Thank you for the nice tutorial. Can we connect the google iot cloud platform via At commands GSM modem?
Hi, appreciate the tutorial you’ve made. Thank you so much.
I followed the steps precisely and installed the mentioned libraries but I encountered an error with the esp8266_mqtt.h. It says
In file included from D:\Users\FCSL_CheskaAbarro\Desktop\Other Files\SampleCodes\Esp8266-lwmqtt\Esp8266-lwmqtt.ino:3:0:
esp8266_mqtt.h:23:37: fatal error: WiFiClientSecureBearSSL.h: No such file or directory’
but when I include the ESP8266 library, more error arises. Can you suggest to me something that I should do? Thank you so much