This tutorial is going to show how to implement a simple IoT cloud based system, that system will collects information from a set of virtual environmental sensors using the MQTT protocol. We are going to use Google Cloud IoT as cloud and the data will be displayed through a Node.js web dashboard that will show the last data received from a device and all the values received in the last hour. So the project is made up by these elements:
- Web client that display interested values.
- Simple Node.js script that simulate a physical device.
- Google Cloud IoT system to make 1, 2 comunicate through MQTT protocol.
- Google Cloud IoTFirst of all it's necessary sign in, with a google account, to Google Cloud Console in order to create a Project then a Cloud IoT Core device registry and finally register a device.Let's select or create a new Cloud Project and after that enable Cloud IoT Core and Cloud Pub/Sub APIs, this passage is pretty simple and well documented on Google.The next step is install and initialize Cloud SDK and the development system based on your prefer language (in this case Node.js).Last phase of setup, let's create a new registry, enter an id, select your region and MQTT as comunication protocol. Then create a new topic. Be sure to remember this IDs that will be necessary in the implementation.For each device it's required a token that will be signed with a couple of keys that can be easy generate with this command (Windows Systems):openssl req -x509 -newkey rsa:2048 -keyout rsa_private.pem -nodes \-out rsa_cert.pem -subj "//CN=unused"With those keys generated go to Registries Page, select Devices tab and then click on Create Device, here in authentication tab insert the public key saved before in "rsa_cert.pem" and select RS256_X509 as format, fill the other fileds and then click on Create.
- MongoDB AtlasDBSing in to Atlas DB, then after registration create a new cluster selecting Google Cloud Platform, free version should be enough. This process can be slow, after that clic on Connect button over the cluster and follow the istructions, choose connection method and save the Connection String.
- Node.js Web ClientLet's now build the system that will display the values comunicated through MQTT by the devices.Through npm install those dependencies (npm install EXAMPLE_PACKAGE --save):"@google-cloud/pubsub": "^1.6.0","@google-cloud/storage": "^4.6.0","body-parser": "^1.19.0","ejs": "^3.0.1","express": "^4.17.1","mongodb": "^3.5.5","mongoose": "^5.9.6","mongoose-paginate": "^5.0.3","socket.io": "^2.3.0"Set up a Node.js web server with your favorite framework, in this project i've used express and socket.io to render live real data.In order to comunicate with Google Cloud System let's use the previusly saved config, this is need to make the listenForMessage function which is our main function to read devices data.It's a preatty simple implementation, when we receive message from the topic we store them to the DB and push it to the dashboard. After a prefixed time the listner on subscription will be removed.Further more information to receive information through pub/sub can be found here.To connect our project to MongoDB we use mongoose which is a nice schema-based solution to model application data, the only thing that we need it's the uri provided by MongoDB Atlas. To retreive last hour data build a simple query filter in the "Data" Model find then the result values will be pushed in a json object that will be rendered through ejs view engine.
- NodeVirtualDevicescriptsAs we discuss before, each device you need to implement has to be registred on Google Cloud Console. Each of that publish random values over MQTT, another solution could be one script that randomly select a divice and push a value emulating a real well balanced system.Using our Google Cloud project config let's configure our connection to the system, we need to use the privateKey to make a token to be validated from google, this can be easy done with jwt library.Obviously we need an async function to publish message this will simply generate a random (from 0 to 360) that will be pushed though the mqtt connection every 5s.
Comments