Let’s talk to Google Cloud IoT Core

This blog continues the blogs:

and will cover how to change the ESP8266 Code for the sensor to be able to communicate with Google Cloud IoT Core.

Connecting the ESP to your computer

If you have disconnected your ESP from your computer, please do connect the ESP again and re-start mos

$ ~/.mos/bin/mos

select the right port for your USB connected ESP

after clicking Select, the Device Logs part of the Mongoose OS web-UI should start showing the log-entries created by the ESP.

As we don’t want to flash the firmware again and don’t need to re-define the WiFi, you can complete this step by clicking Done.

Adapting the ESP Sourcecode

Before we change the source of the ESPs firmware, we need to cover a little bit of theoretical background – sorry!

When we created the Device Registry in Google Cloud IoT Core, we had to define a MQTT Topic. The following screenshot is a re-cap of the relevant step covered in the previous blog.

This MQTT Topic is created in Google Cloud Pub/Sub, but our ESP needs to communicate to Google Cloud IoT Cores MQTT Broker, whose  Protocol Bridge will then forward the messages to our created Topic. To reflect this in the code of the ESP, we will have to update the line

let topic = '/devices/' + Cfg.get('device.id') + '/events';


let topic = '/devices/' + Cfg.get('device.id') + '/state';

Update the code on the ESP by uploading the changed file.

Introduce the ESP to Google Cloud IoT Core

Looking at the source for the ESP, it probably became obvious that there is not a single line connecting the ESP to any MQTT Broker, or even defining the MQTT Broker to communicate with. This is all done under the cover of the flashed Mongoose OS firmware.

What needs to be done now is:

  • define the IP of the MQTT Broker
  • authenticate the ESP with Google Cloud IoT Core

Luckily Mongoose OS provides a simple and easy way to achieve both of these tasks in one step.

On the Mongoose Web-UI select the Terminal

In the provided command-line enter

mos gcp-iot-setup --gcp-project <YOUR_PROJECT_NAME> --gcp-region <REGION> --gcp-registry <YOUR_REGISTRY>

and replace the values with the ones used in the earlier steps. In my case this results in

This command will do many steps for you, like

  • creating a private/public keypair
  • upload the public key to Google Cloud IoT Core
  • upload the private key to the ESP
  • set all environment variables in the ESP to have him communicate to Google Cloud IoT Core
  • restart and reconnect the ESP

In the Device Log Windows of Mongoose, you should see a few lines, including

mgos_mqtt_global_con MQTT connecting to mqtt.googleapis.com:8883


mgos_mqtt_ev MQTT CONNACK 0

Looking at the Device part of the Google Cloud IoT Core, we need to refresh the list of known Devices

to see our newly added Device

Verifying the Message delivery

The code of the ESP only delivers Message to Google Cloud IoT Core when the Flash-Button is pressed, so please do so!

This should result in the following line showing on the Device Log Window

 Published: 1 /devices/esp8266_D608CF/events -> {"free_ram":31576,"total_ram":52008}

To verify if the Message was really received in Google Cloud IoT Core, you will have to select the Device in the Google Cloud IoT Core Registry view.

and there on the Configuration & state history


which will show the received messages

If you rather want to see the message content in a readable form, you just need to click on the message and select “Text” as a format.
This concludes this blog …

2 thoughts on “Let’s talk to Google Cloud IoT Core

  1. nice blog sir.
    can you help with a lead to finding the smalles SoC available to-date. I have about the size of a table sppon handle to deply WiFi, BT, and controller functions for a smart-watch style applicati on aboard. I was tinking about buying https://www.simblee.com/.. howver there is no.. and I do mean no customer service ro support.

    thank you



Leave a Comment

%d bloggers like this: