Using the Renesas AE-CLOUD2 to Send GPS Location Data
to the Google Cloud IoT
By Greg Toth for Mouser Electronics
The Renesas Synergy™ AE-CLOUD2 is an Internet of Things (IoT) development kit for prototyping cloud-connected
sensor applications that can communicate through Long-Term Evolution (LTE) Category M1 (Cat-M1) or Category NB1
(Cat-NB1) cellular networks as well as Wi-Fi or Ethernet. In this project, you’ll set up an IoT development
environment and configure the AE-CLOUD2 and Google Cloud™ to run an application that reads hardware sensors
(including a GPS receiver) and securely transmits real-time data to the cloud. Once sensor data reaches the cloud,
you can store, process, and analyze the data using a variety of cloud computing resources. Along the way,
you’ll see how to modify and rebuild the application software to make your own changes. This provides a
foundation for prototyping other types of IoT applications including location and asset tracking applications.
Project Materials and Resources
The project’s bill of materials (BOM) lists the essential components. It also lists additional hardware and
software development tools.
Project BOM
Hardware
- 802.11 b/g/n Wi-Fi access point with a Dynamic Host Configuration Protocol (DHCP) server and
Internet connectivity without a firewall or proxy that blocks outgoing traffic to the Internet
- Personal computer (PC) running Microsoft Windows®
Accounts and Software
Project Technology Overview
Renesas Synergy AE-CLOUD2 LTE IoT Connectivity Kit
The Synergy AE-CLOUD2
(Figure 1) is a development tool from Renesas
that’s used to test and evaluate Global LTE IoT Cellular cloud-connected applications using the Synergy
Platform. The AE-CLOUD2 lets developers easily connect IoT sensors to cloud services using Cat-M1 or Cat-NB1 LTE
cellular networks and also has Wi-Fi and Ethernet connectivity for prototyping without an LTE SIM and data plan.
Figure 1: The Renesas Synergy AE-CLOUD2 LTE IoT Connectivity Kit empowers
sensor connectivity to cloud services through cellular and GPS receivers that can connect to cellular networks,
Wi-Fi, and Ethernet connections. (Source: Mouser)
Additional information about the kit on the Renesas Synergy S5D9 product microsite.
Synergy Enterprise Cloud Toolbox Demo Dashboard
The Synergy Enterprise Cloud Toolbox Demo Dashboard (Figure 2) is a quick-start Web and cloud
computing application that lets you quickly and easily get your AE-CLOUD2 kit running and transmitting sensor data
to the cloud. It contains built-in cloud connectivity for multiple cloud providers, and once you’re logged
into the site, you can provision new devices, download documentation and firmware, and view real-time dashboards of
your sensor data.
Figure 2: The Renesas Synergy Enterprise Cloud Toolbox Demo Dashboard allows
you to view real-time sensor data feeds to the cloud service of your choice. (Source: Author)
Renesas e² studio
Firmware programs that run on microcontrollers are typically developed and tested using an integrated development
environment (IDE) running on a PC. The IDE provides an editor, compiler, linker, debugger, and a mechanism for
transferring binary program images to the microcontroller.
The AE-CLOUD2 is programmable using the Renesas e²
studio, an Eclipse-based IDE that runs on Windows PCs. It connects to the AE-CLOUD2 kit using a USB cable that
supports programming and debugging and using a serial console port that connects to the microcontroller.
Synergy MQTT/TLS Google Cloud Connectivity Application Project
The Synergy MQTT/TLS Google Cloud Connectivity Application Project is a prebuilt application project containing
source code files and libraries for creating custom applications that run on the AE-CLOUD2 kit. You’ll use it
as a starting point to create your own real-time application that sends sensor data to Google Cloud. You’ll
download the project source code files from Renesas and import them into e² studio on your PC. The source code
and libraries interact with the microcontroller, network interfaces, and sensors on the AE-CLOUD2 boards and
implement the communication and security protocols used by Google Cloud including the Transport Layer Security
(TLS), Message Queue Telemetry Transport (MQTT), and JavaScript Object Notation [JSON] Web Tokens (JWT) protocols.
Google Cloud Platform and the Google Cloud IoT Core
Google Cloud Platform is a collection of computing services and tools that run in Google data centers located
throughout the world. The platform provides a large set of services that can be useful as a building-block to create
a variety of applications using a pay-as-you-go billing model. The cloud’s services include computing,
storage, database management, networking, monitoring, containerization, mapping, data analytics, pub/sub, machine
learning, and the IoT. We’ll use the Google Cloud IoT Core service to integrate our board with Google Cloud.
Wi-Fi Access Point
The AE-CLOUD2 kit contains a built-in Wi-Fi module that connects to the Internet through a Wi-Fi access point
supporting 802.11 b/g/n standards. The access point supplies a DHCP Internet Protocol (IP) address to the Wi-Fi
module and must allow outgoing Internet traffic without proxy or firewall blocks.
Gcloud
Gcloud is a command-line tool for interacting with Google Cloud from the command-line of a PC. It’s available
as part of the Google Cloud SDK. Find additional details in the software development kit (SDK) docs provided by
Google.
Hardware Setup
While setting up the hardware, be sure to remember that electronic components are static-sensitive, so handle them
with care.
Wi-Fi Access Point
Make sure your Wi-Fi access point is running with an active connection to the Internet and to a DHCP server that
assigns IP addresses. You’ll need to know the access point SSID, security type, and security credentials for
later use when configuring the board’s Wi-Fi module.
AE-CLOUD2 Kit
- Remove the boards and cables from the package and place on a suitable work surface.
- Follow the Quick Start Guide (included in the kit) to connect the boards together and connect the cellular and
GPS antennas (Figure 3).
Figure 3: After assembling the boards, the AE-CLOUD2 should resemble this
image. (Source: Renesas)
- Connect the two Type-A to Micro-B USB cables (included in the kit) between the PC and the two micro USB ports
labeled “USB” and “DBG” on the S5D9 PILLAR board (the lower board with the plastic base
attached) of the AE-CLOUD2.
Note: For this project, you’ll use the Wi-Fi network for Internet connectivity instead of
the LTE cellular network interface, so you don’t have to procure an LTE SIM and data plan. As a result, you
won’t need to insert an LTE SIM card (you can add this later).
Additional information and documentation on the AE-CLOUD2 can be found on the Renesas
AE-CLOUD2 Web page. With the hardware setup complete, you can shift your focus to the software setup in these
next two sections. The two major parts in the software setup are:
- The Renesas Synergy Enterprise Cloud Toolbox Demo Dashboard setup to establish end-to-end functionality
- The Google Cloud setup for your IoT application when connecting your board to the Google Cloud
Renesas Synergy Enterprise Cloud Toolbox Demo Dashboard Setup
Get started with the Renesas Synergy Enterprise Cloud Toolbox Demo Dashboard using each of these four stages:
- Run the Enterprise Cloud Toolbox Demo Dashboard
Application
- Configure the Board
- Provision the Device inthe Synergy Enterprise CLoud Toolbox Demo Dashboard
- Publish Renesas Application Data
Run the Enterprise Cloud Toolbox Demo Dashboard Application
- Download a prebuilt firmware binary and program it onto the AE-CLOUD2 board. The board does not come
preprogrammed with firmware for the Enterprise Cloud Toolbox Demo, so you must manually download it and program it
onto the board.
- Browse to the Renesas Synergy Enterprise Cloud Toolbox Demo site and sign up for an account.
- Log in, then navigate to the Development Kit page (Figure 4).
Figure 4: The Renesas Synergy Enterprise Cloud Toolbox Demo Dashboard allows
you to monitor sensor data activity that your AE-CLOUD2 receives. (Source: Renesas)
- Click Binary Firmware for Kit to download a .zip file. You can also click Quick Start
Guide for Kit to download a document containing detailed information about setting up and using the
AE-CLOUD2 kit and software.
- Unzip the downloaded file (which may be named
AE_Cloud2_bat.zip or something similar) to extract
the files.
- Open a Command Prompt window and navigate to the directory containing the unzipped files.
- Type s1.bat and press Enter to run the batch file. This will program the demo
firmware onto the board through the USB cable (Figure 5). If you have trouble with the USB
connection or the programming operation, see the Quick Start Guide for additional setup and
troubleshooting tips.
Figure 5: A firmware programming status window will appear while running the
batch file. (Source: Mouser)
- Open your serial terminal program (e.g., Tera Term) and select the USB serial port connected to the AE-CLOUD2.
The terminal settings can be set at 9600, N, 8, 1. Press any key and you should see a firmware startup banner on
the console (Figure 6).
Figure 6: Once the terminal settings are applied for your serial terminal
program, a firmware startup banner should appear. (Source: Mouser)
If you don’t see a banner like Figure 6, reset the board and clear the flash settings using
the board’s push buttons in this sequence:
- Press the RESET push button. Then, within five seconds of releasing the RESET button,
- Press the USER push button.
- Press any key on the serial console.
At this point, the firmware is running a command-line interface (CLI) where you can type commands on the serial
console.
Note: Type ? (question mark), then press Enter to get a list of
available commands.
Configure the Board
- Configure the Wi-Fi network using the command
wifi <YOUR_SSID> <KEY> <SECURITY_TYPE> with the SSID, Key, and Security Type
(wep, wpa, wpa2, or open) for your Wi-Fi
network.
- Type the command
netif wifi to set Wi-Fi as the active network interface and initiate the Wi-Fi
connection process.
- Type the command
ipcf dhcp to obtain a dynamically assigned IP address from the Wi-Fi access point.
Note: In the future you can change the network interface to LTE or Ethernet by using other network
configuration commands. See the Quick Start Guide for details.
- Obtain the device identification (ID) for your board using the
ktid command. This will print out a
Unique ID for your board, which you’ll use in the next step.
Provision the Device in the Synergy Enterprise Cloud Toolbox Demo Dashboard
On the Synergy Enterprise Cloud Toolbox Demo Dashboard site:
- Click the Device Management tab (Figure 7).
Figure 7: Use the Device Management tab to add your device to the Synergy
Enterprise Cloud Toolbox Demo Dashboard to process and monitor incoming sensor data. (Source: Renesas)
- Once on the Device Management screen, click the Add Device button and name the device.
- Copy and paste the board’s Unique ID obtained earlier using the
ktid command.
- Set the IOT Provider to Google Cloud Platform and click the Add Afterwards,
you should see the device listed on the Device Management page with the Cloud
Provider showing as GCLOUD.
Publish Renesas Application Data
To authenticate the device and start the demo application on the serial console:
- Run the following command:
user <YOUR_USERNAME> <YOUR-PASSWORD> where YOUR_USERNAME and
YOUR_PASSWORD are the same sign-in credentials that you created for the Synergy Enterprise Cloud Toolbox Demo
Dashboard website.
- Run the
boot command to initialize the application and connect to Google Cloud.
- Run the
turb start command to start the application.
You should see messages printed on the serial console containing sensor data, including latitude and longitude
values if the GPS receiver is able to pick up GPS satellites.
On the Synergy Enterprise Cloud Toolbox Demo Dashboard site:
- Click Remote Monitoring
You should see a live display of sensor data for the different types of sensors on the board, including a
location pin on the map widget if your GPS is receiving a signal. If you don’t have a GPS signal:
- Try relocating the antenna outdoors with a clear view of the sky.
- Try moving and rotating the board to see the sensor values change.
- Change the light conditions, and the light value should change on the dashboard.
- Click the Green, Red, and Yellow light-emitting diode
(LED) widgets to turn the board LEDs on or off remotely through the cloud.
Notes:
- To stop the application, power off the device.
- To clear the settings and change the network configuration, reset the board and clear the flash settings using
the push-button steps described earlier.
Google Cloud Setup
To set up Google Cloud, you’ll complete each stage of the following two sections:
Set Up Google Cloud
- Download and Install e² studio
- Download and Import the Application Source Code
- Add and Configure the Board Support Package
- Update the Source Code Files
- Build the Application
- Download the Application to the AE-CLOUD2 Board
Connect Your Board to the Google Cloud IoT
- Generate the Digital Key and Certificate
- Create a Google Cloud Platform Login Account
- Enable the Pub/Sub API
- Enable the Core API
- Create a Device Registry
- Create a Device and Add a Certificate
- Configure the Google Cloud IoT Core Parameters in the Board’s Firmware
- Start the Application
- Publish Sensor Data Messages in the Google Cloud IoT
- Monitor Messages
Set Up Google Cloud
Download and Install e² studio
- Create a My Renesas login account to enable the download if you don’t already have one.
The Web page also has a Release Note document that includes detailed instructions for
installing e² studio.
- Browse to the Renesas e² studio Web page and download the Platform
Installer for the current public release.
- Run the downloaded
setup_sspvXXXXXXX.exe file to install the essential components on your computer.
- Choose the Quick Install mode, which should lead to an installation dialog box similar to
Figure 8.
Figure 8: The Renesas Synergy Software Package with e² studio setup
installer dialog box shows the installation requirements and software that will install once the process is
complete. (Source: Renesas)
- Complete the installation, then start e² studio from the Start menu.
During the first startup, you’ll get a Toolchain Integration dialog box (Figure
9) where you’ll need to:
- Place a check mark next to the GCC ARM Embedded
- Place a check mark next to the "Enable 'Toolchain integration' on startup" option, then click
Register.
After the Toolchain Integration step, you should see a Welcome screen.
Figure 9: To initiate the e² studio registration, select the GCC ARM
Embedded and "Enable 'Toolchain integration' on startup" options in the Toolchain Integration dialogue box.
(source: Mouser)
- Click Workbench in the top-right corner of the Welcome screen to open the main
workspace page (Figure 10).
Figure 10: The e² studio workspace page is where the project’s
application activity begins. (Source: Mouser)
Download and Import the Application Source Code
In this step, you’ll download an application project that you can modify for your own use.
In Renesas AE-CLOUD2:
- Navigate to the Renesas AE-CLOUD2 Web page and look for the Synergy MQTT/TLS Google Cloud
Connectivity icon.
- Click the icon to open the download page and download the
xxxxxx-synergy-mqtt-tls-google-cloud-connectivity.zip file.
- Unzip the downloaded file and you should find several files similar to Figure 11 (file names
may vary slightly).
Figure 11: The contents in the downloaded application source code archive
stores several essential files for your project. (Source: Mouser)
In Renesas e² studio:
- Go to File→Import and in the Import Projects dialog box, select
General→Existing Projects Into Workspace followed by
Next >.
- Click the radio button next to “Select archive file,” then click the
Browse button to pick the
Synergy_GCloudSIn_AECloud2.zip from the list of unzipped
files.
At this point, the Import dialog box should look similar to Figure 12.
Figure 12: The Import dialog box allows you to import your application project
into the Renesas e² studio environment.
- Click Finish to complete the import.
Afterwards, the Project Explorer window should show the imported project files (Figure 13).
Figure 13: Your files for the project should appear in the Renesas e²
studio workspace when the import step is complete. (Source: Mouser)
Add and Configure the Board Support Package
- Locate the
Renesas.s5D9_PILLAR_ARDUINO_MODULE.x.x.x.pack file in the downloaded zip archive.
- Copy that file into the
C:\Renesas\Synergy\e2studio_vX.X.X_ssp_vX.X.X\internal\projectgen\arm\packs
directory on your PC.
Next, you’ll need to configure the board support package and generate the rest of the project files:
- Under the IDE Project Explorer pane (Figure 14), double-click the
configuration.xml file.
If you get a warning about a Pack missing for the
project’s current selected Synergy Software Package (SSP) version, click Yes to switch to
the latest available SSP version. Click OK to any additional warnings about changing the SSP
version.
Afterwards, you should see a Project Summary page showing SSP version 1.5.0 or higher:
- Click the BSP tab and confirm that the SSP version shows 1.5.0 or higher.
- Run the File, then click Save to save the updated configuration.
- Click the Generate Project Content button towards the top-right.
The Project Explorer pane should update to show additional directories (Figure 14).
Figure 14: Once the board support package configuration and content generation
are complete, the e² studio workspace will populate the Project Explorer pane with additional project content
files. (Source: Mouser)
Update the Source Code Files
Two of the provided source code files have been further modified for use with this article:
Synergy_GCloudSIn_AECloud2/src/console_thread_entry.c
Synergy_GCloudSIn_AECloud2/src/sensors.c
The purpose of the modifications is to:
- Allow the application to run when the AE-CLOUD2 board is not connected to a PC—for example when the board
receives power using a USB battery pack in a mobile location tracking application. Without this modified code, the
standard application won’t run unless the USB cable is connected to a PC.
- Start the application automatically when the board is powered up and isn’t connected to a PC.
- Fix some software bugs related to reading and processing GPS measurements.
The two modified files can be found in this GitHub repo: https://github.com/Mouser-Electronics/ma3_files. Download or clone this repo and replace the
contents of your console_thread_entry.c and sensors.c files (located in the
project’s src directory) with the contents of the downloaded GitHub files.
Build the Application
In Project Explorer:
- Right-click Synergy_GCloudSIn_AECloud2 and select Build Project. You should
see a series of build messages that eventually complete with zero errors (the build generates a number of
compilation warnings).
Download the Application to the AE-CLOUD2 Board
Make sure the AE-CLOUD2 board is plugged into the PC using both USB cables.
In Project Explorer:
- Right-click Synergy_GCloudSIn_AECloud2 and select Debug
As→Renesas GDB Hardware Debugging. You should see download progress indicators
(similar to Figure 5) as the application image is downloaded to the board through the USB cable.
- Use the menu command Run → Resume to start the application on the board. You
should see activity on the serial console as the application starts running. If not, press the
Enter key a few times.
Connect Your Board to the Google Cloud IoT
Now you can make the board known to the Google Cloud IoT and add the board’s digital certificate.
Generate the Digital Key and Certificate
This step generates the digital key and certificate needed for AE-CLOUD2 authentication in your Google Cloud IoT
Core environment.
- Run the following commands on the PC:
openssl req -x509 -nodes -newkey rsa:2048 -keyout rsa_private.pem \
-days 365 -out rsa_cert.pem -subj "/CN=unused"
openssl rsa -in rsa_private.pem -out rsa_private_pkcs1.pem
These commands generate three files:
- rsa_private.pem
- rsa_private_pkcs1.pem
- rsa_cert.pem
Create a Google Cloud Platform Login Account
- Web browse to Google Cloud Platform at https://console.cloud.google.com.
- Establish a Google Cloud Platform login account.
- Create a new cloud project with billing support enabled at the project level.
The rest of the steps below will occur within the newly created cloud project.
Enable the Pub/Sub API
- Navigate to the Pub/Sub service and enable the Pub/Sub API (short for application programming
interface) (Figure 15).
Figure 15: Enabling the Pub/Sub API allows you to connect your services and
subscriptions through real-time messaging on the Google Cloud Platform. (Source: Mouser)
Enable the IoT Core API
- Navigate to the IoT Core page and enable the IoT Core API (Figure 16).
Figure 16: Enabling the IoT Core API allows you to view cloud activity on your
Google Cloud Platform console. (Source: Mouser)
Create a Device Registry
- While still in the IoT Core service, click “Create a device registry”
(Figure 17).
Figure 17: Creating a device registry allows you to connect multiple devices
to the cloud. (Source: Mouser)
- Enter these settings:
Registry ID: my-iot-registry
Region: us-central1
Protocol: MQTT (checked) and HTTP (checked)
Default telemetry topic:
- Click "Select a CLoud Pub/Sub topic."
- Select "Create a topic."
- Type the word “events” (lowercase and without quotes) at the end of the topic
string and click Create. This is the Google Cloud Pub/Sub topic to which incoming MQTT messages
will map.
Device state topic:
- Click "Select a Cloud Pub/Sub topic."
- Select "Create a topic."
- Type the word “state” (lowercase and without quotes) at the end of the topic string
and click Create.
Stackdriver Logging: None
The settings should look like Figure 18—except you’ll have a different project name in
the telemetry and state topics’ strings.
- Finally, click Create:
Figure 18: IoT Core registry settings establish the parameters that your
registered devices will follow to send data to the cloud. (Source: Mouser)
Create a Device and Add the Certificate
In the Google Cloud IoT Core navigation menu:
- Select Devices.
- Click “Create a device.”
- Enter these settings:
Device ID: renesas-ae-cloud2
Device communication: Allow
Authentication Input method: Upload
Public key format: RS256_X509
Public key value: Click Browse and select the rsa_cert.pem file generated
using OpenSSL
Public key expiration date: Unchecked
Note: Leave the rest of the settings at the defaults.
The settings should look like Figure 19.
- Last, click Create:
Figure 19: Enter your IoT Core device settings to add your AE-CLOUD2 board as
a recognized device that can publish sensor data messages in Google Cloud. (Source: Mouser)
At this point, you’ve defined your AE-CLOUD2 board as a new device in the Google Cloud IoT, and the board is
ready to be connected.
Configure the Google Cloud IoT Core Parameters in the Board’s Firmware
This application requires a number of run-time parameters to operate. You will manually enter these parameters
through a menu-driven process on the serial console. The code within the console_thread_entry.c source code file
controls the specific commands and parameters. Go through the menu selections and enter the commands and parameters
listed in Table 1.
Table 1: Configuration Commands and Parameters to Enter in the Serial Console
Prompt
|
Keyboard Entry
|
Description
|
|
>
|
Cwiz
|
Configuration Wizard
|
|
Please Enter Your Choice:>
|
1
|
Network Interface Selection
|
|
Please Enter Your Choice:>
|
2
|
Wi-Fi
|
|
>
|
YOUR SSID
|
Your SSID name
|
|
>
|
YOUR PASSPHRASE
|
Your Wi-Fi password or blank if none
|
|
>
|
YOUR SECURITY TYPE
|
Your menu choice for security type
|
|
>
|
2
|
DHCP IP address configuration mode
|
|
Please Enter Your Choice:>
|
2
|
GCloud IoT Core Configuration
|
|
Please Enter Your Choice:>
|
1
|
GCloud IoT Core Setting Menu
|
|
Please Enter Your Choice:>
|
1
|
Your Project ID
|
|
Enter Project ID:
|
lithe-lens-228601
|
Your own project ID instead of lithe-lens-228601
|
|
Please Enter Your Choice:>
|
2
|
Endpoint Information
|
|
Enter Endpoint information:
|
mqtt.googleapis.com
|
Google MQTT endpoint
|
|
Please Enter Your Choice:>
|
3
|
Your Device ID
|
|
Enter Device ID:
|
renesas-ae-cloud2
|
Device ID that was added to IoT registry
|
|
Please Enter Your Choice:>
|
4
|
Cloud Region
|
|
Enter Cloud Region:
|
us-central1
|
Region name for your Google Cloud project
|
|
Please Enter Your Choice:>
|
5
|
Registry ID
|
|
Enter Registry ID:
|
my-iot-registry
|
Name of IoT registry
|
|
Please Enter Your Choice:>
|
6
|
Previous menu
|
|
Please Enter Your Choice:>
|
2
|
Device Certificate/Keys Setting Menu
|
|
Please Enter Your Choice:>
|
1
|
RootCA Certificate
|
|
Enter rootCA Certificate: <paste rootCA and Press Enter key>
|
Copy and paste the entire contents of the gcloud_rootCA.pem file that was included in the unzipped
application project file downloaded from Renesas.
|
File starts with -----BEGIN CERTIFICATE----- and ends with -----END CERTIFICATE-----
|
|
Please Enter Your Choice:>
|
2
|
Thing Certificate
|
|
Enter Thing Certificate: <paste device Cert and Press Enter key>
|
Copy and paste the entire contents of the rsa_cert.pem file you generated using openssl.
|
File starts with -----BEGIN CERTIFICATE----- and ends with -----END CERTIFICATE-----
|
|
Please Enter Your Choice:>
|
3
|
Thing Private Key
|
|
Enter Thing Private Key: <paste Thing private key and Press Enter key>
|
Copy and paste the entire contents of the rsa_private_pkcs1.pem file you generated using openssl.
|
File starts with -----BEGIN RSA PRIVATE KEY----- and ends with -----END RSA PRIVATE KEY-----
|
|
Please Enter Your Choice:>
|
4
|
Previous menu
|
|
Please Enter Your Choice:>
|
3
|
Previous menu
|
|
Please Enter Your Choice>
|
3
|
Dump previous configuration from flash; this is used to view the configuration information stored in flash
memory
|
|
>
|
4
|
Cwiz mode
|
Start the Application
With the USB cable:
- Connect the serial console to the board through the USB cable.
- Start the application by typing the command
demo start. After the application starts you should see
network initialization messages, followed by cloud initialization messages.
Note: To stop the application use the command demo stop.
Without the USB cable:
When the USB cable is not connected to a serial console, the application should start automatically when
the board powers up—if you used the modified source code files. This setup allows board use in mobile
environments, such as when prototyping location or asset tracking applications.
Publish Sensor Data Messages in the Google Cloud IoT
At this point, the board should be running the application and publishing sensor messages approximately every five
to eight seconds. The application code formats each set of sensor readings as a JSON string, like this:
{
"xacc" : "9.28",
"yacc" : "0.69",
"zacc" : "-0.45",
"temperature" : "82.38",
"pressure" : "998.74",
"humidity" : "25.62",
"longitude" : "xx.xxxxxxxx",
"latitude" : "xx.xxxxxxxx ",
"spl" : "13.38"
}
Monitor Messages
You can monitor the published sensor messages by creating a subscription using the gcloud command-line tool on your
PC and running a short Python program that uses the subscription to receive and print messages.
To use gcloud to create a monitoring subscription:
- Use the following code line, substituting your own project name in place of
lithe-lens-228601:
gcloud --project lithe-lens-228601 pubsub \
subscriptions create --topic events monitor-sub
- Install the Google Cloud Pub/Sub Python client package on your PC using this code line:
pip install google-cloud-pubsub
- Create a new file named monitor-sub.py that contains the following Python code; replace lithe-lens-228601 with
your own project name:
import time
from google.cloud import pubsub_v1
project_id = "lithe-lens-228601"
subscription_name = "monitor-sub"
subscriber = pubsub_v1.SubscriberClient()
# The `subscription_path` method creates a fully qualified identifier
# in the form `projects/{project_id}/subscriptions/{subscription_name}`
subscription_path = subscriber.subscription_path(
project_id, subscription_name)
def callback(message):
print('Received message: {}'.format(message.data))
message.ack()
subscriber.subscribe(subscription_path, callback=callback)
# The subscriber is non-blocking. We must keep the main thread from
# exiting to allow it to process messages asynchronously in the background.
print('Listening for messages on {}'.format(subscription_path))
while True:
time.sleep(60)
- Run the Python program using this command:
python monitor-sub.py
As new messages are published in Google Cloud, they will print on your PC, like this:
Received message: {
"xacc" : "9.30",
"yacc" : "0.71",
"zacc" : "-0.45",
"temperature" : "84.88",
"pressure" : "996.51",
"humidity" : "24.78",
"longitude" : "xx.xxxxxxxx ",
"latitude" : "xx.xxxxxxxx",
"spl" : "0.00"
}
- Press Control-C to terminate the monitoring program.
Remember to power off the board when you’re done so it doesn’t continue to send messages to Google
Cloud and consume cloud resources.
Where to Go Next
This project has created an end-to-end sensor-to-cloud application that sends real-time sensor and GPS location
data to the cloud. You can modify and extend this project in a number of ways. Here are a few examples:
- Dive deeper into the application code by reading the Synergy MQTT/TLS Google Cloud Connectivity Solution
document and by studying the source code.
- Learn more about the boards in the AE-CLOUD2 kit by reading the User’s Manual and studying the source
code.
- Obtain an LTE Cat-M1 or Cat-NB1 SIM card and data plan, and reconfigure the board to use the LTE interface for
Internet connectivity.
- Modify the sensor publishing time interval by modifying the source code.
- Include a timestamp in the sensor data message.
- Connect the pub/sub topic to a Google Cloud BigQuery™ table to record sensor and location data for further
analysis.
- Develop a data display application that subscribes to the sensor data topic and displays received data values in
a tabular and/or map format. This can be the foundation for a location or asset tracking application.
Now that you’ve completed this project, how did things go? Would you expand upon it? Let us know on Facebook, Twitter, LinkedIn, or Instagram.
Greg is an architect, engineer and consultant
with more than 30 years experience in sensors, embedded systems, IoT, telecommunications, enterprise systems, cloud
computing, data analytics, and hardware/software/firmware development. He has a BS in Electrical Engineering from
the Univ. of Notre Dame and a MS in Computer Engineering from the Univ. of Southern California.