Search code examples
node.jsgoogle-cloud-platformesp32google-cloud-iotmongoose-os

"Invalid gcp.key" from Mongoose OS when connecting ESP32 to Google Cloud Platform


I am trying to connect my ESP32 device to Google Cloud Platform (GCP) with Mongoose OS. I am new to Mongoose OS, but have got as far as to build and flash code as well as connection to wifi. But I get problems when I add info to the mos.yml file to connect to GCP. This is the error:

mgos.gcp.c:361 Invalid gcp.key (0xffffc200)

I have created my key pair with the following commands:

openssl ecparam -genkey -name prime256v1 -noout -out ec_private.pem
openssl ec -in ec_private.pem -pubout -out ec_public.pem

This is my mos.yml:

author: HiMinds.com
description: Test application for connecting to Google IoT Core via MQTT-bridge
version: 0.1
arch: esp32

libs_version: ${mos.version}
modules_version: ${mos.version}
mongoose_os_version: ${mos.version}

# Optional. List of tags for online search.
tags:

  - js
  - c

# List of files / directories with C sources. No slashes at the end of dir names.
sources:
  - src

# List of dirs. Files from these dirs will be copied to the device filesystem
filesystem:
  - fs

config_schema:
  - ["pins", "o", {title: "Pins layout"}]
  - ["pins.button", "i", 0, {title: "Button GPIO pin"}]
  - ["pins.button_pull_up", "b", true, {title: "True if button is pulled up when inactive"}]
  - ["pins.led", "i", 5, {title: "LED GPIO pin"}]
  - ["wifi.ap.enable", false]
  - ["wifi.sta.enable", true]
  - ["wifi.sta.ssid", "my_SSID"]
  - ["wifi.sta.pass", "my_password"]
  - ["device.id", "esp32"]
  - ["mqtt.enable.", true]
  - ["mqtt.server", "mqtt.googleapis.com:8883"]
  - ["mqtt.ssl_ca_cert", "roots.pem"]
  - ["gcp.enable", true]
  - ["gcp.project", "project_name"]
  - ["gcp.region", "europe-west1"]
  - ["gcp.registry", "registry_name"]
  - ["gcp.device", "esp32"]
  - ["gcp.key", "ec_private.pem"]
  - ["provision.max_state", 3]

libs:
 # - origin: https://github.com/mongoose-os-libs/ca-bundle
  - origin: https://github.com/mongoose-os-libs/core
  - origin: https://github.com/mongoose-os-libs/provision
  - origin: https://github.com/mongoose-os-libs/rpc-service-config
  - origin: https://github.com/mongoose-os-libs/rpc-service-fs
  - origin: https://github.com/mongoose-os-libs/rpc-service-gpio
  - origin: https://github.com/mongoose-os-libs/rpc-uart

  #IoT 
  - origin: https://github.com/mongoose-os-libs/adc
  - origin: https://github.com/mongoose-os-libs/mjs
  - origin: https://github.com/mongoose-os-libs/mqtt
  - origin: https://github.com/mongoose-os-libs/wifi
  - origin: https://github.com/mongoose-os-libs/gcp
  - origin: https://github.com/mongoose-os-libs/sntp

# Used by the mos tool to catch mos binaries incompatible with this file format
manifest_version: 2017-09-29

What am I missing?

I have run mos put full_path_to_key/ec_private.pem to send the key to the device, which works only when I emit the GCP lines from my "mos.yml". Would I need to send the key again after rebuilding and flashing new code including GCP lines? I have also sent a downloaded roots certificate file "roots.pem" (but haven't actually read anywhere that you should send this file to the device).

I am also unsure of if the paths to the key file (and roots.pem file) should be the full ones on my computer, or if they refer to the files on the device itself and it is enough with only the filename without path. None of the options work though.

As a side note, I also get this error, even without the GCP info in mos.yml, if this could have anything to do with it:

MJS error: failed to exec file "api_aws.js": failed to read file "api_aws.js"


Solution

  • So, it turned out I didn't need to set all that GCP info info in "mos.yml", but it was set for me when running

    mos gcp-iot-setup --gcp-project YOUR_PROJECT_ID --gcp-region YOUR_REGION --gcp-registry YOUR_REGISTRY
    

    If I tried running this command with the GCP info in mos.yml, I had an error message

    Error: write /dev/ttyUSB0: file already closed

    So I had to remove the GCP info, then run the command, and the connection was setup!

    The key file was generated when running the above command, so no need to transfer it to the device. The roots certificate file also seemed to be handled by the command.