Skip to content

Patater/mbed-os-example-for-google-iot-cloud

 
 

Repository files navigation

Mbed OS example for Google cloud

The example project is part of the Arm Mbed OS Official Examples. It contains an application that uses the Google Cloud IoT device SDK to connect to an IoT core instance using the MQTT protocol and publishes a message every 3 seconds for 30 seconds or until a message is received.

You can build the project with all supported Mbed OS build tools. However, this example project specifically refers to the command-line interface tool Arm Mbed CLI. (Note: To see a rendered example you can import into the Arm Online Compiler, please see our import quick start.)

It has been tested on K64F with Ethernet and DISCO_L475VG_IOT01A with WiFi, but any Mbed OS 6 targets with Internet access should work.

Downloading this project

  1. Install Mbed CLI.

  2. Clone this repository on your system, and change the current directory to where the project was cloned:

    $ git clone https://github.com/ARMmbed/mbed-os-example-for-google-iot-cloud.git && cd mbed-os-example-for-google-iot-cloud
    $ mbed deploy
    

    Alternatively, you can download the example project with Arm Mbed CLI using the import subcommand:

    $ mbed import mbed-os-example-for-google-iot-cloud && cd mbed-os-example-for-google-iot-cloud
    

Configuring the Google Cloud IoT Core

  1. Follow the below steps to generate the key pair and certificate using OpenSSL

    1. create a self-signed CA with 365 days validity
      $ openssl req -new -x509 -days 365 -extensions v3_ca -keyout ca.key -out ca.crt -subj "/CN=unused"
      
    2. create a client key pair and certification
      $ openssl ecparam -genkey -name prime256v1 -noout -out client.key
      $ openssl req -out client.csr -key client.key -new -subj "/CN=unused"
      $ openssl x509 -req -in client.csr -CA ca.crt -CAkey ca.key -CAcreateserial -out client.crt -days 365
      
      Once you have generated the certificates, you will need to place client.key in the google_cloud_credentials.h file of this example and ca.crt and client.crt to be uploaded in Google Cloud portal while creating the device registry and add a device.
  2. Create a Google Cloud account if you don't have one, and log in to it.

    NOTE: If you have an admin for your Google Cloud account, please contact them to add a user to the account. You should obtain your login credentials from your admin in this case.

  3. Follow Google Cloud IoT Core official documentation here to

    • use existing project created by admin or create a new project if you have permission
    • create a device registry and add a device to the created registry
  4. Follow Google Cloud pub/sub official documentation here to

    • create a pub/sub topic
    • create a subscription for the created topic
  5. Update all the cloud credentials in mbed_app.json file,

    • set project id "google-cloud-project-id".value
    • set cloud region "google-cloud-region".value
    • set device registry "google-cloud-registry".value
    • set device id "google-cloud-device-id".value
    • set a topic "google-cloud-mqtt-topic".value that both your device and the cloud can publish messages to

Building and running

  1. (If you want to use WiFi) set nsapi.default-wifi-ssid to your WiFi name and nsapi.default-wifi-password to your WiFi password, keeping any escaped quotes (\"). If you use a different target, replace "DISCO_L475VG_IOT01A" with your target and remove "target.components_add": ["wifi_ism43362"] (unless it uses the same ISM43362 WiFi module). For example, to use NUCLEO-F429ZI:

    "NUCLEO-F429ZI": {
        "target.network-default-interface-type": "WIFI",
        "nsapi.default-wifi-security": "WPA_WPA2",
        "nsapi.default-wifi-ssid": "\"SSID\"",
        "nsapi.default-wifi-password": "\"PASSWORD\""
    }
  2. For Ethernet (e.g on K64F), connect a cable to the port.

  3. Connect your development board to your PC with a USB cable.

  4. Compile, flash and run the example

    mbed compile -m <TARGET> -t <TOOLCHAIN> -f --sterm --baud 115200
    

For example, <TARGET> can be DISCO_L475VG_IOT01A and <TOOLCHAIN> can be GCC_ARM if you want to use this combination.

Expected output

The example starts fetching time from an NTP server because the security processes require correct date/time when it tries to connect with Google Cloud IoT Core:

Time: Thu Aug 13 17:36:32 2020

Note: If it fails to fetch time or the reported time makes no sense (e.g. the year 2100), the NTP server might be unreachable for a moment. It is usually enough to reset the board to rerun the example.

Then it connects with MQTT host and respective port

connected to mqtt.2030.ltsapis.goog:8883

Now it initializes the Google Cloud IoT device SDK and starts sending one message on every 3 seconds:

publishing msg "10 messages left to send, or until we receive a reply"
Message sent successfully
publishing msg "9 messages left to send, or until we receive a reply"
Message sent successfully
publishing msg "8 messages left to send, or until we receive a reply"
Message sent successfully
...

Sending cloud-to-device messages

On the Google Cloud portal, go to the page of your device, you can send a message to your device using "SEND COMMAND".

Once the message is received by your device, it prints:

Message body: Hello from Google Cloud IoT Core

Tracing the IoT Core client

To monitor incoming/outgoing IoT Core events, you can enable tracing by setting "google-client.iotc-debug-output" in mbed_app.json to true and if you want BSP debug logs then set "google-client.iotc-bsp-debug-log" also to true. This can be useful for debugging issues related to connections or credentials.

Sample trace for the first message sent:

Time: Thu Aug 13 18:07:28 2020
[1597342048000][iotc.c:446 (iotc_connect_to)] New host:port [mqtt.2030.ltsapis.goog]:[8883]
[1597342048000][iotc.c:462 (iotc_connect_to)] new backoff value: 0
[1597342048000][iotc_control_topic_layer.c:77 (iotc_control_topic_layer_init)] control topic layer initializing..
[1597342048000][iotc_tls_layer.c:560 (iotc_tls_layer_init)] BSP TLS initialization successfull
[1597342048000][iotc_tls_layer.c:591 (iotc_tls_layer_init)] successfully initialized BSP TLS module
[1597342048000][iotc_io_net_layer.c:57 (iotc_io_net_layer_connect)] Connecting layer [0] to the endpoint: mqtt.2030.ltsapis.goog:8883
[1597342049000][iotc_io_net_layer.c:90 (iotc_io_net_layer_connect)] Connection successful!
...

Troubleshooting

If you have problems, you can review the documentation for suggestions on what could be wrong and how to fix it.

Related links

License and contributions

The software is provided under Apache-2.0 license. Contributions to this project are accepted under the same license.

This project contains code from other projects. The original license text is included in those source files. They must comply with our license guide.

About

Mbed OS example to connect to Google IoT Cloud

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • C++ 100.0%