Now that your application has been tested in VS Code, it is time to install and run the code on your SmartServer IoT.
You should review lines 7-14 of the helloMqtt.js source code to understand how the application connects to the mqtt broker. In the last exercise, you defined the environment variable DEV_TARGET in the VS code launch.json file to point to your SmartServer IoT IP address. If the variable is not defined, the code will use a localhost or the loopback address (127.0.0.1). If you run the application on the SmartServer, it will connect to the MQTT broker that is part of the SmartServer IoT. This is how the SmartServer internal applications communicate using MQTT.
Remember, the ultimate goal is to write applications that run on the SmartServer IoT, and use the IAP/MQ API to access SmartServer IoT resources exposed by the IAP/MQ API topics.
So let's continue:
- Use Winscp or other SCP or SFTP client to create the directory
/var/apollo/data/apps/hellomqtt as user apollo.
- Use Winscp or other SCP or SFTP client to copy the files helloMqtt.js and package.json created in the previous exercise, into the directory
- From an SSH console of the SmartServer, log in as user apollo, type:
- Install your application by typing:
This command causes npm to go to the internet and install the mqtt module. In the previous exercise, you added the mqtt dependency when you ran the command npm install mqtt --save, which updated the package.json file with mqtt as a dependency to the package. As a result, this npm install on your target SmartServer also loads mqtt from the npm software registry on the internet.
From the directory /var/apollo/data/apps/hellomqtt, type:
The application subscribes to the topic Dave, and publishes four greetings. If you still have an instance of the application running in the second SSH console (from the previous exercise), then both consoles running instances of the application will subscribe to the topic Dave, and publish multiple greetings as shown below. (If you don't have a second instance running, you can run another instance on a second console, and you will see the results shown below.)
While this application does little useful work, it does demonstrate how easy it is to write applications that use mqtt to share simple message data using publish/subscribe mechanisms.
Now that you have successfully run Hello World on the SmartServer IoT, you can move on to the next step in this tutorial: Your First IAP/MQ App.