Connecting ESP32 to Google IoT Core with Zerynth Studio
The Internet of Things is coming and different cloud service providers are offering multiple tools for IoT applications. These tools simplify implementations of connecting sensors/devices, data storage and real time data visualisation. When we reap the benefits of these services, IoT applications become more efficient, traceable and controllable.
Today we are dealing with Google Cloud Platform. The hardware consists of an ESP32 MCU and BME280 sensor. On the software side, Zerynth Studio will help us to virtualise and flash our MCU. Lastly, we need to register on Google Cloud Platform, which is free.
Let's start with Google Cloud Platform. Registration is free, and you will get $300 credit that you will need to spend in order to test different cloud services.
Link: https://console.cloud.google.com/
In order to proceed on Google Cloud Platform, select your country first and accept the Terms of Service.
Now you can start trying it for free.
Google wants you to give your name, address, telephone number and credit card information. Don't worry, you will not be charged for first 12 months. In the end of the trial year, Google asks if you want to proceed.
Now you have $300 credit that you can spend in your trial process! Here is your console:
Google IoT Core:
Open Google Cloud console and choose Google IoT Core service. Create a registry and don't forget to name your MQTT topic:
Click on create and then click on add device:
Let's add subscription to our topic. Go to IoT Core again and click on the topic link:
Click on Create Subscription:
Follow the steps and give it a name, then click on create:
Public and Private Key Generation:
In order to connect device to the cloud, let's generate public key in ES256 format:
$ openssl ecparam -genkey -name prime256v1 -noout -out ec_private.pem
$ openssl ec -in ec_private.pem -pubout -out ec_public.pem
$ cat ec_public.pem
Copy and paste the public key into Google IoT Core configuration and save it.
Zerynth Studio:
Clone the repository and open it with Zerynth Studio:
$ git clone https://github.com/emirez/iot-zerynth-googleiotcore.git
Let's now generate a private key:
$ openssl asn1parse -in ec_private.pem
0:d=0 hl=2 l= 119 cons: SEQUENCE
2:d=1 hl=2 l= 1 prim: INTEGER :01
5:d=1 hl=2 l= 32 prim: OCTET STRING [HEX DUMP]:2BB9B2A82F4AFBDA901822400121B52CB0F67F6DBAB00228C458075334560FD9
39:d=1 hl=2 l= 10 cons: cont [ 0 ]
41:d=2 hl=2 l= 8 prim: OBJECT :prime256v1
51:d=1 hl=2 l= 68 cons: cont [ 1 ]
53:d=2 hl=2 l= 66 prim: BIT STRING
Copy and paste the octet string on third line into your project, in private.hex.key file.
Update your device.conf.json file under your project. Fill the project id, cloud region, registry id and device id which can be found in Google IoT Core console.
{
"project_id": "thingforward1",
"cloud_region": "europe-west1",
"registry_id": "tfregistry",
"device_id": "tfdevice"
}
Update your WiFi credentials on code, plug your MCU and then connect your BME280 sensor:
- SCL to IO17
- SDA to IO16
- 3V3 to 3V
- GND to GND
Let's implement BME280 libraries. To do so, open the library manager and search for BME280:
On Zerynth Studio, choose your device, register, create a VM, virtualize it after. Here is the result on serial monitor:
Starting BME280 Sensor...
Ready!
--------------------------------------------------------
Publishing...
Temperature: 24.3899993 C, Humidity: 39.6220703 %rH
Publishing...
Temperature: 24.7348943 C, Humidity: 39.6285304 %rH
Here is the IoT Core tool seeing MQTT messages:
Data Storage:
Now, we should be able to see published messages on the cloud. Next step is defining a Cloud Function.
Create a new function and select the trigger as your MQTT topic.
Here is your functions list:
Click on the function name, then navigate to source. Click on edit to add this code line:
You will see a code snippet. On the 10th line of index.js, please insert following and save it:
console.log(pubsubMessage);
You will now see "View Logs" next to the Edit button on the function dashboard. Click on that:
Here are published messages coming from the device!
Stack Driver:
Real-time data visualisation made easy with Stack Driver. We already observed the data traffic between device and the cloud. To visualise it, open the IoT Core and click on View in Stackdriver:
Register and create a free account:
Click on launch monitoring in order to start:
Your dashboard on Stackdriver:
Click on the resources on the left pane, choose resource type and metric:
It works! This example only shows the execution counts on cloud function. But feel free to add your own functions and metrics, so you can customize your cockpit.
Conclusion:
Every cloud service has its own advantages in certain services. The documentation on the Google's side is pretty rich and strong, but the practical tutorials need to be prepared in order to simplify cloud-device connections. The data transfer can be made by using HTTP or MQTT protocols like other cloud providers. One of the best features is Stackdriver and you can see all data flow on several charts in seconds.
Zerynth Studio always helps us during linking the sensor/hardware libraries and during compile-virtualise-flash process thanks to its stability. The examples coming from IDE gives you a head-start in the beginning.
That's pretty much it! Stay tuned for upcoming blogposts about cloud services and new use cases.
Eren
Andreas