Google IoT Core with an ESP32 IoT client, Part 2 Google Cloud Functions
Is serverless the new black?
We present several different GCP cloud functions to handle data from IoT devices and work with GCP database solutions. This project is the back-end part of Google IoT Core with an ESP32 IoT client, Part 1 Firmware.
Creating your own project
- Functions: Configure and deploy Cloud Functions
- Default Firebase project: [YOUR PROJECT]
- Do you want to use ESLint to catch probable bugs and enforce style? Yes
- Do you want to install dependencies with npm now? Yes
Edit your package.json and add:
to get Version 8 of NodeJS (Promises 😍 )Prerequisite
Install firebase tools.
npm install -g firebase-tools
I have created the account “hm-iot-project-esp32-demo” via Firebase Console
will show the following:
NameProject ID InstancePermissionshm-iot-project-esp32-demohm-iot-project-esp32-demoOwner
If you need to switch accounts.
cd into the directory cloud-function-pubsub-database
To deploy the functions to the cloud type:
Error: No project active. Run with --project <projectId> or define an alias by
running firebase use --add
firebase deploy --project hm-iot-project-esp32-demo
When the function is deployed, is will look like this in the firebase console:
Google Cloud Functions Events
A platform event is something that triggers a Google Cloud Function to execute. Here are some examples:
- An HTTP Trigger (e.g., for a REST API)
- A pubSub event (e.g., run a function when a message is sent to the topic)
- A Storage event (e.g., Image uploaded into the bucket)
How about using cloud functions for Cron jobs?
Unfortunately, there is no easy way to trigger your cloud functions based on a schedule. In the following project, Google describes how to create an App Engine that provides a Cron service.
Having a look at the code
The cloud function is triggered by a pubSub event from the IoT core. It is called “iot-topic”. We parse the data from our ESP32 and create the following JSON structure:
As seen above the timestamping is done in the cloud function and not on the ESP32. This should be changed later on, it is always better to timestamp the data as close to the source as possible to get the best possible accuracy of the timestamp.
The data structure is stored in:
This is done in the functions:
We use Promise.all to call the functions that wait for all to be fulfilled (or the first function to perform a rejection). .then() automatically returns a promise so we do not need to resolve or reject the promise.
Credits and highly recommended links
- Build a Weather Station using Google Cloud IoT Core and MongooseOS
- Scalable IoT Integration
- Bike Route Data Gatherer
The Source code
The source code for this article can be found here.