This is yet another hobby project to play with serverless implementations. The project is splitted in two separate node.js modules as follows.
Module / Element | Platform | Purpose |
---|---|---|
ifDataGet | Google Cloud Functions | Retrieve metrics from Google Datastore and output as json array to be displayed as chart on a web page |
ifDataPut | Raspberry Pi | Retrieve sensor metics and store them in Google Cloud Datastore. Also periodically calculate daily summaries from old data to reduce stored entries. |
static html | Google Cloud Storage | Hosting of the static web page html code that is used to render the data visualisation. |
Original inspiration was to fetch and store temperature and other environmental data collected from RuuviTag. In absence of RuuviTag, interface counter data was used instead as those were easily available from ER-X.
Project enables extremely cost efficient (practically free of charge) method to store and display sensor metric data with only inexpensive, small and energy conserving device to collect sensor metrics.
http://misc.karilaalo.fi/ifDataDemo/
Access to cloud function is restricted by authentication. To gain simplicity, Google Sign-In for Websites is used. Other Sign-In methods would be available as well with used architectural design.
As the static website is hosted from separate domain from the cloud function serving the data, CORS requirements cause small obstacles for the authentication. Cloud Functions support Google authentication by themselves, but only when they are published from common domain.
To get around CORS requirements, Google Cloud Endpoints Extensible Server proxy (ESP) is used to protect the function with authentication. ESP is deployed as Cloud Run container.
The authentication information about the user is relayed to the function using OIDC ID Token that is received from the Google Sing-In OpenID Connect Provider. Short-lived ID Token is sufficient as it is needed only at the time the chart data is fetched. It is supposed that Google Sign-In client libraries handle refreshing of the ID Token when necessary.
This makes very inexpensive combination of services as the container reserves computing instances only when used. In single user scenario, the container instance is spawned only so often.
Visualisation of the sensor metrics data is provided by Chart.js javascript charting library.
Implementation relies on following services and devices. List may be incomplete:
- Raspberry Pi
- Google Cloud Functions
- Google Cloud Endpoints
- Google Cloud Datastore
- Google Cloud Storage
- Node.js 10
- Chart.js
Deploy ifDataGet function to cloud:
gcloud functions deploy ifDataGet --runtime nodejs10 --update-env-vars A_USER=12345 --trigger-http --memory 128MB --region europe-west1
ESP deployment to the Cloud Run container (beta) is somewhat more complex. Please refer to tutorial.
docker build -t ifdata .
docker run -dv '/opt/ifData/ifDataLogs/:/opt/ifData/logs/' --name ifdata ifdata